supertux
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
ControlTextbox Class Reference
Inheritance diagram for ControlTextbox:
Inheritance graph
[legend]
Collaboration diagram for ControlTextbox:
Collaboration graph
[legend]

Public Member Functions

virtual void draw (DrawingContext &context) override
 
virtual bool on_mouse_button_up (const SDL_MouseButtonEvent &button) override
 
virtual bool on_mouse_button_down (const SDL_MouseButtonEvent &button) override
 
virtual bool on_mouse_motion (const SDL_MouseMotionEvent &motion) override
 
virtual bool on_key_up (const SDL_KeyboardEvent &key) override
 
virtual bool on_key_down (const SDL_KeyboardEvent &key) override
 
virtual bool event (const SDL_Event &ev) override
 
virtual void update (float dt_sec) override
 
void bind_string (std::string *value)
 Binds a string to the textbox.
 
const std::string & get_string () const
 Returns the full string held in m_charlist.
 
int get_text_position (const Vector &pos) const
 Gets at which (absolute) index, in the text, corresponds an on-screen point.
 
bool fits (const std::string &text) const
 Returns true if the given text would fit inside the box.
 
bool copy () const
 Copies the current selected text to the clipboard. More...
 
bool paste ()
 Pastes the text from the clipboard. More...
 
std::string get_selected_text () const
 
bool erase_selected_text ()
 
bool put_text (const std::string &text)
 Puts the given text at the currently selected position, replacing the text that was selected, if any. More...
 
- Public Member Functions inherited from InterfaceControl
void set_focus (bool focus)
 
bool has_focus () const
 
void set_rect (const Rectf &rect)
 
Rectf get_rect () const
 
- Public Member Functions inherited from Widget
virtual void setup ()
 
virtual void resize ()
 
virtual bool on_mouse_wheel (const SDL_MouseWheelEvent &wheel)
 

Public Attributes

bool(* m_validate_string )(ControlTextbox *, std::string)
 Optional, a function to validate the string. More...
 
- Public Attributes inherited from InterfaceControl
std::function< void()> m_on_change
 Optional; a function that will be called each time the bound value is modified.
 
std::unique_ptr< InterfaceLabelm_label
 Optional; the label associated with the control.
 

Protected Member Functions

virtual bool parse_value (bool call_on_change=true)
 Transfers the string into the binded variable, if any. More...
 
virtual void revert_value ()
 Reverts the contents of the char vector to the value of the member variable. More...
 
std::string get_contents () const
 WARNING : These function returns the status of the string as the user types, and therefore are unsuited for callers expecting a validated value. More...
 
std::string get_first_chars (int amount) const
 Returns first "amount" chars held in m_charlist.
 
std::string get_contents_visible () const
 Returns the part of the string that is actually displayed.
 
std::string get_first_chars_visible (int amount) const
 Returns first "amount" chars that are displayed.
 
void delete_char_before_caret ()
 
std::string get_truncated_text (const std::string &text) const
 Returns the largest string fitting in the box. More...
 
void recenter_offset ()
 Changes m_current_offset so that the caret is visible.
 

Protected Attributes

std::list< char > m_charlist
 Holds the list of characters that are in the textbox. More...
 
std::string * m_string
 This is the value that should be looked at by external functions. More...
 
std::string m_internal_string_backup
 Used so that if m_string is not bound, get_string() won't break/segfault. More...
 
float m_cursor_timer
 
int m_caret_pos
 
int m_secondary_caret_pos
 Used for selections.
 
bool m_shift_pressed
 
bool m_ctrl_pressed
 
bool m_mouse_pressed
 
int m_current_offset
 If the string is too long to be contained in the box, use this offset to select which characters will be displayed on the screen.
 
- Protected Attributes inherited from InterfaceControl
bool m_has_focus
 Whether or not the user has this InterfaceControl as focused.
 
Rectf m_rect
 The rectangle where the InterfaceControl should be rendered.
 
InterfaceControlm_parent
 A pointer to the parent container, or null if not in any container. More...
 

Member Function Documentation

◆ copy()

bool ControlTextbox::copy ( ) const

Copies the current selected text to the clipboard.

If no text is selected, does nothing and returns false.

Returns
true if copied successfully, false if error OR nothing was selected.

◆ erase_selected_text()

bool ControlTextbox::erase_selected_text ( )
Returns
true if some text was selected and therefore erased.

◆ get_contents()

std::string ControlTextbox::get_contents ( ) const
protected

WARNING : These function returns the status of the string as the user types, and therefore are unsuited for callers expecting a validated value.

If such case is, use get_string() instead. If you use these functions below, you'll get a half-typed value! (And don't validate while the user is typing, it could make typing impossible!)Converts the internal char vector to an actual string and returns it.

◆ get_selected_text()

std::string ControlTextbox::get_selected_text ( ) const
Returns
the text currently selected by the user.

◆ get_truncated_text()

std::string ControlTextbox::get_truncated_text ( const std::string &  text) const
protected

Returns the largest string fitting in the box.

◆ parse_value()

bool ControlTextbox::parse_value ( bool  call_on_change = true)
protectedvirtual

Transfers the string into the binded variable, if any.

Can be overridden by children if they use different value members (like float, int, etc). parse_value MUST make the validation, and MUST use get_contents() to fetch the string to parse, as get_string() will return the precedent valid string.

Parameters
call_on_changeWhether calling this function should also call m_on_change(). Children classes that implement another m_value member (float, int, etc.) should call ControlTextbox::parse_value(false) to parse the string without calling m_on_change immediately; the child class should call m_on_change() by itself when and only when they have parsed their value.
Returns
Whether or not the value in the textbox was a valid value. If it isn't, then parse_value() should handle reverting the char vector to its last valid state.
See also
get_string()
revert_value()

Reimplemented in ControlTextboxFloat, and ControlTextboxInt.

◆ paste()

bool ControlTextbox::paste ( )

Pastes the text from the clipboard.

If the clipboard doesn't have text, does nothing and returns false.

Returns
true if pasted successfully, false if error OR cliboard was empty/non-text

◆ put_text()

bool ControlTextbox::put_text ( const std::string &  text)

Puts the given text at the currently selected position, replacing the text that was selected, if any.

Returns
true if some text was deleted; false if no text was replaced.

◆ revert_value()

void ControlTextbox::revert_value ( )
protectedvirtual

Reverts the contents of the char vector to the value of the member variable.

Can be overridden by children, as parse_value() does.

Reimplemented in ControlTextboxFloat, and ControlTextboxInt.

Member Data Documentation

◆ m_charlist

std::list<char> ControlTextbox::m_charlist
protected

Holds the list of characters that are in the textbox.

When characters are added or deleted, this is the member variable that is getting edited.

◆ m_internal_string_backup

std::string ControlTextbox::m_internal_string_backup
protected

Used so that if m_string is not bound, get_string() won't break/segfault.

See also
get_string()
bind_string()
m_string

◆ m_string

std::string* ControlTextbox::m_string
protected

This is the value that should be looked at by external functions.

See also
get_string()
bind_string()
m_internal_string_backup

◆ m_validate_string

bool(* ControlTextbox::m_validate_string) (ControlTextbox *, std::string)

Optional, a function to validate the string.

If nullptr, then all values are assumed to be valid.

Parameters
ControlTextbox*A pointer to the original control.
std::stringThe string that is about to be applied to the textbox.
Returns
Whether or not this value is valid. If not, then the internal string will not be modified. Tip : You can manually change the string using set_string() inside this function and return false to establish a custom value (for example, a max string length).

The documentation for this class was generated from the following files: