supertux
control_textbox.hpp
1 // SuperTux
2 // Copyright (C) 2020 A. Semphris <semphris@protonmail.com>
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 <http://www.gnu.org/licenses/>.
16 
17 #ifndef HEADER_SUPERTUX_INTERFACE_CONTROL_TEXTBOX_HPP
18 #define HEADER_SUPERTUX_INTERFACE_CONTROL_TEXTBOX_HPP
19 
20 #include <list>
21 
22 #include "control/input_manager.hpp"
23 #include "interface/control.hpp"
24 
25 
27 {
28 public:
30 
31  virtual void draw(DrawingContext& context) override;
32  virtual bool on_mouse_button_up(const SDL_MouseButtonEvent& button) override;
33  virtual bool on_mouse_button_down(const SDL_MouseButtonEvent& button) override;
34  virtual bool on_mouse_motion(const SDL_MouseMotionEvent& motion) override;
35  virtual bool on_key_up(const SDL_KeyboardEvent& key) override;
36  virtual bool on_key_down(const SDL_KeyboardEvent& key) override;
37  virtual bool event(const SDL_Event& ev) override;
38 
39  virtual void update(float dt_sec) override;
40 
42  void bind_string(std::string* value) { m_string = value; }
43 
45  const std::string& get_string() const;
46 
48  int get_text_position(const Vector& pos) const;
49 
51  bool fits(const std::string& text) const;
52 
59  bool copy() const;
60 
67  bool paste();
68 
70  std::string get_selected_text() const;
71 
73  bool erase_selected_text();
74 
81  bool put_text(const std::string& text);
82 
83 protected:
102  virtual bool parse_value(bool call_on_change = true);
103 
107  virtual void revert_value();
108 
109 protected:
118  std::string get_contents() const;
119 
121  std::string get_first_chars(int amount) const;
122 
124  std::string get_contents_visible() const;
125 
127  std::string get_first_chars_visible(int amount) const;
128 
129 public:
140  bool (*m_validate_string)(ControlTextbox*, std::string);
141 
142 protected:
146  std::list<char> m_charlist;
147 
153  std::string* m_string;
154 
161 
162  float m_cursor_timer;
163  int m_caret_pos;
165  bool m_shift_pressed, m_ctrl_pressed;
166  bool m_mouse_pressed;
167 
174 
175 protected:
176  void delete_char_before_caret();
177 
179  std::string get_truncated_text(const std::string& text) const;
180 
182  void recenter_offset();
183 
184 private:
185  ControlTextbox(const ControlTextbox&) = delete;
186  ControlTextbox& operator=(const ControlTextbox&) = delete;
187 };
188 
189 #endif
190 
191 /* EOF */
int m_secondary_caret_pos
Used for selections.
Definition: control_textbox.hpp:164
void bind_string(std::string *value)
Binds a string to the textbox.
Definition: control_textbox.hpp:42
bool put_text(const std::string &text)
Puts the given text at the currently selected position, replacing the text that was selected...
Definition: control_textbox.cpp:511
std::string get_truncated_text(const std::string &text) const
Returns the largest string fitting in the box.
Definition: control_textbox.cpp:423
std::string get_first_chars_visible(int amount) const
Returns first "amount" chars that are displayed.
Definition: control_textbox.cpp:404
Definition: control.hpp:28
bool erase_selected_text()
Definition: control_textbox.cpp:532
std::string m_internal_string_backup
Used so that if m_string is not bound, get_string() won&#39;t break/segfault.
Definition: control_textbox.hpp:160
void recenter_offset()
Changes m_current_offset so that the caret is visible.
Definition: control_textbox.cpp:441
int m_current_offset
If the string is too long to be contained in the box, use this offset to select which characters will...
Definition: control_textbox.hpp:173
virtual bool parse_value(bool call_on_change=true)
Transfers the string into the binded variable, if any.
Definition: control_textbox.cpp:316
std::string get_contents_visible() const
Returns the part of the string that is actually displayed.
Definition: control_textbox.cpp:389
bool copy() const
Copies the current selected text to the clipboard.
Definition: control_textbox.cpp:457
const std::string & get_string() const
Returns the full string held in m_charlist.
Definition: control_textbox.cpp:358
std::string get_selected_text() const
Definition: control_textbox.cpp:488
Definition: control_textbox.hpp:26
std::string get_first_chars(int amount) const
Returns first "amount" chars held in m_charlist.
Definition: control_textbox.cpp:376
bool paste()
Pastes the text from the clipboard.
Definition: control_textbox.cpp:472
bool(* m_validate_string)(ControlTextbox *, std::string)
Optional, a function to validate the string.
Definition: control_textbox.hpp:140
bool fits(const std::string &text) const
Returns true if the given text would fit inside the box.
Definition: control_textbox.cpp:435
std::string get_contents() const
WARNING : These function returns the status of the string as the user types, and therefore are unsuit...
Definition: control_textbox.cpp:364
std::string * m_string
This is the value that should be looked at by external functions.
Definition: control_textbox.hpp:153
virtual void revert_value()
Reverts the contents of the char vector to the value of the member variable.
Definition: control_textbox.cpp:342
int get_text_position(const Vector &pos) const
Gets at which (absolute) index, in the text, corresponds an on-screen point.
Definition: control_textbox.cpp:410
std::list< char > m_charlist
Holds the list of characters that are in the textbox.
Definition: control_textbox.hpp:146
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42