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 float get_cost(sys::state& state, economy::commodity_set const& cset) {
90 float total = 0.f;
91 for(uint32_t i = 0; i < economy::commodity_set::set_size; ++i) {
92 dcon::commodity_id cid = cset.commodity_type[i];
93 if(bool(cid))
94 total += state.world.commodity_get_current_price(cid) * cset.commodity_amounts[i];
95 }
96 return total;
97 }
98
99 dcon::state_instance_id get_state_instance_id(sys::state& state) {
100 if(std::holds_alternative<dcon::province_building_construction_id>(content)) {
101 auto fat_id = dcon::fatten(state.world, std::get<dcon::province_building_construction_id>(content));
102 return fat_id.get_province().get_state_membership().id;
103 } else if(std::holds_alternative<dcon::state_building_construction_id>(content)) {
104 auto fat_id = dcon::fatten(state.world, std::get<dcon::state_building_construction_id>(content));
105 return fat_id.get_state();
106 }
107 return dcon::state_instance_id{};
108 }
109
110public:
111 std::unique_ptr<element_base> make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept override {
112 if(name == "state_bg") {
113 return make_element_by_type<image_element_base>(state, id);
114 } else if(name == "state_name") {
115 return make_element_by_type<state_name_text>(state, id);
116 } else if(name == "project_icon") {
117 auto ptr = make_element_by_type<image_element_base>(state, id);
118 factory_icon = ptr.get();
119 return ptr;
120 } else if(name == "infra") {
121 auto ptr = make_element_by_type<image_element_base>(state, id);
122 building_icon = ptr.get();
123 return ptr;
124 } else if(name == "project_name") {
125 auto ptr = make_element_by_type<simple_text_element_base>(state, id);
126 name_text = ptr.get();
127 return ptr;
128 } else if(name == "project_cost") {
129 auto ptr = make_element_by_type<simple_text_element_base>(state, id);
130 cost_text = ptr.get();
131 return ptr;
132 } else if(name == "pop_icon" || name == "pop_amount") {
133 return make_element_by_type<invisible_element>(state, id);
134 } else if(name == "invest_project") {
135 return make_element_by_type<production_project_invest_button>(state, id);
136 } else if(name == "input_goods") {
137 auto ptr = make_element_by_type<production_project_input_listbox>(state, id);
138 input_listbox = ptr.get();
139 return ptr;
140 } else {
141 return nullptr;
142 }
143 }
144
145 void on_update(sys::state& state) noexcept override {
146 economy::commodity_set satisfied_commodities{};
147 economy::commodity_set needed_commodities{};
148 if(std::holds_alternative<dcon::province_building_construction_id>(content)) {
149 factory_icon->set_visible(state, false);
150 building_icon->set_visible(state, true);
151 auto fat_id = dcon::fatten(state.world, std::get<dcon::province_building_construction_id>(content));
152 factory_icon->frame = uint16_t(fat_id.get_type());
153 name_text->set_text(state, text::produce_simple_string(state, province_building_type_get_name(economy::province_building_type(fat_id.get_type()))));
154
155 auto type = economy::province_building_type(fat_id.get_type());
156 needed_commodities = state.economy_definitions.building_definitions[int32_t(type)].cost;
157
158 satisfied_commodities = fat_id.get_purchased_goods();
159 } else if(std::holds_alternative<dcon::state_building_construction_id>(content)) {
160 factory_icon->set_visible(state, true);
161 building_icon->set_visible(state, false);
162 auto fat_id = dcon::fatten(state.world, std::get<dcon::state_building_construction_id>(content));
163 factory_icon->frame = uint16_t(fat_id.get_type().get_output().get_icon());
164 name_text->set_text(state, text::produce_simple_string(state, fat_id.get_type().get_name()));
165 needed_commodities = fat_id.get_type().get_construction_costs();
166 satisfied_commodities = fat_id.get_purchased_goods();
167
168 float factory_mod = state.world.nation_get_modifier_values(state.local_player_nation, sys::national_mod_offsets::factory_cost) + 1.0f;
169 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));
170 float admin_cost_factor = pop_factory_mod * factory_mod;
171
172 for(uint32_t i = 0; i < economy::commodity_set::set_size; ++i) {
173 needed_commodities.commodity_amounts[i] *= admin_cost_factor;
174 }
175 }
176
177 if(input_listbox) {
178 input_listbox->row_contents.clear();
179 for(uint32_t i = 0; i < economy::commodity_set::set_size; ++i)
180 if(bool(needed_commodities.commodity_type[i]))
181 input_listbox->row_contents.push_back(production_project_input_data{
182 needed_commodities.commodity_type[i], // cid
183 satisfied_commodities.commodity_amounts[i], // satisfied
184 needed_commodities.commodity_amounts[i] // needed
185 });
186 input_listbox->update(state);
187 }
188
189 float purchased_cost = 0.0f;
190 for(uint32_t i = 0; i < economy::commodity_set::set_size; ++i) {
191 dcon::commodity_id cid = needed_commodities.commodity_type[i];
192 if(bool(cid))
193 purchased_cost += state.world.commodity_get_current_price(cid) * satisfied_commodities.commodity_amounts[i];
194 }
195 float total_cost = get_cost(state, needed_commodities);
196 cost_text->set_text(state, text::format_money(purchased_cost) + "/" + text::format_money(total_cost));
197 }
198
199 message_result get(sys::state& state, Cyto::Any& payload) noexcept override {
200 if(payload.holds_type<dcon::state_instance_id>()) {
201 payload.emplace<dcon::state_instance_id>(get_state_instance_id(state));
203 }
205 }
206};
207
208class production_project_listbox : public listbox_element_base<production_project_info, production_project_data> {
209protected:
210 std::string_view get_row_element_name() override {
211 return "project_info";
212 }
213
214public:
215 void on_update(sys::state& state) noexcept override {
216 row_contents.clear();
217 state.world.nation_for_each_state_building_construction_as_nation(state.local_player_nation,
218 [&](dcon::state_building_construction_id id) {
219 auto fat_id = dcon::fatten(state.world, id);
220 if(fat_id.get_is_pop_project())
221 row_contents.push_back(production_project_data(id));
222 });
223 state.world.nation_for_each_province_building_construction_as_nation(state.local_player_nation,
224 [&](dcon::province_building_construction_id id) {
225 auto fat_id = dcon::fatten(state.world, id);
226 if(fat_id.get_is_pop_project())
227 row_contents.push_back(production_project_data(id));
228 });
229 update(state);
230 }
231};
232} // 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
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:1899
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