Symphony Of Empires
nation_view.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/nation_view.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include "eng3d/ui/components.hpp"
26 #include "eng3d/string.hpp"
27 
30 #include "client/map.hpp"
31 #include "client/map_render.hpp"
32 
34  : UI::Window(-400, -400, 800, 800),
35  gs{ _gs },
36  nation{ _nation }
37 {
38  this->set_text(translate_format("General overview for %s", nation.name.c_str()));
40  this->is_scroll = false;
41  this->set_on_each_tick([this](UI::Widget& w) {
42  w.set_text(this->nation.get_client_hint().name);
43  });
44  this->on_each_tick(*this);
45  this->set_close_btn_function([this](Widget&) {
46  this->kill();
47  });
48 
49  auto& flag_img = this->make_widget<UI::AspectImage>(0, 0, 128, 96, this->gs.get_nation_flag(this->nation));
50  flag_img.set_on_each_tick([this](UI::Widget& w) {
51  w.current_texture = this->gs.get_nation_flag(this->nation);
52  });
53  flag_img.on_each_tick(flag_img);
54 
55  auto& flex_actions_column = this->make_widget<UI::Div>(0, 0, 128, this->height);
56  flex_actions_column.below_of(flag_img);
57  flex_actions_column.flex = UI::Flex::COLUMN;
58 
59  auto& name_lab = flex_actions_column.make_widget<UI::Label>(0, 0, "?");
60  name_lab.set_on_each_tick([this](UI::Widget& w) {
61  w.set_text(this->nation.get_client_hint().name);
62  });
63  name_lab.on_each_tick(name_lab);
64 
65  auto& ideology_img = this->make_widget<UI::Image>(0, 0, 24, 24);
66  ideology_img.set_on_each_tick([this](UI::Widget& w) {
67  const auto& ideology = this->gs.world->ideologies[this->nation.get_client_hint().ideology_id];
68  ((UI::Image&)w).current_texture = this->gs.tex_man.load(gs.package_man.get_unique(ideology.get_icon_path()));
69  w.set_tooltip(ideology.ref_name);
70  });
71  ideology_img.on_each_tick(ideology_img);
72 
73  if(gs.curr_nation != &nation) {
74  auto& rel_lab = flex_actions_column.make_widget<UI::Label>(0, 0, "?");
75  rel_lab.set_on_each_tick([this](UI::Widget& w) {
76  const auto& relation = this->gs.world->get_relation(*this->gs.curr_nation, this->nation);
77  w.set_text(string_format("%.2f%%+%.2f%%", relation.relation * 100.f, relation.alliance * 100.f));
78 
79  std::string text = translate("Our diplomatic relations with them and our alliance:\n");
80  text += (relation.is_customs_union() ? translate("- In customs union") : translate("- Not in a customs union")) + "\n";
81  text += (relation.is_allied() ? translate("- Allied") : translate("- Not allied")) + "\n";
82  text += (relation.has_war ? translate("- At war") : translate("- Not at war")) + "\n";
83  text += (relation.has_landpass() ? translate("- Land access") : translate("- No land access")) + "\n";
84  w.set_tooltip(text);
85  });
86  rel_lab.on_each_tick(rel_lab);
87  }
88 
89  if(gs.curr_nation != &nation) {
90  auto& dow_btn = flex_actions_column.make_widget<UI::Button>(0, 0, 128, 24);
91  dow_btn.set_on_each_tick([this](UI::Widget& w) {
92  const auto& relation = this->gs.world->get_relation(this->gs.world->get_id(*this->gs.curr_nation), this->gs.world->get_id(this->nation));
93  if(relation.has_war) {
94  w.set_text(translate("Propose treaty"));
95  w.set_on_click([this](UI::Widget&) {
96  //new Interface::TreatyDraftView(this->gs, this->nation);
97  });
98  w.set_tooltip(translate("End the war against this country and propose a peace deal"));
99  } else {
100  w.set_text(translate("Declare war"));
101  w.set_on_click([this](UI::Widget&) {
102  //new Interface::WarDeclarePrompt(this->gs, this->nation);
103  });
104  w.set_tooltip(translate("Declaring war on this nation will bring all their allies to their side"));
105  }
106  });
107  dow_btn.on_each_tick(dow_btn);
108 
109  auto& allow_market_access_btn = flex_actions_column.make_widget<UI::Button>(0, 0, 128, 24);
110  allow_market_access_btn.set_text(translate("Market access"));
111 
112  auto& allow_military_access_btn = flex_actions_column.make_widget<UI::Button>(0, 0, 128, 24);
113  allow_military_access_btn.set_text(translate("Land access"));
114  allow_military_access_btn.set_tooltip(translate("Allow this nation to cross our land with their units"));
115  }
116 
117  if(gs.editor && gs.curr_nation != &nation) {
118  auto& switch_btn = flex_actions_column.make_widget<UI::Button>(0, 0, 128, 24);
119  switch_btn.set_text(translate("Switch to this nation"));
120  switch_btn.set_tooltip(translate("Switches to this nation (multiplayer disallow rule)"));
121  switch_btn.set_on_click([this](UI::Widget&) {
122  this->gs.curr_nation = &this->nation;
123  this->gs.map->map_render->request_update_visibility();
124  this->gs.map->map_render->update();
125  });
126  }
127 }
std::shared_ptr< Eng3D::IO::Asset::Base > get_unique(const Eng3D::IO::Path &path)
Obtaining an unique asset means the "first-found" policy applies.
Definition: io.cpp:146
Eng3D::TextureManager tex_man
Definition: state.hpp:124
Eng3D::IO::PackageManager package_man
Definition: state.hpp:122
std::shared_ptr< Eng3D::Texture > load(const std::string &path, TextureOptions options=default_options)
Finds a texture in the list of a texture manager if the texture is already in the list we load the sa...
Definition: texture.cpp:432
Nation * curr_nation
Definition: game_state.hpp:158
std::unique_ptr< Map > map
Definition: game_state.hpp:159
std::shared_ptr< Eng3D::Texture > get_nation_flag(const Nation &nation)
Definition: game_state.cpp:84
World * world
Definition: game_state.hpp:156
NationView(GameState &gs, Nation &nation)
Definition: nation_view.cpp:33
Eng3D::StringRef name
Definition: nation.hpp:125
const Nation::ClientHint & get_client_hint() const
Definition: nation.cpp:202
Button widget.
Definition: button.hpp:32
Image widget, can display pictures or effects on the screen.
Definition: image.hpp:43
Simple widget for drawing text on the screen, no multiline support.
Definition: label.hpp:40
The master widget all the other widgets inherit from, do not use directly instead use one of the many...
Definition: widget.hpp:176
virtual void set_on_click(std::function< void(UI::Widget &)> _on_click)
Sets the on_click function of this widget.
Definition: widget.hpp:259
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
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
void kill()
Kills the current widget, setting it up for deletion when dead widgets are cleared by the UI context.
Definition: widget.hpp:283
std::function< void(UI::Widget &)> on_each_tick
Definition: widget.hpp:352
Widget()=default
virtual void set_text(const std::string &text)
Generates text for the widget and overrides the current text texture.
Definition: widget.cpp:445
size_t height
Definition: widget.hpp:325
void set_close_btn_function(std::function< void(Widget &)> on_click)
Definition: window.cpp:65
Nation::Relation & get_relation(NationId a, NationId b)
Definition: world.hpp:192
T::Id get_id(const T &obj) const
Get the id of an object, this is a template for all types except for tiles and locally-stored types (...
Definition: world.hpp:179
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
const char * c_str() const
Definition: string.hpp:55
Eng3D::StringRef name
Definition: nation.hpp:84