Symphony Of Empires
pop_window.cpp
Go to the documentation of this file.
1 // Copyright (C) 2021, Symphony of Empires contributors
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <https://www.gnu.org/licenses/>.
15 //
16 // ----------------------------------------------------------------------------
17 // Name:
18 // client/interface/pop_window.cpp
19 //
20 // Abstract:
21 // Does some important stuff.
22 // ----------------------------------------------------------------------------
23 
24 #include "eng3d/string.hpp"
25 #include "eng3d/ui/table.hpp"
26 #include "eng3d/ui/label.hpp"
27 #include "eng3d/ui/image.hpp"
28 
30 #include "nation.hpp"
31 #include "indpobj.hpp"
32 #include "world.hpp"
33 
34 std::string pop_qol_tooltip_text(const Pop& pop, const World* world) {
35  std::string text = Eng3D::translate_format("Quality of life: %.3f\n\nNeed for commodities, ", pop.life_needs_met);
36  const auto& needs_amounts = world->pop_types[pop.type_id].basic_needs_amount;
37  auto total_factor = std::reduce(needs_amounts.begin(), needs_amounts.end());
38  for (const auto& commodity : world->commodities) {
39  if (needs_amounts[commodity] == 0) continue;
40  const auto need_factor = needs_amounts[commodity] / total_factor;
41  text += Eng3D::translate_format("\n%s: %.3f", commodity.name.c_str(), need_factor);
42  }
43  return text;
44 }
45 
47  : UI::Window(-400, -400, 0, 800),
48  gs{ _gs }
49 {
51  this->set_text("Population");
52  this->is_scroll = false;
53 
54  this->set_close_btn_function([this](UI::Widget&) {
55  this->kill();
56  });
57 
58  const auto& nation = *gs.curr_nation;
59 
60  auto size = 0.f;
61  for(const auto province_id : nation.owned_provinces)
62  size += gs.world->provinces[province_id].pops.size();
63 
64  std::vector<int> sizes{ 75, 200, 100, 80, 80, 80, 50};
65  std::vector<std::string> header{ "Size", "Province", "Type", "Militancy", "Literacy", "Budget", "QOL" };
66  auto& table = this->make_widget<UI::Table<uint64_t>>(5, 5, 800 - 5, 35, sizes, header);
67  this->width = table.width + 5 + this->padding.x;
68  table.reserve(size);
69  table.set_on_each_tick([this, &nation, &table](UI::Widget&) {
70  for(const auto province_id : nation.owned_provinces) {
71  const auto& province = this->gs.world->provinces[province_id];
72  for(const auto& pop : province.pops) {
73  const auto id = static_cast<size_t>(pop.type_id) + (static_cast<uint64_t>(province) << 32);
74  auto& row = table.get_row(id);
75  size_t row_index = 0;
76 
77  auto size_label = row.get_element(row_index++);
78  size_label->set_text(string_format("%.0f", pop.size));
79  size_label->set_key(pop.size);
80 
81  auto prov_name = row.get_element(row_index++);
82  prov_name->set_text(province.name);
83  prov_name->set_key(province.name);
84 
85  auto type_lable = row.get_element(row_index++);
86  type_lable->set_text(this->gs.world->pop_types[pop.type_id].name);
87  type_lable->set_key(this->gs.world->pop_types[pop.type_id].name);
88 
89  auto militancy = row.get_element(row_index++);
90  militancy->set_text(string_format("%1.2f", pop.militancy));
91  militancy->set_key(pop.militancy);
92 
93  auto literacy = row.get_element(row_index++);
94  literacy->set_text(string_format("%2.0f%%", pop.literacy * 100));
95  literacy->set_key(pop.literacy);
96 
97  auto budget = row.get_element(row_index++);
98  budget->set_text(string_format("%.2f", pop.budget / pop.size));
99  budget->set_key(pop.budget / pop.size);
100  budget->set_tooltip(Eng3D::translate_format("Total budget: %.2f", pop.budget));
101 
102  auto quality_of_life = row.get_element(row_index++);
103  quality_of_life->set_text(string_format("%.2f", pop.life_needs_met));
104  quality_of_life->set_key(pop.life_needs_met);
105  quality_of_life->set_tooltip([pop, this]{
106  return pop_qol_tooltip_text(pop, this->gs.world);
107  });
108  }
109  }
110  });
111  table.on_each_tick(table);
112 }
Nation * curr_nation
Definition: game_state.hpp:158
World * world
Definition: game_state.hpp:156
PopWindow(GameState &gs)
Definition: pop_window.cpp:46
Definition: indpobj.hpp:264
float life_needs_met
Definition: indpobj.hpp:274
PopTypeId type_id
Definition: indpobj.hpp:272
The master widget all the other widgets inherit from, do not use directly instead use one of the many...
Definition: widget.hpp:176
UI::Origin origin
Definition: widget.hpp:318
bool is_scroll
Definition: widget.hpp:306
size_t width
Definition: widget.hpp:325
void kill()
Kills the current widget, setting it up for deletion when dead widgets are cleared by the UI context.
Definition: widget.hpp:283
glm::ivec2 padding
Definition: widget.hpp:323
virtual void set_text(const std::string &text)
Generates text for the widget and overrides the current text texture.
Definition: widget.cpp:445
void set_close_btn_function(std::function< void(Widget &)> on_click)
Definition: window.cpp:65
Definition: world.hpp:114
std::string string_format(const std::string_view format, Args &&... args)
String formatter.
Definition: string.hpp:100
std::string translate_format(const std::string_view format, Args &&... args)
String formatter, with translation.
Definition: string.hpp:128
std::string pop_qol_tooltip_text(const Pop &pop, const World *world)
Definition: pop_window.cpp:34