Project Alice
Loading...
Searching...
No Matches
gui_release_nation_window.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "dcon_generated.hpp"
6#include "nations.hpp"
7#include "system_state.hpp"
8#include "text.hpp"
9
10namespace ui {
11
13 dcon::nation_id content;
14};
16 dcon::nation_id content;
17};
18
20public:
21 void on_update(sys::state& state) noexcept override {
22 const dcon::national_identity_id niid = retrieve<dcon::national_identity_id>(state, parent);
23 disabled = !command::can_release_and_play_as(state, state.local_player_nation, niid);
24 }
25
26 void button_action(sys::state& state) noexcept override {
27 const dcon::national_identity_id niid = retrieve<dcon::national_identity_id>(state, parent);
28 command::release_and_play_as(state, state.local_player_nation, niid);
29 parent->set_visible(state, false);
30 }
31};
32
34public:
35 void on_update(sys::state& state) noexcept override {
36 const dcon::national_identity_id niid = retrieve<dcon::national_identity_id>(state, parent);
37 disabled = !command::can_make_vassal(state, state.local_player_nation, niid);
38 }
39
40 void button_action(sys::state& state) noexcept override {
41 const dcon::national_identity_id niid = retrieve<dcon::national_identity_id>(state, parent);
42 command::make_vassal(state, state.local_player_nation, niid);
43 parent->set_visible(state, false); // Close parent window automatically
44 }
45};
46
48public:
49 void on_update(sys::state& state) noexcept override {
50 set_text(state, text::produce_simple_string(state, "politics_release_vassal"));
51 }
52};
53
54class release_nation_description_text : public generic_multiline_text<dcon::national_identity_id> {
55public:
56 void on_create(sys::state& state) noexcept override {
58 black_text = false;
59 }
60 void populate_layout(sys::state& state, text::endless_layout& contents, dcon::national_identity_id id) noexcept override {
61 int64_t province_count = 0;
62 std::string provinces = "";
63 state.world.national_identity_for_each_core(id, [&](dcon::core_id core) {
64 auto province = state.world.core_get_province(core);
65 if(state.world.province_get_nation_from_province_ownership(province) == state.local_player_nation && province_count++ < 5) {
66 if(!provinces.empty()) {
67 provinces += ", ";
68 }
69 provinces += text::produce_simple_string(state, state.world.province_get_name(province));
70 }
71 });
72 auto box = text::open_layout_box(contents);
77 text::localised_format_box(state, contents, box, std::string_view("politics_release_vassal_desc"), sub);
78 if(province_count > 5) {
79 text::add_to_layout_box(state, contents, box, std::string(" and others."), text::text_color::black);
80 } else {
81 text::add_to_layout_box(state, contents, box, std::string("."), text::text_color::black);
82 }
83 text::close_layout_box(contents, box);
84 }
85};
86
88public:
89 void button_action(sys::state& state) noexcept override {
90 const dcon::national_identity_id niid = retrieve<dcon::national_identity_id>(state, parent);
91 send(state, parent, release_emplace_wrapper{ state.world.national_identity_get_nation_from_identity_holder(niid) });
92 }
93};
94
95class release_nation_option : public listbox_row_element_base<dcon::national_identity_id> {
96public:
97 std::unique_ptr<element_base> make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept override {
98 if(name == "name") {
99 return make_element_by_type<generic_name_text<dcon::national_identity_id>>(state, id);
100 } else if(name == "country_flag") {
101 return make_element_by_type<flag_button>(state, id);
102 } else if(name == "type") {
103 return make_element_by_type<national_identity_vassal_type_text>(state, id);
104 } else if(name == "desc") {
105 return make_element_by_type<release_nation_description_text>(state, id);
106 } else if(name == "country_release_vassal") {
107 return make_element_by_type<release_nation_button>(state, id);
108 } else {
109 return nullptr;
110 }
111 }
112};
113
114class release_nation_listbox : public listbox_element_base<release_nation_option, dcon::national_identity_id> {
115protected:
116 std::string_view get_row_element_name() override {
117 return "vassal_nation";
118 }
119
120public:
121 void on_update(sys::state& state) noexcept override {
122 row_contents.clear();
123 state.world.for_each_national_identity([&](dcon::national_identity_id ident) {
124 if(nations::can_release_as_vassal(state, state.local_player_nation, ident)) {
125 row_contents.push_back(ident);
126 }
127 });
128 std::sort(row_contents.begin(), row_contents.end(), [&](dcon::national_identity_id a, dcon::national_identity_id b) {
129 auto av = text::produce_simple_string(state, state.world.national_identity_get_name(a));
130 auto bv = text::produce_simple_string(state, state.world.national_identity_get_name(b));
131 if(av != bv)
132 return av > bv;
133 else
134 return a.index() < b.index();
135 });
136 update(state);
137 }
138};
139
141public:
142 void on_create(sys::state& state) noexcept override {
143 window_element_base::on_create(state);
144 set_visible(state, false);
145 }
146
147 std::unique_ptr<element_base> make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept override {
148 if(name == "nations") {
149 return make_element_by_type<release_nation_listbox>(state, id);
150 } else {
151 return nullptr;
152 }
153 }
154};
155
156// NOTE FOR OTHERS - THIS CODE IS NOT INTERCHANGEABLE WITH ITS SIMIARLY NAMED VERSION ABOVE DO NOT REMOVE THIS
157class release_nation_window_description_text : public generic_multiline_text<dcon::national_identity_id> {
158protected:
159 void populate_layout(sys::state& state, text::endless_layout& contents, dcon::national_identity_id id) noexcept override {
160 int64_t province_count = 0;
161 std::string provinces = "";
162 state.world.national_identity_for_each_core(id, [&](dcon::core_id core) {
163 auto province = state.world.core_get_province(core);
164 if(state.world.province_get_nation_from_province_ownership(province) == state.local_player_nation && province_count++ < 5) {
165 if(!provinces.empty()) {
166 provinces += ", ";
167 }
168 provinces += text::produce_simple_string(state, state.world.province_get_name(province));
169 }
170 });
171 auto box = text::open_layout_box(contents);
175 text::add_to_substitution_map(sub, text::variable_type::provinces, std::string_view(provinces));
176 text::localised_format_box(state, contents, box, std::string_view("politics_release_vassal_desc"), sub);
177 if(province_count > 5) {
178 text::add_to_layout_box(state, contents, box, std::string(" and others."));
179 } else {
180 text::add_to_layout_box(state, contents, box, std::string("."));
181 }
182 text::close_layout_box(contents, box);
183 for(auto& txt : contents.base_layout.contents) {
184 if(txt.color == text::text_color::black)
185 txt.color = text::text_color::white;
186 }
187 }
188
189public:
190 void on_create(sys::state& state) noexcept override {
192 black_text = false; // Nudge force to white text
193 }
194};
195
197public:
198 void on_create(sys::state& state) noexcept override {
199 window_element_base::on_create(state);
200 set_visible(state, false);
201 base_data.position.x = base_data.position.y = 0;
202 }
203
204 std::unique_ptr<element_base> make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept override {
205 if(name == "background") {
206 auto ptr = make_element_by_type<draggable_target>(state, id);
207 ptr->base_data.size = base_data.size; // Nudge
208 return ptr;
209 } else if(name == "default_popup_banner")
210 return make_element_by_type<image_element_base>(state, id); // Not used in Vic2?
211 else if(name == "title")
212 return make_element_by_type<politics_release_nation_window_title>(state, id);
213 else if(name == "description")
214 return make_element_by_type<release_nation_window_description_text>(state, id);
215 else if(name == "agreebutton")
216 return make_element_by_type<release_agree_button>(state, id);
217 else if(name == "declinebutton")
218 return make_element_by_type<generic_close_button>(state, id);
219 else if(name == "playasbutton")
220 return make_element_by_type<release_play_as_button>(state, id);
221 else
222 return nullptr;
223 }
224
225 message_result get(sys::state& state, Cyto::Any& payload) noexcept override {
226 if(payload.holds_type<dcon::national_identity_id>()) {
227 Cyto::Any n_payload = release_query_wrapper{};
228 parent->impl_get(state, n_payload);
229 auto content = state.world.nation_get_identity_from_identity_holder(any_cast<dcon::nation_id>(n_payload));
230 payload.emplace<dcon::national_identity_id>(content);
231 return message_result::consumed;
232 } else if(payload.holds_type<dcon::nation_id>()) {
233 Cyto::Any n_payload = release_query_wrapper{};
234 parent->impl_get(state, n_payload);
235 auto content = any_cast<release_query_wrapper>(n_payload).content;
236 payload.emplace<dcon::nation_id>(content);
237 return message_result::consumed;
238 }
239 return message_result::unseen;
240 }
241};
242
243} // namespace ui
element_base * parent
virtual void on_create(sys::state &state) noexcept
void set_visible(sys::state &state, bool vis)
void on_update(sys::state &state) noexcept override
void on_create(sys::state &state) noexcept override
std::unique_ptr< element_base > make_child(sys::state &state, std::string_view name, dcon::gui_def_id id) noexcept override
message_result get(sys::state &state, Cyto::Any &payload) noexcept override
void on_update(sys::state &state) noexcept override
void button_action(sys::state &state) noexcept override
void button_action(sys::state &state) noexcept override
void on_create(sys::state &state) noexcept override
void populate_layout(sys::state &state, text::endless_layout &contents, dcon::national_identity_id id) noexcept override
void on_update(sys::state &state) noexcept override
std::string_view get_row_element_name() override
std::unique_ptr< element_base > make_child(sys::state &state, std::string_view name, dcon::gui_def_id id) noexcept override
void populate_layout(sys::state &state, text::endless_layout &contents, dcon::national_identity_id id) noexcept override
void on_create(sys::state &state) noexcept override
void on_create(sys::state &state) noexcept override
std::unique_ptr< element_base > make_child(sys::state &state, std::string_view name, dcon::gui_def_id id) noexcept override
void on_update(sys::state &state) noexcept override
void button_action(sys::state &state) noexcept override
void set_text(sys::state &state, std::string const &new_text)
bool can_release_and_play_as(sys::state &state, dcon::nation_id source, dcon::national_identity_id t)
Definition: commands.cpp:905
void release_and_play_as(sys::state &state, dcon::nation_id source, dcon::national_identity_id t)
Definition: commands.cpp:897
bool can_make_vassal(sys::state &state, dcon::nation_id source, dcon::national_identity_id t)
Definition: commands.cpp:878
bool can_release_as_vassal(sys::state const &state, dcon::nation_id n, dcon::national_identity_id releasable)
Definition: nations.cpp:176
void add_to_layout_box(sys::state &state, layout_base &dest, layout_box &box, embedded_flag ico)
Definition: text.cpp:1165
layout_box open_layout_box(layout_base &dest, int32_t indent)
Definition: text.cpp:1799
void localised_format_box(sys::state &state, layout_base &dest, layout_box &box, std::string_view key, text::substitution_map const &sub)
Definition: text.cpp:1880
void add_to_substitution_map(substitution_map &mp, variable_type key, substitution value)
Definition: text.cpp:1068
ankerl::unordered_dense::map< uint32_t, substitution > substitution_map
Definition: text.hpp:794
std::string produce_simple_string(sys::state const &state, dcon::text_key id)
Definition: text.cpp:617
void close_layout_box(columnar_layout &dest, layout_box &box)
Definition: text.cpp:1807
void send(sys::state &state, element_base *parent, T value)
message_result
@ ident