Symphony Of Empires
industry_window.cpp
Go to the documentation of this file.
1 // Symphony of Empires
2 // Copyright (C) 2021, Symphony of Empires contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // client/interface/industry_window.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include "eng3d/string.hpp"
26 #include "eng3d/ui/div.hpp"
27 #include "eng3d/ui/label.hpp"
28 #include "eng3d/ui/image.hpp"
29 #include "eng3d/ui/scrollbar.hpp"
30 #include "eng3d/ui/table.hpp"
31 #include "eng3d/string.hpp"
32 
35 #include "nation.hpp"
36 #include "action.hpp"
37 #include "indpobj.hpp"
38 #include "world.hpp"
39 
40 UI::Table<uint32_t>* Interface::IndustryWindow::new_table(GameState& gs, int _x, int _y, int _w, int _h, std::vector<ProvinceId> provinces, UI::Widget* parent) {
41  std::vector<int> sizes;
42  if(provinces.size() > 1) sizes.push_back(150);
43  sizes.insert(sizes.end(), { 150, 80, 80, 100, 80, 80, 80, 32 });
44 
45  std::vector<std::string> header;
46  if(provinces.size() > 1) header.push_back("Province");
47  header.insert(header.end(), { "Type", "Workers", "Budget", "Inputs", "Output", "Scale", "Profit", "" });
48 
49  auto& table = parent->make_widget<UI::Table<uint32_t>>(_x, _y, _h, 32, sizes, header);
50  table.reserve(1);
51  table.set_on_each_tick([&gs, &table, provinces](UI::Widget&) {
52  size_t row_num = 0;
53  for(const auto province_id : provinces) {
54  const auto& province = gs.world->provinces[province_id];
55  for(size_t i = 0; i < province.buildings.size(); i++) {
56  const auto& type = gs.world->building_types[i];
57  const auto& building = province.buildings[i];
58  auto& row = table.get_row(row_num++);
59 
60  size_t row_index = 0;
61  if(provinces.size() > 1) {
62  auto* prov_name = row.get_element(row_index++);
63  prov_name->set_text(province.name);
64  prov_name->set_key(province.name);
65  }
66 
67  auto* name = row.get_element(row_index++);
68  name->set_text(type.name);
69  name->set_key(type.name);
70 
71  auto* workers = row.get_element(row_index++);
72  workers->set_text(string_format("%.0f", building.workers));
73  workers->set_key(building.workers);
74 
75  auto* budget = row.get_element(row_index++);
76  budget->set_text(string_format("%.0f", building.budget));
77  budget->set_key(building.budget);
78 
79  row_index++; // Inputs
80  row_index++; // Outputs
81 
82  auto* scale = row.get_element(row_index++);
83  scale->set_text(string_format("%.2f", building.production_scale));
84  scale->set_key(building.production_scale);
85  scale->set_tooltip(translate_format("Production scale %.2f, level %.2f", building.production_scale, building.level));
86 
87  auto* profit = row.get_element(row_index++);
88  profit->set_text(string_format("%.2f", building.get_profit()));
89  profit->set_key(building.get_profit());
90  profit->set_tooltip(translate_format("Profit: %.2f\nInputs cost: %.2f\nWages: %.2f\nState taxes: %.2f\n\nDividends: %.2f (%.2f to state, %.2f to pops, %.2f to private investors)\nTotal expenses: %.2f\nOutputs revenue: %.2f\nTotal revenue: %.2f", building.get_profit(), building.expenses.inputs_cost, building.expenses.wages, building.expenses.state_taxes, building.expenses.get_dividends(), building.expenses.state_dividends, building.expenses.pop_dividends, building.expenses.private_dividends, building.expenses.get_total(), building.revenue.outputs, building.revenue.get_total()));
91 
92  auto* upgrade = row.get_element(row_index++);
93  upgrade->set_text("+");
94  upgrade->set_tooltip(translate_format("Upgrade building to level %.2f", building.level));
95  upgrade->set_key(0);
96  upgrade->set_on_click([&gs, province_id, type_id = type.get_id()](UI::Widget&) {
97  gs.client->send(Action::BuildingAdd::form_packet(gs.world->provinces[province_id], gs.world->building_types[type_id]));
98  });
99  }
100  }
101  });
102  table.on_each_tick(table);
103 
104  size_t row_num = 0;
105  for(const auto province_id : provinces) {
106  const auto& province = gs.world->provinces[province_id];
107  for(size_t i = 0; i < province.buildings.size(); i++) {
108  const auto& type = gs.world->building_types[i];
109  auto& row = table.get_row(row_num++);
110 
111  size_t row_index = 0;
112 
113  if(provinces.size() > 1) row_index++;
114 
115  row_index++;
116  row_index++;
117  row_index++;
118 
119  auto* inputs = row.get_element(row_index++);
120  inputs->set_key(type.input_ids.size());
121  inputs->flex = UI::Flex::ROW;
122  inputs->flex_justify = UI::FlexJustify::START;
123  for(auto good_id : type.input_ids) {
124  auto& commodity = gs.world->commodities[good_id];
125  auto& input_img = inputs->make_widget<UI::Image>(0, 0, 35, 35, commodity.get_icon_path(), true);
126  input_img.set_tooltip(commodity.name);
127  }
128 
129  auto* outputs = row.get_element(row_index++);
130  outputs->set_key(type.output_id);
131  outputs->flex = UI::Flex::ROW;
132  outputs->flex_justify = UI::FlexJustify::START;
133 
134  auto& output = gs.world->commodities[type.output_id];
135  auto& output_img = outputs->make_widget<UI::Image>(0, 0, 35, 35, output.get_icon_path(), true);
136  output_img.set_tooltip(output.name);
137  }
138  }
139  return &table;
140 }
141 
143  : UI::Window(-400, -400, 800, 800),
144  gs{ _gs }
145 {
147  this->set_text(translate("Factories"));
148  this->is_scroll = false;
149  this->set_close_btn_function([this](UI::Widget&) {
150  this->kill();
151  });
152  auto table = this->new_table(gs, 5, 5, 800 - 10, 800 - 5, this->gs.curr_nation->owned_provinces, this);
153  this->width = table->width + 5 + this->padding.x;
154 }
Nation * curr_nation
Definition: game_state.hpp:158
World * world
Definition: game_state.hpp:156
static UI::Table< uint32_t > * new_table(GameState &gs, int _x, int _y, int _w, int _h, std::vector< ProvinceId > provinces, UI::Widget *parent)
std::vector< ProvinceId > owned_provinces
Definition: nation.hpp:148
Image widget, can display pictures or effects on the screen.
Definition: image.hpp:43
A dynamic/smart table that can sort elements by ascending/descending order.
Definition: table.hpp:87
void reserve(size_t _size)
Definition: table.hpp:119
The master widget all the other widgets inherit from, do not use directly instead use one of the many...
Definition: widget.hpp:176
T & make_widget(Targs &&...args)
Definition: widget.hpp:222
UI::Origin origin
Definition: widget.hpp:318
virtual void set_tooltip(UI::Tooltip *tooltip)
Set the tooltip to be shown when this widget is hovered, overrides the previous tooltip.
Definition: widget.cpp:458
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
UI::WidgetType type
Definition: widget.hpp:320
UI::Widget * parent
Definition: widget.hpp:314
void set_close_btn_function(std::function< void(Widget &)> on_click)
Definition: window.cpp:65
std::string translate(const std::string_view str)
Definition: string.cpp:76
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