Symphony Of Empires
table.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/table.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include "eng3d/ui/table.hpp"
26 #include "eng3d/ui/widget.hpp"
27 #include "eng3d/ui/ui.hpp"
28 #include "eng3d/ui/div.hpp"
29 #include "eng3d/ui/label.hpp"
30 #include "eng3d/ui/scrollbar.hpp"
31 #include "eng3d/texture.hpp"
32 #include "eng3d/rectangle.hpp"
33 #include "eng3d/state.hpp"
34 
35 UI::TableElement::TableElement(int _width, int _height, UI::Widget* _parent)
36  : UI::Widget(_parent, 0, 0, _width, _height, UI::WidgetType::TABLE_ELEMENT)
37 {
39 }
40 
41 void UI::TableElement::set_key(const std::string& key) {
42  key_type = UI::TableElement::KeyType::STRING;
43  key_string = key;
44 }
45 
46 void UI::TableElement::set_key(float key) {
47  key_type = UI::TableElement::KeyType::NUMBER;
48  key_number = key;
49 }
50 
51 void UI::TableElement::set_key(float key, const std::string_view format) {
52  this->set_text(string_format("%.0f", key));
53  key_type = UI::TableElement::KeyType::NUMBER;
54  key_number = key;
55 }
56 
58  if(this->key_type != right.key_type)
59  return this->key_type < right.key_type;
60 
61  switch(this->key_type) {
62  case UI::TableElement::KeyType::NUMBER: return this->key_number < right.key_number;
63  case UI::TableElement::KeyType::STRING: return this->key_string < right.key_string;
64  case UI::TableElement::KeyType::NONE: return false;
65  }
66  CXX_THROW(std::runtime_error, "TableElement KeyType not supported");
67 }
68 
69 UI::TableRow::TableRow(int _width, int _height, std::vector<int>& _columns_width, UI::Widget* _parent)
70  : UI::Widget(_parent, 0, 0, _width, _height, UI::WidgetType::TABLE_ROW),
71  columns_width{ _columns_width }
72 {
73  this->flex = UI::Flex::ROW;
75 
76  elements.resize(this->columns_width.size());
77  std::transform(this->columns_width.cbegin(), this->columns_width.cend(), elements.begin(), [this](const auto e) {
78  return &this->make_widget<UI::TableElement>(e, this->height);
79  });
80 
81  this->on_update = ([this](UI::Widget&) {
82  this->is_active = false;
83  });
84 
85  glm::ivec2 size(5, 5);
86  glm::ivec2 texture_size(63, 63);
87  this->border = UI::Border(nullptr, size, texture_size);
88  this->on_pos_recalc = [this](UI::Widget&, int index) {
89  auto& s = Eng3D::State::get_instance();
90  auto& tex_man = s.tex_man;
91  auto border_tex = tex_man.load(s.package_man.get_unique("gfx/test.png"));
92  if(index % 2 == 0) {
93  this->border.texture = border_tex;
94  } else {
95  this->border.texture = nullptr;
96  }
97  };
98 }
99 
101  assert(index < elements.size());
102  return elements[index];
103 }
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 > texture
Definition: widget.hpp:155
An element on a table, operates on a k=v fashion where the key is used for sorting the table.
Definition: table.hpp:48
void set_key(const std::string &key)
Definition: table.cpp:41
TableElement(int width, int height, UI::Widget *_parent)
Definition: table.cpp:35
bool operator<(const UI::TableElement &right) const
Definition: table.cpp:57
UI::TableElement * get_element(size_t index)
Definition: table.cpp:100
bool is_active
Definition: table.hpp:77
TableRow(int width, int height, std::vector< int > &columns_width, UI::Widget *_parent)
Definition: table.cpp:69
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
std::function< void(UI::Widget &)> on_update
Definition: widget.hpp:349
UI::Align text_align_x
Definition: widget.hpp:331
UI::Flex flex
Definition: widget.hpp:337
UI::FlexJustify flex_justify
Definition: widget.hpp:338
std::function< void(UI::Widget &, int i)> on_pos_recalc
Definition: widget.hpp:353
WidgetType
The type of the widget, some widgets share types between them to keep simplicity.
Definition: widget.hpp:76
std::string string_format(const std::string_view format, Args &&... args)
String formatter.
Definition: string.hpp:100
#define CXX_THROW(class,...)
Definition: utils.hpp:98