yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
entity.cc
Go to the documentation of this file.
2
3#include "app/gui/icons.h"
4#include "app/gui/input.h"
5#include "app/gui/style.h"
6
7namespace yaze {
8namespace app {
9namespace editor {
10
11using ImGui::BeginChild;
12using ImGui::Button;
13using ImGui::Checkbox;
14using ImGui::EndChild;
15using ImGui::SameLine;
16using ImGui::Selectable;
17using ImGui::Text;
18
19constexpr float kInputFieldSize = 30.f;
20
22 ImVec2 canvas_p0, ImVec2 scrolling) {
23 // Get the mouse position relative to the canvas
24 const ImGuiIO &io = ImGui::GetIO();
25 const ImVec2 origin(canvas_p0.x + scrolling.x, canvas_p0.y + scrolling.y);
26 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
27
28 // Check if the mouse is hovering over the entity
29 if (mouse_pos.x >= entity.x_ && mouse_pos.x <= entity.x_ + 16 &&
30 mouse_pos.y >= entity.y_ && mouse_pos.y <= entity.y_ + 16) {
31 return true;
32 }
33 return false;
34}
35
36void MoveEntityOnGrid(zelda3::GameEntity *entity, ImVec2 canvas_p0,
37 ImVec2 scrolling, bool free_movement) {
38 // Get the mouse position relative to the canvas
39 const ImGuiIO &io = ImGui::GetIO();
40 const ImVec2 origin(canvas_p0.x + scrolling.x, canvas_p0.y + scrolling.y);
41 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
42
43 // Calculate the new position on the 16x16 grid
44 int new_x = static_cast<int>(mouse_pos.x) / 16 * 16;
45 int new_y = static_cast<int>(mouse_pos.y) / 16 * 16;
46 if (free_movement) {
47 new_x = static_cast<int>(mouse_pos.x) / 8 * 8;
48 new_y = static_cast<int>(mouse_pos.y) / 8 * 8;
49 }
50
51 // Update the entity position
52 entity->set_x(new_x);
53 entity->set_y(new_y);
54}
55
56void HandleEntityDragging(zelda3::GameEntity *entity, ImVec2 canvas_p0,
57 ImVec2 scrolling, bool &is_dragging_entity,
58 zelda3::GameEntity *&dragged_entity,
59 zelda3::GameEntity *&current_entity,
60 bool free_movement) {
61 std::string entity_type = "Entity";
63 entity_type = "Exit";
64 } else if (entity->entity_type_ ==
66 entity_type = "Entrance";
68 entity_type = "Sprite";
70 entity_type = "Item";
71 }
72 const auto is_hovering =
73 IsMouseHoveringOverEntity(*entity, canvas_p0, scrolling);
74
75 const auto drag_or_clicked = ImGui::IsMouseDragging(ImGuiMouseButton_Left) ||
76 ImGui::IsMouseClicked(ImGuiMouseButton_Left);
77
78 if (is_hovering && drag_or_clicked && !is_dragging_entity) {
79 dragged_entity = entity;
80 is_dragging_entity = true;
81 } else if (is_hovering && ImGui::IsMouseClicked(ImGuiMouseButton_Right)) {
82 current_entity = entity;
83 ImGui::OpenPopup(absl::StrFormat("%s editor", entity_type.c_str()).c_str());
84 } else if (is_dragging_entity && dragged_entity == entity &&
85 ImGui::IsMouseReleased(ImGuiMouseButton_Left)) {
86 MoveEntityOnGrid(dragged_entity, canvas_p0, scrolling, free_movement);
87 entity->UpdateMapProperties(entity->map_id_);
88 is_dragging_entity = false;
89 dragged_entity = nullptr;
90 } else if (is_dragging_entity && dragged_entity == entity) {
91 if (ImGui::BeginDragDropSource()) {
92 ImGui::SetDragDropPayload("ENTITY_PAYLOAD", &entity,
93 sizeof(zelda3::GameEntity));
94 Text("Moving %s ID: %s", entity_type.c_str(),
95 core::UppercaseHexByte(entity->entity_id_).c_str());
96 ImGui::EndDragDropSource();
97 }
98 MoveEntityOnGrid(dragged_entity, canvas_p0, scrolling, free_movement);
99 entity->x_ = dragged_entity->x_;
100 entity->y_ = dragged_entity->y_;
101 entity->UpdateMapProperties(entity->map_id_);
102 }
103}
104
106 bool set_done = false;
107 if (set_done) {
108 set_done = false;
109 }
110 if (ImGui::BeginPopup("Entrance Inserter")) {
111 static int entrance_id = 0;
112 gui::InputHex("Entrance ID", &entrance_id);
113
114 if (Button(ICON_MD_DONE)) {
115 set_done = true;
116 ImGui::CloseCurrentPopup();
117 }
118
119 SameLine();
120 if (Button(ICON_MD_CANCEL)) {
121 ImGui::CloseCurrentPopup();
122 }
123
124 ImGui::EndPopup();
125 }
126 return set_done;
127}
128
129// TODO: Implement deleting OverworldEntrance objects, currently only hides them
132 static bool set_done = false;
133 if (set_done) {
134 set_done = false;
135 }
136 if (ImGui::BeginPopupModal("Entrance editor", NULL,
137 ImGuiWindowFlags_AlwaysAutoResize)) {
138 gui::InputHexWord("Map ID", &entrance.map_id_);
139 gui::InputHexByte("Entrance ID", &entrance.entrance_id_,
140 kInputFieldSize + 20);
141 gui::InputHex("X", &entrance.x_);
142 gui::InputHex("Y", &entrance.y_);
143
144 if (Button(ICON_MD_DONE)) {
145 ImGui::CloseCurrentPopup();
146 }
147 SameLine();
148 if (Button(ICON_MD_CANCEL)) {
149 set_done = true;
150 ImGui::CloseCurrentPopup();
151 }
152 SameLine();
153 if (Button(ICON_MD_DELETE)) {
154 entrance.deleted = true;
155 ImGui::CloseCurrentPopup();
156 }
157 ImGui::EndPopup();
158 }
159 return set_done;
160}
161
162// TODO: Implement deleting OverworldExit objects
164 if (ImGui::BeginPopup("Exit Inserter")) {
165 static int exit_id = 0;
166 gui::InputHex("Exit ID", &exit_id);
167
168 if (Button(ICON_MD_DONE)) {
169 ImGui::CloseCurrentPopup();
170 }
171
172 SameLine();
173 if (Button(ICON_MD_CANCEL)) {
174 ImGui::CloseCurrentPopup();
175 }
176
177 ImGui::EndPopup();
178 }
179}
180
182 static bool set_done = false;
183 if (set_done) {
184 set_done = false;
185 }
186 if (ImGui::BeginPopupModal("Exit editor", NULL,
187 ImGuiWindowFlags_AlwaysAutoResize)) {
188 // Normal door: None = 0, Wooden = 1, Bombable = 2
189 static int doorType = exit.door_type_1_;
190 // Fancy door: None = 0, Sanctuary = 1, Palace = 2
191 static int fancyDoorType = exit.door_type_2_;
192
193 static int xPos = 0;
194 static int yPos = 0;
195
196 // Special overworld exit properties
197 static int centerY = 0;
198 static int centerX = 0;
199 static int unk1 = 0;
200 static int unk2 = 0;
201 static int linkPosture = 0;
202 static int spriteGFX = 0;
203 static int bgGFX = 0;
204 static int palette = 0;
205 static int sprPal = 0;
206 static int top = 0;
207 static int bottom = 0;
208 static int left = 0;
209 static int right = 0;
210 static int leftEdgeOfMap = 0;
211
212 gui::InputHexWord("Room", &exit.room_id_);
213 SameLine();
214 gui::InputHex("Entity ID", &exit.entity_id_, 4);
215 gui::InputHexWord("Map", &exit.map_id_);
216 SameLine();
217 Checkbox("Automatic", &exit.is_automatic_);
218
219 gui::InputHex("X Positon", &exit.x_);
220 SameLine();
221 gui::InputHex("Y Position", &exit.y_);
222
223 gui::InputHexByte("X Camera", &exit.x_camera_);
224 SameLine();
225 gui::InputHexByte("Y Camera", &exit.y_camera_);
226
227 gui::InputHexWord("X Scroll", &exit.x_scroll_);
228 SameLine();
229 gui::InputHexWord("Y Scroll", &exit.y_scroll_);
230
231 ImGui::Separator();
232
233 static bool show_properties = false;
234 Checkbox("Show properties", &show_properties);
235 if (show_properties) {
236 Text("Deleted? %s", exit.deleted_ ? "true" : "false");
237 Text("Hole? %s", exit.is_hole_ ? "true" : "false");
238 Text("Large Map? %s", exit.large_map_ ? "true" : "false");
239 }
240
241 gui::TextWithSeparators("Unimplemented below");
242
243 ImGui::RadioButton("None", &doorType, 0);
244 SameLine();
245 ImGui::RadioButton("Wooden", &doorType, 1);
246 SameLine();
247 ImGui::RadioButton("Bombable", &doorType, 2);
248 // If door type is not None, input positions
249 if (doorType != 0) {
250 gui::InputHex("Door X pos", &xPos);
251 gui::InputHex("Door Y pos", &yPos);
252 }
253
254 ImGui::RadioButton("None##Fancy", &fancyDoorType, 0);
255 SameLine();
256 ImGui::RadioButton("Sanctuary", &fancyDoorType, 1);
257 SameLine();
258 ImGui::RadioButton("Palace", &fancyDoorType, 2);
259 // If fancy door type is not None, input positions
260 if (fancyDoorType != 0) {
261 // Placeholder for fancy door's X position
262 gui::InputHex("Fancy Door X pos", &xPos);
263 // Placeholder for fancy door's Y position
264 gui::InputHex("Fancy Door Y pos", &yPos);
265 }
266
267 static bool special_exit = false;
268 Checkbox("Special exit", &special_exit);
269 if (special_exit) {
270 gui::InputHex("Center X", &centerX);
271
272 gui::InputHex("Center Y", &centerY);
273 gui::InputHex("Unk1", &unk1);
274 gui::InputHex("Unk2", &unk2);
275
276 gui::InputHex("Link's posture", &linkPosture);
277 gui::InputHex("Sprite GFX", &spriteGFX);
278 gui::InputHex("BG GFX", &bgGFX);
279 gui::InputHex("Palette", &palette);
280 gui::InputHex("Spr Pal", &sprPal);
281
282 gui::InputHex("Top", &top);
283 gui::InputHex("Bottom", &bottom);
284 gui::InputHex("Left", &left);
285 gui::InputHex("Right", &right);
286
287 gui::InputHex("Left edge of map", &leftEdgeOfMap);
288 }
289
290 if (Button(ICON_MD_DONE)) {
291 ImGui::CloseCurrentPopup();
292 }
293
294 SameLine();
295
296 if (Button(ICON_MD_CANCEL)) {
297 set_done = true;
298 ImGui::CloseCurrentPopup();
299 }
300
301 SameLine();
302 if (Button(ICON_MD_DELETE)) {
303 exit.deleted_ = true;
304 ImGui::CloseCurrentPopup();
305 }
306
307 ImGui::EndPopup();
308 }
309
310 return set_done;
311}
312
314 // Contents of the Context Menu
315 if (ImGui::BeginPopup("Item Inserter")) {
316 static size_t new_item_id = 0;
317 Text("Add Item");
318 BeginChild("ScrollRegion", ImVec2(150, 150), true,
319 ImGuiWindowFlags_AlwaysVerticalScrollbar);
320 for (size_t i = 0; i < zelda3::overworld::kSecretItemNames.size(); i++) {
321 if (Selectable(zelda3::overworld::kSecretItemNames[i].c_str(),
322 i == new_item_id)) {
323 new_item_id = i;
324 }
325 }
326 EndChild();
327
328 if (Button(ICON_MD_DONE)) {
329 // Add the new item to the overworld
330 new_item_id = 0;
331 ImGui::CloseCurrentPopup();
332 }
333 SameLine();
334
335 if (Button(ICON_MD_CANCEL)) {
336 ImGui::CloseCurrentPopup();
337 }
338
339 ImGui::EndPopup();
340 }
341}
342
343// TODO: Implement deleting OverworldItem objects, currently only hides them
345 static bool set_done = false;
346 if (set_done) {
347 set_done = false;
348 }
349 if (ImGui::BeginPopupModal("Item editor", NULL,
350 ImGuiWindowFlags_AlwaysAutoResize)) {
351 BeginChild("ScrollRegion", ImVec2(150, 150), true,
352 ImGuiWindowFlags_AlwaysVerticalScrollbar);
353 ImGui::BeginGroup();
354 for (size_t i = 0; i < zelda3::overworld::kSecretItemNames.size(); i++) {
355 if (Selectable(zelda3::overworld::kSecretItemNames[i].c_str(),
356 item.id_ == i)) {
357 item.id_ = i;
358 }
359 }
360 ImGui::EndGroup();
361 EndChild();
362
363 if (Button(ICON_MD_DONE)) ImGui::CloseCurrentPopup();
364 SameLine();
365 if (Button(ICON_MD_CLOSE)) {
366 set_done = true;
367 ImGui::CloseCurrentPopup();
368 }
369 SameLine();
370 if (Button(ICON_MD_DELETE)) {
371 item.deleted = true;
372 ImGui::CloseCurrentPopup();
373 }
374
375 ImGui::EndPopup();
376 }
377 return set_done;
378}
379
380const ImGuiTableSortSpecs *SpriteItem::s_current_sort_specs = nullptr;
381
382void DrawSpriteTable(std::function<void(int)> onSpriteSelect) {
383 static ImGuiTextFilter filter;
384 static int selected_id = 0;
385 static std::vector<SpriteItem> items;
386
387 // Initialize items if empty
388 if (items.empty()) {
389 for (int i = 0; i < 256; ++i) {
390 items.push_back(SpriteItem{i, zelda3::kSpriteDefaultNames[i].data()});
391 }
392 }
393
394 filter.Draw("Filter", 180);
395
396 if (ImGui::BeginTable("##sprites", 2,
397 ImGuiTableFlags_Sortable | ImGuiTableFlags_Resizable)) {
398 ImGui::TableSetupColumn("ID", ImGuiTableColumnFlags_DefaultSort, 0.0f,
400 ImGui::TableSetupColumn("Name", 0, 0.0f, MyItemColumnID_Name);
401 ImGui::TableHeadersRow();
402
403 // Handle sorting
404 if (ImGuiTableSortSpecs *sort_specs = ImGui::TableGetSortSpecs()) {
405 if (sort_specs->SpecsDirty) {
406 SpriteItem::SortWithSortSpecs(sort_specs, items);
407 sort_specs->SpecsDirty = false;
408 }
409 }
410
411 // Display filtered and sorted items
412 for (const auto &item : items) {
413 if (filter.PassFilter(item.name)) {
414 ImGui::TableNextRow();
415 ImGui::TableSetColumnIndex(0);
416 Text("%d", item.id);
417 ImGui::TableSetColumnIndex(1);
418
419 if (Selectable(item.name, selected_id == item.id,
420 ImGuiSelectableFlags_SpanAllColumns)) {
421 selected_id = item.id;
422 onSpriteSelect(item.id);
423 }
424 }
425 }
426 ImGui::EndTable();
427 }
428}
429
430// TODO: Implement deleting OverworldSprite objects
432 if (ImGui::BeginPopup("Sprite Inserter")) {
433 static int new_sprite_id = 0;
434 Text("Add Sprite");
435 BeginChild("ScrollRegion", ImVec2(250, 250), true,
436 ImGuiWindowFlags_AlwaysVerticalScrollbar);
437 DrawSpriteTable([](int selected_id) { new_sprite_id = selected_id; });
438 EndChild();
439
440 if (Button(ICON_MD_DONE)) {
441 // Add the new item to the overworld
442 new_sprite_id = 0;
443 ImGui::CloseCurrentPopup();
444 }
445 SameLine();
446
447 if (Button(ICON_MD_CANCEL)) {
448 ImGui::CloseCurrentPopup();
449 }
450
451 ImGui::EndPopup();
452 }
453}
454
456 static bool set_done = false;
457 if (set_done) {
458 set_done = false;
459 }
460 if (ImGui::BeginPopupModal("Sprite editor", NULL,
461 ImGuiWindowFlags_AlwaysAutoResize)) {
462 BeginChild("ScrollRegion", ImVec2(350, 350), true,
463 ImGuiWindowFlags_AlwaysVerticalScrollbar);
464 ImGui::BeginGroup();
465 Text("%s", sprite.name().c_str());
466
467 DrawSpriteTable([&sprite](int selected_id) {
468 sprite.set_id(selected_id);
469 sprite.UpdateMapProperties(sprite.map_id());
470 });
471 ImGui::EndGroup();
472 EndChild();
473
474 if (Button(ICON_MD_DONE)) ImGui::CloseCurrentPopup();
475 SameLine();
476 if (Button(ICON_MD_CLOSE)) {
477 set_done = true;
478 ImGui::CloseCurrentPopup();
479 }
480 SameLine();
481 if (Button(ICON_MD_DELETE)) {
482 sprite.set_deleted(true);
483 ImGui::CloseCurrentPopup();
484 }
485
486 ImGui::EndPopup();
487 }
488 return set_done;
489}
490
491} // namespace editor
492} // namespace app
493} // namespace yaze
Base class for all overworld and dungeon entities.
Definition common.h:35
virtual void UpdateMapProperties(uint16_t map_id)=0
enum yaze::app::zelda3::GameEntity::EntityType entity_type_
A class for managing sprites in the overworld and underworld.
Definition sprite.h:280
auto map_id() const
Definition sprite.h:348
void UpdateMapProperties(uint16_t map_id) override
Definition sprite.cc:9
auto set_deleted(bool deleted)
Definition sprite.h:360
auto set_id(uint8_t id)
Definition sprite.h:343
#define ICON_MD_CANCEL
Definition icons.h:362
#define ICON_MD_DONE
Definition icons.h:605
#define ICON_MD_DELETE
Definition icons.h:528
#define ICON_MD_CLOSE
Definition icons.h:416
std::string UppercaseHexByte(uint8_t byte, bool leading)
Definition common.cc:103
void DrawSpriteInserterPopup()
Definition entity.cc:431
bool IsMouseHoveringOverEntity(const zelda3::GameEntity &entity, ImVec2 canvas_p0, ImVec2 scrolling)
Definition entity.cc:21
bool DrawEntranceInserterPopup()
Definition entity.cc:105
bool DrawExitEditorPopup(zelda3::overworld::OverworldExit &exit)
Definition entity.cc:181
bool DrawOverworldEntrancePopup(zelda3::overworld::OverworldEntrance &entrance)
Definition entity.cc:130
bool DrawSpriteEditorPopup(zelda3::Sprite &sprite)
Definition entity.cc:455
void DrawItemInsertPopup()
Definition entity.cc:313
void DrawExitInserterPopup()
Definition entity.cc:163
void MoveEntityOnGrid(zelda3::GameEntity *entity, ImVec2 canvas_p0, ImVec2 scrolling, bool free_movement)
Definition entity.cc:36
void DrawSpriteTable(std::function< void(int)> onSpriteSelect)
Definition entity.cc:382
void HandleEntityDragging(zelda3::GameEntity *entity, ImVec2 canvas_p0, ImVec2 scrolling, bool &is_dragging_entity, zelda3::GameEntity *&dragged_entity, zelda3::GameEntity *&current_entity, bool free_movement)
Definition entity.cc:56
bool DrawItemEditorPopup(zelda3::overworld::OverworldItem &item)
Definition entity.cc:344
constexpr float kInputFieldSize
Definition entity.cc:19
bool InputHexByte(const char *label, uint8_t *data, float input_width, bool no_step)
Definition input.cc:175
bool InputHex(const char *label, uint64_t *data)
Definition input.cc:142
void TextWithSeparators(const absl::string_view &text)
Definition style.cc:424
bool InputHexWord(const char *label, uint16_t *data, float input_width, bool no_step)
Definition input.cc:161
const std::vector< std::string > kSecretItemNames
Definition overworld.h:39
Definition common.cc:22
static const ImGuiTableSortSpecs * s_current_sort_specs
Definition entity.h:46
static void SortWithSortSpecs(ImGuiTableSortSpecs *sort_specs, std::vector< SpriteItem > &items)
Definition entity.h:48