Symphony Of Empires
window.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/window.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include "eng3d/ui/window.hpp"
26 #include "eng3d/ui/image.hpp"
27 #include "eng3d/ui/div.hpp"
28 #include "eng3d/ui/ui.hpp"
29 #include "eng3d/state.hpp"
30 #include "eng3d/texture.hpp"
31 
32 using namespace UI;
33 
34 Window::Window(int _x, int _y, unsigned w, unsigned h, Widget* _parent)
35  : Widget(_parent, _x, _y, w, h, UI::WidgetType::WINDOW),
36  is_movable{ true }
37 {
38  auto& s = Eng3D::State::get_instance();
39  this->padding = glm::ivec2(8, 24 + 8);
40  this->current_texture = s.tex_man.load(s.package_man.get_unique("gfx/window_background.png"));
41  this->is_scroll = true;
42  this->text_color = Eng3D::Color(1.f, 1.f, 1.f);
43 
44  glm::ivec2 size(4, 4);
45  glm::ivec2 texture_size(10, 10);
46  glm::ivec2 offset(0, 24);
47  this->border = Border(g_ui_context->border_tex, size, texture_size, offset);
48 
49  this->set_on_drag([this](glm::ivec2 start_pos, glm::ivec2 current_pos) {
50  if (start_pos == current_pos) {
51  this->start_drag_position = glm::ivec2{this->x, this->y};
52  }
53  if(!this->is_movable) return;
54  const auto move_offset = current_pos - start_pos;
55  this->x = this->start_drag_position.x + move_offset.x;
56  this->y = this->start_drag_position.y + move_offset.y;
57  });
58 }
59 
60 // There are a number of improvement to be made here
61 // * This places the button on in the end of the children vector,
62 // would be better to have it in the beginning
63 // * This function is not general, would be nice to set your own exit btn
64 // * We should add an on_close event that is called when exiting
65 void Window::set_close_btn_function(std::function<void(Widget&)> _on_click) {
66  if(_on_click) {
67  if(!this->close_btn) {
68  const int size = 24;
69  auto& btn_wrapper = this->make_widget<UI::Div>(-size - padding.x, -padding.y, size, size);
70  btn_wrapper.origin = UI::Origin::UPPER_RIGHT;
71 
72  btn_wrapper.make_widget<UI::Image>(0, 0, size, size, "gfx/ui/button/exit_btn_shadow.png", true);
73  const int btn_size = (int)(size * 0.75f);
74  const int offset = (size - btn_size) / 2;
75  auto* btn = Image::make_transparent(offset, offset, btn_size, btn_size, "gfx/ui/button/exit_btn.png", true, &btn_wrapper);
76  btn->set_on_click(_on_click);
77  this->close_btn = &btn_wrapper;
78  }
79  } else {
80  if(this->close_btn) {
81  this->close_btn->kill();
82  this->close_btn = nullptr;
83  }
84  }
85 }
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 > border_tex
Definition: ui.hpp:153
Image widget, can display pictures or effects on the screen.
Definition: image.hpp:43
static Image * make_transparent(int x, int y, unsigned w, unsigned h, const std::string &tex_path, Widget *parent=nullptr)
Definition: image.cpp:64
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
bool is_scroll
Definition: widget.hpp:306
virtual void set_on_drag(std::function< void(glm::ivec2, glm::ivec2)> _on_drag)
Definition: widget.hpp:268
std::shared_ptr< Eng3D::Texture > current_texture
Definition: widget.hpp:327
void kill()
Kills the current widget, setting it up for deletion when dead widgets are cleared by the UI context.
Definition: widget.hpp:283
Eng3D::Color text_color
Definition: widget.hpp:332
glm::ivec2 padding
Definition: widget.hpp:323
void set_close_btn_function(std::function< void(Widget &)> on_click)
Definition: window.cpp:65
Window(int x, int y, unsigned w, unsigned h, Widget *parent=nullptr)
Definition: window.cpp:34
bool is_movable
Definition: window.hpp:46
WidgetType
The type of the widget, some widgets share types between them to keep simplicity.
Definition: widget.hpp:76
Context * g_ui_context
Definition: ui.cpp:63
Primitive color type used through the engine.
Definition: color.hpp:32