yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
entity_workbench.cc
Go to the documentation of this file.
2#include "util/i18n/tr.h"
3
9#include "imgui/imgui.h"
10
11namespace yaze {
12namespace editor {
13
14namespace {
15
17 switch (type) {
19 return "Exit";
21 return "Entrance";
23 return "Item";
25 return "Sprite";
27 return "Transport";
29 return "Music";
31 return "Tilemap";
33 return "Properties";
35 return "Dungeon Sprite";
36 default:
37 return "Unknown";
38 }
39}
40
41} // namespace
42
44 (void)p_open;
45 const auto ctx = CurrentOverworldWindowContext();
46 if (!ctx)
47 return;
48
49 auto* editor = ctx.editor;
50
51 auto* active_entity = editor->current_entity();
52 if (active_entity == nullptr) {
53 ImGui::TextDisabled(tr("No entity selected"));
54 return;
55 }
56
57 ImGui::Text(tr("Type: %s"), GetEntityTypeLabel(active_entity->entity_type_));
58 ImGui::Separator();
59
60 if (ImGui::Button(ICON_MD_SETTINGS " Open Popup Editor")) {
61 OpenEditorFor(active_entity);
62 }
63}
64
66 const auto ctx = CurrentOverworldWindowContext();
67 if (ctx) {
68 ctx.editor->SetCurrentEntity(entity);
69 }
70}
71
73 const auto ctx = CurrentOverworldWindowContext();
74 if (!ctx || !entity)
75 return;
76
77 auto* editor = ctx.editor;
78 editor->SetCurrentEntity(entity);
79 editing_entity_ = entity;
80
81 switch (entity->entity_type_) {
83 editor->edit_exit() = *static_cast<zelda3::OverworldExit*>(entity);
84 ImGui::OpenPopup(
86 .c_str());
87 break;
89 editor->edit_entrance() =
90 *static_cast<zelda3::OverworldEntrance*>(entity);
91 ImGui::OpenPopup(
93 .c_str());
94 break;
96 editor->edit_item() = *static_cast<zelda3::OverworldItem*>(entity);
97 ImGui::OpenPopup(
99 .c_str());
100 break;
102 editor->edit_sprite() = *static_cast<zelda3::Sprite*>(entity);
103 ImGui::OpenPopup(
105 .c_str());
106 break;
107 default:
108 break;
109 }
110}
111
113 const auto ctx = CurrentOverworldWindowContext();
114 if (!ctx || !entity) {
115 return;
116 }
117 ctx.editor->SetCurrentEntity(entity);
118 ImGui::OpenPopup(kContextMenuPopupId);
119}
120
122 const auto ctx = CurrentOverworldWindowContext();
123 if (!ctx)
124 return;
125
126 auto* editor = ctx.editor;
127 auto* editing_entity = editing_entity_;
128
129 if (ImGui::BeginPopup(kContextMenuPopupId)) {
131 ImGui::EndPopup();
132 }
133
134 if (ImGui::BeginPopupModal("Entity Insert Error", nullptr,
135 ImGuiWindowFlags_AlwaysAutoResize)) {
136 ImGui::TextWrapped("%s", editor->insert_error().c_str());
137 ImGui::Spacing();
138 if (ImGui::Button(tr("OK"), ImVec2(120.0f, 0.0f))) {
139 editor->insert_error().clear();
140 ImGui::CloseCurrentPopup();
141 }
142 ImGui::EndPopup();
143 }
144
145 if (DrawExitEditorPopup(editor->edit_exit())) {
146 if (editing_entity &&
147 editing_entity->entity_type_ == zelda3::GameEntity::EntityType::kExit) {
148 *static_cast<zelda3::OverworldExit*>(editing_entity) =
149 editor->edit_exit();
150 editor->NotifyEntityModified(editing_entity);
151 }
152 }
153 if (DrawOverworldEntrancePopup(editor->edit_entrance())) {
154 if (editing_entity && editing_entity->entity_type_ ==
156 *static_cast<zelda3::OverworldEntrance*>(editing_entity) =
157 editor->edit_entrance();
158 editor->NotifyEntityModified(editing_entity);
159 }
160 }
161 if (DrawItemEditorPopup(editor->edit_item())) {
162 if (editing_entity &&
163 editing_entity->entity_type_ == zelda3::GameEntity::EntityType::kItem) {
164 if (editor->edit_item().deleted) {
165 (void)editor->DeleteSelectedItem();
166 } else {
167 *static_cast<zelda3::OverworldItem*>(editing_entity) =
168 editor->edit_item();
169 editor->NotifyEntityModified(editing_entity);
170 }
171 }
172 }
173 if (DrawSpriteEditorPopup(editor->edit_sprite())) {
174 if (editing_entity && editing_entity->entity_type_ ==
176 *static_cast<zelda3::Sprite*>(editing_entity) = editor->edit_sprite();
177 editor->NotifyEntityModified(editing_entity);
178 }
179 }
180
181 const bool exit_popup_open = ImGui::IsPopupOpen(
182 gui::MakePopupId(gui::EditorNames::kOverworld, "Exit Editor").c_str());
183 const bool entrance_popup_open = ImGui::IsPopupOpen(
185 .c_str());
186 const bool item_popup_open = ImGui::IsPopupOpen(
187 gui::MakePopupId(gui::EditorNames::kOverworld, "Item Editor").c_str());
188 const bool sprite_popup_open = ImGui::IsPopupOpen(
189 gui::MakePopupId(gui::EditorNames::kOverworld, "Sprite Editor").c_str());
190 if (!exit_popup_open && !entrance_popup_open && !item_popup_open &&
191 !sprite_popup_open) {
192 editing_entity_ = nullptr;
193 }
194}
195
197 ImVec2 pos) {
198 const auto ctx = CurrentOverworldWindowContext();
199 if (!ctx)
200 return;
201 ctx.editor->pending_insert_type() = type;
202 ctx.editor->pending_insert_pos() = pos;
203}
204
206 EntityMutationService* mutation_service, int current_map, int game_state) {
207 const auto ctx = CurrentOverworldWindowContext();
208 if (!ctx || !mutation_service)
209 return;
210
211 auto* editor = ctx.editor;
212 if (editor->pending_insert_type().empty())
213 return;
214
215 auto res = mutation_service->InsertEntity(editor->pending_insert_type(),
216 editor->pending_insert_pos(),
217 current_map, game_state);
218
219 if (res.ok()) {
220 OpenEditorFor(res.entity);
221 editor->NotifyEntityModified(res.entity);
222 } else {
223 editor->insert_error() = res.error_message;
224 ImGui::OpenPopup("Entity Insert Error");
225 }
226
227 editor->pending_insert_type().clear();
228}
229
231 const auto ctx = CurrentOverworldWindowContext();
232 if (!ctx)
233 return;
234
235 auto* editor = ctx.editor;
236 auto* active_entity = editor->current_entity();
237 if (!active_entity)
238 return;
239
240 if (ImGui::Selectable(ICON_MD_EDIT " Edit Properties")) {
241 OpenEditorFor(active_entity);
242 ImGui::CloseCurrentPopup();
243 }
244
245 if (active_entity->entity_type_ == zelda3::GameEntity::EntityType::kExit) {
246 auto* exit = static_cast<zelda3::OverworldExit*>(active_entity);
247 if (ImGui::Selectable(ICON_MD_LINK " Jump to Room")) {
248 editor->RequestJumpToRoom(exit->room_id_);
249 ImGui::CloseCurrentPopup();
250 }
251 } else if (active_entity->entity_type_ ==
253 auto* entrance = static_cast<zelda3::OverworldEntrance*>(active_entity);
254 if (ImGui::Selectable(ICON_MD_LINK " Jump to Entrance")) {
255 editor->RequestJumpToEntrance(entrance->entrance_id_);
256 ImGui::CloseCurrentPopup();
257 }
258 }
259}
260
262
263} // namespace editor
264} // namespace yaze
Editor-level service responsible for overworld entity mutations.
MutationResult InsertEntity(const std::string &type, ImVec2 pos, int map_id, int game_state)
Dispatches entity insertion based on type string.
Authoritative component for entity editing state and UI.
void SetPendingInsertion(const std::string &type, ImVec2 pos)
void OpenEditorFor(zelda3::GameEntity *entity)
static constexpr const char * kContextMenuPopupId
void Draw(bool *p_open) override
Draw the panel content.
void ProcessPendingInsertion(EntityMutationService *mutation_service, int current_map, int game_state)
void OpenContextMenuFor(zelda3::GameEntity *entity)
void SetActiveEntity(zelda3::GameEntity *entity)
Base class for all overworld and dungeon entities.
Definition common.h:31
enum yaze::zelda3::GameEntity::EntityType entity_type_
Represents an overworld exit that transitions from dungeon to overworld.
A class for managing sprites in the overworld and underworld.
Definition sprite.h:37
#define ICON_MD_SETTINGS
Definition icons.h:1699
#define ICON_MD_LINK
Definition icons.h:1090
#define ICON_MD_EDIT
Definition icons.h:645
const char * GetEntityTypeLabel(zelda3::GameEntity::EntityType type)
bool DrawSpriteEditorPopup(zelda3::Sprite &sprite)
Definition entity.cc:534
bool DrawOverworldEntrancePopup(zelda3::OverworldEntrance &entrance)
Definition entity.cc:115
bool DrawItemEditorPopup(zelda3::OverworldItem &item)
Definition entity.cc:402
OverworldWindowContext CurrentOverworldWindowContext()
bool DrawExitEditorPopup(zelda3::OverworldExit &exit)
Definition entity.cc:200
constexpr const char * kOverworld
Definition popup_id.h:53
std::string MakePopupId(size_t session_id, const std::string &editor_name, const std::string &popup_name)
Generate session-aware popup IDs to prevent conflicts in multi-editor layouts.
Definition popup_id.h:23
#define REGISTER_PANEL(PanelClass)
Auto-registration macro for panels with default constructors.