Skip to content

Commit 495fd32

Browse files
authored
Merge pull request #615 from OpenVicProject/move/convert-functions
Move Godot type conversion functionality to core/Convert.hpp
2 parents 721871d + 31f624f commit 495fd32

35 files changed

Lines changed: 362 additions & 278 deletions

extension/src/openvic-extension/classes/GFXMaskedFlagTexture.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "GFXMaskedFlagTexture.hpp"
22

3+
#include "openvic-extension/core/Convert.hpp"
34
#include "openvic-extension/singletons/AssetManager.hpp"
45
#include "openvic-extension/singletons/GameSingleton.hpp"
56
#include "openvic-extension/core/Bind.hpp"
@@ -127,11 +128,11 @@ Error GFXMaskedFlagTexture::set_gfx_masked_flag(GFX::MaskedFlag const* new_gfx_m
127128
AssetManager* asset_manager = AssetManager::get_singleton();
128129
ERR_FAIL_NULL_V(asset_manager, FAILED);
129130

130-
const StringName overlay_file = Utilities::std_to_godot_string(new_gfx_masked_flag->get_overlay_file());
131+
const StringName overlay_file = convert_to<String>(new_gfx_masked_flag->get_overlay_file());
131132
const Ref<Image> new_overlay_image = asset_manager->get_image(overlay_file);
132133
ERR_FAIL_NULL_V_MSG(new_overlay_image, FAILED, Utilities::format("Failed to load flag overlay image: %s", overlay_file));
133134

134-
const StringName mask_file = Utilities::std_to_godot_string(new_gfx_masked_flag->get_mask_file());
135+
const StringName mask_file = convert_to<String>(new_gfx_masked_flag->get_mask_file());
135136
const Ref<Image> new_mask_image = asset_manager->get_image(mask_file);
136137
ERR_FAIL_NULL_V_MSG(new_mask_image, FAILED, Utilities::format("Failed to load flag mask image: %s", mask_file));
137138

@@ -154,16 +155,16 @@ Error GFXMaskedFlagTexture::set_gfx_masked_flag_name(String const& gfx_masked_fl
154155
ERR_FAIL_NULL_V_MSG(
155156
new_masked_flag, FAILED, Utilities::format(
156157
"Invalid type for GFX sprite %s: %s (expected %s)", gfx_masked_flag_name,
157-
Utilities::std_to_godot_string(sprite->get_type()),
158-
Utilities::std_to_godot_string(GFX::MaskedFlag::get_type_static())
158+
convert_to<String>(sprite->get_type()),
159+
convert_to<String>(GFX::MaskedFlag::get_type_static())
159160
)
160161
);
161162

162163
return set_gfx_masked_flag(new_masked_flag);
163164
}
164165

165166
String GFXMaskedFlagTexture::get_gfx_masked_flag_name() const {
166-
return gfx_masked_flag != nullptr ? Utilities::std_to_godot_string(gfx_masked_flag->get_name()) : String {};
167+
return gfx_masked_flag != nullptr ? convert_to<String>(gfx_masked_flag->get_name()) : String {};
167168
}
168169

169170
Error GFXMaskedFlagTexture::set_flag_country_and_type(
@@ -206,7 +207,7 @@ Error GFXMaskedFlagTexture::set_flag_country_name_and_type(
206207
CountryDefinition const* new_flag_country = game_singleton->get_definition_manager()
207208
.get_country_definition_manager()
208209
.get_country_definition_by_identifier(
209-
Utilities::godot_to_std_string(new_flag_country_name)
210+
convert_to<std::string>(new_flag_country_name)
210211
);
211212
ERR_FAIL_NULL_V_MSG(new_flag_country, FAILED, Utilities::format("Country not found: %s", new_flag_country_name));
212213

@@ -221,7 +222,7 @@ Error GFXMaskedFlagTexture::set_flag_country(CountryInstance* new_flag_country)
221222
GovernmentType const* government_type = new_flag_country->flag_government_type.get_untracked();
222223

223224
const StringName new_flag_type = government_type != nullptr
224-
? StringName { Utilities::std_to_godot_string(government_type->get_flag_type()) }
225+
? StringName { convert_to<String>(government_type->get_flag_type()) }
225226
: StringName {};
226227

227228
return set_flag_country_and_type(&new_flag_country->get_country_definition(), new_flag_type);
@@ -240,13 +241,13 @@ Error GFXMaskedFlagTexture::set_flag_country_name(String const& new_flag_country
240241

241242
CountryInstance* new_flag_country = instance_manager->get_country_instance_manager()
242243
.get_country_instance_by_identifier(
243-
Utilities::godot_to_std_string(new_flag_country_name)
244+
convert_to<std::string>(new_flag_country_name)
244245
);
245246
ERR_FAIL_NULL_V_MSG(new_flag_country, FAILED, Utilities::format("Country not found: %s", new_flag_country_name));
246247

247248
return set_flag_country(new_flag_country);
248249
}
249250

250251
String GFXMaskedFlagTexture::get_flag_country_name() const {
251-
return flag_country != nullptr ? Utilities::std_to_godot_string(flag_country->get_identifier()) : String {};
252+
return flag_country != nullptr ? convert_to<String>(flag_country->get_identifier()) : String {};
252253
}

extension/src/openvic-extension/classes/GFXPieChartTexture.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
#include <numbers>
44

55
#include "openvic-extension/core/Bind.hpp"
6+
#include "openvic-extension/core/Convert.hpp"
67
#include "openvic-extension/utility/UITools.hpp"
8+
#include "openvic-extension/utility/Utilities.hpp"
79

810
using namespace godot;
911
using namespace OpenVic;
@@ -184,13 +186,13 @@ Error GFXPieChartTexture::set_gfx_pie_chart_name(String const& gfx_pie_chart_nam
184186
ERR_FAIL_NULL_V_MSG(
185187
new_pie_chart, FAILED, Utilities::format(
186188
"Invalid type for GFX sprite %s: %s (expected %s)", gfx_pie_chart_name,
187-
Utilities::std_to_godot_string(sprite->get_type()),
188-
Utilities::std_to_godot_string(GFX::PieChart::get_type_static())
189+
convert_to<String>(sprite->get_type()),
190+
convert_to<String>(GFX::PieChart::get_type_static())
189191
)
190192
);
191193
return set_gfx_pie_chart(new_pie_chart);
192194
}
193195

194196
String GFXPieChartTexture::get_gfx_pie_chart_name() const {
195-
return gfx_pie_chart != nullptr ? Utilities::std_to_godot_string(gfx_pie_chart->get_name()) : String {};
197+
return gfx_pie_chart != nullptr ? convert_to<String>(gfx_pie_chart->get_name()) : String {};
196198
}

extension/src/openvic-extension/classes/GFXPieChartTexture.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#include <openvic-simulation/types/IndexedFlatMap.hpp>
88
#include <openvic-simulation/utility/Logger.hpp>
99

10+
#include "openvic-extension/core/Convert.hpp"
1011
#include "openvic-extension/utility/MapHelpers.hpp"
11-
#include "openvic-extension/utility/Utilities.hpp"
1212

1313
namespace OpenVic {
1414
template<typename MapType>
@@ -111,14 +111,14 @@ namespace OpenVic {
111111
for (size_t index = 0; index < array.size(); ++index) {
112112
auto const& [key, value] = sorted_distribution[index];
113113

114-
String identifier = Utilities::std_to_godot_string(key->get_identifier());
114+
String identifier = convert_to<String>(key->get_identifier());
115115
identifier += identifier_suffix;
116116

117117
Dictionary sub_dict;
118118

119119
sub_dict[_slice_tooltip_key()] = make_tooltip(key, identifier, value, total_weight);
120120
sub_dict[_slice_identifier_key()] = std::move(identifier);
121-
sub_dict[_slice_colour_key()] = Utilities::to_godot_color(key->get_colour());
121+
sub_dict[_slice_colour_key()] = convert_to<Color>(key->get_colour());
122122
sub_dict[_slice_weight_key()] = value;
123123

124124
array[index] = std::move(sub_dict);

extension/src/openvic-extension/classes/GFXSpriteTexture.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <godot_cpp/classes/rendering_server.hpp>
44
#include <godot_cpp/variant/utility_functions.hpp>
55

6+
#include "openvic-extension/core/Convert.hpp"
67
#include "openvic-extension/singletons/AssetManager.hpp"
78
#include "openvic-extension/core/Bind.hpp"
89
#include "openvic-extension/utility/UITools.hpp"
@@ -56,7 +57,7 @@ Error GFXSpriteTexture::set_gfx_texture_sprite(GFX::TextureSprite const* new_gfx
5657
AssetManager* asset_manager = AssetManager::get_singleton();
5758
ERR_FAIL_NULL_V(asset_manager, FAILED);
5859

59-
const StringName texture_file = Utilities::std_to_godot_string(new_gfx_texture_sprite->get_texture_file());
60+
const StringName texture_file = convert_to<String>(new_gfx_texture_sprite->get_texture_file());
6061

6162
/* Needed for GFXButtonStateTexture, AssetManager::get_texture will re-use this image from its internal cache. */
6263
const Ref<Image> image = asset_manager->get_image(texture_file);
@@ -80,7 +81,7 @@ Error GFXSpriteTexture::set_gfx_texture_sprite(GFX::TextureSprite const* new_gfx
8081
GFX::CorneredTileTextureSprite const* const cornered_tile_texture_sprite =
8182
gfx_texture_sprite->cast_to<GFX::CorneredTileTextureSprite>();
8283
if (cornered_tile_texture_sprite != nullptr) {
83-
cornered_tile_border_size = Utilities::to_godot_ivec2(cornered_tile_texture_sprite->get_border_size());
84+
cornered_tile_border_size = convert_to<Vector2i>(cornered_tile_texture_sprite->get_border_size());
8485
} else {
8586
cornered_tile_border_size = {};
8687
}
@@ -98,15 +99,15 @@ Error GFXSpriteTexture::set_gfx_texture_sprite_name(String const& gfx_texture_sp
9899
ERR_FAIL_NULL_V_MSG(
99100
new_texture_sprite, FAILED, Utilities::format(
100101
"Invalid type for GFX sprite %s: %s (expected %s)", gfx_texture_sprite_name,
101-
Utilities::std_to_godot_string(sprite->get_type()),
102-
Utilities::std_to_godot_string(GFX::TextureSprite::get_type_static())
102+
convert_to<String>(sprite->get_type()),
103+
convert_to<String>(GFX::TextureSprite::get_type_static())
103104
)
104105
);
105106
return set_gfx_texture_sprite(new_texture_sprite, icon);
106107
}
107108

108109
String GFXSpriteTexture::get_gfx_texture_sprite_name() const {
109-
return gfx_texture_sprite != nullptr ? Utilities::std_to_godot_string(gfx_texture_sprite->get_name()) : String {};
110+
return gfx_texture_sprite != nullptr ? convert_to<String>(gfx_texture_sprite->get_name()) : String {};
110111
}
111112

112113
Error GFXSpriteTexture::set_icon_index(int32_t new_icon_index) {

extension/src/openvic-extension/classes/GUIButton.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
#include <godot_cpp/variant/utility_functions.hpp>
66

7+
#include "openvic-extension/core/Convert.hpp"
78
#include "openvic-extension/singletons/AssetManager.hpp"
8-
#include "openvic-extension/utility/Utilities.hpp"
99

1010
using namespace godot;
1111
using namespace OpenVic;
@@ -82,7 +82,7 @@ Error GUIButton::set_gfx_font(GFX::Font const* gfx_font) {
8282

8383
Error err = OK;
8484

85-
const StringName font_file = Utilities::std_to_godot_string(gfx_font->get_fontname());
85+
const StringName font_file = convert_to<String>(gfx_font->get_fontname());
8686
const Ref<Font> font = asset_manager->get_font(font_file);
8787

8888
if (font.is_valid()) {
@@ -99,7 +99,7 @@ Error GUIButton::set_gfx_font(GFX::Font const* gfx_font) {
9999
"font_color", "font_hover_color", "font_hover_pressed_color", "font_pressed_color", "font_disabled_color"
100100
};
101101

102-
const Color colour = Utilities::to_godot_color(gfx_font->get_colour());
102+
const Color colour = convert_to<Color>(gfx_font->get_colour());
103103

104104
for (StringName const& theme_name : button_font_themes) {
105105
add_theme_color_override(theme_name, colour);

extension/src/openvic-extension/classes/GUILabel.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <godot_cpp/variant/utility_functions.hpp>
99

1010
#include "openvic-extension/classes/GUINode.hpp"
11+
#include "openvic-extension/core/Convert.hpp"
1112
#include "openvic-extension/singletons/AssetManager.hpp"
1213
#include "openvic-extension/singletons/GameSingleton.hpp"
1314
#include "openvic-extension/core/Bind.hpp"
@@ -245,7 +246,7 @@ void GUILabel::clear() {
245246
}
246247

247248
String GUILabel::get_gui_text_name() const {
248-
return gui_text != nullptr ? Utilities::std_to_godot_string(gui_text->get_name()) : String {};
249+
return gui_text != nullptr ? convert_to<godot::String>(gui_text->get_name()) : String {};
249250
}
250251

251252
Error GUILabel::set_gui_text(GUI::Text const* new_gui_text, GFX::Font::colour_codes_t const* override_colour_codes) {
@@ -260,7 +261,7 @@ Error GUILabel::set_gui_text(GUI::Text const* new_gui_text, GFX::Font::colour_co
260261

261262
gui_text = new_gui_text;
262263

263-
set_text(Utilities::std_to_godot_string(gui_text->get_text()));
264+
set_text(convert_to<String>(gui_text->get_text()));
264265

265266
using enum text_format_t;
266267
static const ordered_map<text_format_t, HorizontalAlignment> format_map {
@@ -272,11 +273,11 @@ Error GUILabel::set_gui_text(GUI::Text const* new_gui_text, GFX::Font::colour_co
272273
const decltype(format_map)::const_iterator it = format_map.find(gui_text->get_format());
273274
set_horizontal_alignment(it != format_map.end() ? it->second : HORIZONTAL_ALIGNMENT_LEFT);
274275

275-
set_max_size(Utilities::to_godot_fvec2(gui_text->get_max_size()));
276-
set_border_size(Utilities::to_godot_fvec2(gui_text->get_border_size()));
276+
set_max_size(convert_to<Vector2>(gui_text->get_max_size()));
277+
set_border_size(convert_to<Vector2>(gui_text->get_border_size()));
277278

278279
colour_codes = override_colour_codes != nullptr ? override_colour_codes : &gui_text->get_font()->get_colour_codes();
279-
set_default_colour(Utilities::to_godot_color(gui_text->get_font()->get_colour()));
280+
set_default_colour(convert_to<Color>(gui_text->get_font()->get_colour()));
280281

281282
font.unref();
282283
font_size = DEFAULT_FONT_SIZE;
@@ -285,7 +286,7 @@ Error GUILabel::set_gui_text(GUI::Text const* new_gui_text, GFX::Font::colour_co
285286

286287
Error err = OK;
287288

288-
const StringName font_filepath = Utilities::std_to_godot_string(gui_text->get_font()->get_fontname());
289+
const StringName font_filepath = convert_to<String>(gui_text->get_font()->get_fontname());
289290

290291
AssetManager& asset_manager = *AssetManager::get_singleton();
291292
Ref<FontFile> font_file = asset_manager.get_font(font_filepath);
@@ -299,7 +300,7 @@ Error GUILabel::set_gui_text(GUI::Text const* new_gui_text, GFX::Font::colour_co
299300
}
300301

301302
if (!gui_text->get_texture_file().empty()) {
302-
const StringName texture_path = Utilities::std_to_godot_string(gui_text->get_texture_file());
303+
const StringName texture_path = convert_to<String>(gui_text->get_texture_file());
303304
Ref<ImageTexture> texture = asset_manager.get_texture(texture_path);
304305
if (texture.is_valid()) {
305306
set_background_texture(texture);
@@ -358,7 +359,7 @@ void GUILabel::set_horizontal_alignment(HorizontalAlignment new_horizontal_align
358359
}
359360

360361
Size2 GUILabel::get_base_max_size() const {
361-
return gui_text != nullptr ? Utilities::to_godot_fvec2(gui_text->get_max_size()) : Size2 {};
362+
return gui_text != nullptr ? convert_to<Vector2>(gui_text->get_max_size()) : Size2 {};
362363
}
363364

364365
void GUILabel::set_max_size(Size2 new_max_size) {
@@ -616,7 +617,7 @@ std::vector<GUILabel::line_t> GUILabel::generate_lines_and_segments(
616617
ERR_CONTINUE(colour_codes == nullptr);
617618
const GFX::Font::colour_codes_t::const_iterator it = colour_codes->find(colour_it->second);
618619
if (it != colour_codes->end()) {
619-
new_colour = Utilities::to_godot_color(it->second);
620+
new_colour = convert_to<Color>(it->second);
620621
}
621622
}
622623
}
@@ -697,21 +698,21 @@ GUILabel::flag_segment_t GUILabel::make_flag_segment(String const& identifier) {
697698
if (instance_manager != nullptr) {
698699
CountryInstance* country_instance =
699700
instance_manager->get_country_instance_manager().get_country_instance_by_identifier(
700-
Utilities::godot_to_std_string(identifier)
701+
convert_to<std::string>(identifier) //convert_to<std::string>(identifier)
701702
);
702703

703704
if (country_instance != nullptr) {
704705
country_index = country_instance->get_country_definition().get_index();
705706

706707
GovernmentType const* government_type = country_instance->flag_government_type.get_untracked();
707708
if (government_type != nullptr) {
708-
flag_type = Utilities::std_to_godot_string(government_type->get_flag_type());
709+
flag_type = convert_to<String>(government_type->get_flag_type());
709710
}
710711
}
711712
} else {
712713
CountryDefinition const* country_definition =
713714
game_singleton.get_definition_manager().get_country_definition_manager().get_country_definition_by_identifier(
714-
Utilities::godot_to_std_string(identifier)
715+
convert_to<std::string>(identifier)
715716
);
716717

717718
if (country_definition != nullptr) {

extension/src/openvic-extension/classes/GUILineChart.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include "GUILineChart.hpp"
22

33
#include <godot_cpp/classes/gradient.hpp>
4+
#include <godot_cpp/variant/vector2i.hpp>
45

56
#include "openvic-extension/core/Bind.hpp"
7+
#include "openvic-extension/core/Convert.hpp"
68
#include "openvic-extension/utility/UITools.hpp"
79
#include "openvic-extension/utility/Utilities.hpp"
810

@@ -70,7 +72,7 @@ Error GUILineChart::set_gfx_line_chart(GFX::LineChart const* new_gfx_line_chart)
7072

7173
gfx_line_chart = new_gfx_line_chart;
7274

73-
set_custom_minimum_size(Utilities::to_godot_ivec2(gfx_line_chart->get_size()));
75+
set_custom_minimum_size(convert_to<godot::Vector2i>(gfx_line_chart->get_size()));
7476

7577
return OK;
7678
}
@@ -87,16 +89,16 @@ Error GUILineChart::set_gfx_line_chart_name(String const& new_gfx_line_chart_nam
8789
ERR_FAIL_NULL_V_MSG(
8890
new_gfx_line_chart, FAILED, Utilities::format(
8991
"Invalid type for GFX sprite %s: %s (expected %s)", new_gfx_line_chart_name,
90-
Utilities::std_to_godot_string(sprite->get_type()),
91-
Utilities::std_to_godot_string(GFX::LineChart::get_type_static())
92+
convert_to<String>(sprite->get_type()),
93+
convert_to<String>(GFX::LineChart::get_type_static())
9294
)
9395
);
9496

9597
return set_gfx_line_chart(new_gfx_line_chart);
9698
}
9799

98100
String GUILineChart::get_gfx_line_chart_name() const {
99-
return gfx_line_chart != nullptr ? Utilities::std_to_godot_string(gfx_line_chart->get_name()) : String {};
101+
return gfx_line_chart != nullptr ? convert_to<String>(gfx_line_chart->get_name()) : String {};
100102
}
101103

102104
Error GUILineChart::set_gradient_line(PackedFloat32Array const& line_values, float central_value, float min_value_range) {

extension/src/openvic-extension/classes/GUIListBox.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <godot_cpp/variant/utility_functions.hpp>
55

66
#include "openvic-extension/core/Bind.hpp"
7+
#include "openvic-extension/core/Convert.hpp"
78
#include "openvic-extension/utility/UITools.hpp"
89
#include "openvic-extension/utility/Utilities.hpp"
910

@@ -78,7 +79,7 @@ Error GUIListBox::_calculate_max_scroll_index(bool signal) {
7879
Error GUIListBox::_update_child_positions() {
7980
const int32_t child_count = get_child_count();
8081
const real_t max_height = get_size().height;
81-
const Vector2 offset = gui_listbox != nullptr ? Utilities::to_godot_fvec2(gui_listbox->get_items_offset()) : Vector2 {};
82+
const Vector2 offset = gui_listbox != nullptr ? convert_to<Vector2>(gui_listbox->get_items_offset()) : Vector2 {};
8283

8384
real_t height = 0.0_real;
8485

@@ -137,7 +138,7 @@ GUIListBox::GUIListBox() : fixed_item_height { 0.0_real } {}
137138

138139
Vector2 GUIListBox::_get_minimum_size() const {
139140
if (gui_listbox != nullptr) {
140-
Size2 size = Utilities::to_godot_fvec2(gui_listbox->get_size());
141+
Size2 size = convert_to<Vector2>(gui_listbox->get_size());
141142

142143
if (scrollbar != nullptr) {
143144
size.width += scrollbar->get_minimum_size().width;
@@ -257,7 +258,7 @@ Error GUIListBox::set_gui_listbox(GUI::ListBox const* new_gui_listbox) {
257258

258259
gui_listbox = new_gui_listbox;
259260

260-
const String scrollbar_name = Utilities::std_to_godot_string(gui_listbox->get_scrollbar_name());
261+
const String scrollbar_name = convert_to<String>(gui_listbox->get_scrollbar_name());
261262

262263
Error err = OK;
263264

@@ -279,8 +280,8 @@ Error GUIListBox::set_gui_listbox(GUI::ListBox const* new_gui_listbox) {
279280
if (scrollbar != nullptr) {
280281
add_child(scrollbar, false, INTERNAL_MODE_BACK);
281282

282-
const Size2 size = Utilities::to_godot_fvec2(gui_listbox->get_size());
283-
Vector2 position = Utilities::to_godot_fvec2(gui_listbox->get_scrollbar_offset());
283+
const Size2 size = convert_to<Vector2>(gui_listbox->get_size());
284+
Vector2 position = convert_to<Vector2>(gui_listbox->get_scrollbar_offset());
284285
position.x += size.width;
285286
scrollbar->set_position(position);
286287
scrollbar->set_length_override(size.height);
@@ -305,7 +306,7 @@ Error GUIListBox::set_gui_listbox(GUI::ListBox const* new_gui_listbox) {
305306
}
306307

307308
String GUIListBox::get_gui_listbox_name() const {
308-
return gui_listbox != nullptr ? Utilities::std_to_godot_string(gui_listbox->get_name()) : String {};
309+
return gui_listbox != nullptr ? convert_to<String>(gui_listbox->get_name()) : String {};
309310
}
310311

311312
GUIScrollbar* GUIListBox::get_scrollbar() const {

0 commit comments

Comments
 (0)