3#include "absl/strings/str_format.h"
4#include "absl/strings/str_replace.h"
8namespace layout_designer {
13 code += absl::StrFormat(
"// Generated by YAZE Layout Designer\n");
14 code += absl::StrFormat(
"// Panel: %s\n", design.
panel_name);
15 code += absl::StrFormat(
"// Generated: <timestamp>\n\n");
17 code += absl::StrFormat(
"void %sPanel::Draw(bool* p_open) {\n", design.
panel_id);
20 for (
const auto& widget : design.
widgets) {
32 std::string indent =
GetIndent(indent_level);
35 code += indent + absl::StrFormat(
"// Widget: %s\n", widget.
id);
38 switch (widget.
type) {
73 code += indent +
"ImGui::Separator();\n";
77 code += indent +
"ImGui::SameLine();\n";
81 code += indent +
"ImGui::Spacing();\n";
85 code += indent +
"ImGui::NewLine();\n";
97 code += indent + absl::StrFormat(
"// TODO: Generate code for %s\n",
104 code += indent +
"ImGui::SameLine();\n";
113 code +=
" // Widget state variables\n";
115 for (
const auto& widget : design.
widgets) {
118 switch (widget->type) {
120 code += absl::StrFormat(
" bool %s = false;\n", var_name);
126 int size = buffer_size_prop ? buffer_size_prop->
int_value : 256;
127 code += absl::StrFormat(
" char %s[%d] = {};\n", var_name, size);
132 code += absl::StrFormat(
" int %s = 0;\n", var_name);
137 code += absl::StrFormat(
" float %s = 0.0f;\n", var_name);
142 code += absl::StrFormat(
" ImVec4 %s = ImVec4(1,1,1,1);\n", var_name);
157 for (
const auto& widget : design.
widgets) {
160 if (hint_prop && !hint_prop->string_value.empty()) {
162 code += absl::StrFormat(
" // Initialize %s hint\n", var_name);
173 return std::string(level * 2,
' ');
177 return absl::StrReplaceAll(str, {{
"\\",
"\\\\"}, {
"\"",
"\\\""}});
185 auto* label_prop =
const_cast<WidgetDefinition&
>(widget).GetProperty(
"label");
186 std::string label = label_prop ? label_prop->string_value :
"Button";
188 auto* size_prop =
const_cast<WidgetDefinition&
>(widget).GetProperty(
"size");
189 ImVec2 size = size_prop ? size_prop->vec2_value : ImVec2(0, 0);
192 code += ind + absl::StrFormat(
"if (ImGui::SmallButton(\"%s\")) {\n",
194 }
else if (size.x != 0 || size.y != 0) {
195 code += ind + absl::StrFormat(
196 "if (ImGui::Button(\"%s\", ImVec2(%.1f, %.1f))) {\n",
199 code += ind + absl::StrFormat(
"if (ImGui::Button(\"%s\")) {\n",
203 code += ind +
" // TODO: Button callback\n";
205 code += ind + absl::StrFormat(
" %s();\n", widget.
callback_name);
210 code += ind +
"if (ImGui::IsItemHovered()) {\n";
211 code += ind + absl::StrFormat(
" ImGui::SetTooltip(\"%s\");\n",
224 auto* text_prop =
const_cast<WidgetDefinition&
>(widget).GetProperty(
"text");
225 std::string text = text_prop ? text_prop->string_value :
"Text";
227 switch (widget.
type) {
229 code += ind + absl::StrFormat(
"ImGui::Text(\"%s\");\n",
EscapeString(text));
232 code += ind + absl::StrFormat(
"ImGui::TextWrapped(\"%s\");\n",
EscapeString(text));
235 code += ind + absl::StrFormat(
"ImGui::BulletText(\"%s\");\n",
EscapeString(text));
238 auto* color_prop =
const_cast<WidgetDefinition&
>(widget).GetProperty(
"color");
239 ImVec4 color = color_prop ? color_prop->color_value : ImVec4(1, 1, 1, 1);
240 code += ind + absl::StrFormat(
241 "ImGui::TextColored(ImVec4(%.2f, %.2f, %.2f, %.2f), \"%s\");\n",
242 color.x, color.y, color.z, color.w,
EscapeString(text));
258 auto* label_prop =
const_cast<WidgetDefinition&
>(widget).GetProperty(
"label");
259 std::string label = label_prop ? label_prop->string_value :
"Input";
261 switch (widget.
type) {
263 code += ind + absl::StrFormat(
"ImGui::Checkbox(\"%s\", &%s);\n",
268 auto* hint_prop =
const_cast<WidgetDefinition&
>(widget).GetProperty(
"hint");
269 if (hint_prop && !hint_prop->string_value.empty()) {
270 code += ind + absl::StrFormat(
271 "ImGui::InputTextWithHint(\"%s\", \"%s\", %s, sizeof(%s));\n",
275 code += ind + absl::StrFormat(
"ImGui::InputText(\"%s\", %s, sizeof(%s));\n",
282 code += ind + absl::StrFormat(
"ImGui::InputInt(\"%s\", &%s);\n",
289 int min_val = min_prop ? min_prop->int_value : 0;
290 int max_val = max_prop ? max_prop->int_value : 100;
291 code += ind + absl::StrFormat(
"ImGui::SliderInt(\"%s\", &%s, %d, %d);\n",
308 switch (widget.
type) {
311 auto* columns_prop =
const_cast<WidgetDefinition&
>(widget).GetProperty(
"columns");
312 std::string
id = id_prop ? id_prop->string_value :
"table";
313 int columns = columns_prop ? columns_prop->int_value : 2;
315 code += ind + absl::StrFormat(
"if (ImGui::BeginTable(\"%s\", %d)) {\n",
319 for (
const auto& child : widget.
children) {
323 code += ind +
" ImGui::EndTable();\n";
329 code += ind +
"ImGui::TableNextRow();\n";
333 code += ind +
"ImGui::TableNextColumn();\n";
348 auto* size_prop =
const_cast<WidgetDefinition&
>(widget).GetProperty(
"size");
349 ImVec2 size = size_prop ? size_prop->vec2_value : ImVec2(300, 200);
351 code += ind + absl::StrFormat(
"// Custom canvas - size: %.0fx%.0f\n",
353 code += ind +
"ImVec2 canvas_pos = ImGui::GetCursorScreenPos();\n";
354 code += ind + absl::StrFormat(
"ImVec2 canvas_size = ImVec2(%.0ff, %.0ff);\n",
356 code += ind +
"ImDrawList* draw_list = ImGui::GetWindowDrawList();\n";
357 code += ind +
"// TODO: Add custom drawing code here\n";
358 code += ind +
"ImGui::Dummy(canvas_size);\n";
369 code += ind + absl::StrFormat(
"// TODO: Container widget: %s\n",
377 std::string var_name = widget.
id;
378 std::replace(var_name.begin(), var_name.end(),
'.',
'_');
379 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