Project Alice
Loading...
Searching...
No Matches
gui_projects_window.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <variant>
7
8namespace ui {
9
10typedef std::variant< dcon::province_building_construction_id, dcon::state_building_construction_id> production_project_data;
11
13 dcon::commodity_id cid{};
14 float satisfied = 0.f;
15 float needed = 0.f;
16};
17
18class production_project_input_item : public listbox_row_element_base<production_project_input_data> {
19 simple_text_element_base* amount_text = nullptr;
20 element_base* commodity_icon = nullptr;
21
22public:
23 void on_create(sys::state& state) noexcept override {
25 amount_text->base_data.position.y = commodity_icon->base_data.position.y + commodity_icon->base_data.size.y - 4;
26 }
27
28 std::unique_ptr<element_base> make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept override {
29 if(name == "goods_type") {
30 auto ptr = make_element_by_type<commodity_image>(state, id);
31 commodity_icon = ptr.get();
32 return ptr;
33 } else if(name == "goods_amount") {
34 auto ptr = make_element_by_type<simple_text_element_base>(state, id);
35 amount_text = ptr.get();
36 return ptr;
37 } else {
38 return nullptr;
39 }
40 }
41
42 void on_update(sys::state& state) noexcept override {
44 }
45
46 message_result get(sys::state& state, Cyto::Any& payload) noexcept override {
47 if(payload.holds_type<dcon::commodity_id>()) {
48 payload.emplace<dcon::commodity_id>(content.cid);
50 }
52 }
53};
54
56 : public overlapping_listbox_element_base<production_project_input_item, production_project_input_data> {
57protected:
58 std::string_view get_row_element_name() override {
59 return "goods_need_template";
60 }
61};
62
64public:
65 void on_create(sys::state& state) noexcept override {
66 disabled = true;
67 }
68 void button_action(sys::state& state) noexcept override {
69 //if(parent) {
70 // Cyto::Any payload = element_selection_wrapper<production_action>{production_action{production_action::investment_window}};
71 // parent->impl_get(state, payload);
72 //}
73 }
76 }
77 void update_tooltip(sys::state& state, int32_t x, int32_t y, text::columnar_layout& contents) noexcept override {
78 text::add_line(state, contents, "alice_domestic_investment_button");
79 }
80};
81
82class production_project_info : public listbox_row_element_base<production_project_data> {
83 image_element_base* building_icon = nullptr;
84 image_element_base* factory_icon = nullptr;
85 simple_text_element_base* name_text = nullptr;
86 simple_text_element_base* cost_text = nullptr;
87 production_project_input_listbox* input_listbox = nullptr;
88
89 dcon::state_instance_id get_state_instance_id(sys::state& state) {
90 if(std::holds_alternative<dcon::province_building_construction_id>(content)) {
91 auto fat_id = dcon::fatten(state.world, std::get<dcon::province_building_construction_id>(content));
92 return fat_id.get_province().get_state_membership().id;
93 } else if(std::holds_alternative<dcon::state_building_construction_id>(content)) {
94 auto fat_id = dcon::fatten(state.world, std::get<dcon::state_building_construction_id>(content));
95 return fat_id.get_state();
96 }
97 return dcon::state_instance_id{};
98 }
99
100 float get_cost(sys::state& state, economy::commodity_set const& cset) {
101 float total = 0.f;
102 auto s = get_state_instance_id(state);
103 for(uint32_t i = 0; i < economy::commodity_set::set_size; ++i) {
104 dcon::commodity_id cid = cset.commodity_type[i];
105 if(bool(cid))
106 total += economy::price(state, s, cid) * cset.commodity_amounts[i];
107 }
108 return total;
109 }
110
111public:
112 std::unique_ptr<element_base> make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept override {
113 if(name == "state_bg") {
114 return make_element_by_type<image_element_base>(state, id);
115 } else if(name == "state_name") {
116 return make_element_by_type<state_name_text>(state, id);
117 } else if(name == "project_icon") {
118 auto ptr = make_element_by_type<image_element_base>(state, id);
119 factory_icon = ptr.get();
120 return ptr;
121 } else if(name == "infra") {
122 auto ptr = make_element_by_type<image_element_base>(state, id);
123 building_icon = ptr.get();
124 return ptr;
125 } else if(name == "project_name") {
126 auto ptr = make_element_by_type<simple_text_element_base>(state, id);
127 name_text = ptr.get();
128 return ptr;
129 } else if(name == "project_cost") {
130 auto ptr = make_element_by_type<simple_text_element_base>(state, id);
131 cost_text = ptr.get();
132 return ptr;
133 } else if(name == "pop_icon" || name == "pop_amount") {
134 return make_element_by_type<invisible_element>(state, id);
135 } else if(name == "invest_project") {
136 return make_element_by_type<production_project_invest_button>(state, id);
137 } else if(name == "input_goods") {
138 auto ptr = make_element_by_type<production_project_input_listbox>(state, id);
139 input_listbox = ptr.get();
140 return ptr;
141 } else {
142 return nullptr;
143 }
144 }
145
146 void on_update(sys::state& state) noexcept override {
147 economy::commodity_set satisfied_commodities{};
148 economy::commodity_set needed_commodities{};
149 if(std::holds_alternative<dcon::province_building_construction_id>(content)) {
150 factory_icon->set_visible(state, false);
151 building_icon->set_visible(state, true);
152 auto fat_id = dcon::fatten(state.world, std::get<dcon::province_building_construction_id>(content));
153 factory_icon->frame = uint16_t(fat_id.get_type());
154 name_text->set_text(state, text::produce_simple_string(state, province_building_type_get_name(economy::province_building_type(fat_id.get_type()))));
155
156 auto type = economy::province_building_type(fat_id.get_type());
157 needed_commodities = state.economy_definitions.building_definitions[int32_t(type)].cost;
158
159 satisfied_commodities = fat_id.get_purchased_goods();
160 } else if(std::holds_alternative<dcon::state_building_construction_id>(content)) {
161 factory_icon->set_visible(state, true);
162 building_icon->set_visible(state, false);
163 auto fat_id = dcon::fatten(state.world, std::get<dcon::state_building_construction_id>(content));
164 factory_icon->frame = uint16_t(fat_id.get_type().get_output().get_icon());
165 name_text->set_text(state, text::produce_simple_string(state, fat_id.get_type().get_name()));
166 needed_commodities = fat_id.get_type().get_construction_costs();
167 satisfied_commodities = fat_id.get_purchased_goods();
168
169 float factory_mod = state.world.nation_get_modifier_values(state.local_player_nation, sys::national_mod_offsets::factory_cost) + 1.0f;
170 float pop_factory_mod = std::max(0.1f, state.world.nation_get_modifier_values(state.local_player_nation, sys::national_mod_offsets::factory_owner_cost));
171 float admin_cost_factor = pop_factory_mod * factory_mod;
172
173 for(uint32_t i = 0; i < economy::commodity_set::set_size; ++i) {
174 needed_commodities.commodity_amounts[i] *= admin_cost_factor;
175 }
176 }
177
178 if(input_listbox) {
179 input_listbox->row_contents.clear();
180 for(uint32_t i = 0; i < economy::commodity_set::set_size; ++i)
181 if(bool(needed_commodities.commodity_type[i]))
182 input_listbox->row_contents.push_back(production_project_input_data{
183 needed_commodities.commodity_type[i], // cid
184 satisfied_commodities.commodity_amounts[i], // satisfied
185 needed_commodities.commodity_amounts[i] // needed
186 });
187 input_listbox->update(state);
188 }
189
190 auto s = get_state_instance_id(state);
191
192 float purchased_cost = 0.0f;
193 for(uint32_t i = 0; i < economy::commodity_set::set_size; ++i) {
194 dcon::commodity_id cid = needed_commodities.commodity_type[i];
195 if(bool(cid))
196 purchased_cost += economy::price(state, s, cid) * satisfied_commodities.commodity_amounts[i];
197 }
198 float total_cost = get_cost(state, needed_commodities);
199 cost_text->set_text(state, text::format_money(purchased_cost) + "/" + text::format_money(total_cost));
200 }
201
202 message_result get(sys::state& state, Cyto::Any& payload) noexcept override {
203 if(payload.holds_type<dcon::state_instance_id>()) {
204 payload.emplace<dcon::state_instance_id>(get_state_instance_id(state));
206 }
208 }
209};
210
211class production_project_listbox : public listbox_element_base<production_project_info, production_project_data> {
212protected:
213 std::string_view get_row_element_name() override {
214 return "project_info";
215 }
216
217public:
218 void on_update(sys::state& state) noexcept override {
219 row_contents.clear();
220 state.world.nation_for_each_state_building_construction_as_nation(state.local_player_nation,
221 [&](dcon::state_building_construction_id id) {
222 auto fat_id = dcon::fatten(state.world, id);
223 if(fat_id.get_is_pop_project())
224 row_contents.push_back(production_project_data(id));
225 });
226 state.world.nation_for_each_province_building_construction_as_nation(state.local_player_nation,
227 [&](dcon::province_building_construction_id id) {
228 auto fat_id = dcon::fatten(state.world, id);
229 if(fat_id.get_is_pop_project())
230 row_contents.push_back(production_project_data(id));
231 });
232 update(state);
233 }
234};
235} // namespace ui
virtual message_result get(sys::state &state, Cyto::Any &payload) noexcept
virtual void on_create(sys::state &state) noexcept
element_data base_data
void set_visible(sys::state &state, bool vis)
message_result get(sys::state &state, Cyto::Any &payload) noexcept override
void on_update(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_create(sys::state &state) noexcept override
message_result get(sys::state &state, Cyto::Any &payload) noexcept override
void on_update(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
std::string_view get_row_element_name() override
void on_create(sys::state &state) noexcept override
void button_action(sys::state &state) noexcept override
void update_tooltip(sys::state &state, int32_t x, int32_t y, text::columnar_layout &contents) noexcept override
tooltip_behavior has_tooltip(sys::state &state) noexcept override
void on_update(sys::state &state) noexcept override
std::string_view get_row_element_name() override
void set_text(sys::state &state, std::string const &new_text)
pop_satisfaction_wrapper_fat fatten(data_container const &c, pop_satisfaction_wrapper_id id) noexcept
float price(sys::state const &state, dcon::state_instance_id s, dcon::commodity_id c)
Definition: economy.cpp:150
province_building_type
Definition: constants.hpp:578
std::string format_money(float num)
Definition: text.cpp:1029
std::string format_float(float num, size_t digits)
Definition: text.cpp:981
void add_line(sys::state &state, layout_base &dest, dcon::text_key txt, int32_t indent)
Definition: text.cpp:1923
std::string produce_simple_string(sys::state const &state, dcon::text_key id)
Definition: text.cpp:617
tooltip_behavior
message_result
std::variant< dcon::province_building_construction_id, dcon::state_building_construction_id > production_project_data
uint uint32_t
float commodity_amounts[set_size]
dcon::commodity_id commodity_type[set_size]
static constexpr uint32_t set_size
Holds important data about the game world, state, and other data regarding windowing,...