3#include "absl/strings/str_format.h"
11#include "imgui/imgui.h"
33 const int encoded_x = std::clamp(clamped_x / 4, 0, 255);
34 const int encoded_y = std::clamp(clamped_y / 16, 0, 255);
35 return static_cast<uint16_t
>((encoded_y << 8) | encoded_x);
41 const ImGuiIO& io = ImGui::GetIO();
43 const bool mouse_left_down = ImGui::IsMouseDown(ImGuiMouseButton_Left);
44 const bool mouse_left_released =
45 ImGui::IsMouseReleased(ImGuiMouseButton_Left);
50 const bool should_process_without_hover =
54 (mouse_left_down || mouse_left_released)) ||
57 if (!hovered && !should_process_without_hover) {
61 const ImVec2 canvas_mouse_pos =
63 const int canvas_mouse_x =
static_cast<int>(std::floor(canvas_mouse_pos.x));
64 const int canvas_mouse_y =
static_cast<int>(std::floor(canvas_mouse_pos.y));
65 const bool pointer_within_room =
69 if (ImGui::IsKeyPressed(ImGuiKey_Escape) &&
76 if (pointer_within_room &&
87 if (hovered && mouse_left_down) {
90 if (pointer_within_room) {
96 if (pointer_within_room) {
103 if (hovered && ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
108 if (ImGui::IsMouseDragging(ImGuiMouseButton_Left)) {
113 if (mouse_left_released) {
119 int canvas_x =
static_cast<int>(std::floor(canvas_mouse_pos.x));
120 int canvas_y =
static_cast<int>(std::floor(canvas_mouse_pos.y));
146 const ImVec2& canvas_mouse_pos) {
147 auto [room_x, room_y] =
149 static_cast<int>(canvas_mouse_pos.y));
155 if (room_x >= 0 && room_x < 64 && room_y >= 0 && room_y < 64) {
157 if (!state.is_painting) {
159 state.paint_mutation_started =
false;
160 state.paint_last_tile_x = room_x;
161 state.paint_last_tile_y = room_y;
165 (state.paint_last_tile_x >= 0) ? state.paint_last_tile_x : room_x;
167 (state.paint_last_tile_y >= 0) ? state.paint_last_tile_y : room_y;
169 bool changed =
false;
170 auto ensure_mutation = [&]() {
171 if (!state.paint_mutation_started) {
173 state.paint_mutation_started =
true;
178 x0, y0, room_x, room_y, [&](
int lx,
int ly) {
180 lx, ly, state.paint_brush_radius,
182 [&](
int bx,
int by) {
183 if (room.GetCollisionTile(bx, by) ==
184 state.paint_collision_value) {
188 room.SetCollisionTile(bx, by, state.paint_collision_value);
198 state.paint_last_tile_x = room_x;
199 state.paint_last_tile_y = room_y;
204void DungeonObjectInteraction::UpdateWaterFillPainting(
205 const ImVec2& canvas_mouse_pos) {
207 mode_manager_.SetMode(InteractionMode::Select);
211 const ImGuiIO& io = ImGui::GetIO();
212 const bool erase = io.KeyAlt;
214 auto [room_x, room_y] =
215 CanvasToRoomCoordinates(
static_cast<int>(canvas_mouse_pos.x),
216 static_cast<int>(canvas_mouse_pos.y));
217 if (rooms_ && current_room_id_ >= 0 && current_room_id_ < 296) {
218 auto& room = (*rooms_)[current_room_id_];
219 auto& state = mode_manager_.GetModeState();
222 if (room_x >= 0 && room_x < 64 && room_y >= 0 && room_y < 64) {
223 const bool new_val = !erase;
225 if (!state.is_painting) {
226 state.is_painting =
true;
227 state.paint_mutation_started =
false;
228 state.paint_last_tile_x = room_x;
229 state.paint_last_tile_y = room_y;
233 (state.paint_last_tile_x >= 0) ? state.paint_last_tile_x : room_x;
235 (state.paint_last_tile_y >= 0) ? state.paint_last_tile_y : room_y;
237 bool changed =
false;
238 auto ensure_mutation = [&]() {
239 if (!state.paint_mutation_started) {
240 interaction_context_.NotifyMutation(MutationDomain::kWaterFill);
241 state.paint_mutation_started =
true;
245 paint_util::ForEachPointOnLine(
246 x0, y0, room_x, room_y, [&](
int lx,
int ly) {
247 paint_util::ForEachPointInSquareBrush(
248 lx, ly, state.paint_brush_radius,
250 [&](
int bx,
int by) {
251 if (room.GetWaterFillTile(bx, by) == new_val) {
255 room.SetWaterFillTile(bx, by, new_val);
261 interaction_context_.NotifyInvalidateCache(MutationDomain::kWaterFill);
264 state.paint_last_tile_x = room_x;
265 state.paint_last_tile_y = room_y;
270void DungeonObjectInteraction::HandleObjectSelectionStart(
271 const ImVec2& canvas_mouse_pos) {
272 const bool has_object_selection = selection_.HasSelection();
273 const bool has_entity_selection = HasEntitySelection();
274 if (!has_object_selection && !has_entity_selection) {
278 mode_manager_.SetMode(InteractionMode::DraggingObjects);
279 if (has_object_selection) {
280 entity_coordinator_.tile_handler().InitDrag(canvas_mouse_pos);
282 if (has_entity_selection) {
283 entity_coordinator_.BeginSelectionDrag(canvas_mouse_pos);
287void DungeonObjectInteraction::HandleEmptySpaceClick(
288 const ImVec2& canvas_mouse_pos) {
289 const ImGuiIO& io = ImGui::GetIO();
290 const bool additive = io.KeyShift || io.KeyCtrl || io.KeySuper;
291 const bool had_selection =
292 selection_.HasSelection() || entity_coordinator_.HasEntitySelection();
297 ClearEntitySelection();
298 selection_.ClearSelection();
301 if (!had_selection) {
302 entity_coordinator_.tile_handler().BeginMarqueeSelection(canvas_mouse_pos);
306void DungeonObjectInteraction::HandleMouseRelease() {
310 const auto mode = mode_manager_.GetMode();
311 if (mode == InteractionMode::PaintCollision ||
312 mode == InteractionMode::PaintWaterFill) {
313 auto& state = mode_manager_.GetModeState();
314 const bool had_mutation = state.paint_mutation_started;
315 state.is_painting =
false;
316 state.paint_mutation_started =
false;
317 state.paint_last_tile_x = -1;
318 state.paint_last_tile_y = -1;
322 interaction_context_.NotifyInvalidateCache(
323 (mode == InteractionMode::PaintCollision)
324 ? MutationDomain::kCustomCollision
325 : MutationDomain::kWaterFill);
330 if (mode_manager_.GetMode() == InteractionMode::DraggingObjects) {
331 mode_manager_.SetMode(InteractionMode::Select);
333 entity_coordinator_.HandleRelease();
338bool DungeonObjectInteraction::HandleKeyboardNudge() {
339 if (!rooms_ || current_room_id_ < 0 ||
340 current_room_id_ >=
static_cast<int>(rooms_->size())) {
344 const ImGuiIO& io = ImGui::GetIO();
345 if (ImGui::IsAnyItemActive() || io.KeyCtrl || io.KeySuper || io.KeyAlt ||
346 ImGui::IsMouseDown(ImGuiMouseButton_Left) ||
347 entity_coordinator_.IsPlacementActive()) {
351 const auto mode = mode_manager_.GetMode();
352 if (mode == InteractionMode::DraggingObjects ||
353 mode == InteractionMode::PaintCollision ||
354 mode == InteractionMode::PaintWaterFill) {
360 if (ImGui::IsKeyPressed(ImGuiKey_LeftArrow,
true)) {
362 }
else if (ImGui::IsKeyPressed(ImGuiKey_RightArrow,
true)) {
365 if (ImGui::IsKeyPressed(ImGuiKey_UpArrow,
true)) {
367 }
else if (ImGui::IsKeyPressed(ImGuiKey_DownArrow,
true)) {
371 if (delta_x == 0 && delta_y == 0) {
375 return NudgeSelected(delta_x, delta_y);
378bool DungeonObjectInteraction::NudgeSelected(
int delta_x,
int delta_y) {
379 if (!rooms_ || current_room_id_ < 0 ||
380 current_room_id_ >=
static_cast<int>(rooms_->size())) {
384 bool handled =
false;
385 if (selection_.HasSelection()) {
386 entity_coordinator_.tile_handler().MoveObjects(
387 current_room_id_, selection_.GetSelectedIndices(), delta_x, delta_y);
391 if (entity_coordinator_.HasEntitySelection()) {
392 handled = entity_coordinator_.NudgeSelected(delta_x, delta_y) || handled;
398void DungeonObjectInteraction::CheckForObjectSelection() {
400 const ImGuiIO& io = ImGui::GetIO();
401 const ImVec2 mouse_pos = GetCanvasTransform().ScreenToRoomPixels(io.MousePos);
402 const bool mouse_left_released =
403 ImGui::IsMouseReleased(ImGuiMouseButton_Left);
405 if (mouse_left_released && selection_.IsRectangleSelectionActive()) {
406 selection_.UpdateRectangleSelection(
static_cast<int>(mouse_pos.x),
407 static_cast<int>(mouse_pos.y));
408 constexpr int kMinRectPixels = 6;
409 if (!io.KeyAlt && selection_.IsRectangleLargeEnough(kMinRectPixels)) {
410 entity_coordinator_.SelectEntitiesInRect(
411 selection_.GetRectangleSelectionBounds(),
417 entity_coordinator_.tile_handler().HandleMarqueeSelection(
419 ImGui::IsMouseDown(ImGuiMouseButton_Left),
422 io.KeyCtrl || io.KeySuper,
426void DungeonObjectInteraction::DrawSelectionHighlights() {
427 if (!rooms_ || current_room_id_ < 0 || current_room_id_ >= 296)
430 auto& room = (*rooms_)[current_room_id_];
431 const auto& objects = room.GetTileObjects();
434 selection_.DrawSelectionHighlights(
437 return std::make_tuple(result.offset_x_tiles * 8,
438 result.offset_y_tiles * 8, result.width_pixels(),
439 result.height_pixels());
444 if (entity_coordinator_.HasEntitySelection()) {
448 if (canvas_->IsMouseHovering()) {
450 ImGuiIO& io = ImGui::GetIO();
451 const auto [cursor_x, cursor_y] =
452 GetCanvasTransform().ScreenToRoomPixelCoordinates(io.MousePos);
453 auto entity_at_cursor =
454 entity_coordinator_.GetEntityAtPosition(cursor_x, cursor_y);
455 if (entity_at_cursor.has_value()) {
457 DrawHoverHighlight(objects);
461 auto hovered_index = entity_coordinator_.tile_handler().GetEntityAtPosition(
463 if (hovered_index.has_value() && *hovered_index < objects.size()) {
464 const auto&
object = objects[*hovered_index];
467 const int layer =
object.GetLayerValue();
470 const char* subtype_names[] = {
"Unknown",
"Type 1",
"Type 2",
"Type 3"};
471 const char* subtype_name =
472 (subtype >= 0 && subtype <= 3) ? subtype_names[subtype] :
"Unknown";
476 tooltip += object_name;
477 tooltip +=
" (" + std::string(subtype_name) +
")";
479 tooltip +=
"ID: 0x" + absl::StrFormat(
"%03X",
object.id_);
481 static constexpr const char* kStreamNames[] = {
"Primary",
"BG2 overlay",
483 const char* stream_name =
484 (layer >= 0 && layer < 3) ? kStreamNames[layer] :
"Unknown";
485 tooltip +=
" | Object stream: " + std::string(stream_name);
487 const char* layer_name =
488 layer == 0 ?
"Upper layer (BG1)" :
"Lower layer (BG2)";
489 tooltip +=
" | Special layer: " + std::string(layer_name);
491 tooltip +=
" | Pos: (" + std::to_string(
object.x_) +
", " +
492 std::to_string(
object.y_) +
")";
495 tooltip += absl::StrFormat(
496 "\nSize: %d x %d tiles",
500 tooltip += absl::StrFormat(
"\nSize: 0x%02X",
object.size_);
503 if (selection_.IsObjectSelected(*hovered_index)) {
505 tooltip +=
"\n" ICON_MD_MOUSE " Wheel: height | Shift+wheel: width";
514 ImGui::SetTooltip(
"%s", tooltip.c_str());
519 DrawHoverHighlight(objects);
522void DungeonObjectInteraction::DrawHoverHighlight(
523 const std::vector<zelda3::RoomObject>& objects) {
524 if (!canvas_->IsMouseHovering())
528 if (entity_coordinator_.HasEntitySelection())
533 ImGuiIO& io = ImGui::GetIO();
535 const auto [cursor_canvas_x, cursor_canvas_y] =
537 auto entity_at_cursor =
538 entity_coordinator_.GetEntityAtPosition(cursor_canvas_x, cursor_canvas_y);
539 if (entity_at_cursor.has_value()) {
543 auto hovered_index = entity_coordinator_.tile_handler().GetEntityAtPosition(
544 cursor_canvas_x, cursor_canvas_y);
545 if (!hovered_index.has_value() || *hovered_index >= objects.size()) {
548 const auto&
object = objects[*hovered_index];
551 if (selection_.IsObjectSelected(*hovered_index)) {
555 const auto& theme = AgentUI::GetTheme();
556 ImDrawList* draw_list = ImGui::GetWindowDrawList();
558 auto [sel_x_px, sel_y_px, pixel_width, pixel_height] =
562 ImVec2(
static_cast<float>(sel_x_px),
static_cast<float>(sel_y_px)));
564 static_cast<float>(pixel_width),
static_cast<float>(pixel_height)));
565 ImVec2 obj_end(obj_start.x + obj_size.x, obj_start.y + obj_size.y);
568 constexpr float margin = 2.0f;
569 obj_start.x -= margin;
570 obj_start.y -= margin;
575 ImVec4 hover_fill = theme.selection_hover;
576 hover_fill.w *= 0.5f;
578 ImVec4 hover_border = theme.selection_hover;
581 draw_list->AddRectFilled(obj_start, obj_end, ImGui::GetColorU32(hover_fill));
584 draw_list->AddRect(obj_start, obj_end, ImGui::GetColorU32(hover_border), 0.0f,
588void DungeonObjectInteraction::PlaceObjectAtPosition(
int room_x,
int room_y) {
589 auto& tile_handler = entity_coordinator_.tile_handler();
590 const auto object = tile_handler.GetPreviewObject();
591 tile_handler.PlaceObjectAt(current_room_id_,
object, room_x, room_y);
593 if (object_placed_callback_) {
594 object_placed_callback_(
object);
597 interaction_context_.NotifyInvalidateCache(MutationDomain::kTileObjects);
601std::pair<int, int> DungeonObjectInteraction::RoomToCanvasCoordinates(
602 int room_x,
int room_y)
const {
604 return {room_x * 8, room_y * 8};
607std::pair<int, int> DungeonObjectInteraction::CanvasToRoomCoordinates(
608 int canvas_x,
int canvas_y)
const {
610 return {canvas_x / 8, canvas_y / 8};
613bool DungeonObjectInteraction::IsWithinCanvasBounds(
int canvas_x,
int canvas_y,
615 return dungeon_coords::IsWithinBounds(canvas_x, canvas_y, margin);
621 current_room_id_ = room_id;
622 interaction_context_.rooms = rooms;
623 interaction_context_.current_room_id = room_id;
624 interaction_context_.selection = &selection_;
625 entity_coordinator_.SetContext(&interaction_context_);
628void DungeonObjectInteraction::SetPreviewObject(
630 if (loaded &&
object.id_ >= 0) {
633 entity_coordinator_.CancelPlacement();
636 mode_manager_.SetMode(InteractionMode::PlaceObject);
640 auto& tile_handler = entity_coordinator_.tile_handler();
641 tile_handler.SetPreviewObject(
object);
642 if (!tile_handler.IsPlacementActive()) {
643 tile_handler.BeginPlacement();
647 if (mode_manager_.GetMode() == InteractionMode::PlaceObject) {
653void DungeonObjectInteraction::ClearSelection() {
654 selection_.ClearSelection();
655 if (mode_manager_.GetMode() == InteractionMode::DraggingObjects) {
656 mode_manager_.SetMode(InteractionMode::Select);
660void DungeonObjectInteraction::HandleDeleteSelected() {
661 auto indices = selection_.GetSelectedIndices();
662 if (!indices.empty()) {
663 entity_coordinator_.tile_handler().DeleteObjects(current_room_id_, indices);
664 selection_.ClearSelection();
667 if (entity_coordinator_.HasEntitySelection()) {
668 entity_coordinator_.DeleteSelectedEntity();
672void DungeonObjectInteraction::HandleDeleteAllObjects() {
673 entity_coordinator_.tile_handler().DeleteAllObjects(current_room_id_);
674 selection_.ClearSelection();
677void DungeonObjectInteraction::HandleCopySelected() {
678 if (!rooms_ || current_room_id_ < 0 ||
679 current_room_id_ >=
static_cast<int>(rooms_->size())) {
683 const auto selected_objects = selection_.GetSelectedIndices();
684 const bool has_object_selection = !selected_objects.empty();
685 const bool has_entity_selection = entity_coordinator_.HasEntitySelection();
686 if (!has_object_selection && !has_entity_selection) {
690 entity_clipboard_.Clear();
691 bool clipboard_origin_set =
false;
693 if (has_object_selection) {
694 entity_coordinator_.tile_handler().CopyObjectsToClipboard(current_room_id_,
696 const auto& objects = (*rooms_)[current_room_id_].GetTileObjects();
697 for (
size_t index : selected_objects) {
698 if (index >= objects.size()) {
701 entity_clipboard_.origin_tile_x = objects[index].x_;
702 entity_clipboard_.origin_tile_y = objects[index].y_;
703 entity_clipboard_.origin_pixel_x =
704 entity_clipboard_.origin_tile_x * dungeon_coords::kTileSize;
705 entity_clipboard_.origin_pixel_y =
706 entity_clipboard_.origin_tile_y * dungeon_coords::kTileSize;
707 clipboard_origin_set =
true;
711 entity_coordinator_.tile_handler().ClearClipboard();
714 CopySelectedEntitiesToClipboard(clipboard_origin_set);
717void DungeonObjectInteraction::CopySelectedEntitiesToClipboard(
718 bool clipboard_origin_set) {
719 if (!rooms_ || current_room_id_ < 0 ||
720 current_room_id_ >=
static_cast<int>(rooms_->size())) {
724 const auto& room = (*rooms_)[current_room_id_];
725 auto selected_entities = entity_coordinator_.GetSelectedEntities();
726 if (selected_entities.empty() && entity_coordinator_.HasEntitySelection()) {
727 const SelectedEntity selected = entity_coordinator_.GetSelectedEntity();
728 if (selected.
type != EntityType::None) {
729 selected_entities.push_back(selected);
733 for (
const auto entity : selected_entities) {
734 switch (entity.type) {
735 case EntityType::Sprite: {
736 const auto& sprites = room.GetSprites();
737 if (entity.index >= sprites.size()) {
740 const auto& sprite = sprites[entity.index];
741 if (!clipboard_origin_set && !entity_clipboard_.HasData()) {
742 entity_clipboard_.origin_pixel_x =
743 sprite.x() * dungeon_coords::kSpriteTileSize;
744 entity_clipboard_.origin_pixel_y =
745 sprite.y() * dungeon_coords::kSpriteTileSize;
746 entity_clipboard_.origin_tile_x =
747 entity_clipboard_.origin_pixel_x / dungeon_coords::kTileSize;
748 entity_clipboard_.origin_tile_y =
749 entity_clipboard_.origin_pixel_y / dungeon_coords::kTileSize;
751 entity_clipboard_.sprites.push_back(sprite);
754 case EntityType::Item: {
755 const auto& items = room.GetPotItems();
756 if (entity.index >= items.size()) {
759 const auto& item = items[entity.index];
760 if (!clipboard_origin_set && !entity_clipboard_.HasData()) {
761 entity_clipboard_.origin_pixel_x = item.GetPixelX();
762 entity_clipboard_.origin_pixel_y = item.GetPixelY();
763 entity_clipboard_.origin_tile_x =
764 entity_clipboard_.origin_pixel_x / dungeon_coords::kTileSize;
765 entity_clipboard_.origin_tile_y =
766 entity_clipboard_.origin_pixel_y / dungeon_coords::kTileSize;
768 entity_clipboard_.items.push_back(item);
771 case EntityType::Door:
772 case EntityType::Object:
773 case EntityType::None:
780std::vector<SelectedEntity> DungeonObjectInteraction::PasteEntityClipboardAt(
781 int target_pixel_x,
int target_pixel_y) {
782 std::vector<SelectedEntity> pasted_entities;
783 if (!entity_clipboard_.HasData() || !rooms_ || current_room_id_ < 0 ||
784 current_room_id_ >=
static_cast<int>(rooms_->size())) {
785 return pasted_entities;
788 auto& room = (*rooms_)[current_room_id_];
789 const int delta_pixel_x = target_pixel_x - entity_clipboard_.origin_pixel_x;
790 const int delta_pixel_y = target_pixel_y - entity_clipboard_.origin_pixel_y;
792 if (!entity_clipboard_.sprites.empty()) {
793 interaction_context_.NotifyMutation(MutationDomain::kSprites);
794 auto& sprites = room.GetSprites();
795 for (
auto sprite : entity_clipboard_.sprites) {
796 const int next_x = std::clamp(
797 (sprite.x() * dungeon_coords::kSpriteTileSize + delta_pixel_x) /
798 dungeon_coords::kSpriteTileSize,
799 0, dungeon_coords::kSpriteGridMax);
800 const int next_y = std::clamp(
801 (sprite.y() * dungeon_coords::kSpriteTileSize + delta_pixel_y) /
802 dungeon_coords::kSpriteTileSize,
803 0, dungeon_coords::kSpriteGridMax);
804 sprite.set_x(next_x);
805 sprite.set_y(next_y);
806 sprites.push_back(sprite);
807 pasted_entities.push_back(
810 room.MarkSpritesDirty();
811 interaction_context_.NotifyInvalidateCache(MutationDomain::kSprites);
814 if (!entity_clipboard_.items.empty()) {
815 interaction_context_.NotifyMutation(MutationDomain::kItems);
816 auto& items = room.GetPotItems();
817 for (
auto item : entity_clipboard_.items) {
818 item.position = EncodePotItemPosition(item.GetPixelX() + delta_pixel_x,
819 item.GetPixelY() + delta_pixel_y);
820 items.push_back(item);
821 pasted_entities.push_back(
824 room.MarkPotItemsDirty();
825 interaction_context_.NotifyInvalidateCache(MutationDomain::kItems);
828 if (!pasted_entities.empty()) {
829 interaction_context_.NotifyEntityChanged();
831 return pasted_entities;
834void DungeonObjectInteraction::HandlePasteObjects() {
835 if (!HasClipboardData()) {
839 if (!rooms_ || current_room_id_ < 0 ||
840 current_room_id_ >=
static_cast<int>(rooms_->size())) {
844 auto& handler = entity_coordinator_.tile_handler();
845 const ImGuiIO& io = ImGui::GetIO();
846 const auto [canvas_mouse_x, canvas_mouse_y] =
847 GetCanvasTransform().ScreenToRoomPixelCoordinates(io.MousePos);
848 auto [paste_x, paste_y] =
849 CanvasToRoomCoordinates(canvas_mouse_x, canvas_mouse_y);
850 int paste_pixel_x = paste_x * dungeon_coords::kTileSize;
851 int paste_pixel_y = paste_y * dungeon_coords::kTileSize;
853 if (!IsWithinCanvasBounds(canvas_mouse_x, canvas_mouse_y, 0)) {
854 const int fallback_delta = entity_clipboard_.sprites.empty()
855 ? dungeon_coords::kTileSize
856 : dungeon_coords::kSpriteTileSize;
858 std::clamp(entity_clipboard_.origin_pixel_x + fallback_delta, 0,
859 dungeon_coords::kRoomPixelWidth - dungeon_coords::kTileSize);
860 paste_pixel_y = std::clamp(
861 entity_clipboard_.origin_pixel_y + fallback_delta, 0,
862 dungeon_coords::kRoomPixelHeight - dungeon_coords::kTileSize);
863 paste_x = paste_pixel_x / dungeon_coords::kTileSize;
864 paste_y = paste_pixel_y / dungeon_coords::kTileSize;
867 std::vector<size_t> new_indices;
868 if (handler.HasClipboardData()) {
869 new_indices = handler.PasteFromClipboard(
870 current_room_id_, paste_x - entity_clipboard_.origin_tile_x,
871 paste_y - entity_clipboard_.origin_tile_y);
873 auto new_entities = PasteEntityClipboardAt(paste_pixel_x, paste_pixel_y);
875 if (!new_indices.empty() || !new_entities.empty()) {
876 selection_.ClearSelection();
877 for (
size_t idx : new_indices) {
878 selection_.SelectObject(idx, ObjectSelection::SelectionMode::Add);
880 entity_coordinator_.SetSelectedEntities(std::move(new_entities));
884void DungeonObjectInteraction::DrawGhostPreview() {
885 entity_coordinator_.DrawGhostPreviews();
888void DungeonObjectInteraction::HandleScrollWheelResize() {
889 const ImGuiIO& io = ImGui::GetIO();
890 entity_coordinator_.HandleMouseWheel(io.MouseWheel);
893bool DungeonObjectInteraction::SetObjectId(
size_t index, int16_t
id) {
894 entity_coordinator_.tile_handler().UpdateObjectsId(current_room_id_, {index},
899bool DungeonObjectInteraction::SetObjectSize(
size_t index, uint8_t size) {
900 entity_coordinator_.tile_handler().UpdateObjectsSize(current_room_id_,
905bool DungeonObjectInteraction::SetObjectLayer(
907 return entity_coordinator_.tile_handler().UpdateObjectsLayer(
908 current_room_id_, {index},
static_cast<int>(layer));
911std::pair<int, int> DungeonObjectInteraction::CalculateObjectBounds(
916bool DungeonObjectInteraction::CanAssignSelectedObjectsToLayer(
917 int target_layer)
const {
918 if (!rooms_ || current_room_id_ < 0 ||
919 current_room_id_ >=
static_cast<int>(rooms_->size()) ||
920 target_layer < 0 || target_layer > 2) {
924 const auto selected = selection_.GetSelectedIndices();
925 if (selected.empty()) {
929 const auto& objects = (*rooms_)[current_room_id_].GetTileObjects();
930 for (
const size_t index : selected) {
931 if (index >= objects.size() ||
939bool DungeonObjectInteraction::SendSelectedToLayer(
int target_layer) {
940 if (!CanAssignSelectedObjectsToLayer(target_layer)) {
943 return entity_coordinator_.tile_handler().UpdateObjectsLayer(
944 current_room_id_, selection_.GetSelectedIndices(), target_layer);
947void DungeonObjectInteraction::SendSelectedToFront() {
948 entity_coordinator_.tile_handler().SendToFront(
949 current_room_id_, selection_.GetSelectedIndices());
952void DungeonObjectInteraction::SendSelectedToBack() {
953 entity_coordinator_.tile_handler().SendToBack(
954 current_room_id_, selection_.GetSelectedIndices());
957void DungeonObjectInteraction::BringSelectedForward() {
958 entity_coordinator_.tile_handler().MoveForward(
959 current_room_id_, selection_.GetSelectedIndices());
962void DungeonObjectInteraction::SendSelectedBackward() {
963 entity_coordinator_.tile_handler().MoveBackward(
964 current_room_id_, selection_.GetSelectedIndices());
967void DungeonObjectInteraction::HandleLayerKeyboardShortcuts() {
969 if (!selection_.HasSelection())
973 if (ImGui::IsAnyItemActive())
978 if (ImGui::IsKeyPressed(ImGuiKey_1)) {
979 SendSelectedToLayer(0);
980 }
else if (ImGui::IsKeyPressed(ImGuiKey_2)) {
981 SendSelectedToLayer(1);
982 }
else if (ImGui::IsKeyPressed(ImGuiKey_3)) {
983 SendSelectedToLayer(2);
989 auto& io = ImGui::GetIO();
990 if (io.KeyCtrl && io.KeyShift) {
991 if (ImGui::IsKeyPressed(ImGuiKey_RightBracket)) {
992 SendSelectedToFront();
993 }
else if (ImGui::IsKeyPressed(ImGuiKey_LeftBracket)) {
994 SendSelectedToBack();
996 }
else if (io.KeyCtrl) {
997 if (ImGui::IsKeyPressed(ImGuiKey_RightBracket)) {
998 BringSelectedForward();
999 }
else if (ImGui::IsKeyPressed(ImGuiKey_LeftBracket)) {
1000 SendSelectedBackward();
1009void DungeonObjectInteraction::SetDoorPlacementMode(
bool enabled,
1012 mode_manager_.SetMode(InteractionMode::PlaceDoor);
1013 entity_coordinator_.door_handler().SetDoorType(type);
1014 entity_coordinator_.door_handler().BeginPlacement();
1016 entity_coordinator_.door_handler().CancelPlacement();
1017 if (mode_manager_.GetMode() == InteractionMode::PlaceDoor)
1018 mode_manager_.SetMode(InteractionMode::Select);
1026void DungeonObjectInteraction::SetSpritePlacementMode(
bool enabled,
1027 uint8_t sprite_id) {
1029 mode_manager_.SetMode(InteractionMode::PlaceSprite);
1030 entity_coordinator_.sprite_handler().SetSpriteId(sprite_id);
1031 entity_coordinator_.sprite_handler().BeginPlacement();
1033 entity_coordinator_.sprite_handler().CancelPlacement();
1034 if (mode_manager_.GetMode() == InteractionMode::PlaceSprite)
1035 mode_manager_.SetMode(InteractionMode::Select);
1043void DungeonObjectInteraction::SetItemPlacementMode(
bool enabled,
1046 mode_manager_.SetMode(InteractionMode::PlaceItem);
1047 entity_coordinator_.item_handler().SetItemId(item_id);
1048 entity_coordinator_.item_handler().BeginPlacement();
1050 entity_coordinator_.item_handler().CancelPlacement();
1051 if (mode_manager_.GetMode() == InteractionMode::PlaceItem)
1052 mode_manager_.SetMode(InteractionMode::Select);
1060void DungeonObjectInteraction::SelectEntity(
EntityType type,
size_t index) {
1061 selection_.ClearSelection();
1062 entity_coordinator_.SelectEntity(type, index);
1065void DungeonObjectInteraction::ClearEntitySelection() {
1066 entity_coordinator_.ClearEntitySelection();
1069void DungeonObjectInteraction::CancelPlacement() {
1070 entity_coordinator_.CancelPlacement();
1071 if (mode_manager_.IsPlacementActive()) {
1072 mode_manager_.SetMode(InteractionMode::Select);
1076void DungeonObjectInteraction::DrawEntitySelectionHighlights() {
1077 entity_coordinator_.DrawSelectionHighlights();
1078 entity_coordinator_.DrawPostPlacementOverlays();
1081void 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)
int RoomObjectSizeAxisStep(int object_id)
bool UsesRoomObjectStream(const RoomObject &object)
std::string GetObjectName(int object_id)
bool IsRoomObjectResizable(int object_id)
int RoomObjectSizeAxisTiles(int object_id, uint8_t size, bool horizontal)
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.