yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
drag_drop.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GUI_CORE_DRAG_DROP_H_
2#define YAZE_APP_GUI_CORE_DRAG_DROP_H_
3
4#include <cstdint>
5#include <cstring>
6#include "util/i18n/tr.h"
7
8#include "imgui/imgui.h"
9
10namespace yaze {
11
12namespace gui {
13
14// ============================================================================
15// Payload type identifiers for ImGui drag-drop
16// ============================================================================
17
18constexpr const char* kDragPayloadTile16 = "YAZE_TILE16";
19constexpr const char* kDragPayloadSprite = "YAZE_SPRITE";
20constexpr const char* kDragPayloadPalette = "YAZE_PALETTE";
21constexpr const char* kDragPayloadRoomObject = "YAZE_ROOM_OBJ";
22constexpr const char* kDragPayloadPanel = "YAZE_PANEL";
23
24// ============================================================================
25// Payload structs
26// ============================================================================
27
32
37
43
45 uint16_t object_id;
47 int x;
48 int y;
49 uint8_t size;
50};
51
52// Drag payload for the Layout Designer. Panel IDs are bounded strings
53// ("dungeon.room_selector" style), so the payload stays POD-copyable
54// through ImGui's raw-memory drag-drop.
56 char panel_id[128] = {};
57};
58
59// ============================================================================
60// Drag source helpers (call inside an ImGui widget loop)
61// ============================================================================
62
65inline bool BeginTileDragSource(int tile_id, int map_id) {
66 if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_SourceAllowNullID)) {
67 TileDragPayload payload{tile_id, map_id};
68 ImGui::SetDragDropPayload(kDragPayloadTile16, &payload, sizeof(payload));
69 ImGui::Text(tr("Tile #%d"), tile_id);
70 ImGui::EndDragDropSource();
71 return true;
72 }
73 return false;
74}
75
76inline bool BeginSpriteDragSource(int sprite_id, int room_id) {
77 if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_SourceAllowNullID)) {
78 SpriteDragPayload payload{sprite_id, room_id};
79 ImGui::SetDragDropPayload(kDragPayloadSprite, &payload, sizeof(payload));
80 ImGui::Text(tr("Sprite #%d"), sprite_id);
81 ImGui::EndDragDropSource();
82 return true;
83 }
84 return false;
85}
86
87inline bool BeginPaletteDragSource(int group_idx, int palette_idx,
88 int color_idx) {
89 if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_SourceAllowNullID)) {
90 PaletteDragPayload payload{group_idx, palette_idx, color_idx};
91 ImGui::SetDragDropPayload(kDragPayloadPalette, &payload, sizeof(payload));
92 ImGui::Text(tr("Color [%d:%d:%d]"), group_idx, palette_idx, color_idx);
93 ImGui::EndDragDropSource();
94 return true;
95 }
96 return false;
97}
98
99inline bool BeginRoomObjectDragSource(uint16_t object_id, int room_id,
100 int pos_x, int pos_y, uint8_t size = 0) {
101 if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_SourceAllowNullID)) {
102 RoomObjectDragPayload payload{object_id, room_id, pos_x, pos_y, size};
103 ImGui::SetDragDropPayload(kDragPayloadRoomObject, &payload,
104 sizeof(payload));
105 ImGui::Text(tr("Object 0x%04X"), object_id);
106 ImGui::EndDragDropSource();
107 return true;
108 }
109 return false;
110}
111
112inline bool BeginPanelDragSource(const char* panel_id,
113 const char* preview_label) {
114 if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_SourceAllowNullID)) {
115 PanelDragPayload payload;
116 if (panel_id != nullptr) {
117 std::strncpy(payload.panel_id, panel_id, sizeof(payload.panel_id) - 1);
118 }
119 ImGui::SetDragDropPayload(kDragPayloadPanel, &payload, sizeof(payload));
120 ImGui::TextUnformatted(preview_label != nullptr ? preview_label : panel_id);
121 ImGui::EndDragDropSource();
122 return true;
123 }
124 return false;
125}
126
127// ============================================================================
128// Drop target helpers (call inside a widget that should accept drops)
129// ============================================================================
130
133 if (ImGui::BeginDragDropTarget()) {
134 if (const ImGuiPayload* payload =
135 ImGui::AcceptDragDropPayload(kDragPayloadTile16)) {
136 *out = *static_cast<const TileDragPayload*>(payload->Data);
137 ImGui::EndDragDropTarget();
138 return true;
139 }
140 ImGui::EndDragDropTarget();
141 }
142 return false;
143}
144
146 if (ImGui::BeginDragDropTarget()) {
147 if (const ImGuiPayload* payload =
148 ImGui::AcceptDragDropPayload(kDragPayloadSprite)) {
149 *out = *static_cast<const SpriteDragPayload*>(payload->Data);
150 ImGui::EndDragDropTarget();
151 return true;
152 }
153 ImGui::EndDragDropTarget();
154 }
155 return false;
156}
157
159 if (ImGui::BeginDragDropTarget()) {
160 if (const ImGuiPayload* payload =
161 ImGui::AcceptDragDropPayload(kDragPayloadPalette)) {
162 *out = *static_cast<const PaletteDragPayload*>(payload->Data);
163 ImGui::EndDragDropTarget();
164 return true;
165 }
166 ImGui::EndDragDropTarget();
167 }
168 return false;
169}
170
172 if (ImGui::BeginDragDropTarget()) {
173 if (const ImGuiPayload* payload =
174 ImGui::AcceptDragDropPayload(kDragPayloadRoomObject)) {
175 *out = *static_cast<const RoomObjectDragPayload*>(payload->Data);
176 ImGui::EndDragDropTarget();
177 return true;
178 }
179 ImGui::EndDragDropTarget();
180 }
181 return false;
182}
183
184// Accepts a panel drag payload assuming BeginDragDropTarget has already
185// been entered by the caller (so the target can draw drop-preview hints
186// before committing to accept). Returns true when the user releases over
187// the target with a YAZE_PANEL payload in flight.
189 if (const ImGuiPayload* payload =
190 ImGui::AcceptDragDropPayload(kDragPayloadPanel)) {
191 *out = *static_cast<const PanelDragPayload*>(payload->Data);
192 return true;
193 }
194 return false;
195}
196
197} // namespace gui
198} // namespace yaze
199
200#endif // YAZE_APP_GUI_CORE_DRAG_DROP_H_
constexpr const char * kDragPayloadRoomObject
Definition drag_drop.h:21
bool BeginPanelDragSource(const char *panel_id, const char *preview_label)
Definition drag_drop.h:112
bool BeginSpriteDragSource(int sprite_id, int room_id)
Definition drag_drop.h:76
constexpr const char * kDragPayloadTile16
Definition drag_drop.h:18
bool AcceptTileDrop(TileDragPayload *out)
Accept a tile16 drop. Returns true if a payload was accepted.
Definition drag_drop.h:132
constexpr const char * kDragPayloadPalette
Definition drag_drop.h:20
bool BeginTileDragSource(int tile_id, int map_id)
Definition drag_drop.h:65
bool AcceptRoomObjectDrop(RoomObjectDragPayload *out)
Definition drag_drop.h:171
constexpr const char * kDragPayloadPanel
Definition drag_drop.h:22
bool AcceptSpriteDrop(SpriteDragPayload *out)
Definition drag_drop.h:145
bool AcceptPaletteDrop(PaletteDragPayload *out)
Definition drag_drop.h:158
bool AcceptPanelDropWithinTarget(PanelDragPayload *out)
Definition drag_drop.h:188
bool BeginRoomObjectDragSource(uint16_t object_id, int room_id, int pos_x, int pos_y, uint8_t size=0)
Definition drag_drop.h:99
bool BeginPaletteDragSource(int group_idx, int palette_idx, int color_idx)
Definition drag_drop.h:87
constexpr const char * kDragPayloadSprite
Definition drag_drop.h:19