1 #include "lxgui/gui_edit_box.hpp"
3 #include "lxgui/gui_alive_checker.hpp"
4 #include "lxgui/gui_font_string.hpp"
5 #include "lxgui/gui_localizer.hpp"
6 #include "lxgui/gui_manager.hpp"
7 #include "lxgui/gui_out.hpp"
8 #include "lxgui/gui_quad.hpp"
9 #include "lxgui/gui_region_tpl.hpp"
10 #include "lxgui/gui_texture.hpp"
11 #include "lxgui/input_window.hpp"
12 #include "lxgui/utils_range.hpp"
14 #include <lxgui/extern_sol2_state.hpp>
21 frame(block, mgr, attr),
22 carret_timer_(blink_time_, utils::periodic_timer::start_type::first_tick, false) {
35 script_name ==
"OnEnterPressed" || script_name ==
"OnEscapePressed" ||
36 script_name ==
"OnSpacePressed" || script_name ==
"OnTabPressed" ||
37 script_name ==
"OnUpPressed" || script_name ==
"OnDownPressed" ||
38 script_name ==
"OnTextChanged" || script_name ==
"OnTextSet";
44 const edit_box* box_obj = down_cast<edit_box>(&obj);
60 attr.
name = fs->get_raw_name();
63 auto fnt = this->create_layered_region<font_string>(fs->get_draw_layer(), std::move(attr));
66 fnt->set_manually_inherited(
true);
101 bool bypass_event =
false;
103 (script_name ==
"OnKeyUp" || script_name ==
"OnKeyDown" || script_name ==
"OnKeyRepeat")) {
106 if (!
has_focus() && (script_name ==
"OnChar")) {
116 if ((script_name ==
"OnKeyDown" || script_name ==
"OnKeyRepeat") &&
has_focus()) {
118 bool shift_is_pressed = data.
get<
bool>(1);
119 bool ctrl_is_pressed = data.
get<
bool>(2);
121 if (key_id == key::k_return || key_id == key::k_numpadenter) {
125 }
else if (key_id == key::k_tab) {
129 }
else if (key_id == key::k_up) {
133 }
else if (key_id == key::k_down) {
137 }
else if (key_id == key::k_space) {
141 }
else if (key_id == key::k_escape) {
147 process_key_(key_id, shift_is_pressed, ctrl_is_pressed);
151 }
else if (script_name ==
"OnChar" &&
has_focus()) {
152 std::uint32_t c = data.
get<std::uint32_t>(1);
162 }
else if (script_name ==
"OnSizeChanged") {
166 }
else if (script_name ==
"OnDragStart") {
169 }
else if (script_name ==
"OnDragMove") {
172 if (pos != std::numeric_limits<std::size_t>::max()) {
188 }
else if (script_name ==
"OnMouseDown") {
250 std::size_t
left = std::min(start, end);
251 std::size_t
right = std::max(start, end);
357 if (max_letters == 0) {
368 bool cursor_changed =
false;
374 cursor_changed =
true;
384 if (cursor_changed) {
544 if (max_history_lines == 0) {
692 auto fnt = create_layered_region<font_string>(
layer::artwork,
"$parentFontString");
696 fnt->set_manually_inherited(
true);
697 fnt->notify_loaded();
725 auto carret = create_layered_region<texture>(
layer::highlight,
"$parentCarret");
729 carret->set_manually_inherited(
true);
733 carret->notify_loaded();
738 for (std::size_t i = 0; i < 4; ++i)
747 bool modified =
false;
766 utils::from_string<double>(locale,
unicode_text_).value_or(-1.0) < 0.0) {
835 utils::ustring::iterator iter_display_carret;
843 float left_string_max_size = box_width * 0.25f;
844 float left_string_size = 0.0f;
845 utils::ustring left_string;
849 (left_string_size < left_string_max_size)) {
851 left_string.insert(left_string.begin(), *iter);
864 float left_string_max_size = box_width * 0.75f;
865 float left_string_size = 0.0f;
866 utils::ustring left_string;
870 (left_string_size < left_string_max_size)) {
872 left_string.insert(left_string.begin(), *iter);
885 iter_display_carret =
968 return std::numeric_limits<std::size_t>::max();
984 std::size_t num_letters =
987 for (std::size_t index = 0u; index < num_letters; ++index) {
989 if (local_x < 0.5f * (
quad[0].pos.x +
quad[2].pos.x))
1003 if (pos != std::numeric_limits<std::size_t>::max()) {
1075 if (key_id == key::k_return || key_id == key::k_numpadenter) {
1078 key_event.
add(std::string(
"\n"));
1083 }
else if (key_id == key::k_end) {
1087 if (shift_is_pressed) {
1096 }
else if (key_id == key::k_home) {
1100 if (shift_is_pressed) {
1109 }
else if (key_id == key::k_back || key_id == key::k_delete) {
1122 key_id == key::k_left || key_id == key::k_right ||
1123 (
is_multi_line_ && (key_id == key::k_up || key_id == key::k_down))) {
1127 if (key_id == key::k_left || key_id == key::k_right) {
1129 std::size_t offset = 0;
1130 if (key_id == key::k_left)
1158 if (shift_is_pressed) {
1171 !
is_multi_line_ && (key_id == key::k_up || key_id == key::k_down) &&
1173 if (key_id == key::k_up) {
1199 }
else if (key_id == key::k_c && ctrl_is_pressed) {
1203 utils::ustring selected =
unicode_text_.substr(min_pos, max_pos - min_pos);
1206 }
else if (key_id == key::k_v && ctrl_is_pressed) {
1207 bool text_added =
false;
1208 for (char32_t c :
get_manager().get_window().get_clipboard_content()) {
1228 return get_type_list_impl_<edit_box>();
Utility class for safe checking of region validity.
bool is_alive() const
Check if the wrapped region is still alive.
Holds a single color (float RGBA, 128 bits)
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 update_displayed_text_()
std::size_t get_max_letters() const
Returns the maximum number of letters to allow in this edit_box.
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)
void set_font_string(utils::observer_ptr< font_string > fstr)
Sets the font_string to use to render the content.
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.
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_
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)
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.
bool move_carret_at_(const vector2f &position)
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 get_letter_id_at_(const vector2f &position) const
void update_carret_position_()
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.
void create_font_string_()
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 update_font_string_()
utils::ustring unicode_text_
Stores a variable number of arguments for an event.
const utils::variant & get(std::size_t index) const
Returns a parameter of this event.
void add(T &&value)
Adds a parameter to this event.
A layered_region that can draw text on the screen.
A region that can contain other regions and react to events.
void enable_keyboard()
Marks this frame as able to receive any keyboard input.
void copy_from(const region &obj) override
Copies a region's parameters into this frame (inheritance).
void enable_mouse()
Marks this frame as able to receive mouse input (click & move).
virtual void fire_script(const std::string &script_name, const event_data &data=event_data{})
Calls a script.
virtual void update_(float delta)
virtual bool can_use_script(const std::string &script_name) const
Returns 'true' if this frame can use a script.
bool has_focus() const
Check if this frame currently has focus.
virtual void notify_focus(bool focus)
Notifies this frame that it has received or lost focus.
void set_focus(bool focus)
Asks for focus for this frame.
void notify_scaling_factor_updated() override
Tells this region that the global interface scaling factor has changed.
void enable_drag(const std::string &button_name)
Tells this frame to react to mouse drag.
const std::locale & get_locale() const
Returns the current locale (used to format numbers).
Manages the user interface.
localizer & get_localizer()
Returns the object used for localizing strings.
const input::window & get_window() const
Returns the window in which this gui is being displayed.
The base class of all elements in the GUI.
void initialize_(T &self, const region_core_attributes &attr)
Set up function to call in all derived class constructors.
bool is_virtual() const
Checks if this region is virtual.
manager & get_manager()
Returns this region's manager.
Used to draw some text on the screen.
float get_line_spacing() const
Returns this text's line spacing.
float get_box_width() const
Returns the width of the text box.
float get_string_width(const std::string &content) const
Returns the length of a provided string.
const std::array< vertex, 4 > & get_letter_quad(std::size_t index) const
Returns the quad for the letter at the provided index (position, texture coords, color).
std::size_t get_line_count() const
Returns the number of text lines.
float get_line_height() const
Returns the height of one line (constant).
std::size_t get_letter_count() const
Returns the number of letters currently displayed.
@ first_tick
The timer will start when you first call ticks()
void zero()
Resets the timer but doesn't pause it.
bool ticks()
Checks if the timer's period has been reached.
void update(double delta)
Updates this timer (adds time).
vector2< float > vector2f
Holds 2D coordinates (as floats)
vector2< T > bottom_right() const noexcept
vector2< T > top_left() const noexcept
Struct holding all the core information about a frame necessary for its creation.
Simple structure holding four vertices and a material.
std::array< vertex, 4 > v
Struct holding all the core information about a region necessary for its creation.
std::vector< utils::observer_ptr< const region > > inheritance