3#include "absl/strings/str_format.h"
4#include "absl/strings/str_replace.h"
8namespace layout_designer {
14 code += absl::StrFormat(
"// Generated by YAZE Layout Designer\n");
15 code += absl::StrFormat(
"// Panel: %s\n", design.
panel_name);
16 code += absl::StrFormat(
"// Generated: <timestamp>\n\n");
19 absl::StrFormat(
"void %sPanel::Draw(bool* p_open) {\n", design.
panel_id);
22 for (
const auto& widget : design.
widgets) {
34 std::string indent =
GetIndent(indent_level);
37 code += indent + absl::StrFormat(
"// Widget: %s\n", widget.
id);
40 switch (widget.
type) {
75 code += indent +
"ImGui::Separator();\n";
79 code += indent +
"ImGui::SameLine();\n";
83 code += indent +
"ImGui::Spacing();\n";
87 code += indent +
"ImGui::NewLine();\n";
99 code += indent + absl::StrFormat(
"// TODO: Generate code for %s\n",
106 code += indent +
"ImGui::SameLine();\n";
116 code +=
" // Widget state variables\n";
118 for (
const auto& widget : design.
widgets) {
121 switch (widget->type) {
123 code += absl::StrFormat(
" bool %s = false;\n", var_name);
127 auto* buffer_size_prop =
129 int size = buffer_size_prop ? buffer_size_prop->
int_value : 256;
130 code += absl::StrFormat(
" char %s[%d] = {};\n", var_name, size);
135 code += absl::StrFormat(
" int %s = 0;\n", var_name);
140 code += absl::StrFormat(
" float %s = 0.0f;\n", var_name);
145 code += absl::StrFormat(
" ImVec4 %s = ImVec4(1,1,1,1);\n", var_name);
161 for (
const auto& widget : design.
widgets) {
165 if (hint_prop && !hint_prop->string_value.empty()) {
167 code += absl::StrFormat(
" // Initialize %s hint\n", var_name);
178 return std::string(level * 2,
' ');
182 return absl::StrReplaceAll(str, {{
"\\",
"\\\\"}, {
"\"",
"\\\""}});
190 auto* label_prop =
const_cast<WidgetDefinition&
>(widget).GetProperty(
"label");
191 std::string label = label_prop ? label_prop->string_value :
"Button";
193 auto* size_prop =
const_cast<WidgetDefinition&
>(widget).GetProperty(
"size");
194 ImVec2 size = size_prop ? size_prop->vec2_value : ImVec2(0, 0);
197 code += ind + absl::StrFormat(
"if (ImGui::SmallButton(\"%s\")) {\n",
199 }
else if (size.x != 0 || size.y != 0) {
200 code += ind + absl::StrFormat(
201 "if (ImGui::Button(\"%s\", ImVec2(%.1f, %.1f))) {\n",
204 code += ind + absl::StrFormat(
"if (ImGui::Button(\"%s\")) {\n",
208 code += ind +
" // TODO: Button callback\n";
210 code += ind + absl::StrFormat(
" %s();\n", widget.
callback_name);
215 code += ind +
"if (ImGui::IsItemHovered()) {\n";
216 code += ind + absl::StrFormat(
" ImGui::SetTooltip(\"%s\");\n",
229 auto* text_prop =
const_cast<WidgetDefinition&
>(widget).GetProperty(
"text");
230 std::string text = text_prop ? text_prop->string_value :
"Text";
232 switch (widget.
type) {
235 ind + absl::StrFormat(
"ImGui::Text(\"%s\");\n",
EscapeString(text));
238 code += ind + absl::StrFormat(
"ImGui::TextWrapped(\"%s\");\n",
242 code += ind + absl::StrFormat(
"ImGui::BulletText(\"%s\");\n",
248 ImVec4 color = color_prop ? color_prop->color_value : ImVec4(1, 1, 1, 1);
252 "ImGui::TextColored(ImVec4(%.2f, %.2f, %.2f, %.2f), \"%s\");\n",
253 color.x, color.y, color.z, color.w,
EscapeString(text));
269 auto* label_prop =
const_cast<WidgetDefinition&
>(widget).GetProperty(
"label");
270 std::string label = label_prop ? label_prop->string_value :
"Input";
272 switch (widget.
type) {
274 code += ind + absl::StrFormat(
"ImGui::Checkbox(\"%s\", &%s);\n",
281 if (hint_prop && !hint_prop->string_value.empty()) {
285 "ImGui::InputTextWithHint(\"%s\", \"%s\", %s, sizeof(%s));\n",
290 ind + absl::StrFormat(
"ImGui::InputText(\"%s\", %s, sizeof(%s));\n",
297 code += ind + absl::StrFormat(
"ImGui::InputInt(\"%s\", &%s);\n",
304 int min_val = min_prop ? min_prop->int_value : 0;
305 int max_val = max_prop ? max_prop->int_value : 100;
306 code += ind + absl::StrFormat(
"ImGui::SliderInt(\"%s\", &%s, %d, %d);\n",
324 switch (widget.
type) {
329 std::string
id = id_prop ? id_prop->string_value :
"table";
330 int columns = columns_prop ? columns_prop->int_value : 2;
332 code += ind + absl::StrFormat(
"if (ImGui::BeginTable(\"%s\", %d)) {\n",
336 for (
const auto& child : widget.
children) {
340 code += ind +
" ImGui::EndTable();\n";
346 code += ind +
"ImGui::TableNextRow();\n";
350 code += ind +
"ImGui::TableNextColumn();\n";
365 auto* size_prop =
const_cast<WidgetDefinition&
>(widget).GetProperty(
"size");
366 ImVec2 size = size_prop ? size_prop->vec2_value : ImVec2(300, 200);
368 code += ind + absl::StrFormat(
"// Custom canvas - size: %.0fx%.0f\n", size.x,
370 code += ind +
"ImVec2 canvas_pos = ImGui::GetCursorScreenPos();\n";
371 code += ind + absl::StrFormat(
"ImVec2 canvas_size = ImVec2(%.0ff, %.0ff);\n",
373 code += ind +
"ImDrawList* draw_list = ImGui::GetWindowDrawList();\n";
374 code += ind +
"// TODO: Add custom drawing code here\n";
375 code += ind +
"ImGui::Dummy(canvas_size);\n";
386 code += ind + absl::StrFormat(
"// TODO: Container widget: %s\n",
395 std::string var_name = widget.
id;
396 std::replace(var_name.begin(), var_name.end(),
'.',
'_');
397 std::replace(var_name.begin(), var_name.end(),
'-',
'_');
const char * GetWidgetTypeName(WidgetType type)
Get human-readable name for widget type.
Complete design definition for a panel's internal layout.
std::vector< std::unique_ptr< WidgetDefinition > > widgets