Symphony Of Empires
numeric_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/group.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include "eng3d/ui/numeric_input.hpp"
26 #include "eng3d/ui/widget.hpp"
27 #include "eng3d/ui/group.hpp"
28 #include "eng3d/ui/input.hpp"
29 #include "eng3d/ui/button.hpp"
30 
31 UI::NumericInput::NumericInput(int _x, int _y, unsigned _w, unsigned _h, Widget* _parent)
32  : Group(_x, _y, _w, _h, _parent)
33 {
34  inp = &this->make_widget<UI::Input>(0, 0, width - 32 - 32, height);
35  inp->set_buffer("100.0");
36 
37  add_btn = &this->make_widget<UI::Button>(0, 0, 32, height);
38  add_btn->set_text("+");
41  auto* p = (UI::NumericInput*)w.parent;
42  float val = atof(p->inp->get_buffer().c_str()) + 1.f;
43  char tmpbuf[100];
44  snprintf(tmpbuf, 100, "%.2f", val);
45  p->set_buffer(tmpbuf);
46  });
47 
48  sub_btn = &this->make_widget<UI::Button>(0, 0, 32, height);
49  sub_btn->set_text("-");
52  auto* p = (NumericInput*)w.parent;
53  float val = atof(p->get_buffer().c_str()) + 1.f;
54  char tmpbuf[100];
55  snprintf(tmpbuf, 100, "%.2f", val);
56  p->set_buffer(tmpbuf);
57  });
58 }
59 
60 void UI::NumericInput::set_buffer(const std::string& _buffer) {
61  inp->set_buffer(_buffer);
62 }
63 
64 std::string UI::NumericInput::get_buffer() const {
65  return inp->get_buffer();
66 }
Grouping to keep widgets together without triggering events.
Definition: group.hpp:44
void set_buffer(const std::string &_buffer)
Definition: input.cpp:64
A version of the Input widget that only allows numbers.
std::string get_buffer() const
void set_buffer(const std::string &_buffer)
NumericInput(int _x, int _y, unsigned w, unsigned h, Widget *_parent)
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_on_click(std::function< void(UI::Widget &)> _on_click)
Sets the on_click function of this widget.
Definition: widget.hpp:259
size_t width
Definition: widget.hpp:325
constexpr void right_side_of(const UI::Widget &rhs)
Definition: widget.hpp:254
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
UI::Widget * parent
Definition: widget.hpp:314