yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
widget_definition.cc
Go to the documentation of this file.
2
3#include <chrono>
4
6
7namespace yaze {
8namespace editor {
9namespace layout_designer {
10
11// ============================================================================
12// WidgetProperty Implementation
13// ============================================================================
14
15void WidgetDefinition::AddProperty(const std::string& name,
17 WidgetProperty prop;
18 prop.name = name;
19 prop.type = type;
20 properties.push_back(prop);
21}
22
24 for (auto& prop : properties) {
25 if (prop.name == name) {
26 return &prop;
27 }
28 }
29 return nullptr;
30}
31
32void WidgetDefinition::AddChild(std::unique_ptr<WidgetDefinition> child) {
33 if (CanHaveChildren()) {
34 children.push_back(std::move(child));
35 }
36}
37
41
45
47 return RequiresEndCall(type);
48}
49
50// ============================================================================
51// PanelDesign Implementation
52// ============================================================================
53
54void PanelDesign::AddWidget(std::unique_ptr<WidgetDefinition> widget) {
55 widgets.push_back(std::move(widget));
56 Touch();
57}
58
60 // Recursive search through widget tree
61 std::function<WidgetDefinition*(WidgetDefinition*)> search =
62 [&](WidgetDefinition* widget) -> WidgetDefinition* {
63 if (widget->id == id) {
64 return widget;
65 }
66 for (auto& child : widget->children) {
67 if (auto* found = search(child.get())) {
68 return found;
69 }
70 }
71 return nullptr;
72 };
73
74 for (auto& widget : widgets) {
75 if (auto* found = search(widget.get())) {
76 return found;
77 }
78 }
79 return nullptr;
80}
81
82std::vector<WidgetDefinition*> PanelDesign::GetAllWidgets() {
83 std::vector<WidgetDefinition*> result;
84
85 std::function<void(WidgetDefinition*)> collect =
86 [&](WidgetDefinition* widget) {
87 result.push_back(widget);
88 for (auto& child : widget->children) {
89 collect(child.get());
90 }
91 };
92
93 for (auto& widget : widgets) {
94 collect(widget.get());
95 }
96
97 return result;
98}
99
100bool PanelDesign::Validate(std::string* error_message) const {
101 if (panel_id.empty()) {
102 if (error_message) *error_message = "Panel ID cannot be empty";
103 return false;
104 }
105
106 // Validate widget IDs are unique
107 std::set<std::string> ids;
108 for (const auto& widget : widgets) {
109 if (ids.count(widget->id)) {
110 if (error_message) *error_message = "Duplicate widget ID: " + widget->id;
111 return false;
112 }
113 ids.insert(widget->id);
114 }
115
116 return true;
117}
118
120 auto now = std::chrono::system_clock::now();
121 modified_timestamp = std::chrono::duration_cast<std::chrono::seconds>(
122 now.time_since_epoch()).count();
123}
124
125// ============================================================================
126// Utility Functions
127// ============================================================================
128
129const char* GetWidgetTypeName(WidgetType type) {
130 switch (type) {
131 case WidgetType::Text: return "Text";
132 case WidgetType::TextWrapped: return "Text (Wrapped)";
133 case WidgetType::TextColored: return "Colored Text";
134 case WidgetType::Button: return "Button";
135 case WidgetType::SmallButton: return "Small Button";
136 case WidgetType::Checkbox: return "Checkbox";
137 case WidgetType::RadioButton: return "Radio Button";
138 case WidgetType::InputText: return "Text Input";
139 case WidgetType::InputInt: return "Integer Input";
140 case WidgetType::InputFloat: return "Float Input";
141 case WidgetType::SliderInt: return "Integer Slider";
142 case WidgetType::SliderFloat: return "Float Slider";
143 case WidgetType::ColorEdit: return "Color Editor";
144 case WidgetType::ColorPicker: return "Color Picker";
145 case WidgetType::Separator: return "Separator";
146 case WidgetType::SameLine: return "Same Line";
147 case WidgetType::Spacing: return "Spacing";
148 case WidgetType::Dummy: return "Dummy Space";
149 case WidgetType::NewLine: return "New Line";
150 case WidgetType::Indent: return "Indent";
151 case WidgetType::Unindent: return "Unindent";
152 case WidgetType::BeginGroup: return "Group (Begin)";
153 case WidgetType::EndGroup: return "Group (End)";
154 case WidgetType::BeginChild: return "Child Window (Begin)";
155 case WidgetType::EndChild: return "Child Window (End)";
156 case WidgetType::CollapsingHeader: return "Collapsing Header";
157 case WidgetType::TreeNode: return "Tree Node";
158 case WidgetType::TabBar: return "Tab Bar";
159 case WidgetType::TabItem: return "Tab Item";
160 case WidgetType::BeginTable: return "Table (Begin)";
161 case WidgetType::EndTable: return "Table (End)";
162 case WidgetType::TableNextRow: return "Table Next Row";
163 case WidgetType::TableNextColumn: return "Table Next Column";
164 case WidgetType::TableSetupColumn: return "Table Setup Column";
165 case WidgetType::Canvas: return "Custom Canvas";
166 case WidgetType::Image: return "Image";
167 case WidgetType::ImageButton: return "Image Button";
168 case WidgetType::ProgressBar: return "Progress Bar";
169 case WidgetType::BulletText: return "Bullet Text";
170 case WidgetType::BeginMenu: return "Menu (Begin)";
171 case WidgetType::EndMenu: return "Menu (End)";
172 case WidgetType::MenuItem: return "Menu Item";
173 case WidgetType::BeginCombo: return "Combo (Begin)";
174 case WidgetType::EndCombo: return "Combo (End)";
175 case WidgetType::Selectable: return "Selectable";
176 case WidgetType::ListBox: return "List Box";
177 default: return "Unknown";
178 }
179}
180
181const char* GetWidgetTypeIcon(WidgetType type) {
182 switch (type) {
183 case WidgetType::Text:
187 return ICON_MD_TEXT_FIELDS;
188
193
195 return ICON_MD_CHECK_BOX;
196
199
203 return ICON_MD_INPUT;
204
207 return ICON_MD_TUNE;
208
211 return ICON_MD_PALETTE;
212
215
218 return ICON_MD_TABLE_CHART;
219
223
226 return ICON_MD_TAB;
227
229 return ICON_MD_DRAW;
230
232 return ICON_MD_IMAGE;
233
236
240 return ICON_MD_MENU;
241
246
249
250 default:
251 return ICON_MD_WIDGETS;
252 }
253}
254
256 switch (type) {
266 return true;
267 default:
268 return false;
269 }
270}
271
273 switch (type) {
281 return true;
282 default:
283 return false;
284 }
285}
286
287std::vector<WidgetProperty> GetDefaultProperties(WidgetType type) {
288 std::vector<WidgetProperty> props;
289
290 switch (type) {
291 case WidgetType::Text:
294 WidgetProperty prop;
295 prop.name = "text";
297 prop.string_value = "Sample Text";
298 props.push_back(prop);
299 break;
300 }
301
304 WidgetProperty label;
305 label.name = "label";
307 label.string_value = "Button";
308 props.push_back(label);
309
310 WidgetProperty size;
311 size.name = "size";
313 size.vec2_value = ImVec2(0, 0); // Auto size
314 props.push_back(size);
315 break;
316 }
317
319 WidgetProperty label;
320 label.name = "label";
322 label.string_value = "Checkbox";
323 props.push_back(label);
324
325 WidgetProperty checked;
326 checked.name = "checked";
328 checked.bool_value = false;
329 props.push_back(checked);
330 break;
331 }
332
334 WidgetProperty label;
335 label.name = "label";
337 label.string_value = "Input";
338 props.push_back(label);
339
340 WidgetProperty hint;
341 hint.name = "hint";
343 hint.string_value = "Enter text...";
344 props.push_back(hint);
345
346 WidgetProperty buffer_size;
347 buffer_size.name = "buffer_size";
348 buffer_size.type = WidgetProperty::Type::Int;
349 buffer_size.int_value = 256;
350 props.push_back(buffer_size);
351 break;
352 }
353
355 WidgetProperty label;
356 label.name = "label";
358 label.string_value = "Slider";
359 props.push_back(label);
360
361 WidgetProperty min_val;
362 min_val.name = "min";
364 min_val.int_value = 0;
365 props.push_back(min_val);
366
367 WidgetProperty max_val;
368 max_val.name = "max";
370 max_val.int_value = 100;
371 props.push_back(max_val);
372 break;
373 }
374
377 id.name = "id";
379 id.string_value = "table";
380 props.push_back(id);
381
382 WidgetProperty columns;
383 columns.name = "columns";
385 columns.int_value = 2;
386 props.push_back(columns);
387
388 WidgetProperty flags;
389 flags.name = "flags";
391 flags.flags_value = 0; // ImGuiTableFlags
392 props.push_back(flags);
393 break;
394 }
395
396 case WidgetType::Canvas: {
397 WidgetProperty size;
398 size.name = "size";
400 size.vec2_value = ImVec2(300, 200);
401 props.push_back(size);
402
403 WidgetProperty bg_color;
404 bg_color.name = "background";
406 bg_color.color_value = ImVec4(0.2f, 0.2f, 0.2f, 1.0f);
407 props.push_back(bg_color);
408 break;
409 }
410
411 default:
412 break;
413 }
414
415 return props;
416}
417
418} // namespace layout_designer
419} // namespace editor
420} // namespace yaze
421
#define ICON_MD_ACCOUNT_TREE
Definition icons.h:83
#define ICON_MD_CHECK_BOX
Definition icons.h:398
#define ICON_MD_DRAW
Definition icons.h:625
#define ICON_MD_TEXT_FIELDS
Definition icons.h:1954
#define ICON_MD_TUNE
Definition icons.h:2022
#define ICON_MD_TABLE_CHART
Definition icons.h:1933
#define ICON_MD_WIDGETS
Definition icons.h:2156
#define ICON_MD_SMART_BUTTON
Definition icons.h:1778
#define ICON_MD_IMAGE
Definition icons.h:982
#define ICON_MD_CHECK_CIRCLE
Definition icons.h:400
#define ICON_MD_HORIZONTAL_RULE
Definition icons.h:960
#define ICON_MD_RADIO_BUTTON_CHECKED
Definition icons.h:1548
#define ICON_MD_TAB
Definition icons.h:1930
#define ICON_MD_MENU
Definition icons.h:1196
#define ICON_MD_PALETTE
Definition icons.h:1370
#define ICON_MD_LINEAR_SCALE
Definition icons.h:1089
#define ICON_MD_INPUT
Definition icons.h:995
#define ICON_MD_ARROW_DROP_DOWN
Definition icons.h:181
std::vector< WidgetProperty > GetDefaultProperties(WidgetType type)
Get default properties for a widget type.
WidgetType
Types of ImGui widgets available in the designer.
const char * GetWidgetTypeName(WidgetType type)
Get human-readable name for widget type.
bool IsContainerWidget(WidgetType type)
Check if widget type is a container.
bool RequiresEndCall(WidgetType type)
Check if widget type requires an End*() call.
const char * GetWidgetTypeIcon(WidgetType type)
Get icon for widget type.
void AddWidget(std::unique_ptr< WidgetDefinition > widget)
std::vector< std::unique_ptr< WidgetDefinition > > widgets
bool Validate(std::string *error_message=nullptr) const
std::vector< WidgetDefinition * > GetAllWidgets()
WidgetDefinition * FindWidget(const std::string &id)
Defines a widget instance in a panel layout.
void AddProperty(const std::string &name, WidgetProperty::Type type)
WidgetProperty * GetProperty(const std::string &name)
void AddChild(std::unique_ptr< WidgetDefinition > child)
std::vector< std::unique_ptr< WidgetDefinition > > children
Represents a configurable property of a widget.
enum yaze::editor::layout_designer::WidgetProperty::Type type