supertux
item_textfield.hpp
1 // SuperTux
2 // Copyright (C) 2015 Hume2 <teratux.mail@gmail.com>
3 // 2022 Vankata453
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 #ifndef HEADER_SUPERTUX_GUI_ITEM_TEXTFIELD_HPP
19 #define HEADER_SUPERTUX_GUI_ITEM_TEXTFIELD_HPP
20 
21 #include "gui/menu_item.hpp"
22 
23 class ItemTextField : public MenuItem
24 {
25 public:
26  ItemTextField(const std::string& text_, std::string* input_, int id_ = -1);
27 
29  virtual void draw(DrawingContext&, const Vector& pos, int menu_width, bool active) override;
30 
32  virtual int get_width() const override;
33 
35  virtual void process_action(const MenuAction& action) override;
36 
37  std::string* input;
38 
39  void change_input(const std::string& input_) {
40  *input = input_;
41  m_input_undo.clear();
42  m_input_redo.clear();
43  }
44 
46  virtual void invalid_remove() {}
47 
49  virtual void event(const SDL_Event& ev) override;
50 
52  virtual bool changes_width() const override {
53  return true;
54  }
55 
57  virtual void update_undo();
58 
60  virtual void on_input_update() {}
61 
62  // Text manipulation and navigation functions
63 
64  virtual void insert_text(const std::string& text, const int left_offset_pos);
65  virtual void clear();
66  virtual void go_left();
67  virtual void go_right();
68  virtual void go_to_beginning();
69  virtual void go_to_end();
70  virtual void delete_front();
71  virtual void delete_back();
72  virtual void cut();
73  virtual void copy();
74  virtual void paste();
75  virtual void undo();
76  virtual void redo();
77 
78 protected:
79  std::string m_input_undo;
80  std::string m_input_redo;
81 
82  const std::string m_cursor;
83  float m_cursor_width;
84  int m_cursor_left_offset;
85 
86 private:
87  ItemTextField(const ItemTextField&) = delete;
88  ItemTextField& operator=(const ItemTextField&) = delete;
89 };
90 
91 #endif
92 
93 /* EOF */
virtual void process_action(const MenuAction &action) override
Processes the menu action.
Definition: item_textfield.cpp:122
virtual void event(const SDL_Event &ev) override
Processes the given event.
Definition: item_textfield.cpp:75
virtual void draw(DrawingContext &, const Vector &pos, int menu_width, bool active) override
Draws the menu item.
Definition: item_textfield.cpp:40
Definition: item_textfield.hpp:23
Definition: menu_item.hpp:23
virtual void on_input_update()
Calls when the input gets updated.
Definition: item_textfield.hpp:60
virtual bool changes_width() const override
Indicates that this item changes its width.
Definition: item_textfield.hpp:52
virtual void update_undo()
Updates undo and redo status.
Definition: item_textfield.cpp:139
virtual void invalid_remove()
Calls when the user wants to remove an invalid char.
Definition: item_textfield.hpp:46
virtual int get_width() const override
Returns the minimum width of the menu item.
Definition: item_textfield.cpp:69
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42