Project Alice
Loading...
Searching...
No Matches
gui_production_window.cpp
Go to the documentation of this file.
2
3namespace ui {
4
5void populate_production_states_list(sys::state& state, std::vector<dcon::state_instance_id>& row_contents, dcon::nation_id n, bool show_empty, production_sort_order sort_order) {
6
7 for(auto const fat_id : state.world.nation_get_state_ownership(n)) {
8 if(show_empty && !fat_id.get_state().get_capital().get_is_colonial()) {
9 row_contents.push_back(fat_id.get_state());
10 } else if(economy::has_factory(state, fat_id.get_state().id)) {
11 // Then account for factories **hidden** by the filter from goods...
12 size_t count = 0;
13 province::for_each_province_in_state_instance(state, fat_id.get_state(), [&](dcon::province_id pid) {
14 auto ffact_id = dcon::fatten(state.world, pid);
15 ffact_id.for_each_factory_location_as_province([&](dcon::factory_location_id flid) {
16 auto fid = state.world.factory_location_get_factory(flid);
17 Cyto::Any payload = commodity_filter_query_data{
18 state.world.factory_type_get_output(state.world.factory_get_building_type(fid)).id, false};
19 state.ui_state.production_subwindow->impl_get(state, payload);
20 auto content = any_cast<commodity_filter_query_data>(payload);
21 count += content.filter ? 1 : 0;
22 });
23 });
24 for(auto construct : fat_id.get_state().get_state_building_construction()) {
25 Cyto::Any payload = commodity_filter_query_data{construct.get_type().get_output().id, false};
26 state.ui_state.production_subwindow->impl_get(state, payload);
27 auto content = any_cast<commodity_filter_query_data>(payload);
28 count += content.filter ? 1 : 0;
29 }
30
31
32 if(count > 0)
33 row_contents.push_back(fat_id.get_state());
34 }
35 }
36
37 auto sort_by_name = [&](dcon::state_instance_id a, dcon::state_instance_id b) {
38 auto a_name =
39 text::produce_simple_string(state, state.world.state_definition_get_name(state.world.state_instance_get_definition(a)));
40 auto b_name =
41 text::produce_simple_string(state, state.world.state_definition_get_name(state.world.state_instance_get_definition(b)));
42 return a_name < b_name;
43 };
44 auto sort_by_factories = [&](dcon::state_instance_id a, dcon::state_instance_id b) {
45 auto acount = economy::state_factory_count(state, a, state.world.state_instance_get_nation_from_state_ownership(a));
46 auto bcount = economy::state_factory_count(state, b, state.world.state_instance_get_nation_from_state_ownership(b));
47 return acount > bcount;
48 };
49 auto sort_by_primary_workers = [&](dcon::state_instance_id a, dcon::state_instance_id b) {
50 return state.world.state_instance_get_demographics(a,
51 demographics::to_key(state, state.culture_definitions.primary_factory_worker)) >
52 state.world.state_instance_get_demographics(b,
53 demographics::to_key(state, state.culture_definitions.primary_factory_worker));
54 };
55 auto sort_by_secondary_workers = [&](dcon::state_instance_id a, dcon::state_instance_id b) {
56 return state.world.state_instance_get_demographics(a,
57 demographics::to_key(state, state.culture_definitions.secondary_factory_worker)) >
58 state.world.state_instance_get_demographics(b,
59 demographics::to_key(state, state.culture_definitions.secondary_factory_worker));
60 };
61 auto sort_by_owners = [&](dcon::state_instance_id a, dcon::state_instance_id b) {
62 auto atotal = state.world.state_instance_get_demographics(a, demographics::total);
63 auto btotal = state.world.state_instance_get_demographics(b, demographics::total);
64 return state.world.state_instance_get_demographics(a, demographics::to_key(state, state.culture_definitions.capitalists)) /
65 atotal >
66 state.world.state_instance_get_demographics(b, demographics::to_key(state, state.culture_definitions.capitalists)) /
67 btotal;
68 };
69 auto sort_by_infrastructure = [&](dcon::state_instance_id a, dcon::state_instance_id b) {
70 float atotal = 0.0f;
71 float ap_total = 0.0f;
72 province::for_each_province_in_state_instance(state, a, [&](dcon::province_id p) {
73 atotal += float(state.world.province_get_building_level(p, uint8_t(economy::province_building_type::railroad)));
74 ap_total += 1.0f;
75 });
76 float btotal = 0.0f;
77 float bp_total = 0.0f;
78 province::for_each_province_in_state_instance(state, b, [&](dcon::province_id p) {
79 btotal += float(state.world.province_get_building_level(p, uint8_t(economy::province_building_type::railroad)));
80 bp_total += 1.0f;
81 });
82 return atotal / ap_total > btotal / bp_total;
83 };
84
85 switch(sort_order) {
87 std::sort(row_contents.begin(), row_contents.end(), sort_by_name);
88 break;
90 std::sort(row_contents.begin(), row_contents.end(), sort_by_factories);
91 break;
93 std::sort(row_contents.begin(), row_contents.end(), sort_by_primary_workers);
94 break;
96 std::sort(row_contents.begin(), row_contents.end(), sort_by_secondary_workers);
97 break;
99 std::sort(row_contents.begin(), row_contents.end(), sort_by_owners);
100 break;
102 std::sort(row_contents.begin(), row_contents.end(), sort_by_infrastructure);
103 break;
104 }
105}
106
107void open_foreign_investment(sys::state& state, dcon::nation_id n) {
108 if(state.ui_state.topbar_subwindow->is_visible()) {
109 state.ui_state.topbar_subwindow->set_visible(state, false);
110 }
112 state.ui_state.root->move_child_to_front(state.ui_state.production_subwindow);
114
116}
117
118void open_build_foreign_factory(sys::state& state, dcon::state_instance_id st) {
119 if(state.ui_state.topbar_subwindow->is_visible()) {
120 state.ui_state.topbar_subwindow->set_visible(state, false);
121 }
123 state.ui_state.root->move_child_to_front(state.ui_state.production_subwindow);
125
127 open_investment_nation{state.world.state_instance_get_nation_from_state_ownership(st)});
128
129 send(state, state.ui_state.production_subwindow, production_selection_wrapper{st, true, xy_pair{0, 0}});
130}
131
132} // namespace ui
bool is_visible() const
message_result impl_get(sys::state &state, Cyto::Any &payload) noexcept
void set_visible(sys::state &state, bool vis)
constexpr dcon::demographics_key total(0)
dcon::demographics_key to_key(sys::state const &state, dcon::pop_type_id v)
int32_t state_factory_count(sys::state &state, dcon::state_instance_id sid, dcon::nation_id n)
Definition: economy.cpp:4911
bool has_factory(sys::state const &state, dcon::state_instance_id si)
Definition: economy.cpp:128
void for_each_province_in_state_instance(sys::state &state, dcon::state_instance_id s, F const &func)
std::string produce_simple_string(sys::state const &state, dcon::text_key id)
Definition: text.cpp:617
production_sort_order
Definition: constants.hpp:584
void send(sys::state &state, element_base *parent, T value)
@ count
Definition: gui_event.hpp:126
void open_foreign_investment(sys::state &state, dcon::nation_id n)
void populate_production_states_list(sys::state &state, std::vector< dcon::state_instance_id > &row_contents, dcon::nation_id n, bool show_empty, production_sort_order sort_order)
void open_build_foreign_factory(sys::state &state, dcon::state_instance_id st)
uchar uint8_t
element_base * topbar_subwindow
std::unique_ptr< element_base > root
element_base * production_subwindow