3#include "absl/strings/str_format.h"
11#include "imgui/imgui.h"
32 const int encoded_x = std::clamp(clamped_x / 4, 0, 255);
33 const int encoded_y = std::clamp(clamped_y / 16, 0, 255);
34 return static_cast<uint16_t
>((encoded_y << 8) | encoded_x);
40 const ImGuiIO& io = ImGui::GetIO();
42 const bool mouse_left_down = ImGui::IsMouseDown(ImGuiMouseButton_Left);
43 const bool mouse_left_released =
44 ImGui::IsMouseReleased(ImGuiMouseButton_Left);
49 const bool should_process_without_hover =
53 (mouse_left_down || mouse_left_released)) ||
56 if (!hovered && !should_process_without_hover) {
60 const ImVec2 canvas_mouse_pos =
62 const int canvas_mouse_x =
static_cast<int>(std::floor(canvas_mouse_pos.x));
63 const int canvas_mouse_y =
static_cast<int>(std::floor(canvas_mouse_pos.y));
64 const bool pointer_within_room =
68 if (ImGui::IsKeyPressed(ImGuiKey_Escape) &&
75 if (pointer_within_room &&
86 if (hovered && mouse_left_down) {
89 if (pointer_within_room) {
95 if (pointer_within_room) {
102 if (hovered && ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
107 if (ImGui::IsMouseDragging(ImGuiMouseButton_Left)) {
112 if (mouse_left_released) {
118 int canvas_x =
static_cast<int>(std::floor(canvas_mouse_pos.x));
119 int canvas_y =
static_cast<int>(std::floor(canvas_mouse_pos.y));
145 const ImVec2& canvas_mouse_pos) {
146 auto [room_x, room_y] =
148 static_cast<int>(canvas_mouse_pos.y));
154 if (room_x >= 0 && room_x < 64 && room_y >= 0 && room_y < 64) {
156 if (!state.is_painting) {
158 state.paint_mutation_started =
false;
159 state.paint_last_tile_x = room_x;
160 state.paint_last_tile_y = room_y;
164 (state.paint_last_tile_x >= 0) ? state.paint_last_tile_x : room_x;
166 (state.paint_last_tile_y >= 0) ? state.paint_last_tile_y : room_y;
168 bool changed =
false;
169 auto ensure_mutation = [&]() {
170 if (!state.paint_mutation_started) {
172 state.paint_mutation_started =
true;
177 x0, y0, room_x, room_y, [&](
int lx,
int ly) {
179 lx, ly, state.paint_brush_radius,
181 [&](
int bx,
int by) {
182 if (room.GetCollisionTile(bx, by) ==
183 state.paint_collision_value) {
187 room.SetCollisionTile(bx, by, state.paint_collision_value);
197 state.paint_last_tile_x = room_x;
198 state.paint_last_tile_y = room_y;
203void DungeonObjectInteraction::UpdateWaterFillPainting(
204 const ImVec2& canvas_mouse_pos) {
205 const ImGuiIO& io = ImGui::GetIO();
206 const bool erase = io.KeyAlt;
208 auto [room_x, room_y] =
209 CanvasToRoomCoordinates(
static_cast<int>(canvas_mouse_pos.x),
210 static_cast<int>(canvas_mouse_pos.y));
211 if (rooms_ && current_room_id_ >= 0 && current_room_id_ < 296) {
212 auto& room = (*rooms_)[current_room_id_];
213 auto& state = mode_manager_.GetModeState();
216 if (room_x >= 0 && room_x < 64 && room_y >= 0 && room_y < 64) {
217 const bool new_val = !erase;
219 if (!state.is_painting) {
220 state.is_painting =
true;
221 state.paint_mutation_started =
false;
222 state.paint_last_tile_x = room_x;
223 state.paint_last_tile_y = room_y;
227 (state.paint_last_tile_x >= 0) ? state.paint_last_tile_x : room_x;
229 (state.paint_last_tile_y >= 0) ? state.paint_last_tile_y : room_y;
231 bool changed =
false;
232 auto ensure_mutation = [&]() {
233 if (!state.paint_mutation_started) {
234 interaction_context_.NotifyMutation(MutationDomain::kWaterFill);
235 state.paint_mutation_started =
true;
239 paint_util::ForEachPointOnLine(
240 x0, y0, room_x, room_y, [&](
int lx,
int ly) {
241 paint_util::ForEachPointInSquareBrush(
242 lx, ly, state.paint_brush_radius,
244 [&](
int bx,
int by) {
245 if (room.GetWaterFillTile(bx, by) == new_val) {
249 room.SetWaterFillTile(bx, by, new_val);
255 interaction_context_.NotifyInvalidateCache(MutationDomain::kWaterFill);
258 state.paint_last_tile_x = room_x;
259 state.paint_last_tile_y = room_y;
264void DungeonObjectInteraction::HandleObjectSelectionStart(
265 const ImVec2& canvas_mouse_pos) {
266 const bool has_object_selection = selection_.HasSelection();
267 const bool has_entity_selection = HasEntitySelection();
268 if (!has_object_selection && !has_entity_selection) {
272 mode_manager_.SetMode(InteractionMode::DraggingObjects);
273 if (has_object_selection) {
274 entity_coordinator_.tile_handler().InitDrag(canvas_mouse_pos);
276 if (has_entity_selection) {
277 entity_coordinator_.BeginSelectionDrag(canvas_mouse_pos);
281void DungeonObjectInteraction::HandleEmptySpaceClick(
282 const ImVec2& canvas_mouse_pos) {
283 const ImGuiIO& io = ImGui::GetIO();
284 const bool additive = io.KeyShift || io.KeyCtrl || io.KeySuper;
285 const bool had_selection =
286 selection_.HasSelection() || entity_coordinator_.HasEntitySelection();
291 ClearEntitySelection();
292 selection_.ClearSelection();
295 if (!had_selection) {
296 entity_coordinator_.tile_handler().BeginMarqueeSelection(canvas_mouse_pos);
300void DungeonObjectInteraction::HandleMouseRelease() {
304 const auto mode = mode_manager_.GetMode();
305 if (mode == InteractionMode::PaintCollision ||
306 mode == InteractionMode::PaintWaterFill) {
307 auto& state = mode_manager_.GetModeState();
308 const bool had_mutation = state.paint_mutation_started;
309 state.is_painting =
false;
310 state.paint_mutation_started =
false;
311 state.paint_last_tile_x = -1;
312 state.paint_last_tile_y = -1;
316 interaction_context_.NotifyInvalidateCache(
317 (mode == InteractionMode::PaintCollision)
318 ? MutationDomain::kCustomCollision
319 : MutationDomain::kWaterFill);
324 if (mode_manager_.GetMode() == InteractionMode::DraggingObjects) {
325 mode_manager_.SetMode(InteractionMode::Select);
327 entity_coordinator_.HandleRelease();
332bool DungeonObjectInteraction::HandleKeyboardNudge() {
333 if (!rooms_ || current_room_id_ < 0 ||
334 current_room_id_ >=
static_cast<int>(rooms_->size())) {
338 const ImGuiIO& io = ImGui::GetIO();
339 if (ImGui::IsAnyItemActive() || io.KeyCtrl || io.KeySuper || io.KeyAlt ||
340 ImGui::IsMouseDown(ImGuiMouseButton_Left) ||
341 entity_coordinator_.IsPlacementActive()) {
345 const auto mode = mode_manager_.GetMode();
346 if (mode == InteractionMode::DraggingObjects ||
347 mode == InteractionMode::PaintCollision ||
348 mode == InteractionMode::PaintWaterFill) {
354 if (ImGui::IsKeyPressed(ImGuiKey_LeftArrow,
true)) {
356 }
else if (ImGui::IsKeyPressed(ImGuiKey_RightArrow,
true)) {
359 if (ImGui::IsKeyPressed(ImGuiKey_UpArrow,
true)) {
361 }
else if (ImGui::IsKeyPressed(ImGuiKey_DownArrow,
true)) {
365 if (delta_x == 0 && delta_y == 0) {
369 return NudgeSelected(delta_x, delta_y);
372bool DungeonObjectInteraction::NudgeSelected(
int delta_x,
int delta_y) {
373 if (!rooms_ || current_room_id_ < 0 ||
374 current_room_id_ >=
static_cast<int>(rooms_->size())) {
378 bool handled =
false;
379 if (selection_.HasSelection()) {
380 entity_coordinator_.tile_handler().MoveObjects(
381 current_room_id_, selection_.GetSelectedIndices(), delta_x, delta_y);
385 if (entity_coordinator_.HasEntitySelection()) {
386 handled = entity_coordinator_.NudgeSelected(delta_x, delta_y) || handled;
392void DungeonObjectInteraction::CheckForObjectSelection() {
394 const ImGuiIO& io = ImGui::GetIO();
395 const ImVec2 mouse_pos = GetCanvasTransform().ScreenToRoomPixels(io.MousePos);
396 const bool mouse_left_released =
397 ImGui::IsMouseReleased(ImGuiMouseButton_Left);
399 if (mouse_left_released && selection_.IsRectangleSelectionActive()) {
400 selection_.UpdateRectangleSelection(
static_cast<int>(mouse_pos.x),
401 static_cast<int>(mouse_pos.y));
402 constexpr int kMinRectPixels = 6;
403 if (!io.KeyAlt && selection_.IsRectangleLargeEnough(kMinRectPixels)) {
404 entity_coordinator_.SelectEntitiesInRect(
405 selection_.GetRectangleSelectionBounds(),
411 entity_coordinator_.tile_handler().HandleMarqueeSelection(
413 ImGui::IsMouseDown(ImGuiMouseButton_Left),
416 io.KeyCtrl || io.KeySuper,
420void DungeonObjectInteraction::DrawSelectionHighlights() {
421 if (!rooms_ || current_room_id_ < 0 || current_room_id_ >= 296)
424 auto& room = (*rooms_)[current_room_id_];
425 const auto& objects = room.GetTileObjects();
428 selection_.DrawSelectionHighlights(
431 return std::make_tuple(result.offset_x_tiles * 8,
432 result.offset_y_tiles * 8, result.width_pixels(),
433 result.height_pixels());
438 if (entity_coordinator_.HasEntitySelection()) {
442 if (canvas_->IsMouseHovering()) {
444 ImGuiIO& io = ImGui::GetIO();
445 const auto [cursor_x, cursor_y] =
446 GetCanvasTransform().ScreenToRoomPixelCoordinates(io.MousePos);
447 auto entity_at_cursor =
448 entity_coordinator_.GetEntityAtPosition(cursor_x, cursor_y);
449 if (entity_at_cursor.has_value()) {
451 DrawHoverHighlight(objects);
455 auto hovered_index = entity_coordinator_.tile_handler().GetEntityAtPosition(
457 if (hovered_index.has_value() && *hovered_index < objects.size()) {
458 const auto&
object = objects[*hovered_index];
461 const int layer =
object.GetLayerValue();
464 const char* subtype_names[] = {
"Unknown",
"Type 1",
"Type 2",
"Type 3"};
465 const char* subtype_name =
466 (subtype >= 0 && subtype <= 3) ? subtype_names[subtype] :
"Unknown";
470 tooltip += object_name;
471 tooltip +=
" (" + std::string(subtype_name) +
")";
473 tooltip +=
"ID: 0x" + absl::StrFormat(
"%03X",
object.id_);
475 static constexpr const char* kStreamNames[] = {
"Primary",
"BG2 overlay",
477 const char* stream_name =
478 (layer >= 0 && layer < 3) ? kStreamNames[layer] :
"Unknown";
479 tooltip +=
" | Object stream: " + std::string(stream_name);
481 const char* layer_name =
482 layer == 0 ?
"Upper layer (BG1)" :
"Lower layer (BG2)";
483 tooltip +=
" | Special layer: " + std::string(layer_name);
485 tooltip +=
" | Pos: (" + std::to_string(
object.x_) +
", " +
486 std::to_string(
object.y_) +
")";
487 tooltip +=
"\nSize: " + std::to_string(
object.size_) +
" (0x" +
488 absl::StrFormat(
"%02X",
object.size_) +
")";
490 if (selection_.IsObjectSelected(*hovered_index)) {
497 ImGui::SetTooltip(
"%s", tooltip.c_str());
502 DrawHoverHighlight(objects);
505void DungeonObjectInteraction::DrawHoverHighlight(
506 const std::vector<zelda3::RoomObject>& objects) {
507 if (!canvas_->IsMouseHovering())
511 if (entity_coordinator_.HasEntitySelection())
516 ImGuiIO& io = ImGui::GetIO();
518 const auto [cursor_canvas_x, cursor_canvas_y] =
520 auto entity_at_cursor =
521 entity_coordinator_.GetEntityAtPosition(cursor_canvas_x, cursor_canvas_y);
522 if (entity_at_cursor.has_value()) {
526 auto hovered_index = entity_coordinator_.tile_handler().GetEntityAtPosition(
527 cursor_canvas_x, cursor_canvas_y);
528 if (!hovered_index.has_value() || *hovered_index >= objects.size()) {
531 const auto&
object = objects[*hovered_index];
534 if (selection_.IsObjectSelected(*hovered_index)) {
538 const auto& theme = AgentUI::GetTheme();
539 ImDrawList* draw_list = ImGui::GetWindowDrawList();
541 auto [sel_x_px, sel_y_px, pixel_width, pixel_height] =
545 ImVec2(
static_cast<float>(sel_x_px),
static_cast<float>(sel_y_px)));
547 static_cast<float>(pixel_width),
static_cast<float>(pixel_height)));
548 ImVec2 obj_end(obj_start.x + obj_size.x, obj_start.y + obj_size.y);
551 constexpr float margin = 2.0f;
552 obj_start.x -= margin;
553 obj_start.y -= margin;
558 ImVec4 hover_fill = theme.selection_hover;
559 hover_fill.w *= 0.5f;
561 ImVec4 hover_border = theme.selection_hover;
564 draw_list->AddRectFilled(obj_start, obj_end, ImGui::GetColorU32(hover_fill));
567 draw_list->AddRect(obj_start, obj_end, ImGui::GetColorU32(hover_border), 0.0f,
571void DungeonObjectInteraction::PlaceObjectAtPosition(
int room_x,
int room_y) {
572 entity_coordinator_.tile_handler().PlaceObjectAt(
573 current_room_id_, preview_object_, room_x, room_y);
575 if (object_placed_callback_) {
576 object_placed_callback_(preview_object_);
579 interaction_context_.NotifyInvalidateCache(MutationDomain::kTileObjects);
583std::pair<int, int> DungeonObjectInteraction::RoomToCanvasCoordinates(
584 int room_x,
int room_y)
const {
586 return {room_x * 8, room_y * 8};
589std::pair<int, int> DungeonObjectInteraction::CanvasToRoomCoordinates(
590 int canvas_x,
int canvas_y)
const {
592 return {canvas_x / 8, canvas_y / 8};
595bool DungeonObjectInteraction::IsWithinCanvasBounds(
int canvas_x,
int canvas_y,
597 return dungeon_coords::IsWithinBounds(canvas_x, canvas_y, margin);
603 current_room_id_ = room_id;
604 interaction_context_.rooms = rooms;
605 interaction_context_.current_room_id = room_id;
606 interaction_context_.selection = &selection_;
607 entity_coordinator_.SetContext(&interaction_context_);
610void DungeonObjectInteraction::SetPreviewObject(
612 preview_object_ = object;
614 if (loaded &&
object.id_ >= 0) {
617 entity_coordinator_.CancelPlacement();
620 mode_manager_.SetMode(InteractionMode::PlaceObject);
621 mode_manager_.GetModeState().preview_object = object;
625 auto& tile_handler = entity_coordinator_.tile_handler();
626 tile_handler.SetPreviewObject(preview_object_);
627 if (!tile_handler.IsPlacementActive()) {
628 tile_handler.BeginPlacement();
632 if (mode_manager_.GetMode() == InteractionMode::PlaceObject) {
638void DungeonObjectInteraction::ClearSelection() {
639 selection_.ClearSelection();
640 if (mode_manager_.GetMode() == InteractionMode::DraggingObjects) {
641 mode_manager_.SetMode(InteractionMode::Select);
645void DungeonObjectInteraction::HandleDeleteSelected() {
646 auto indices = selection_.GetSelectedIndices();
647 if (!indices.empty()) {
648 entity_coordinator_.tile_handler().DeleteObjects(current_room_id_, indices);
649 selection_.ClearSelection();
652 if (entity_coordinator_.HasEntitySelection()) {
653 entity_coordinator_.DeleteSelectedEntity();
657void DungeonObjectInteraction::HandleDeleteAllObjects() {
658 entity_coordinator_.tile_handler().DeleteAllObjects(current_room_id_);
659 selection_.ClearSelection();
662void DungeonObjectInteraction::HandleCopySelected() {
663 if (!rooms_ || current_room_id_ < 0 ||
664 current_room_id_ >=
static_cast<int>(rooms_->size())) {
668 const auto selected_objects = selection_.GetSelectedIndices();
669 const bool has_object_selection = !selected_objects.empty();
670 const bool has_entity_selection = entity_coordinator_.HasEntitySelection();
671 if (!has_object_selection && !has_entity_selection) {
675 entity_clipboard_.Clear();
676 bool clipboard_origin_set =
false;
678 if (has_object_selection) {
679 entity_coordinator_.tile_handler().CopyObjectsToClipboard(current_room_id_,
681 const auto& objects = (*rooms_)[current_room_id_].GetTileObjects();
682 for (
size_t index : selected_objects) {
683 if (index >= objects.size()) {
686 entity_clipboard_.origin_tile_x = objects[index].x_;
687 entity_clipboard_.origin_tile_y = objects[index].y_;
688 entity_clipboard_.origin_pixel_x =
689 entity_clipboard_.origin_tile_x * dungeon_coords::kTileSize;
690 entity_clipboard_.origin_pixel_y =
691 entity_clipboard_.origin_tile_y * dungeon_coords::kTileSize;
692 clipboard_origin_set =
true;
696 entity_coordinator_.tile_handler().ClearClipboard();
699 CopySelectedEntitiesToClipboard(clipboard_origin_set);
702void DungeonObjectInteraction::CopySelectedEntitiesToClipboard(
703 bool clipboard_origin_set) {
704 if (!rooms_ || current_room_id_ < 0 ||
705 current_room_id_ >=
static_cast<int>(rooms_->size())) {
709 const auto& room = (*rooms_)[current_room_id_];
710 auto selected_entities = entity_coordinator_.GetSelectedEntities();
711 if (selected_entities.empty() && entity_coordinator_.HasEntitySelection()) {
712 const SelectedEntity selected = entity_coordinator_.GetSelectedEntity();
713 if (selected.
type != EntityType::None) {
714 selected_entities.push_back(selected);
718 for (
const auto entity : selected_entities) {
719 switch (entity.type) {
720 case EntityType::Sprite: {
721 const auto& sprites = room.GetSprites();
722 if (entity.index >= sprites.size()) {
725 const auto& sprite = sprites[entity.index];
726 if (!clipboard_origin_set && !entity_clipboard_.HasData()) {
727 entity_clipboard_.origin_pixel_x =
728 sprite.x() * dungeon_coords::kSpriteTileSize;
729 entity_clipboard_.origin_pixel_y =
730 sprite.y() * dungeon_coords::kSpriteTileSize;
731 entity_clipboard_.origin_tile_x =
732 entity_clipboard_.origin_pixel_x / dungeon_coords::kTileSize;
733 entity_clipboard_.origin_tile_y =
734 entity_clipboard_.origin_pixel_y / dungeon_coords::kTileSize;
736 entity_clipboard_.sprites.push_back(sprite);
739 case EntityType::Item: {
740 const auto& items = room.GetPotItems();
741 if (entity.index >= items.size()) {
744 const auto& item = items[entity.index];
745 if (!clipboard_origin_set && !entity_clipboard_.HasData()) {
746 entity_clipboard_.origin_pixel_x = item.GetPixelX();
747 entity_clipboard_.origin_pixel_y = item.GetPixelY();
748 entity_clipboard_.origin_tile_x =
749 entity_clipboard_.origin_pixel_x / dungeon_coords::kTileSize;
750 entity_clipboard_.origin_tile_y =
751 entity_clipboard_.origin_pixel_y / dungeon_coords::kTileSize;
753 entity_clipboard_.items.push_back(item);
756 case EntityType::Door:
757 case EntityType::Object:
758 case EntityType::None:
765std::vector<SelectedEntity> DungeonObjectInteraction::PasteEntityClipboardAt(
766 int target_pixel_x,
int target_pixel_y) {
767 std::vector<SelectedEntity> pasted_entities;
768 if (!entity_clipboard_.HasData() || !rooms_ || current_room_id_ < 0 ||
769 current_room_id_ >=
static_cast<int>(rooms_->size())) {
770 return pasted_entities;
773 auto& room = (*rooms_)[current_room_id_];
774 const int delta_pixel_x = target_pixel_x - entity_clipboard_.origin_pixel_x;
775 const int delta_pixel_y = target_pixel_y - entity_clipboard_.origin_pixel_y;
777 if (!entity_clipboard_.sprites.empty()) {
778 interaction_context_.NotifyMutation(MutationDomain::kSprites);
779 auto& sprites = room.GetSprites();
780 for (
auto sprite : entity_clipboard_.sprites) {
781 const int next_x = std::clamp(
782 (sprite.x() * dungeon_coords::kSpriteTileSize + delta_pixel_x) /
783 dungeon_coords::kSpriteTileSize,
784 0, dungeon_coords::kSpriteGridMax);
785 const int next_y = std::clamp(
786 (sprite.y() * dungeon_coords::kSpriteTileSize + delta_pixel_y) /
787 dungeon_coords::kSpriteTileSize,
788 0, dungeon_coords::kSpriteGridMax);
789 sprite.set_x(next_x);
790 sprite.set_y(next_y);
791 sprites.push_back(sprite);
792 pasted_entities.push_back(
795 room.MarkSpritesDirty();
796 interaction_context_.NotifyInvalidateCache(MutationDomain::kSprites);
799 if (!entity_clipboard_.items.empty()) {
800 interaction_context_.NotifyMutation(MutationDomain::kItems);
801 auto& items = room.GetPotItems();
802 for (
auto item : entity_clipboard_.items) {
803 item.position = EncodePotItemPosition(item.GetPixelX() + delta_pixel_x,
804 item.GetPixelY() + delta_pixel_y);
805 items.push_back(item);
806 pasted_entities.push_back(
809 room.MarkPotItemsDirty();
810 interaction_context_.NotifyInvalidateCache(MutationDomain::kItems);
813 if (!pasted_entities.empty()) {
814 interaction_context_.NotifyEntityChanged();
816 return pasted_entities;
819void DungeonObjectInteraction::HandlePasteObjects() {
820 if (!HasClipboardData()) {
824 if (!rooms_ || current_room_id_ < 0 ||
825 current_room_id_ >=
static_cast<int>(rooms_->size())) {
829 auto& handler = entity_coordinator_.tile_handler();
830 const ImGuiIO& io = ImGui::GetIO();
831 const auto [canvas_mouse_x, canvas_mouse_y] =
832 GetCanvasTransform().ScreenToRoomPixelCoordinates(io.MousePos);
833 auto [paste_x, paste_y] =
834 CanvasToRoomCoordinates(canvas_mouse_x, canvas_mouse_y);
835 int paste_pixel_x = paste_x * dungeon_coords::kTileSize;
836 int paste_pixel_y = paste_y * dungeon_coords::kTileSize;
838 if (!IsWithinCanvasBounds(canvas_mouse_x, canvas_mouse_y, 0)) {
839 const int fallback_delta = entity_clipboard_.sprites.empty()
840 ? dungeon_coords::kTileSize
841 : dungeon_coords::kSpriteTileSize;
843 std::clamp(entity_clipboard_.origin_pixel_x + fallback_delta, 0,
844 dungeon_coords::kRoomPixelWidth - dungeon_coords::kTileSize);
845 paste_pixel_y = std::clamp(
846 entity_clipboard_.origin_pixel_y + fallback_delta, 0,
847 dungeon_coords::kRoomPixelHeight - dungeon_coords::kTileSize);
848 paste_x = paste_pixel_x / dungeon_coords::kTileSize;
849 paste_y = paste_pixel_y / dungeon_coords::kTileSize;
852 std::vector<size_t> new_indices;
853 if (handler.HasClipboardData()) {
854 new_indices = handler.PasteFromClipboard(
855 current_room_id_, paste_x - entity_clipboard_.origin_tile_x,
856 paste_y - entity_clipboard_.origin_tile_y);
858 auto new_entities = PasteEntityClipboardAt(paste_pixel_x, paste_pixel_y);
860 if (!new_indices.empty() || !new_entities.empty()) {
861 selection_.ClearSelection();
862 for (
size_t idx : new_indices) {
863 selection_.SelectObject(idx, ObjectSelection::SelectionMode::Add);
865 entity_coordinator_.SetSelectedEntities(std::move(new_entities));
869void DungeonObjectInteraction::DrawGhostPreview() {
870 entity_coordinator_.DrawGhostPreviews();
873void DungeonObjectInteraction::HandleScrollWheelResize() {
874 const ImGuiIO& io = ImGui::GetIO();
875 entity_coordinator_.HandleMouseWheel(io.MouseWheel);
878bool DungeonObjectInteraction::SetObjectId(
size_t index, int16_t
id) {
879 entity_coordinator_.tile_handler().UpdateObjectsId(current_room_id_, {index},
884bool DungeonObjectInteraction::SetObjectSize(
size_t index, uint8_t size) {
885 entity_coordinator_.tile_handler().UpdateObjectsSize(current_room_id_,
890bool DungeonObjectInteraction::SetObjectLayer(
892 return entity_coordinator_.tile_handler().UpdateObjectsLayer(
893 current_room_id_, {index},
static_cast<int>(layer));
896std::pair<int, int> DungeonObjectInteraction::CalculateObjectBounds(
901bool DungeonObjectInteraction::CanAssignSelectedObjectsToLayer(
902 int target_layer)
const {
903 if (!rooms_ || current_room_id_ < 0 ||
904 current_room_id_ >=
static_cast<int>(rooms_->size()) ||
905 target_layer < 0 || target_layer > 2) {
909 const auto selected = selection_.GetSelectedIndices();
910 if (selected.empty()) {
914 const auto& objects = (*rooms_)[current_room_id_].GetTileObjects();
915 for (
const size_t index : selected) {
916 if (index >= objects.size() ||
924bool DungeonObjectInteraction::SendSelectedToLayer(
int target_layer) {
925 if (!CanAssignSelectedObjectsToLayer(target_layer)) {
928 return entity_coordinator_.tile_handler().UpdateObjectsLayer(
929 current_room_id_, selection_.GetSelectedIndices(), target_layer);
932void DungeonObjectInteraction::SendSelectedToFront() {
933 entity_coordinator_.tile_handler().SendToFront(
934 current_room_id_, selection_.GetSelectedIndices());
937void DungeonObjectInteraction::SendSelectedToBack() {
938 entity_coordinator_.tile_handler().SendToBack(
939 current_room_id_, selection_.GetSelectedIndices());
942void DungeonObjectInteraction::BringSelectedForward() {
943 entity_coordinator_.tile_handler().MoveForward(
944 current_room_id_, selection_.GetSelectedIndices());
947void DungeonObjectInteraction::SendSelectedBackward() {
948 entity_coordinator_.tile_handler().MoveBackward(
949 current_room_id_, selection_.GetSelectedIndices());
952void DungeonObjectInteraction::HandleLayerKeyboardShortcuts() {
954 if (!selection_.HasSelection())
958 if (ImGui::IsAnyItemActive())
963 if (ImGui::IsKeyPressed(ImGuiKey_1)) {
964 SendSelectedToLayer(0);
965 }
else if (ImGui::IsKeyPressed(ImGuiKey_2)) {
966 SendSelectedToLayer(1);
967 }
else if (ImGui::IsKeyPressed(ImGuiKey_3)) {
968 SendSelectedToLayer(2);
974 auto& io = ImGui::GetIO();
975 if (io.KeyCtrl && io.KeyShift) {
976 if (ImGui::IsKeyPressed(ImGuiKey_RightBracket)) {
977 SendSelectedToFront();
978 }
else if (ImGui::IsKeyPressed(ImGuiKey_LeftBracket)) {
979 SendSelectedToBack();
981 }
else if (io.KeyCtrl) {
982 if (ImGui::IsKeyPressed(ImGuiKey_RightBracket)) {
983 BringSelectedForward();
984 }
else if (ImGui::IsKeyPressed(ImGuiKey_LeftBracket)) {
985 SendSelectedBackward();
994void DungeonObjectInteraction::SetDoorPlacementMode(
bool enabled,
997 mode_manager_.SetMode(InteractionMode::PlaceDoor);
998 entity_coordinator_.door_handler().SetDoorType(type);
999 entity_coordinator_.door_handler().BeginPlacement();
1001 entity_coordinator_.door_handler().CancelPlacement();
1002 if (mode_manager_.GetMode() == InteractionMode::PlaceDoor)
1003 mode_manager_.SetMode(InteractionMode::Select);
1011void DungeonObjectInteraction::SetSpritePlacementMode(
bool enabled,
1012 uint8_t sprite_id) {
1014 mode_manager_.SetMode(InteractionMode::PlaceSprite);
1015 entity_coordinator_.sprite_handler().SetSpriteId(sprite_id);
1016 entity_coordinator_.sprite_handler().BeginPlacement();
1018 entity_coordinator_.sprite_handler().CancelPlacement();
1019 if (mode_manager_.GetMode() == InteractionMode::PlaceSprite)
1020 mode_manager_.SetMode(InteractionMode::Select);
1028void DungeonObjectInteraction::SetItemPlacementMode(
bool enabled,
1031 mode_manager_.SetMode(InteractionMode::PlaceItem);
1032 entity_coordinator_.item_handler().SetItemId(item_id);
1033 entity_coordinator_.item_handler().BeginPlacement();
1035 entity_coordinator_.item_handler().CancelPlacement();
1036 if (mode_manager_.GetMode() == InteractionMode::PlaceItem)
1037 mode_manager_.SetMode(InteractionMode::Select);
1045void DungeonObjectInteraction::SelectEntity(
EntityType type,
size_t index) {
1046 selection_.ClearSelection();
1047 entity_coordinator_.SelectEntity(type, index);
1050void DungeonObjectInteraction::ClearEntitySelection() {
1051 entity_coordinator_.ClearEntitySelection();
1054void DungeonObjectInteraction::CancelPlacement() {
1055 entity_coordinator_.CancelPlacement();
1056 if (mode_manager_.IsPlacementActive()) {
1057 mode_manager_.SetMode(InteractionMode::Select);
1061void DungeonObjectInteraction::DrawEntitySelectionHighlights() {
1062 entity_coordinator_.DrawSelectionHighlights();
1063 entity_coordinator_.DrawPostPlacementOverlays();
1066void DungeonObjectInteraction::DrawDoorSnapIndicators() {
DungeonCanvasTransform GetCanvasTransform() const
InteractionContext interaction_context_
void HandleCanvasMouseInput()
void HandleObjectSelectionStart(const ImVec2 &canvas_mouse_pos)
InteractionCoordinator entity_coordinator_
ObjectSelection selection_
void HandleMouseRelease()
void HandleLayerKeyboardShortcuts()
void HandleLeftClick(const ImVec2 &canvas_mouse_pos)
void UpdateWaterFillPainting(const ImVec2 &canvas_mouse_pos)
bool HasEntitySelection() const
std::pair< int, int > CanvasToRoomCoordinates(int canvas_x, int canvas_y) const
void HandleEmptySpaceClick(const ImVec2 &canvas_mouse_pos)
DungeonRoomStore * rooms_
bool HandleKeyboardNudge()
InteractionModeManager mode_manager_
void UpdateCollisionPainting(const ImVec2 &canvas_mouse_pos)
bool IsPlacementActive() const
Check if any placement mode is active.
bool HandleClick(int canvas_x, int canvas_y)
Handle click at canvas position.
bool HandleMouseWheel(float delta)
bool HasEntitySelection() const
void HandleDrag(ImVec2 current_pos, ImVec2 delta)
Handle drag operation.
InteractionMode GetMode() const
Get current interaction mode.
ModeState & GetModeState()
Get mutable reference to mode state.
bool IsRectangleSelectionActive() const
Check if a rectangle selection is in progress.
bool HasSelection() const
Check if any objects are selected.
bool IsMouseHovering() const
static DimensionService & Get()
std::tuple< int, int, int, int > GetSelectionBoundsPixels(const RoomObject &obj) const
DimensionResult GetDimensions(const RoomObject &obj) const
std::pair< int, int > GetPixelDimensions(const RoomObject &obj) const
#define ICON_MD_DRAG_INDICATOR
#define ICON_MD_TOUCH_APP
constexpr int kRoomPixelMax
uint16_t EncodePotItemPosition(int pixel_x, int pixel_y)
bool IsWithinBounds(int canvas_x, int canvas_y, int margin=0)
Check if coordinates are within room bounds.
void ForEachPointInSquareBrush(int cx, int cy, int radius, int min_x, int min_y, int max_x, int max_y, Fn &&fn)
void ForEachPointOnLine(int x0, int y0, int x1, int y1, Fn &&fn)
Editors are the view controllers for the application.
EntityType
Type of entity that can be selected in the dungeon editor.
DoorType
Door types from ALTTP.
int GetObjectSubtype(int object_id)
bool UsesRoomObjectStream(const RoomObject &object)
std::string GetObjectName(int object_id)
void NotifyInvalidateCache(MutationDomain domain=MutationDomain::kUnknown) const
Notify that cache invalidation is needed.
void NotifyMutation(MutationDomain domain=MutationDomain::kUnknown) const
Notify that a mutation is about to happen.
Represents a selected entity in the dungeon editor.