Skip to content

Commit 6489da9

Browse files
authored
Merge pull request #145 from OpenVicProject/gui-loading
Added support for loading all gui files
2 parents 068c13e + 87fa1c7 commit 6489da9

File tree

25 files changed

+515
-168
lines changed

25 files changed

+515
-168
lines changed

src/openvic-simulation/dataloader/Dataloader.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,21 +283,34 @@ bool Dataloader::_load_interface_files(UIManager& ui_manager) const {
283283
ui_manager.lock_sprites();
284284
ui_manager.lock_fonts();
285285

286-
// Hard-coded example until the mechanism for requesting them from GDScript is fleshed out
286+
/* Hard-coded GUI file names, might be replaced with a dynamic system but everything should still be loaded on startup. */
287287
static const std::vector<std::string_view> gui_files {
288-
"province_interface.gui", "topbar.gui"
288+
/* Contains generic listbox scrollbar */
289+
"core",
290+
291+
/* Over-map menus */
292+
"province_interface", "topbar", "menubar", "outliner",
293+
294+
/* Nation management screens */
295+
"country_production", "country_budget", "country_technology", "country_politics", "country_pops", "country_trade",
296+
"country_diplomacy", "country_military"
289297
};
290298

299+
static constexpr std::string_view gui_file_extension = ".gui";
300+
291301
ui_manager.reserve_more_scenes(gui_files.size());
292302

293303
for (std::string_view const& gui_file : gui_files) {
294304
if (!ui_manager.load_gui_file(
295-
gui_file, parse_defines(lookup_file(append_string_views(interface_directory, gui_file))).get_file_node()
305+
gui_file, parse_defines(lookup_file(
306+
append_string_views(interface_directory, gui_file, gui_file_extension)
307+
)).get_file_node()
296308
)) {
297309
Logger::error("Failed to load interface gui file: ", gui_file);
298310
ret = false;
299311
}
300312
}
313+
301314
ui_manager.lock_scenes();
302315

303316
return ret;

src/openvic-simulation/dataloader/NodeTools.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ node_callback_t NodeTools::expect_date(callback_t<Date> callback) {
169169
return expect_identifier(expect_date_str(callback));
170170
}
171171

172+
node_callback_t NodeTools::expect_date_string(callback_t<Date> callback) {
173+
return expect_string(expect_date_str(callback));
174+
}
175+
176+
node_callback_t NodeTools::expect_date_identifier_or_string(callback_t<Date> callback) {
177+
return expect_identifier_or_string(expect_date_str(callback));
178+
}
179+
172180
node_callback_t NodeTools::expect_years(callback_t<Timespan> callback) {
173181
return expect_uint<Timespan::day_t>([callback](Timespan::day_t val) -> bool {
174182
return callback(Timespan::from_years(val));

src/openvic-simulation/dataloader/NodeTools.hpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ namespace OpenVic {
119119

120120
callback_t<std::string_view> expect_date_str(callback_t<Date> callback);
121121
node_callback_t expect_date(callback_t<Date> callback);
122+
node_callback_t expect_date_string(callback_t<Date> callback);
123+
node_callback_t expect_date_identifier_or_string(callback_t<Date> callback);
122124
node_callback_t expect_years(callback_t<Timespan> callback);
123125
node_callback_t expect_months(callback_t<Timespan> callback);
124126
node_callback_t expect_days(callback_t<Timespan> callback);
@@ -371,15 +373,15 @@ namespace OpenVic {
371373

372374
template<typename T, StringMapCase Case>
373375
Callback<std::string_view> auto expect_mapped_string(
374-
template_string_map_t<T, Case> const& map, Callback<T> auto callback
376+
template_string_map_t<T, Case> const& map, Callback<T> auto callback, bool warn = false
375377
) {
376-
return [&map, callback](std::string_view string) -> bool {
378+
return [&map, callback, warn](std::string_view string) -> bool {
377379
const typename template_string_map_t<T, Case>::const_iterator it = map.find(string);
378380
if (it != map.end()) {
379381
return callback(it->second);
380382
}
381-
Logger::error("String not found in map: ", string);
382-
return false;
383+
Logger::warn_or_error(warn, "String not found in map: ", string);
384+
return warn;
383385
};
384386
}
385387

@@ -470,24 +472,14 @@ namespace OpenVic {
470472
};
471473
}
472474

473-
template<typename... Args>
474-
bool warn_or_error(bool warn, Args&&... args) {
475-
if (warn) {
476-
Logger::warning(std::forward<Args>(args)...);
477-
return true;
478-
} else {
479-
Logger::error(std::forward<Args>(args)...);
480-
return false;
481-
}
482-
}
483-
484475
template<typename T, typename U, typename... SetArgs>
485476
Callback<T> auto set_callback(tsl::ordered_set<U, SetArgs...>& set, bool warn = false) {
486477
return [&set, warn](T val) -> bool {
487478
if (set.emplace(std::move(val)).second) {
488479
return true;
489480
}
490-
return warn_or_error(warn, "Duplicate set entry: \"", val, "\"");
481+
Logger::warn_or_error(warn, "Duplicate set entry: \"", val, "\"");
482+
return warn;
491483
};
492484
}
493485

@@ -497,7 +489,8 @@ namespace OpenVic {
497489
if (set.emplace(&val).second) {
498490
return true;
499491
}
500-
return warn_or_error(warn, "Duplicate set entry: \"", &val, "\"");
492+
Logger::warn_or_error(warn, "Duplicate set entry: \"", &val, "\"");
493+
return warn;
501494
};
502495
}
503496

@@ -509,7 +502,8 @@ namespace OpenVic {
509502
if (map.emplace(key, std::move(value)).second) {
510503
return true;
511504
}
512-
return warn_or_error(warn, "Duplicate map entry with key: \"", key, "\"");
505+
Logger::warn_or_error(warn, "Duplicate map entry with key: \"", key, "\"");
506+
return warn;
513507
};
514508
}
515509
}

src/openvic-simulation/history/DiplomaticHistory.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ AllianceHistory::AllianceHistory(
3232
Country const* new_first,
3333
Country const* new_second,
3434
const Period new_period
35-
) : first { new_first }, second { new_second }, period {new_period} {}
35+
) : first { new_first }, second { new_second }, period { new_period } {}
3636

3737
ReparationsHistory::ReparationsHistory(
3838
Country const* new_receiver,
3939
Country const* new_sender,
4040
const Period new_period
41-
) : receiver { new_receiver }, sender { new_sender }, period {new_period} {}
41+
) : receiver { new_receiver }, sender { new_sender }, period { new_period } {}
4242

4343
SubjectHistory::SubjectHistory(
4444
Country const* new_overlord,
4545
Country const* new_subject,
4646
const type_t new_type,
4747
const Period new_period
48-
) : overlord { new_overlord }, subject { new_subject }, type { new_type }, period {new_period} {}
48+
) : overlord { new_overlord }, subject { new_subject }, type { new_type }, period { new_period } {}
4949

5050
void DiplomaticHistoryManager::reserve_more_wars(size_t size) {
5151
if (locked) {
@@ -115,10 +115,10 @@ bool DiplomaticHistoryManager::load_diplomacy_history_file(CountryManager const&
115115
std::optional<Date> end {};
116116

117117
bool ret = expect_dictionary_keys(
118-
"first", ONE_EXACTLY, expect_identifier_or_string(country_manager.expect_country_str(assign_variable_callback_pointer(first))),
119-
"second", ONE_EXACTLY, expect_identifier_or_string(country_manager.expect_country_str(assign_variable_callback_pointer(second))),
120-
"start_date", ONE_EXACTLY, expect_identifier_or_string(expect_date_str(assign_variable_callback(start))),
121-
"end_date", ZERO_OR_ONE, expect_identifier_or_string(expect_date_str(assign_variable_callback(end)))
118+
"first", ONE_EXACTLY, country_manager.expect_country_identifier_or_string(assign_variable_callback_pointer(first)),
119+
"second", ONE_EXACTLY, country_manager.expect_country_identifier_or_string(assign_variable_callback_pointer(second)),
120+
"start_date", ONE_EXACTLY, expect_date_identifier_or_string(assign_variable_callback(start)),
121+
"end_date", ZERO_OR_ONE, expect_date_identifier_or_string(assign_variable_callback(end))
122122
)(node);
123123

124124
alliances.push_back({ first, second, { start, end } });
@@ -131,10 +131,10 @@ bool DiplomaticHistoryManager::load_diplomacy_history_file(CountryManager const&
131131
std::optional<Date> end {};
132132

133133
bool ret = expect_dictionary_keys(
134-
"first", ONE_EXACTLY, expect_identifier_or_string(country_manager.expect_country_str(assign_variable_callback_pointer(overlord))),
135-
"second", ONE_EXACTLY, expect_identifier_or_string(country_manager.expect_country_str(assign_variable_callback_pointer(subject))),
136-
"start_date", ONE_EXACTLY, expect_identifier_or_string(expect_date_str(assign_variable_callback(start))),
137-
"end_date", ZERO_OR_ONE, expect_identifier_or_string(expect_date_str(assign_variable_callback(end)))
134+
"first", ONE_EXACTLY, country_manager.expect_country_identifier_or_string(assign_variable_callback_pointer(overlord)),
135+
"second", ONE_EXACTLY, country_manager.expect_country_identifier_or_string(assign_variable_callback_pointer(subject)),
136+
"start_date", ONE_EXACTLY, expect_date_identifier_or_string(assign_variable_callback(start)),
137+
"end_date", ZERO_OR_ONE, expect_date_identifier_or_string(assign_variable_callback(end))
138138
)(node);
139139

140140
subjects.push_back({ overlord, subject, SubjectHistory::type_t::VASSAL, { start, end } });
@@ -147,10 +147,10 @@ bool DiplomaticHistoryManager::load_diplomacy_history_file(CountryManager const&
147147
std::optional<Date> end {};
148148

149149
bool ret = expect_dictionary_keys(
150-
"first", ONE_EXACTLY, country_manager.expect_country_identifier(assign_variable_callback_pointer(overlord)),
151-
"second", ONE_EXACTLY, country_manager.expect_country_identifier(assign_variable_callback_pointer(subject)),
152-
"start_date", ONE_EXACTLY, expect_date(assign_variable_callback(start)),
153-
"end_date", ZERO_OR_ONE, expect_date(assign_variable_callback(end))
150+
"first", ONE_EXACTLY, country_manager.expect_country_identifier_or_string(assign_variable_callback_pointer(overlord)),
151+
"second", ONE_EXACTLY, country_manager.expect_country_identifier_or_string(assign_variable_callback_pointer(subject)),
152+
"start_date", ONE_EXACTLY, expect_date_identifier_or_string(assign_variable_callback(start)),
153+
"end_date", ZERO_OR_ONE, expect_date_identifier_or_string(assign_variable_callback(end))
154154
)(node);
155155

156156
subjects.push_back({ overlord, subject, SubjectHistory::type_t::UNION, { start, end } });
@@ -163,10 +163,10 @@ bool DiplomaticHistoryManager::load_diplomacy_history_file(CountryManager const&
163163
std::optional<Date> end {};
164164

165165
bool ret = expect_dictionary_keys(
166-
"first", ONE_EXACTLY, country_manager.expect_country_identifier(assign_variable_callback_pointer(overlord)),
167-
"second", ONE_EXACTLY, country_manager.expect_country_identifier(assign_variable_callback_pointer(subject)),
168-
"start_date", ONE_EXACTLY, expect_date(assign_variable_callback(start)),
169-
"end_date", ZERO_OR_ONE, expect_date(assign_variable_callback(end))
166+
"first", ONE_EXACTLY, country_manager.expect_country_identifier_or_string(assign_variable_callback_pointer(overlord)),
167+
"second", ONE_EXACTLY, country_manager.expect_country_identifier_or_string(assign_variable_callback_pointer(subject)),
168+
"start_date", ONE_EXACTLY, expect_date_identifier_or_string(assign_variable_callback(start)),
169+
"end_date", ZERO_OR_ONE, expect_date_identifier_or_string(assign_variable_callback(end))
170170
)(node);
171171

172172
subjects.push_back({ overlord, subject, SubjectHistory::type_t::SUBSTATE, { start, end } });
@@ -179,10 +179,10 @@ bool DiplomaticHistoryManager::load_diplomacy_history_file(CountryManager const&
179179
std::optional<Date> end {};
180180

181181
bool ret = expect_dictionary_keys(
182-
"first", ONE_EXACTLY, country_manager.expect_country_identifier(assign_variable_callback_pointer(receiver)),
183-
"second", ONE_EXACTLY, country_manager.expect_country_identifier(assign_variable_callback_pointer(sender)),
184-
"start_date", ONE_EXACTLY, expect_date(assign_variable_callback(start)),
185-
"end_date", ZERO_OR_ONE, expect_date(assign_variable_callback(end))
182+
"first", ONE_EXACTLY, country_manager.expect_country_identifier_or_string(assign_variable_callback_pointer(receiver)),
183+
"second", ONE_EXACTLY, country_manager.expect_country_identifier_or_string(assign_variable_callback_pointer(sender)),
184+
"start_date", ONE_EXACTLY, expect_date_identifier_or_string(assign_variable_callback(start)),
185+
"end_date", ZERO_OR_ONE, expect_date_identifier_or_string(assign_variable_callback(end))
186186
)(node);
187187

188188
reparations.push_back({ receiver, sender, { start, end } });

src/openvic-simulation/history/Period.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using namespace OpenVic;
55
Period::Period(
66
const Date new_start_date,
77
const std::optional<Date> new_end_date
8-
) : start_date {new_start_date}, end_date {new_end_date} {}
8+
) : start_date { new_start_date }, end_date { new_end_date } {}
99

1010
bool Period::is_date_in_period(const Date date) const {
1111
return start_date <= date && (!end_date.has_value() || end_date.value() >= date);

src/openvic-simulation/interface/GFX.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ using namespace OpenVic;
44
using namespace OpenVic::GFX;
55
using namespace OpenVic::NodeTools;
66

7-
Font::Font(std::string_view new_identifier, colour_argb_t new_colour, std::string_view new_fontname)
8-
: HasIdentifierAndAlphaColour { new_identifier, new_colour, false }, fontname { new_fontname } {}
7+
Font::Font(
8+
std::string_view new_identifier, colour_argb_t new_colour, std::string_view new_fontname, std::string_view new_charset,
9+
uint32_t new_height
10+
) : HasIdentifierAndAlphaColour { new_identifier, new_colour, false }, fontname { new_fontname }, charset { new_charset },
11+
height { new_height } {}
912

1013
node_callback_t Sprite::expect_sprites(length_callback_t length_callback, callback_t<std::unique_ptr<Sprite>&&> callback) {
1114
return expect_dictionary_keys_and_length(
@@ -17,8 +20,9 @@ node_callback_t Sprite::expect_sprites(length_callback_t length_callback, callba
1720
"textSpriteType", ZERO_OR_MORE, _expect_instance<Sprite, TextureSprite>(callback),
1821
"maskedShieldType", ZERO_OR_MORE, _expect_instance<Sprite, MaskedFlag>(callback),
1922
"tileSpriteType", ZERO_OR_MORE, _expect_instance<Sprite, TileTextureSprite>(callback),
20-
// TODO - add the rest of the sprite types
21-
"corneredTileSpriteType", ZERO_OR_MORE, success_callback,
23+
"corneredTileSpriteType", ZERO_OR_MORE, _expect_instance<Sprite, CorneredTileTextureSprite>(callback),
24+
25+
/* Each only has one vanilla instance which isn't used anywhere. */
2226
"BarChartType", ZERO_OR_MORE, success_callback,
2327
"scrollingSprite", ZERO_OR_MORE, success_callback
2428
);
@@ -47,16 +51,31 @@ TileTextureSprite::TileTextureSprite() : texture_file {}, size {} {}
4751
bool TileTextureSprite::_fill_key_map(case_insensitive_key_map_t& key_map) {
4852
bool ret = Sprite::_fill_key_map(key_map);
4953
ret &= add_key_map_entries(key_map,
50-
"texturefile", ZERO_OR_ONE, expect_string(assign_variable_callback_string(texture_file)),
51-
"size", ZERO_OR_ONE, expect_ivec2(assign_variable_callback(size)),
54+
"texturefile", ONE_EXACTLY, expect_string(assign_variable_callback_string(texture_file)),
55+
"size", ONE_EXACTLY, expect_ivec2(assign_variable_callback(size)),
5256

5357
"norefcount", ZERO_OR_ONE, success_callback,
5458
"loadType", ZERO_OR_ONE, success_callback
5559
);
5660
return ret;
5761
}
5862

59-
ProgressBar::ProgressBar() : back_colour {}, progress_colour {} {}
63+
CorneredTileTextureSprite::CorneredTileTextureSprite() : texture_file {}, size {}, border_size {} {}
64+
65+
bool CorneredTileTextureSprite::_fill_key_map(case_insensitive_key_map_t& key_map) {
66+
bool ret = Sprite::_fill_key_map(key_map);
67+
ret &= add_key_map_entries(key_map,
68+
"texturefile", ZERO_OR_ONE, expect_string(assign_variable_callback_string(texture_file)),
69+
"size", ONE_EXACTLY, expect_ivec2(assign_variable_callback(size)),
70+
"borderSize", ONE_EXACTLY, expect_ivec2(assign_variable_callback(border_size)),
71+
72+
"allwaystransparent", ZERO_OR_ONE, success_callback,
73+
"loadType", ZERO_OR_ONE, success_callback
74+
);
75+
return ret;
76+
}
77+
78+
ProgressBar::ProgressBar() : back_colour {}, back_texture_file {}, progress_colour {}, progress_texture_file {}, size {} {}
6079

6180
bool ProgressBar::_fill_key_map(case_insensitive_key_map_t& key_map) {
6281
bool ret = Sprite::_fill_key_map(key_map);
@@ -90,6 +109,7 @@ bool LineChart::_fill_key_map(case_insensitive_key_map_t& key_map) {
90109
ret &= add_key_map_entries(key_map,
91110
"size", ONE_EXACTLY, expect_ivec2(assign_variable_callback(size)),
92111
"linewidth", ONE_EXACTLY, expect_uint(assign_variable_callback(linewidth)),
112+
93113
"allwaystransparent", ZERO_OR_ONE, success_callback
94114
);
95115
return ret;
@@ -102,6 +122,7 @@ bool MaskedFlag::_fill_key_map(case_insensitive_key_map_t& key_map) {
102122
ret &= add_key_map_entries(key_map,
103123
"textureFile1", ONE_EXACTLY, expect_string(assign_variable_callback_string(overlay_file)),
104124
"textureFile2", ONE_EXACTLY, expect_string(assign_variable_callback_string(mask_file)),
125+
105126
"effectFile", ONE_EXACTLY, success_callback,
106127
"allwaystransparent", ZERO_OR_ONE, success_callback,
107128
"flipv", ZERO_OR_ONE, success_callback

src/openvic-simulation/interface/GFX.hpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ namespace OpenVic::GFX {
1212
friend class OpenVic::UIManager;
1313

1414
private:
15-
const std::string PROPERTY(fontname);
15+
std::string PROPERTY(fontname);
16+
std::string PROPERTY(charset);
17+
uint32_t PROPERTY(height);
1618

1719
// TODO - colorcodes, effect
1820

19-
Font(std::string_view new_identifier, colour_argb_t new_colour, std::string_view new_fontname);
21+
Font(
22+
std::string_view new_identifier, colour_argb_t new_colour, std::string_view new_fontname,
23+
std::string_view new_charset, uint32_t new_height
24+
);
2025

2126
public:
2227
Font(Font&&) = default;
@@ -79,6 +84,25 @@ namespace OpenVic::GFX {
7984
OV_DETAIL_GET_TYPE
8085
};
8186

87+
class CorneredTileTextureSprite final : public Sprite {
88+
friend std::unique_ptr<CorneredTileTextureSprite> std::make_unique<CorneredTileTextureSprite>();
89+
90+
std::string PROPERTY(texture_file);
91+
ivec2_t PROPERTY(size);
92+
ivec2_t PROPERTY(border_size);
93+
94+
protected:
95+
CorneredTileTextureSprite();
96+
97+
bool _fill_key_map(NodeTools::case_insensitive_key_map_t& key_map) override;
98+
99+
public:
100+
CorneredTileTextureSprite(CorneredTileTextureSprite&&) = default;
101+
virtual ~CorneredTileTextureSprite() = default;
102+
103+
OV_DETAIL_GET_TYPE
104+
};
105+
82106
class ProgressBar final : public Sprite {
83107
friend std::unique_ptr<ProgressBar> std::make_unique<ProgressBar>();
84108

0 commit comments

Comments
 (0)