3#include "absl/strings/str_format.h"
11#include "imgui/imgui.h"
25 const ImGuiIO& io = ImGui::GetIO();
27 const bool mouse_left_down = ImGui::IsMouseDown(ImGuiMouseButton_Left);
28 const bool mouse_left_released =
29 ImGui::IsMouseReleased(ImGuiMouseButton_Left);
34 const bool should_process_without_hover =
38 (mouse_left_down || mouse_left_released)) ||
41 if (!hovered && !should_process_without_hover) {
46 if (ImGui::IsKeyPressed(ImGuiKey_Escape) &&
59 ImVec2 mouse_pos = io.MousePos;
61 ImVec2 canvas_mouse_pos =
62 ImVec2(mouse_pos.x - canvas_pos.x, mouse_pos.y - canvas_pos.y);
65 if (hovered && mouse_left_down) {
77 if (hovered && ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
82 if (ImGui::IsMouseDragging(ImGuiMouseButton_Left)) {
87 if (mouse_left_released) {
93 int canvas_x =
static_cast<int>(canvas_mouse_pos.x);
94 int canvas_y =
static_cast<int>(canvas_mouse_pos.y);
110 const ImVec2& canvas_mouse_pos) {
111 auto [room_x, room_y] =
113 static_cast<int>(canvas_mouse_pos.y));
119 if (room_x >= 0 && room_x < 64 && room_y >= 0 && room_y < 64) {
121 if (!state.is_painting) {
123 state.paint_mutation_started =
false;
124 state.paint_last_tile_x = room_x;
125 state.paint_last_tile_y = room_y;
129 (state.paint_last_tile_x >= 0) ? state.paint_last_tile_x : room_x;
131 (state.paint_last_tile_y >= 0) ? state.paint_last_tile_y : room_y;
133 bool changed =
false;
134 auto ensure_mutation = [&]() {
135 if (!state.paint_mutation_started) {
137 state.paint_mutation_started =
true;
142 x0, y0, room_x, room_y, [&](
int lx,
int ly) {
144 lx, ly, state.paint_brush_radius,
146 [&](
int bx,
int by) {
147 if (room.GetCollisionTile(bx, by) ==
148 state.paint_collision_value) {
152 room.SetCollisionTile(bx, by, state.paint_collision_value);
162 state.paint_last_tile_x = room_x;
163 state.paint_last_tile_y = room_y;
168void DungeonObjectInteraction::UpdateWaterFillPainting(
169 const ImVec2& canvas_mouse_pos) {
170 const ImGuiIO& io = ImGui::GetIO();
171 const bool erase = io.KeyAlt;
173 auto [room_x, room_y] =
174 CanvasToRoomCoordinates(
static_cast<int>(canvas_mouse_pos.x),
175 static_cast<int>(canvas_mouse_pos.y));
176 if (rooms_ && current_room_id_ >= 0 && current_room_id_ < 296) {
177 auto& room = (*rooms_)[current_room_id_];
178 auto& state = mode_manager_.GetModeState();
181 if (room_x >= 0 && room_x < 64 && room_y >= 0 && room_y < 64) {
182 const bool new_val = !erase;
184 if (!state.is_painting) {
185 state.is_painting =
true;
186 state.paint_mutation_started =
false;
187 state.paint_last_tile_x = room_x;
188 state.paint_last_tile_y = room_y;
192 (state.paint_last_tile_x >= 0) ? state.paint_last_tile_x : room_x;
194 (state.paint_last_tile_y >= 0) ? state.paint_last_tile_y : room_y;
196 bool changed =
false;
197 auto ensure_mutation = [&]() {
198 if (!state.paint_mutation_started) {
199 interaction_context_.NotifyMutation(MutationDomain::kWaterFill);
200 state.paint_mutation_started =
true;
204 paint_util::ForEachPointOnLine(
205 x0, y0, room_x, room_y, [&](
int lx,
int ly) {
206 paint_util::ForEachPointInSquareBrush(
207 lx, ly, state.paint_brush_radius,
209 [&](
int bx,
int by) {
210 if (room.GetWaterFillTile(bx, by) == new_val) {
214 room.SetWaterFillTile(bx, by, new_val);
220 interaction_context_.NotifyInvalidateCache(MutationDomain::kWaterFill);
223 state.paint_last_tile_x = room_x;
224 state.paint_last_tile_y = room_y;
229void DungeonObjectInteraction::HandleObjectSelectionStart(
230 const ImVec2& canvas_mouse_pos) {
231 ClearEntitySelection();
232 if (selection_.HasSelection()) {
233 mode_manager_.SetMode(InteractionMode::DraggingObjects);
234 entity_coordinator_.tile_handler().InitDrag(canvas_mouse_pos);
238void DungeonObjectInteraction::HandleEmptySpaceClick(
239 const ImVec2& canvas_mouse_pos) {
240 const ImGuiIO& io = ImGui::GetIO();
241 const bool additive = io.KeyShift || io.KeyCtrl || io.KeySuper;
244 ClearEntitySelection();
248 selection_.ClearSelection();
253 entity_coordinator_.tile_handler().BeginMarqueeSelection(canvas_mouse_pos);
256void DungeonObjectInteraction::HandleMouseRelease() {
260 const auto mode = mode_manager_.GetMode();
261 if (mode == InteractionMode::PaintCollision ||
262 mode == InteractionMode::PaintWaterFill) {
263 auto& state = mode_manager_.GetModeState();
264 const bool had_mutation = state.paint_mutation_started;
265 state.is_painting =
false;
266 state.paint_mutation_started =
false;
267 state.paint_last_tile_x = -1;
268 state.paint_last_tile_y = -1;
272 interaction_context_.NotifyInvalidateCache(
273 (mode == InteractionMode::PaintCollision)
274 ? MutationDomain::kCustomCollision
275 : MutationDomain::kWaterFill);
280 if (mode_manager_.GetMode() == InteractionMode::DraggingObjects) {
281 mode_manager_.SetMode(InteractionMode::Select);
283 entity_coordinator_.HandleRelease();
288void DungeonObjectInteraction::CheckForObjectSelection() {
290 const ImGuiIO& io = ImGui::GetIO();
291 const ImVec2 canvas_pos = canvas_->zero_point();
292 const ImVec2 mouse_pos =
293 ImVec2(io.MousePos.x - canvas_pos.x, io.MousePos.y - canvas_pos.y);
295 entity_coordinator_.tile_handler().HandleMarqueeSelection(
297 ImGui::IsMouseDown(ImGuiMouseButton_Left),
298 ImGui::IsMouseReleased(ImGuiMouseButton_Left),
300 io.KeyCtrl || io.KeySuper,
304void DungeonObjectInteraction::DrawSelectionHighlights() {
305 if (!rooms_ || current_room_id_ < 0 || current_room_id_ >= 296)
308 auto& room = (*rooms_)[current_room_id_];
309 const auto& objects = room.GetTileObjects();
312 selection_.DrawSelectionHighlights(
315 return std::make_tuple(result.offset_x_tiles * 8,
316 result.offset_y_tiles * 8, result.width_pixels(),
317 result.height_pixels());
322 if (entity_coordinator_.HasEntitySelection()) {
326 if (canvas_->IsMouseHovering()) {
328 ImGuiIO& io = ImGui::GetIO();
329 ImVec2 canvas_pos = canvas_->zero_point();
330 int cursor_x =
static_cast<int>(io.MousePos.x - canvas_pos.x);
331 int cursor_y =
static_cast<int>(io.MousePos.y - canvas_pos.y);
332 auto entity_at_cursor =
333 entity_coordinator_.GetEntityAtPosition(cursor_x, cursor_y);
334 if (entity_at_cursor.has_value()) {
336 DrawHoverHighlight(objects);
340 auto hovered_index = entity_coordinator_.tile_handler().GetEntityAtPosition(
342 if (hovered_index.has_value() && *hovered_index < objects.size()) {
343 const auto&
object = objects[*hovered_index];
346 int layer =
object.GetLayerValue();
349 const char* subtype_names[] = {
"Unknown",
"Type 1",
"Type 2",
"Type 3"};
350 const char* subtype_name =
351 (subtype >= 0 && subtype <= 3) ? subtype_names[subtype] :
"Unknown";
355 tooltip += object_name;
356 tooltip +=
" (" + std::string(subtype_name) +
")";
358 tooltip +=
"ID: 0x" + absl::StrFormat(
"%03X",
object.id_);
359 tooltip +=
" | Layer: " + std::to_string(layer + 1);
360 tooltip +=
" | Pos: (" + std::to_string(
object.x_) +
", " +
361 std::to_string(
object.y_) +
")";
362 tooltip +=
"\nSize: " + std::to_string(
object.size_) +
" (0x" +
363 absl::StrFormat(
"%02X",
object.size_) +
")";
365 if (selection_.IsObjectSelected(*hovered_index)) {
372 ImGui::SetTooltip(
"%s", tooltip.c_str());
377 DrawHoverHighlight(objects);
380void DungeonObjectInteraction::DrawHoverHighlight(
381 const std::vector<zelda3::RoomObject>& objects) {
382 if (!canvas_->IsMouseHovering())
386 if (entity_coordinator_.HasEntitySelection())
391 ImGuiIO& io = ImGui::GetIO();
392 ImVec2 canvas_pos = canvas_->zero_point();
393 int cursor_canvas_x =
static_cast<int>(io.MousePos.x - canvas_pos.x);
394 int cursor_canvas_y =
static_cast<int>(io.MousePos.y - canvas_pos.y);
395 auto entity_at_cursor =
396 entity_coordinator_.GetEntityAtPosition(cursor_canvas_x, cursor_canvas_y);
397 if (entity_at_cursor.has_value()) {
401 auto hovered_index = entity_coordinator_.tile_handler().GetEntityAtPosition(
402 cursor_canvas_x, cursor_canvas_y);
403 if (!hovered_index.has_value() || *hovered_index >= objects.size()) {
406 const auto&
object = objects[*hovered_index];
409 if (selection_.IsObjectSelected(*hovered_index)) {
413 const auto& theme = AgentUI::GetTheme();
414 ImDrawList* draw_list = ImGui::GetWindowDrawList();
416 float scale = canvas_->global_scale();
419 auto [sel_x_px, sel_y_px, pixel_width, pixel_height] =
423 ImVec2 obj_start(canvas_pos.x + sel_x_px * scale,
424 canvas_pos.y + sel_y_px * scale);
425 ImVec2 obj_end(obj_start.x + pixel_width * scale,
426 obj_start.y + pixel_height * scale);
429 constexpr float margin = 2.0f;
430 obj_start.x -= margin;
431 obj_start.y -= margin;
436 ImVec4 hover_fill = theme.selection_hover;
437 hover_fill.w *= 0.5f;
439 ImVec4 hover_border = theme.selection_hover;
442 draw_list->AddRectFilled(obj_start, obj_end, ImGui::GetColorU32(hover_fill));
445 draw_list->AddRect(obj_start, obj_end, ImGui::GetColorU32(hover_border), 0.0f,
449void DungeonObjectInteraction::PlaceObjectAtPosition(
int room_x,
int room_y) {
450 entity_coordinator_.tile_handler().PlaceObjectAt(
451 current_room_id_, preview_object_, room_x, room_y);
453 if (object_placed_callback_) {
454 object_placed_callback_(preview_object_);
457 interaction_context_.NotifyInvalidateCache(MutationDomain::kTileObjects);
461std::pair<int, int> DungeonObjectInteraction::RoomToCanvasCoordinates(
462 int room_x,
int room_y)
const {
464 return {room_x * 8, room_y * 8};
467std::pair<int, int> DungeonObjectInteraction::CanvasToRoomCoordinates(
468 int canvas_x,
int canvas_y)
const {
470 return {canvas_x / 8, canvas_y / 8};
473bool DungeonObjectInteraction::IsWithinCanvasBounds(
int canvas_x,
int canvas_y,
475 auto canvas_size = canvas_->canvas_size();
476 auto global_scale = canvas_->global_scale();
477 int scaled_width =
static_cast<int>(canvas_size.x * global_scale);
478 int scaled_height =
static_cast<int>(canvas_size.y * global_scale);
480 return (canvas_x >= -margin && canvas_y >= -margin &&
481 canvas_x <= scaled_width + margin &&
482 canvas_y <= scaled_height + margin);
488 current_room_id_ = room_id;
489 interaction_context_.rooms = rooms;
490 interaction_context_.current_room_id = room_id;
491 interaction_context_.selection = &selection_;
492 entity_coordinator_.SetContext(&interaction_context_);
495void DungeonObjectInteraction::SetPreviewObject(
497 preview_object_ = object;
499 if (loaded &&
object.id_ >= 0) {
502 entity_coordinator_.CancelPlacement();
505 mode_manager_.SetMode(InteractionMode::PlaceObject);
506 mode_manager_.GetModeState().preview_object = object;
510 auto& tile_handler = entity_coordinator_.tile_handler();
511 tile_handler.SetPreviewObject(preview_object_);
512 if (!tile_handler.IsPlacementActive()) {
513 tile_handler.BeginPlacement();
517 if (mode_manager_.GetMode() == InteractionMode::PlaceObject) {
523void DungeonObjectInteraction::ClearSelection() {
524 selection_.ClearSelection();
525 if (mode_manager_.GetMode() == InteractionMode::DraggingObjects) {
526 mode_manager_.SetMode(InteractionMode::Select);
530void DungeonObjectInteraction::HandleDeleteSelected() {
531 auto indices = selection_.GetSelectedIndices();
532 if (!indices.empty()) {
533 entity_coordinator_.tile_handler().DeleteObjects(current_room_id_, indices);
534 selection_.ClearSelection();
537 if (entity_coordinator_.HasEntitySelection()) {
538 entity_coordinator_.DeleteSelectedEntity();
542void DungeonObjectInteraction::HandleDeleteAllObjects() {
543 entity_coordinator_.tile_handler().DeleteAllObjects(current_room_id_);
544 selection_.ClearSelection();
547void DungeonObjectInteraction::HandleCopySelected() {
548 entity_coordinator_.tile_handler().CopyObjectsToClipboard(
549 current_room_id_, selection_.GetSelectedIndices());
552void DungeonObjectInteraction::HandlePasteObjects() {
553 auto& handler = entity_coordinator_.tile_handler();
554 if (!handler.HasClipboardData())
557 const ImGuiIO& io = ImGui::GetIO();
558 ImVec2 canvas_mouse_pos = ImVec2(io.MousePos.x - canvas_->zero_point().x,
559 io.MousePos.y - canvas_->zero_point().y);
560 auto [paste_x, paste_y] =
561 CanvasToRoomCoordinates(
static_cast<int>(canvas_mouse_pos.x),
562 static_cast<int>(canvas_mouse_pos.y));
565 handler.PasteFromClipboardAt(current_room_id_, paste_x, paste_y);
568 if (!new_indices.empty()) {
569 selection_.ClearSelection();
570 for (
size_t idx : new_indices) {
571 selection_.SelectObject(idx, ObjectSelection::SelectionMode::Add);
576void DungeonObjectInteraction::DrawGhostPreview() {
577 entity_coordinator_.DrawGhostPreviews();
580void DungeonObjectInteraction::HandleScrollWheelResize() {
581 const ImGuiIO& io = ImGui::GetIO();
582 entity_coordinator_.HandleMouseWheel(io.MouseWheel);
585bool DungeonObjectInteraction::SetObjectId(
size_t index, int16_t
id) {
586 entity_coordinator_.tile_handler().UpdateObjectsId(current_room_id_, {index},
591bool DungeonObjectInteraction::SetObjectSize(
size_t index, uint8_t size) {
592 entity_coordinator_.tile_handler().UpdateObjectsSize(current_room_id_,
597bool DungeonObjectInteraction::SetObjectLayer(
599 entity_coordinator_.tile_handler().UpdateObjectsLayer(
600 current_room_id_, {index},
static_cast<int>(layer));
604std::pair<int, int> DungeonObjectInteraction::CalculateObjectBounds(
609void DungeonObjectInteraction::SendSelectedToLayer(
int target_layer) {
610 entity_coordinator_.tile_handler().UpdateObjectsLayer(
611 current_room_id_, selection_.GetSelectedIndices(), target_layer);
614void DungeonObjectInteraction::SendSelectedToFront() {
615 entity_coordinator_.tile_handler().SendToFront(
616 current_room_id_, selection_.GetSelectedIndices());
619void DungeonObjectInteraction::SendSelectedToBack() {
620 entity_coordinator_.tile_handler().SendToBack(
621 current_room_id_, selection_.GetSelectedIndices());
624void DungeonObjectInteraction::BringSelectedForward() {
625 entity_coordinator_.tile_handler().MoveForward(
626 current_room_id_, selection_.GetSelectedIndices());
629void DungeonObjectInteraction::SendSelectedBackward() {
630 entity_coordinator_.tile_handler().MoveBackward(
631 current_room_id_, selection_.GetSelectedIndices());
634void DungeonObjectInteraction::HandleLayerKeyboardShortcuts() {
636 if (!selection_.HasSelection())
640 if (ImGui::IsAnyItemActive())
644 if (ImGui::IsKeyPressed(ImGuiKey_1)) {
645 SendSelectedToLayer(0);
646 }
else if (ImGui::IsKeyPressed(ImGuiKey_2)) {
647 SendSelectedToLayer(1);
648 }
else if (ImGui::IsKeyPressed(ImGuiKey_3)) {
649 SendSelectedToLayer(2);
655 auto& io = ImGui::GetIO();
656 if (io.KeyCtrl && io.KeyShift) {
657 if (ImGui::IsKeyPressed(ImGuiKey_RightBracket)) {
658 SendSelectedToFront();
659 }
else if (ImGui::IsKeyPressed(ImGuiKey_LeftBracket)) {
660 SendSelectedToBack();
662 }
else if (io.KeyCtrl) {
663 if (ImGui::IsKeyPressed(ImGuiKey_RightBracket)) {
664 BringSelectedForward();
665 }
else if (ImGui::IsKeyPressed(ImGuiKey_LeftBracket)) {
666 SendSelectedBackward();
675void DungeonObjectInteraction::SetDoorPlacementMode(
bool enabled,
678 mode_manager_.SetMode(InteractionMode::PlaceDoor);
679 entity_coordinator_.door_handler().SetDoorType(type);
680 entity_coordinator_.door_handler().BeginPlacement();
682 entity_coordinator_.door_handler().CancelPlacement();
683 if (mode_manager_.GetMode() == InteractionMode::PlaceDoor)
684 mode_manager_.SetMode(InteractionMode::Select);
692void DungeonObjectInteraction::SetSpritePlacementMode(
bool enabled,
695 mode_manager_.SetMode(InteractionMode::PlaceSprite);
696 entity_coordinator_.sprite_handler().SetSpriteId(sprite_id);
697 entity_coordinator_.sprite_handler().BeginPlacement();
699 entity_coordinator_.sprite_handler().CancelPlacement();
700 if (mode_manager_.GetMode() == InteractionMode::PlaceSprite)
701 mode_manager_.SetMode(InteractionMode::Select);
709void DungeonObjectInteraction::SetItemPlacementMode(
bool enabled,
712 mode_manager_.SetMode(InteractionMode::PlaceItem);
713 entity_coordinator_.item_handler().SetItemId(item_id);
714 entity_coordinator_.item_handler().BeginPlacement();
716 entity_coordinator_.item_handler().CancelPlacement();
717 if (mode_manager_.GetMode() == InteractionMode::PlaceItem)
718 mode_manager_.SetMode(InteractionMode::Select);
726void DungeonObjectInteraction::SelectEntity(
EntityType type,
size_t index) {
727 entity_coordinator_.SelectEntity(type, index);
730void DungeonObjectInteraction::ClearEntitySelection() {
731 entity_coordinator_.ClearEntitySelection();
734void DungeonObjectInteraction::CancelPlacement() {
735 entity_coordinator_.CancelPlacement();
736 if (mode_manager_.IsPlacementActive()) {
737 mode_manager_.SetMode(InteractionMode::Select);
741void DungeonObjectInteraction::DrawEntitySelectionHighlights() {
742 entity_coordinator_.DrawSelectionHighlights();
743 entity_coordinator_.DrawPostPlacementOverlays();
746void DungeonObjectInteraction::DrawDoorSnapIndicators() {
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_
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
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)
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.