lxgui
gui_edit_box.hpp
1 #ifndef LXGUI_GUI_EDIT_BOX_HPP
2 #define LXGUI_GUI_EDIT_BOX_HPP
3 
4 #include "lxgui/gui_bounds2.hpp"
5 #include "lxgui/gui_color.hpp"
6 #include "lxgui/gui_frame.hpp"
7 #include "lxgui/input_keys.hpp"
8 #include "lxgui/lxgui.hpp"
9 #include "lxgui/utils.hpp"
10 #include "lxgui/utils_periodic_timer.hpp"
11 #include "lxgui/utils_string.hpp"
12 
13 namespace lxgui::gui {
14 
15 class font_string;
16 class texture;
17 
62 class edit_box : public frame {
63 public:
64  using base = frame;
65 
67  explicit edit_box(utils::control_block& block, manager& mgr, const frame_core_attributes& attr);
68 
73  void copy_from(const region& obj) override;
74 
83  void
84  fire_script(const std::string& script_name, const event_data& data = event_data{}) override;
85 
91  bool can_use_script(const std::string& script_name) const override;
92 
97  void set_text(const utils::ustring& content);
98 
103  const utils::ustring& get_text() const;
104 
112  void highlight_text(
113  std::size_t start = 0u,
114  std::size_t end = std::numeric_limits<std::size_t>::max(),
115  bool force_update = false);
116 
118  void unlight_text();
119 
124  void set_highlight_color(const color& c);
125 
130  void insert_after_cursor(const utils::ustring& content);
131 
137  std::size_t get_cursor_position() const;
138 
144  void set_cursor_position(std::size_t pos);
145 
150  void set_max_letters(std::size_t max_letters);
151 
156  std::size_t get_max_letters() const;
157 
162  std::size_t get_letter_count() const;
163 
168  void set_blink_time(double blink_time);
169 
174  double get_blink_time() const;
175 
180  void set_numeric_only(bool numeric_only);
181 
187  void set_positive_only(bool positive_only);
188 
194  void set_integer_only(bool integer_only);
195 
200  bool is_numeric_only() const;
201 
206  bool is_positive_only() const;
207 
212  bool is_integer_only() const;
213 
220  void set_password_mode_enabled(bool enable);
221 
228  }
229 
236  }
237 
242  bool is_password_mode_enabled() const;
243 
253  void set_multi_line(bool multi_line);
254 
259  bool is_multi_line() const;
260 
265  void set_max_history_lines(std::size_t max_history_lines);
266 
271  std::size_t get_max_history_lines() const;
272 
278  void add_history_line(const utils::ustring& history_line);
279 
285  const std::vector<utils::ustring>& get_history_lines() const;
286 
288  void clear_history_lines();
289 
294  void set_arrows_ignored(bool arrows_ignored);
295 
302  void set_text_insets(const bounds2f& insets);
303 
308  const bounds2f& get_text_insets() const;
309 
314  const utils::observer_ptr<font_string>& get_font_string() {
315  return font_string_;
316  }
317 
322  utils::observer_ptr<const font_string> get_font_string() const {
323  return font_string_;
324  }
325 
330  void set_font_string(utils::observer_ptr<font_string> fstr);
331 
337  void set_font(const std::string& font_name, float height);
338 
343  void notify_focus(bool focus) override;
344 
346  void notify_scaling_factor_updated() override;
347 
349  static void register_on_lua(sol::state& lua);
350 
351  static constexpr const char* class_name = "EditBox";
352 
353 protected:
354  void parse_attributes_(const layout_node& node) override;
355  void parse_all_nodes_before_children_(const layout_node& node) override;
356  void parse_font_string_node_(const layout_node& node);
357  void parse_text_insets_node_(const layout_node& node);
358 
359  void update_(float delta) override;
360 
361  const std::vector<std::string>& get_type_list_() const override;
362 
363  void create_font_string_();
364  void create_highlight_();
365  void create_carret_();
366 
367  bool check_text_();
368  void update_displayed_text_();
369  void update_font_string_();
371 
372  bool add_char_(char32_t c);
373  bool remove_char_();
374  std::size_t get_letter_id_at_(const vector2f& position) const;
375  bool move_carret_at_(const vector2f& position);
376  bool move_carret_horizontally_(bool forward = true);
377  bool move_carret_vertically_(bool down = true);
378 
379  void process_key_(input::key key_id, bool shift_is_pressed, bool ctrl_is_pressed);
380 
381  utils::ustring unicode_text_;
382  utils::ustring displayed_text_;
383  utils::ustring::iterator iter_carret_pos_;
384 
385  std::size_t display_pos_ = 0;
386  std::size_t num_letters_ = 0;
387  std::size_t max_letters_ = std::numeric_limits<std::size_t>::max();
388  bool is_numeric_only_ = false;
389  bool is_positive_only_ = false;
390  bool is_integer_only_ = false;
391  bool is_password_mode_ = false;
392  bool is_multi_line_ = false;
393  bool are_arrows_ignored_ = false;
394 
395  utils::observer_ptr<texture> highlight_ = nullptr;
396  color highlight_color_ = color(1.0f, 1.0f, 1.0f, 0.5f);
397  std::size_t selection_start_pos_ = 0u;
398  std::size_t selection_end_pos_ = 0u;
399  bool is_text_selected_ = false;
400 
401  utils::observer_ptr<texture> carret_ = nullptr;
402  double blink_time_ = 0.5;
404 
405  std::vector<utils::ustring> history_line_list_;
406  std::size_t max_history_lines_ = std::numeric_limits<std::size_t>::max();
407  std::size_t current_history_line_ = std::numeric_limits<std::size_t>::max();
408 
409  utils::observer_ptr<font_string> font_string_ = nullptr;
410 
412 };
413 
414 } // namespace lxgui::gui
415 
416 #endif
Holds a single color (float RGBA, 128 bits)
Definition: gui_color.hpp:12
A frame with an editable text box.
void set_integer_only(bool integer_only)
Makes this edit_box allow integer numbers only.
bool add_char_(char32_t c)
const bounds2f & get_text_insets() const
Returns the text insets.
void disable_password_mode()
Disables password mode.
std::size_t get_max_letters() const
Returns the maximum number of letters to allow in this edit_box.
static void register_on_lua(sol::state &lua)
Registers this region class to the provided Lua state.
void notify_scaling_factor_updated() override
Tells this region that the global interface scaling factor has changed.
void set_max_letters(std::size_t max_letters)
Sets the maximum number of letters to allow in this edit_box.
void set_text_insets(const bounds2f &insets)
Sets the insets used to render the content text.
const std::vector< std::string > & get_type_list_() const override
void add_history_line(const utils::ustring &history_line)
Adds a new history line to the history line list.
utils::ustring::iterator iter_carret_pos_
void clear_history_lines()
Clears the history line list.
void process_key_(input::key key_id, bool shift_is_pressed, bool ctrl_is_pressed)
utils::observer_ptr< const font_string > get_font_string() const
Returns the font_string used to render the content.
void set_font_string(utils::observer_ptr< font_string > fstr)
Sets the font_string to use to render the content.
std::size_t num_letters_
void parse_attributes_(const layout_node &node) override
bool move_carret_vertically_(bool down=true)
double get_blink_time() const
Returns the carret's blink time.
void set_highlight_color(const color &c)
Sets the color of the highlight quad.
const utils::observer_ptr< font_string > & get_font_string()
Returns the font_string used to render the content.
void parse_font_string_node_(const layout_node &node)
bool is_positive_only() const
Checks if this edit_box allows positive numbers only.
utils::periodic_timer carret_timer_
std::size_t selection_start_pos_
void highlight_text(std::size_t start=0u, std::size_t end=std::numeric_limits< std::size_t >::max(), bool force_update=false)
Selects a portion of the content.
void set_cursor_position(std::size_t pos)
Moves the cursor to a chosen position.
void set_positive_only(bool positive_only)
Makes this edit_box allow positive numbers only.
std::size_t get_letter_count() const
Returns the number of letters in the content.
const utils::ustring & get_text() const
Returns the content of this edit_box.
std::size_t max_history_lines_
static constexpr const char * class_name
void set_font(const std::string &font_name, float height)
Sets the font (file and size) to render the content.
void set_text(const utils::ustring &content)
Sets the content of this edit_box.
bool move_carret_horizontally_(bool forward=true)
edit_box(utils::control_block &block, manager &mgr, const frame_core_attributes &attr)
Constructor.
const std::vector< utils::ustring > & get_history_lines() const
Returns the history line list.
std::size_t get_max_history_lines() const
Returns the maximum number of history lines this edit_box can keep.
bool is_numeric_only() const
Checks if this edit_box allows numeric characters only.
std::size_t get_cursor_position() const
Returns the current position of the cursor.
utils::observer_ptr< font_string > font_string_
void insert_after_cursor(const utils::ustring &content)
Inserts some text after the cursor.
bool is_password_mode_enabled() const
Checks if this edit_box is in password mode.
bool is_integer_only() const
Checks if this edit_box allows integer numbers only.
void parse_text_insets_node_(const layout_node &node)
void enable_password_mode()
Enables password mode.
bool move_carret_at_(const vector2f &position)
std::size_t display_pos_
void copy_from(const region &obj) override
Copies a region's parameters into this edit_box (inheritance).
void set_password_mode_enabled(bool enable)
Enables or disables password mode.
std::size_t selection_end_pos_
void notify_focus(bool focus) override
Notifies this frame that it has received or lost focus.
void set_numeric_only(bool numeric_only)
Makes this edit_box allow numeric characters only.
std::size_t max_letters_
std::size_t get_letter_id_at_(const vector2f &position) const
std::vector< utils::ustring > history_line_list_
void set_arrows_ignored(bool arrows_ignored)
Sets whether keyboard arrows move the carret or not.
void unlight_text()
Deselects the selected text, if any.
void set_blink_time(double blink_time)
Sets the carret's blink time.
utils::observer_ptr< texture > highlight_
void set_multi_line(bool multi_line)
Allows this edit_box to have several lines in it.
bool can_use_script(const std::string &script_name) const override
Returns 'true' if this edit_box can use a script.
std::size_t current_history_line_
utils::ustring displayed_text_
void update_(float delta) override
void fire_script(const std::string &script_name, const event_data &data=event_data{}) override
Calls a script.
utils::observer_ptr< texture > carret_
bool is_multi_line() const
Checks if this edit_box can have several lines in it.
void set_max_history_lines(std::size_t max_history_lines)
Sets the maximum number of history lines this edit_box can keep.
void parse_all_nodes_before_children_(const layout_node &node) override
utils::ustring unicode_text_
Stores a variable number of arguments for an event.
A region that can contain other regions and react to events.
Definition: gui_frame.hpp:255
An node in a layout file.
Manages the user interface.
Definition: gui_manager.hpp:44
The base class of all elements in the GUI.
Definition: gui_region.hpp:161
static const bounds2 zero
Definition: gui_bounds2.hpp:85
Struct holding all the core information about a frame necessary for its creation.