Symphony Of Empires
tooltip.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/tooltip.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include "eng3d/ui/tooltip.hpp"
26 #include "eng3d/ui/widget.hpp"
27 #include "eng3d/ui/text.hpp"
28 #include "eng3d/ui/ui.hpp"
29 #include "eng3d/state.hpp"
30 #include "eng3d/texture.hpp"
31 
32 using namespace UI;
33 
35  : UI::Widget()
36 {
37  init(_parent, 600, 600);
38 }
39 
40 UI::Tooltip::Tooltip(UI::Widget* _parent, const std::string& text)
41  : UI::Widget()
42 {
43  size_t w = glm::min(text.size() * 12, (size_t)512);
44  size_t h = ((text.size() * 12) / 512) * 24 + 24;
45  init(_parent, w, h);
46  this->set_text(text);
47 }
48 
49 UI::Tooltip::Tooltip(UI::Widget* _parent, unsigned w, unsigned h)
50  : UI::Widget()
51 {
52  init(_parent, w, h);
53 }
54 
55 void UI::Tooltip::init(UI::Widget* _parent, size_t w, size_t h) {
56  this->parent = _parent;
57  if (this->parent)
58  this->parent->set_tooltip(this);
59  this->type = UI::WidgetType::TOOLTIP;
60  this->have_shadow = true;
61  this->width = w;
62  this->height = h;
63 
64  this->current_texture = Eng3D::State::get_instance().ui_ctx.tooltip_tex;
65  this->text_color = Eng3D::Color(1.f, 1.f, 1.f);
66 
67  const glm::ivec2 size{ 4, 4 };
68  const glm::ivec2 texture_size{ 10, 10 };
69  this->border = UI::Border(g_ui_context->border_tex, size, texture_size);
70 }
71 
72 void UI::Tooltip::set_pos(int _x, int _y, int, int _height, int screen_w, int screen_h) {
73  int extra_above = _y;
74  int extra_below = screen_h - _y - _height;
75  if(extra_above > extra_below) {
76  y = _y - height - 10;
77  } else {
78  y = _y + 24 + 10;
79  }
80  x = _x;
81 
82  // Make sure the tooltip is visable when it's too long and that it doesn't just disappear
83  int right_side = this->x + this->width;
84  if(right_side > screen_w)
85  this->x = screen_w - this->width;
86 }
87 
88 // Note! Code duplication of Text::text
89 void UI::Tooltip::set_text(const std::string& text) {
90  this->kill_children();
91  if(text.empty()) return;
92 
93  auto& s = Eng3D::State::get_instance();
94  this->width = s.width - this->x;
95  this->height = s.height - this->x;
96 
97  auto& text_txt = this->make_widget<UI::Text>(0, 0, text);
98  this->width = text_txt.width;
99  this->height = text_txt.height;
100 }
UI::Context ui_ctx
Definition: state.hpp:128
static State & get_instance()
Definition: state.cpp:514
Border class that defines the texture and size of borders of the widgets.
Definition: widget.hpp:153
std::shared_ptr< Eng3D::Texture > tooltip_tex
Definition: ui.hpp:151
std::shared_ptr< Eng3D::Texture > border_tex
Definition: ui.hpp:153
void set_pos(int x, int y, int width, int height, int screen_width, int screen_height)
Definition: tooltip.cpp:72
Tooltip(UI::Widget *parant=nullptr)
Definition: tooltip.cpp:34
void set_text(const std::string &text)
Generates text for the widget and overrides the current text texture.
Definition: tooltip.cpp:89
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_tooltip(UI::Tooltip *tooltip)
Set the tooltip to be shown when this widget is hovered, overrides the previous tooltip.
Definition: widget.cpp:458
Context * g_ui_context
Definition: ui.cpp:63
Primitive color type used through the engine.
Definition: color.hpp:32