Symphony Of Empires
selected_units_menu.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/selected_units_menu.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include "eng3d/texture.hpp"
26 #include "eng3d/string.hpp"
27 #include "eng3d/ui/piechart.hpp"
28 #include "eng3d/ui/label.hpp"
29 #include "eng3d/ui/image.hpp"
30 #include "eng3d/ui/tooltip.hpp"
31 #include "eng3d/ui/button.hpp"
32 #include "eng3d/ui/table.hpp"
33 #include "eng3d/ui/close_button.hpp"
34 
36 #include "client/game_state.hpp"
37 #include "client/map.hpp"
38 #include "client/map_render.hpp"
39 #include "nation.hpp"
40 #include "world.hpp"
41 #include "building.hpp"
42 #include "action.hpp"
44 
45 using namespace Interface;
46 
47 void SelectedUnitsMenu::update_unit_list(std::vector<Unit::Id> selected_units, UI::Table<UnitId::Type>& unit_table) {
48  unit_table.make_rows_unactive();
49  for(auto& unit_id : selected_units) {
50  auto& row = unit_table.get_row(unit_id);
51  size_t row_index = 0;
52  auto& unit = gs.world->unit_manager.units[unit_id];
53 
54  auto name = row.get_element(row_index++);
55  name->set_text(string_format("%.2f", unit.size));
56  }
57  unit_table.clear_unactive_rows();
58 }
59 
61  : UI::Window(-200, -300, 200, 300),
62  gs{ _gs }
63 {
65  this->is_pinned = true;
66  this->is_scroll = false;
67 
68  std::vector<int> sizes{ 160 };
69  std::vector<std::string> header{ "Size" };
70  auto& unit_table = this->make_widget<UI::Table<UnitId::Type>>(0, 0, this->height, 30, sizes, header);
71 
72  this->is_render = !this->gs.client_state.get_selected_units().empty();
73  gs.client_state.add_listener([this, &unit_table] (std::vector<Unit::Id> selected_units) {
74  this->is_render = !selected_units.empty();
75  update_unit_list(selected_units, unit_table);
76  });
77 }
const std::vector< UnitId > get_selected_units() const
Definition: game_state.hpp:60
auto add_listener(std::function< void(std::vector< UnitId >)> callback)
Definition: game_state.hpp:86
ClientState client_state
Definition: game_state.hpp:161
World * world
Definition: game_state.hpp:156
A dynamic/smart table that can sort elements by ascending/descending order.
Definition: table.hpp:87
void make_rows_unactive()
Definition: table.hpp:140
UI::TableRow & get_row(T _row_id)
Definition: table.hpp:123
void clear_unactive_rows()
Definition: table.hpp:146
UI::Origin origin
Definition: widget.hpp:318
bool is_scroll
Definition: widget.hpp:306
bool is_pinned
Definition: widget.hpp:302
size_t height
Definition: widget.hpp:325
bool is_render
Definition: widget.hpp:303
Eng3D::Freelist< Unit > units
Definition: unit.hpp:173
UnitManager unit_manager
Definition: world.hpp:145
@ MIDDLE_RIGHT_SCREEN
std::string string_format(const std::string_view format, Args &&... args)
String formatter.
Definition: string.hpp:100