Symphony Of Empires
input.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/input.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include <cstdlib>
26 #include <cstring>
27 #include <string>
28 #include <algorithm>
29 #include <codecvt>
30 
31 #include <glm/vec2.hpp>
32 
33 #include "eng3d/ui/widget.hpp"
34 #include "eng3d/ui/input.hpp"
35 #include "eng3d/ui/ui.hpp"
36 #include "eng3d/texture.hpp"
37 #include "eng3d/rectangle.hpp"
38 #include "eng3d/state.hpp"
39 
40 UI::Input::Input(int _x, int _y, unsigned _w, unsigned _h, Widget* _parent)
41  : Widget(_parent, _x, _y, _w, _h, UI::WidgetType::INPUT)
42 {
43  this->on_textinput = ([](UI::Input& w, const char* input) {
44  // Carefully convert into UTF32 then back into UTF8 so we don't have to
45  // use a dedicated library for UTF8 handling (for now)
46  std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv_utf8_utf32;
47  std::u32string unicode_text = conv_utf8_utf32.from_bytes(w.buffer);
48  if(input != nullptr) {
49  std::u32string unicode_input = conv_utf8_utf32.from_bytes(input);
50  unicode_text.insert(w.curpos, unicode_input);
51  w.curpos += unicode_input.length();
52  } else if(!unicode_text.empty() && w.curpos) {
53  unicode_text.erase(w.curpos - 1, 1);
54  w.curpos--;
55  }
56  w.buffer = conv_utf8_utf32.to_bytes(unicode_text);
57  w.set_text(w.buffer.empty() ? " " : w.buffer);
58  });
62 }
63 
64 void UI::Input::set_buffer(const std::string& _buffer) {
65  buffer = _buffer;
66  this->set_text(buffer);
67 
68  std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv_utf8_utf32;
69  std::u32string unicode_text = conv_utf8_utf32.from_bytes(this->buffer);
70  this->curpos = unicode_text.length();
71 }
72 
73 std::string UI::Input::get_buffer() const {
74  return buffer;
75 }
76 
78  auto &input = static_cast<UI::Input&>(w);
79  input.is_selected = true;
80 }
81 
83  auto &input = static_cast<UI::Input&>(w);
84  if(input.is_selected)
85  input.set_text(input.buffer);
86  input.is_selected = false;
87 }
88 
90  auto &input = static_cast<UI::Input&>(w);
91  std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv_utf8_utf32;
92  std::u32string unicode_text = conv_utf8_utf32.from_bytes(input.buffer);
93  if(input.curpos == unicode_text.length()) {
94  input.timer = (input.timer + 1) % 30;
95  const std::string cursor = input.timer >= 10 ? "_" : "";
96  if(input.is_selected && input.timer % 30 == 0) {
97  if(!input.buffer.empty()) {
98  input.set_text(input.buffer + cursor);
99  } else if(!cursor.empty()) {
100  input.set_text(cursor);
101  }
102  }
103  }
104 }
Input widget for keyboard inputs.
Definition: input.hpp:34
std::function< void(UI::Input &, const char *)> on_textinput
Definition: input.hpp:45
static void on_click_outside_default(UI::Widget &w)
Definition: input.cpp:82
bool is_selected
Definition: input.hpp:46
Input(int x, int y, unsigned w, unsigned h, Widget *parent=nullptr)
Definition: input.cpp:40
void set_buffer(const std::string &_buffer)
Definition: input.cpp:64
std::string get_buffer() const
Definition: input.cpp:73
static void on_update_default(UI::Widget &w)
Definition: input.cpp:89
static void on_click_default(UI::Widget &w)
Definition: input.cpp:77
The master widget all the other widgets inherit from, do not use directly instead use one of the many...
Definition: widget.hpp:176
std::function< void(UI::Widget &)> on_update
Definition: widget.hpp:349
virtual void set_on_click(std::function< void(UI::Widget &)> _on_click)
Sets the on_click function of this widget.
Definition: widget.hpp:259
std::function< void(UI::Widget &)> on_click_outside
Definition: widget.hpp:351
virtual void set_text(const std::string &text)
Generates text for the widget and overrides the current text texture.
Definition: widget.cpp:445
WidgetType
The type of the widget, some widgets share types between them to keep simplicity.
Definition: widget.hpp:76