aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic-extension/utility/UITools.cpp
blob: 1fcc5746dbd04d3373824a2f70faf2f81d22b58c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#include "UITools.hpp"

#include <godot_cpp/classes/button.hpp>
#include <godot_cpp/classes/check_box.hpp>
#include <godot_cpp/classes/color_rect.hpp>
#include <godot_cpp/classes/label.hpp>
#include <godot_cpp/classes/panel.hpp>
#include <godot_cpp/classes/style_box_texture.hpp>
#include <godot_cpp/classes/texture_progress_bar.hpp>
#include <godot_cpp/classes/texture_rect.hpp>
#include <godot_cpp/classes/theme.hpp>
#include <godot_cpp/variant/utility_functions.hpp>

#include "openvic-extension/classes/GFXButtonStateTexture.hpp"
#include "openvic-extension/classes/GFXIconTexture.hpp"
#include "openvic-extension/classes/GFXMaskedFlagTexture.hpp"
#include "openvic-extension/classes/GFXPieChartTexture.hpp"
#include "openvic-extension/classes/GUIOverlappingElementsBox.hpp"
#include "openvic-extension/singletons/AssetManager.hpp"
#include "openvic-extension/singletons/GameSingleton.hpp"
#include "openvic-extension/utility/Utilities.hpp"

using namespace godot;
using namespace OpenVic;

using OpenVic::Utilities::godot_to_std_string;
using OpenVic::Utilities::std_view_to_godot_string;
using OpenVic::Utilities::std_view_to_godot_string_name;

GFX::Sprite const* UITools::get_gfx_sprite(godot::String const& gfx_sprite) {
   GameSingleton* game_singleton = GameSingleton::get_singleton();
   ERR_FAIL_NULL_V(game_singleton, nullptr);
   GFX::Sprite const* sprite = game_singleton->get_game_manager().get_ui_manager().get_sprite_by_identifier(
      godot_to_std_string(gfx_sprite)
   );
   ERR_FAIL_NULL_V_MSG(sprite, nullptr, vformat("GFX sprite not found: %s", gfx_sprite));
   return sprite;
}

GUI::Element const* UITools::get_gui_element(godot::String const& gui_scene, godot::String const& gui_element) {
   GameSingleton const* game_singleton = GameSingleton::get_singleton();
   ERR_FAIL_NULL_V(game_singleton, nullptr);
   GUI::Scene const* scene =
      game_singleton->get_game_manager().get_ui_manager().get_scene_by_identifier(godot_to_std_string(gui_scene));
   ERR_FAIL_NULL_V_MSG(scene, nullptr, vformat("Failed to find GUI scene %s", gui_scene));
   GUI::Element const* element = scene->get_scene_element_by_identifier(godot_to_std_string(gui_element));
   ERR_FAIL_NULL_V_MSG(element, nullptr, vformat("Failed to find GUI element %s in GUI scene %s", gui_element, gui_scene));
   return element;
}

GUI::Position const* UITools::get_gui_position(String const& gui_scene, String const& gui_position) {
   GameSingleton const* game_singleton = GameSingleton::get_singleton();
   ERR_FAIL_NULL_V(game_singleton, nullptr);
   GUI::Scene const* scene =
      game_singleton->get_game_manager().get_ui_manager().get_scene_by_identifier(godot_to_std_string(gui_scene));
   ERR_FAIL_NULL_V_MSG(scene, nullptr, vformat("Failed to find GUI scene %s", gui_scene));
   GUI::Position const* position = scene->get_scene_position_by_identifier(godot_to_std_string(gui_position));
   ERR_FAIL_NULL_V_MSG(position, nullptr, vformat("Failed to find GUI position %s in GUI scene %s", gui_position, gui_scene));
   return position;
}

/* GUI::Element tree -> godot::Control tree conversion code below: */

namespace OpenVic {
   struct generate_gui_args_t {
      GUI::Element const& element;
      godot::String const& name;
      AssetManager& asset_manager;
      godot::Control*& result;

      constexpr generate_gui_args_t(
         GUI::Element const& new_element, godot::String const& new_name, AssetManager& new_asset_manager,
         godot::Control*& new_result
      ) : element { new_element }, name { new_name }, asset_manager { new_asset_manager }, result { new_result } {}
   };
}

template<std::derived_from<Control> T>
static bool new_control(T*& node, GUI::Element const& element, String const& name) {
   node = memnew(T);
   ERR_FAIL_NULL_V(node, false);

   using enum GUI::Element::orientation_t;
   using enum Control::LayoutPreset;
   static const ordered_map<GUI::Element::orientation_t, Control::LayoutPreset> orientation_map {
      { UPPER_LEFT, PRESET_TOP_LEFT }, { LOWER_LEFT, PRESET_BOTTOM_LEFT },
      { LOWER_RIGHT, PRESET_BOTTOM_RIGHT }, { UPPER_RIGHT, PRESET_TOP_RIGHT },
      { CENTER, PRESET_CENTER }
   };

   if (name.is_empty()) {
      node->set_name(std_view_to_godot_string(element.get_name()));
   } else {
      node->set_name(name);
   }

   bool ret = true;
   const decltype(orientation_map)::const_iterator it = orientation_map.find(element.get_orientation());
   if (it != orientation_map.end()) {
      node->set_anchors_and_offsets_preset(it->second);
   } else {
      UtilityFunctions::push_error("Invalid orientation for GUI element ", std_view_to_godot_string(element.get_name()));
      ret = false;
   }

   node->set_position(Utilities::to_godot_fvec2(element.get_position()));
   node->set_h_size_flags(Control::SizeFlags::SIZE_SHRINK_BEGIN);
   node->set_v_size_flags(Control::SizeFlags::SIZE_SHRINK_BEGIN);
   node->set_focus_mode(Control::FOCUS_NONE);

   return ret;
}

static bool add_theme_stylebox(Control* control, StringName const& theme_name, Ref<Texture2D> const& texture) {
   Ref<StyleBoxTexture> stylebox;
   stylebox.instantiate();
   ERR_FAIL_NULL_V(stylebox, false);
   stylebox->set_texture(texture);
   control->add_theme_stylebox_override(theme_name, stylebox);
   return true;
};

static bool generate_icon(generate_gui_args_t&& args) {
   GUI::Icon const& icon = static_cast<GUI::Icon const&>(args.element);

   const String icon_name = std_view_to_godot_string(icon.get_name());

   /* Change to use sprite type to choose Godot node type! */
   bool ret = true;
   if (icon.get_sprite() != nullptr) {
      if (icon.get_sprite()->is_type<GFX::TextureSprite>()) {
         TextureRect* godot_texture_rect = nullptr;
         ret &= new_control(godot_texture_rect, icon, args.name);
         ERR_FAIL_NULL_V_MSG(godot_texture_rect, false, vformat("Failed to create TextureRect for GUI icon %s", icon_name));

         GFX::TextureSprite const* texture_sprite = icon.get_sprite()->cast_to<GFX::TextureSprite>();
         Ref<GFXIconTexture> texture = GFXIconTexture::make_gfx_icon_texture(texture_sprite, icon.get_frame());
         if (texture.is_valid()) {
            godot_texture_rect->set_texture(texture);
         } else {
            UtilityFunctions::push_error("Failed to make GFXIconTexture for GUI icon ", icon_name);
            ret = false;
         }

         args.result = godot_texture_rect;
      } else if (icon.get_sprite()->is_type<GFX::MaskedFlag>()) {
         TextureRect* godot_texture_rect = nullptr;
         ret &= new_control(godot_texture_rect, icon, args.name);
         ERR_FAIL_NULL_V_MSG(godot_texture_rect, false, vformat("Failed to create TextureRect for GUI icon %s", icon_name));

         GFX::MaskedFlag const* masked_flag = icon.get_sprite()->cast_to<GFX::MaskedFlag>();
         Ref<GFXMaskedFlagTexture> texture = GFXMaskedFlagTexture::make_gfx_masked_flag_texture(masked_flag);
         if (texture.is_valid()) {
            godot_texture_rect->set_texture(texture);
         } else {
            UtilityFunctions::push_error("Failed to make GFXMaskedFlagTexture for GUI icon ", icon_name);
            ret = false;
         }

         args.result = godot_texture_rect;
      } else if (icon.get_sprite()->is_type<GFX::ProgressBar>()) {
         TextureProgressBar* godot_progress_bar = nullptr;
         ret &= new_control(godot_progress_bar, icon, args.name);
         ERR_FAIL_NULL_V_MSG(
            godot_progress_bar, false, vformat("Failed to create TextureProgressBar for GUI icon %s", icon_name)
         );

         GFX::ProgressBar const* progress_bar = icon.get_sprite()->cast_to<GFX::ProgressBar>();

         Ref<ImageTexture> back_texture;
         if (!progress_bar->get_back_texture_file().empty()) {
            const StringName back_texture_file = std_view_to_godot_string_name(progress_bar->get_back_texture_file());
            back_texture = args.asset_manager.get_texture(back_texture_file);
            if (back_texture.is_null()) {
               UtilityFunctions::push_error(
                  "Failed to load progress bar sprite back texture ", back_texture_file, " for GUI icon ", icon_name
               );
               ret = false;
            }
         }
         if (back_texture.is_null()) {
            const Color back_colour = Utilities::to_godot_color(progress_bar->get_back_colour());
            back_texture = Utilities::make_solid_colour_texture(
               back_colour, progress_bar->get_size().x, progress_bar->get_size().y
            );
            if (back_texture.is_null()) {
               UtilityFunctions::push_error(
                  "Failed to generate progress bar sprite ", back_colour, " back texture for GUI icon ", icon_name
               );
               ret = false;
            }
         }
         if (back_texture.is_valid()) {
            godot_progress_bar->set_under_texture(back_texture);
         } else {
            UtilityFunctions::push_error(
               "Failed to create and set progress bar sprite back texture for GUI icon ", icon_name
            );
            ret = false;
         }

         Ref<ImageTexture> progress_texture;
         if (!progress_bar->get_progress_texture_file().empty()) {
            const StringName progress_texture_file = std_view_to_godot_string_name(progress_bar->get_progress_texture_file());
            progress_texture = args.asset_manager.get_texture(progress_texture_file);
            if (progress_texture.is_null()) {
               UtilityFunctions::push_error(
                  "Failed to load progress bar sprite progress texture ", progress_texture_file, " for GUI icon ", icon_name
               );
               ret = false;
            }
         }
         if (progress_texture.is_null()) {
            const Color progress_colour = Utilities::to_godot_color(progress_bar->get_progress_colour());
            progress_texture = Utilities::make_solid_colour_texture(
               progress_colour, progress_bar->get_size().x, progress_bar->get_size().y
            );
            if (progress_texture.is_null()) {
               UtilityFunctions::push_error(
                  "Failed to generate progress bar sprite ", progress_colour, " progress texture for GUI icon ", icon_name
               );
               ret = false;
            }
         }
         if (progress_texture.is_valid()) {
            godot_progress_bar->set_progress_texture(progress_texture);
         } else {
            UtilityFunctions::push_error(
               "Failed to create and set progress bar sprite progress texture for GUI icon ", icon_name
            );
            ret = false;
         }

         // TODO - work out why progress bar is missing bottom border pixel (e.g. province building expansion bar)
         godot_progress_bar->set_custom_minimum_size(Utilities::to_godot_fvec2(static_cast<fvec2_t>(progress_bar->get_size())));

         args.result = godot_progress_bar;
      } else if (icon.get_sprite()->is_type<GFX::PieChart>()) {
         TextureRect* godot_texture_rect = nullptr;
         ret &= new_control(godot_texture_rect, icon, args.name);
         ERR_FAIL_NULL_V_MSG(godot_texture_rect, false, vformat("Failed to create TextureRect for GUI icon %s", icon_name));

         GFX::PieChart const* pie_chart = icon.get_sprite()->cast_to<GFX::PieChart>();
         Ref<GFXPieChartTexture> texture = GFXPieChartTexture::make_gfx_pie_chart_texture(pie_chart);
         if (texture.is_valid()) {
            godot_texture_rect->set_texture(texture);
            // TODO - work out why this is needed
            Vector2 pos = godot_texture_rect->get_position();
            pos.x -= texture->get_width() / 2.0f;
            godot_texture_rect->set_position(pos);
         } else {
            UtilityFunctions::push_error("Failed to make GFXPieChartTexture for GUI icon ", icon_name);
            ret = false;
         }

         args.result = godot_texture_rect;
      } else if (icon.get_sprite()->is_type<GFX::LineChart>()) {
         // TODO - generate line chart
      } else {
         UtilityFunctions::push_error("Invalid sprite type ", std_view_to_godot_string(icon.get_sprite()->get_type()),
            " for GUI icon ", icon_name);
         ret = false;
      }

      if (args.result != nullptr) {
         const float scale = icon.get_scale();
         args.result->set_scale({ scale, scale });
         // TODO - rotation (may have to translate as godot rotates around the top left corner)
      }
   } else {
      UtilityFunctions::push_error("Null sprite for GUI icon ", icon_name);
      ret = false;
   }
   return ret;
}

static bool generate_button(generate_gui_args_t&& args) {
   GUI::Button const& button = static_cast<GUI::Button const&>(args.element);

   // TODO - shortcut, sprite, text
   const String button_name = std_view_to_godot_string(button.get_name());

   Button* godot_button = nullptr;
   bool ret = new_control(godot_button, button, args.name);
   ERR_FAIL_NULL_V_MSG(godot_button, false, vformat("Failed to create Button for GUI button %s", button_name));

   godot_button->set_mouse_filter(Control::MOUSE_FILTER_PASS);

   if (!button.get_text().empty()) {
      godot_button->set_text(std_view_to_godot_string(button.get_text()));
   }

   using enum GFXButtonStateTexture::ButtonState;
   static constexpr std::array<GFXButtonStateTexture::ButtonState, 3> button_states { HOVER, PRESSED, DISABLED };

   std::vector<Ref<GFXButtonStateTexture>> button_state_textures;
   for (GFXButtonStateTexture::ButtonState button_state : button_states) {
      Ref<GFXButtonStateTexture> button_state_texture = GFXButtonStateTexture::make_gfx_button_state_texture(button_state);
      if (button_state_texture.is_valid()) {
         button_state_textures.push_back(button_state_texture);
      } else {
         UtilityFunctions::push_error(
            "Failed to make ", GFXButtonStateTexture::button_state_to_theme_name(button_state),
            " GFXButtonStateTexture for GUI button ", button_name
         );
         ret = false;
      }
   }

   if (button.get_sprite() != nullptr) {
      Ref<Texture2D> texture;
      if (button.get_sprite()->is_type<GFX::TextureSprite>()) {
         GFX::TextureSprite const* texture_sprite = button.get_sprite()->cast_to<GFX::TextureSprite>();
         texture = GFXIconTexture::make_gfx_icon_texture(texture_sprite, 0, button_state_textures);
         if (texture.is_null()) {
            UtilityFunctions::push_error("Failed to make GFXIconTexture for GUI button ", button_name);
            ret = false;
         }
      } else if (button.get_sprite()->is_type<GFX::MaskedFlag>()) {
         GFX::MaskedFlag const* masked_flag = button.get_sprite()->cast_to<GFX::MaskedFlag>();
         texture = GFXMaskedFlagTexture::make_gfx_masked_flag_texture(masked_flag, button_state_textures);
         if (texture.is_null()) {
            UtilityFunctions::push_error("Failed to make GFXMaskedFlagTexture for GUI button ", button_name);
            ret = false;
         }
      } else {
         UtilityFunctions::push_error("Invalid sprite type ", std_view_to_godot_string(button.get_sprite()->get_type()),
            " for GUI button ", button_name);
         ret = false;
      }

      if (texture.is_valid()) {
         godot_button->set_custom_minimum_size(texture->get_size());

         const auto add_stylebox = [godot_button, &button_name](StringName const& theme_name, Ref<Texture2D> const& texture) -> bool {
            Ref<StyleBoxTexture> stylebox;
            stylebox.instantiate();
            ERR_FAIL_NULL_V(stylebox, false);
            stylebox->set_texture(texture);
            godot_button->add_theme_stylebox_override(theme_name, stylebox);
            return true;
         };

         static const StringName theme_name_normal = "normal";
         ret &= add_theme_stylebox(godot_button, theme_name_normal, texture);

         for (Ref<GFXButtonStateTexture> const& button_state_texture : button_state_textures) {
            ret &= add_theme_stylebox(godot_button, button_state_texture->get_button_state_theme(), button_state_texture);
         }
      }
   } else {
      UtilityFunctions::push_error("Null sprite for GUI button ", button_name);
      ret = false;
   }

   if (button.get_font() != nullptr) {
      const StringName font_file = std_view_to_godot_string_name(button.get_font()->get_fontname());
      const Ref<Font> font = args.asset_manager.get_font(font_file);
      if (font.is_valid()) {
         godot_button->add_theme_font_override("font", font);
      } else {
         UtilityFunctions::push_error("Failed to load font for GUI button ", button_name);
         ret = false;
      }

      static const std::vector<StringName> button_font_themes {
         "font_color", "font_hover_color", "font_hover_pressed_color", "font_pressed_color", "font_disabled_color"
      };
      const Color colour = Utilities::to_godot_color(button.get_font()->get_colour());
      for (StringName const& theme_name : button_font_themes) {
         godot_button->add_theme_color_override(theme_name, colour);
      }
   }

   args.result = godot_button;
   return ret;
}

static bool generate_checkbox(generate_gui_args_t&& args) {
   GUI::Checkbox const& checkbox = static_cast<GUI::Checkbox const&>(args.element);

   // TODO - shortcut, sprite, text
   const String checkbox_name = std_view_to_godot_string(checkbox.get_name());

   CheckBox* godot_checkbox = nullptr;
   bool ret = new_control(godot_checkbox, checkbox, args.name);
   ERR_FAIL_NULL_V_MSG(godot_checkbox, false, vformat("Failed to create CheckBox for GUI checkbox %s", checkbox_name));

   if (checkbox.get_sprite() != nullptr) {
      GFX::TextureSprite const* texture_sprite = checkbox.get_sprite()->cast_to<GFX::TextureSprite>();
      if (texture_sprite != nullptr) {
         Ref<GFXIconTexture> icon_texture = GFXIconTexture::make_gfx_icon_texture(texture_sprite, 1);
         if (icon_texture.is_valid()) {
            godot_checkbox->set_custom_minimum_size(icon_texture->get_size());
            godot_checkbox->add_theme_icon_override("unchecked", icon_texture);
         } else {
            UtilityFunctions::push_error("Failed to make unchecked GFXIconTexture for GUI checkbox ", checkbox_name);
            ret = false;
         }
         icon_texture = GFXIconTexture::make_gfx_icon_texture(texture_sprite, 2);
         if (icon_texture.is_valid()) {
            godot_checkbox->add_theme_icon_override("checked", icon_texture);
         } else {
            UtilityFunctions::push_error("Failed to make checked GFXIconTexture for GUI checkbox ", checkbox_name);
            ret = false;
         }
      } else {
         UtilityFunctions::push_error(
            "Invalid sprite type ", std_view_to_godot_string(checkbox.get_sprite()->get_type()), " for GUI checkbox ",
            checkbox_name
         );
         ret = false;
      }
   } else {
      UtilityFunctions::push_error("Null sprite for GUI checkbox ", checkbox_name);
      ret = false;
   }

   args.result = godot_checkbox;
   return ret;
}

static bool generate_text(generate_gui_args_t&& args) {
   GUI::Text const& text = static_cast<GUI::Text const&>(args.element);

   const String text_name = std_view_to_godot_string(text.get_name());

   Label* godot_label = nullptr;
   bool ret = new_control(godot_label, text, args.name);
   ERR_FAIL_NULL_V_MSG(godot_label, false, vformat("Failed to create Label for GUI text %s", text_name));

   godot_label->set_text(std_view_to_godot_string(text.get_text()));
   godot_label->set_custom_minimum_size(Utilities::to_godot_fvec2(text.get_max_size()));

   using enum GUI::AlignedElement::format_t;
   static const ordered_map<GUI::AlignedElement::format_t, HorizontalAlignment> format_map {
      { left, HORIZONTAL_ALIGNMENT_LEFT },
      { centre, HORIZONTAL_ALIGNMENT_CENTER },
      { right, HORIZONTAL_ALIGNMENT_RIGHT }
   };

   const decltype(format_map)::const_iterator it = format_map.find(text.get_format());
   if (it != format_map.end()) {
      godot_label->set_horizontal_alignment(it->second);
   } else {
      UtilityFunctions::push_error("Invalid text format (horizontal alignment) for GUI text ", text_name);
      ret = false;
   }

   if (text.get_font() != nullptr) {
      const StringName font_file = std_view_to_godot_string_name(text.get_font()->get_fontname());
      const Ref<Font> font = args.asset_manager.get_font(font_file);
      if (font.is_valid()) {
         godot_label->add_theme_font_override("font", font);
      } else {
         UtilityFunctions::push_error("Failed to load font for GUI text ", text_name);
         ret = false;
      }
      const Color colour = Utilities::to_godot_color(text.get_font()->get_colour());
      godot_label->add_theme_color_override("font_color", colour);
   }

   args.result = godot_label;
   return ret;
}

static bool generate_overlapping_elements(generate_gui_args_t&& args) {
   GUI::OverlappingElementsBox const& overlapping_elements = static_cast<GUI::OverlappingElementsBox const&>(args.element);

   const String overlapping_elements_name = std_view_to_godot_string(overlapping_elements.get_name());

   GUIOverlappingElementsBox* box = nullptr;
   bool ret = new_control(box, overlapping_elements, args.name);
   ERR_FAIL_NULL_V_MSG(
      box, false,
      vformat("Failed to create GUIOverlappingElementsBox for GUI overlapping elements %s", overlapping_elements_name)
   );
   ret &= box->set_gui_overlapping_elements_box(&overlapping_elements) == OK;
   args.result = box;
   return ret;
}

template<std::derived_from<GUI::Element> T>
requires requires(T const& element) {
   { element.get_size() } -> std::same_as<fvec2_t>;
}
static bool generate_placeholder(generate_gui_args_t&& args, Color colour) {
   T const& cast_element = static_cast<T const&>(args.element);

   static const String type_name = std_view_to_godot_string(T::get_type_static());
   const String placeholder_name = std_view_to_godot_string(cast_element.get_name());
   const Vector2 godot_size = Utilities::to_godot_fvec2(cast_element.get_size());

   UtilityFunctions::push_warning(
      "Generating placeholder ColorRect for GUI ", type_name, " ", placeholder_name, " (size ", godot_size, ")"
   );

   ColorRect* godot_rect = nullptr;
   bool ret = new_control(godot_rect, cast_element, args.name);
   ERR_FAIL_NULL_V_MSG(
      godot_rect, false, vformat("Failed to create placeholder ColorRect for GUI %s %s", type_name, placeholder_name)
   );

   godot_rect->set_custom_minimum_size(godot_size);
   godot_rect->set_color(colour);

   args.result = godot_rect;
   return ret;
}

static bool generate_listbox(generate_gui_args_t&& args) {
   return generate_placeholder<GUI::ListBox>(std::move(args), { 0.0f, 0.0f, 1.0f, 0.3f });
}

static bool generate_texteditbox(generate_gui_args_t&& args) {
   return generate_placeholder<GUI::TextEditBox>(std::move(args), { 0.0f, 1.0f, 0.0f, 0.3f });
}

static bool generate_scrollbar(generate_gui_args_t&& args) {
   return generate_placeholder<GUI::Scrollbar>(std::move(args), { 1.0f, 0.0f, 0.0f, 0.3f });
}

/* Forward declaration for use in generate_window. */
static bool generate_element(GUI::Element const* element, String const& name, AssetManager& asset_manager, Control*& result);

static bool generate_window(generate_gui_args_t&& args) {
   GUI::Window const& window = static_cast<GUI::Window const&>(args.element);

   // TODO - moveable, fullscreen, dontRender (disable visibility?)
   const String window_name = std_view_to_godot_string(window.get_name());

   Panel* godot_panel = nullptr;
   bool ret = new_control(godot_panel, window, args.name);
   ERR_FAIL_NULL_V_MSG(godot_panel, false, vformat("Failed to create Panel for GUI window %s", window_name));

   godot_panel->set_custom_minimum_size(Utilities::to_godot_fvec2(window.get_size()));
   godot_panel->set_self_modulate({ 1.0f, 1.0f, 1.0f, 0.0f });

   for (std::unique_ptr<GUI::Element> const& element : window.get_window_elements()) {
      Control* node = nullptr;
      const bool element_ret = generate_element(element.get(), "", args.asset_manager, node);
      if (node != nullptr) {
         godot_panel->add_child(node);
      }
      if (!element_ret) {
         UtilityFunctions::push_error("Errors generating GUI element ", std_view_to_godot_string(element->get_name()));
         ret = false;
      }
   }

   args.result = godot_panel;
   return ret;
}

static bool generate_element(GUI::Element const* element, String const& name, AssetManager& asset_manager, Control*& result) {
   ERR_FAIL_NULL_V(element, false);
   static const ordered_map<std::string_view, bool (*)(generate_gui_args_t&&)> type_map {
      { GUI::Icon::get_type_static(), &generate_icon },
      { GUI::Button::get_type_static(), &generate_button },
      { GUI::Checkbox::get_type_static(), &generate_checkbox },
      { GUI::Text::get_type_static(), &generate_text },
      { GUI::OverlappingElementsBox::get_type_static(), &generate_overlapping_elements },
      { GUI::ListBox::get_type_static(), &generate_listbox },
      { GUI::TextEditBox::get_type_static(), &generate_texteditbox },
      { GUI::Scrollbar::get_type_static(), &generate_scrollbar },
      { GUI::Window::get_type_static(), &generate_window }
   };
   const decltype(type_map)::const_iterator it = type_map.find(element->get_type());
   ERR_FAIL_COND_V_MSG(
      it == type_map.end(), false, vformat("Invalid GUI element type: %s", std_view_to_godot_string(element->get_type()))
   );
   return it->second({ *element, name, asset_manager, result });
}

bool UITools::generate_gui_element(
   GUI::Element const* element, String const& name, Control*& result
) {
   result = nullptr;
   AssetManager* asset_manager = AssetManager::get_singleton();
   ERR_FAIL_NULL_V(asset_manager, false);
   return generate_element(element, name, *asset_manager, result);
}

bool UITools::generate_gui_element(
   godot::String const& gui_scene, godot::String const& gui_element, godot::String const& name, godot::Control*& result
) {
   return generate_gui_element(get_gui_element(gui_scene, gui_element), name, result);
}