Symphony Of Empires
decision.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/decision.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include "eng3d/serializer.hpp"
26 #include "eng3d/ui/text.hpp"
27 #include "eng3d/ui/tooltip.hpp"
28 #include "eng3d/ui/close_button.hpp"
29 #include "eng3d/ui/div.hpp"
30 #include "eng3d/ui/image.hpp"
31 #include "eng3d/network.hpp"
32 
35 #include "event.hpp"
36 #include "action.hpp"
37 #include "client/game_state.hpp"
38 
40  : UI::Window(-(600 / 2), 0, 600, 0),
41  gs{ _gs },
42  event{ _event }
43 {
44  // Title of the event
46  this->set_text(this->event.title);
47  this->flex = UI::Flex::COLUMN;
48 
49  // Display an image iff it exists
50  const auto& path = string_format("gfx/events/%s.png", event.ref_name.c_str());
51  if(this->gs.package_man.get_unique(path) != nullptr) {
52  this->make_widget<UI::Image>(0, 24, this->width, 200, path);
53  } else {
54  Eng3D::Log::warning("event", path.c_str());
55  }
56 
57  auto& txt = this->make_widget<UI::Text>(0, 0, this->width, 24);
58  txt.text_color = Eng3D::Color::rgb8(0, 0, 0);
59  txt.set_text(this->event.text);
60  txt.is_scroll = true;
61 
62  auto button_font = gs.ttf_man.load(gs.package_man.get_unique("fonts/neon_euler/euler.ttf"));
63  auto button_text_color = Eng3D::Color(1.f, 1.f, 1.f);
64  auto button_image = gs.tex_man.load(gs.package_man.get_unique("gfx/ui/button/button.png"));
65  auto button_border_image = gs.tex_man.load(gs.package_man.get_unique("gfx/ui/button/button_border.png"));
66  glm::ivec2 size(3, 3);
67  glm::ivec2 texture_size(3, 3);
68  auto button_border = UI::Border(button_border_image, size, texture_size);
69 
70  // Buttons for decisions for the event
71  for(const auto& decision : this->event.decisions) {
72  auto& flex_column = this->make_widget<UI::Div>(0, 0, this->width - 24, 24);
73  flex_column.flex = UI::Flex::ROW;
74 
75  if(decision.ref_name.get_string().empty())
76  CXX_THROW(std::runtime_error, string_format("Event ref_name=%s", event.ref_name.c_str()));
77 
78  auto& decide_btn = flex_column.make_widget<UI::Button>(0, 0, flex_column.width - 24, 24);
79  decide_btn.border = button_border;
80  decide_btn.current_texture = button_image;
81  decide_btn.font = button_font;
82  decide_btn.text_color = button_text_color;
83  decide_btn.set_text(decision.name);
84  decide_btn.set_tooltip(decision.effects);
85  decide_btn.set_on_click([this, &decision](UI::Widget&) {
86  if(this->gs.client)
87  this->gs.client->send(Action::NationTakeDecision::form_packet(this->event, decision));
88  this->kill();
89  });
90 
91  auto& remind_ibtn = flex_column.make_widget<UI::Image>(0, 0, 24, 24, "gfx/noicon.png");
92  remind_ibtn.set_tooltip(translate("Automatically take this descision"));
93  remind_ibtn.set_on_click([this, &decision](UI::Widget&) {
94  this->gs.decision_autodo.push_back(decision.ref_name);
95  if(this->gs.client)
96  this->gs.client->send(Action::NationTakeDecision::form_packet(this->event, decision));
97  this->kill();
98  });
99 
100  // Check if decision has been automatized
101  auto it = std::find(this->gs.decision_autodo.begin(), this->gs.decision_autodo.end(), decision.ref_name);
102  if(it != this->gs.decision_autodo.end()) {
103  decide_btn.on_click(decide_btn);
104  return;
105  }
106  }
107 
108  this->height = this->max_height() + 32;
109  this->y = -(this->height / 2);
110 }
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::TrueType::Manager ttf_man
Definition: state.hpp:127
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
std::shared_ptr< Eng3D::TrueType::Font > load(std::shared_ptr< Eng3D::IO::Asset::Base > asset)
Definition: ttf.cpp:58
std::unique_ptr< Client > client
Definition: game_state.hpp:143
std::vector< Eng3D::StringRef > decision_autodo
Definition: game_state.hpp:190
DecisionWindow(GameState &gs, Event event)
Definition: decision.cpp:39
Border class that defines the texture and size of borders of the widgets.
Definition: widget.hpp:153
Button widget.
Definition: button.hpp:32
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
UI::Border border
Definition: widget.hpp:334
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
size_t width
Definition: widget.hpp:325
UI::Flex flex
Definition: widget.hpp:337
void kill()
Kills the current widget, setting it up for deletion when dead widgets are cleared by the UI context.
Definition: widget.hpp:283
size_t max_height() const
Definition: widget.hpp:294
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
std::string translate(const std::string_view str)
Definition: string.cpp:76
void warning(const std::string_view category, const std::string_view msg)
Definition: log.cpp:64
std::string string_format(const std::string_view format, Args &&... args)
String formatter.
Definition: string.hpp:100
static Eng3D::Networking::Packet form_packet(const Event &event, const Decision &decision)
Definition: action.cpp:103
Primitive color type used through the engine.
Definition: color.hpp:32
constexpr static Color rgb8(uint8_t red, uint8_t green, uint8_t blue)
Create a color from RGB components.
Definition: color.hpp:71
const char * c_str() const
Definition: string.hpp:55
Definition: event.hpp:54
std::vector< Decision > decisions
Definition: event.hpp:58
Eng3D::StringRef title
Definition: event.hpp:60
Eng3D::StringRef text
Definition: event.hpp:59
Eng3D::StringRef ref_name
Definition: entity.hpp:161
#define CXX_THROW(class,...)
Definition: utils.hpp:98