Symphony Of Empires
battle_widget.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 // eng3d/ui/battle_widget.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include "eng3d/ui/image.hpp"
26 #include "eng3d/ui/progress_bar.hpp"
27 #include "eng3d/camera.hpp"
28 #include "eng3d/color.hpp"
29 
31 #include "client/map.hpp"
32 #include "client/game_state.hpp"
33 #include "nation.hpp"
34 #include "diplomacy.hpp"
35 #include "unit.hpp"
36 #include "nation.hpp"
37 #include "world.hpp"
38 
39 using namespace Interface;
40 
42  : UI::Div(0, 0, 188, 30, _parent),
43  map{ _map }
44 {
45  this->background_color = Eng3D::Color(1.f, 1.f, 1.f, 1.f);
46 
48  auto& gs = static_cast<GameState&>(Eng3D::State::get_instance());
49 
50  this->left_flag_img = new UI::Image(1, 1, 38, 28, gs.tex_man.get_white(), this);
51  this->left_size_label = new UI::Div(41, 1, 48, 28, this);
52  this->left_size_label->text_align_x = UI::Align::END;
53  this->left_size_label->background_color = Eng3D::Color(0.8f, 0.f, 0.f, 1.f);
54  this->left_size_label->set_on_each_tick([this](UI::Widget&) {
55  if(this->province == nullptr) return;
56  const auto& battle = this->province->battle;
57  if(!battle.active) return;
58 
59  auto unit_size = 0.f;
60  const auto attacker_unit_ids = battle.get_attacker_unit_ids();
61  for(const auto unit_id : attacker_unit_ids) {
62  const auto& unit = g_world.unit_manager.units[unit_id];
63  unit_size += unit.size;
64  }
65  this->left_size_label->set_text(Eng3D::string_format("%.0f", unit_size));
66  });
67 
68  this->right_flag_img = new UI::Image(139, 1, 38, 28, gs.tex_man.get_white(), this);
69  this->right_size_label = new UI::Div(90, 1, 48, 28, this);
70  this->right_size_label->text_align_x = UI::Align::END;
71  this->right_size_label->background_color = Eng3D::Color(0.f, 0.f, 0.8f, 1.f);
72  this->right_size_label->set_on_each_tick([this](UI::Widget&) {
73  if(this->province == nullptr) return;
74  const auto& battle = this->province->battle;
75  if(!battle.active) return;
76 
77  auto unit_size = 0.f;
78  const auto defender_unit_ids = battle.get_defender_unit_ids();
79  for(const auto unit_id : defender_unit_ids) {
80  const auto& unit = g_world.unit_manager.units[unit_id];
81  unit_size += unit.size;
82  }
83  this->right_size_label->set_text(Eng3D::string_format("%.0f", unit_size));
84  });
85 }
86 
88  this->province = &_province;
89  const auto& battle = this->province->battle;
90  const auto& camera = *map.camera;
91  const auto battle_pos = province->get_pos();
92  const auto screen_pos = camera.get_tile_screen_pos(battle_pos);
93 
94  this->x = screen_pos.x - this->width / 2;
95  this->y = screen_pos.y - this->height / 2;
96 
97  auto& gs = static_cast<GameState&>(Eng3D::State::get_instance());
98 
99  const auto attacker_unit_ids = battle.get_attacker_unit_ids();
100  if(!attacker_unit_ids.empty()) {
101  auto left_nation_flag = gs.get_nation_flag(gs.world->nations[gs.world->unit_manager.units[attacker_unit_ids[0]].owner_id]);
102  this->left_flag_img->current_texture = left_nation_flag;
103  this->left_size_label->on_each_tick(*this->left_size_label);
104  }
105 
106  const auto defender_unit_ids = battle.get_defender_unit_ids();
107  if(!defender_unit_ids.empty()) {
108  auto right_nation_flag = gs.get_nation_flag(gs.world->nations[gs.world->unit_manager.units[defender_unit_ids[0]].owner_id]);
109  this->right_flag_img->current_texture = right_nation_flag;
110  this->right_size_label->on_each_tick(*this->right_size_label);
111  }
112 }
static State & get_instance()
Definition: state.cpp:514
std::shared_ptr< Eng3D::Texture > get_nation_flag(const Nation &nation)
Definition: game_state.cpp:84
void set_battle(Province &province)
BattleWidget(Map &map, UI::Widget *parent)
Definition: map.hpp:89
std::unique_ptr< Eng3D::Camera > camera
Definition: map.hpp:151
A single province, which is used to simulate economy in a "bulk-tiles" way instead of doing economica...
Definition: province.hpp:48
glm::vec2 get_pos() const
Definition: province.hpp:70
struct Province::Battle battle
Image widget, can display pictures or effects on the screen.
Definition: image.hpp:43
The master widget all the other widgets inherit from, do not use directly instead use one of the many...
Definition: widget.hpp:176
Eng3D::Color background_color
Definition: widget.hpp:335
size_t width
Definition: widget.hpp:325
UI::Align text_align_x
Definition: widget.hpp:331
std::shared_ptr< Eng3D::Texture > current_texture
Definition: widget.hpp:327
virtual void set_on_each_tick(std::function< void(UI::Widget &)> _on_each_tick)
Sets the on_each_tick function of this widget.
Definition: widget.hpp:264
std::function< void(UI::Widget &)> on_each_tick
Definition: widget.hpp:352
size_t height
Definition: widget.hpp:325
std::string string_format(const std::string_view format, Args &&... args)
String formatter.
Definition: string.hpp:100
Primitive color type used through the engine.
Definition: color.hpp:32
std::vector< UnitId > get_attacker_unit_ids() const
Definition: province.cpp:72
std::vector< UnitId > get_defender_unit_ids() const
Definition: province.cpp:84
A basic widget without any presets.
Definition: div.hpp:38