TextInput - Horizon-NTH/HorizonGUI GitHub Wiki

TextInput

Overview

The TextInput class, located in the hgui::kernel namespace, is a fundamental component in graphical user interfaces for accepting textual input from users. It inherits from the Widget class and provides functionality for displaying and interacting with text input fields. This class offers extensive customization options, allowing you to define the appearance, behavior, and functionality of text input fields within your application. The class can be found in the header file TextInput.h.

Constructors

  • TextInput(const std::shared_ptr<Shader>& shader, const size& size, const point& position, const std::shared_ptr<Label>& text, const std::pair<color, color>& colors, const std::pair<std::string, color>& placeHolder, const std::pair<color, std::chrono::milliseconds>& caret, const std::tuple<Function, Function, Function>& onChanges, unsigned sizeLimit, HGUI_PRECISION cornerRadius, unsigned borderWidth): Constructs a TextInput object with the specified shader, size, position, label, colors, a placeholder, customize the caret, functions callback (new typed text, on focus, on unfocused), a size limit to user input, a corner radius and a border width.

using Function = std::variant<std::function<void()>, std::function<void(const std::shared_ptr<TextInput>&)>>

Member Functions

  • void focus(): Focuses the text input field, allowing the user to enter text.

  • void unfocus(): Unfocused the text input field, preventing further text input.

  • bool is_focused() const: Retrieves the focus state of the text input field.

  • void set_label(const std::shared_ptr<Label>& newText): Sets the label associated with the text input field.

  • unsigned get_caret_position_from_point(const point& point) const: Retrieves the caret position based on the given point.

  • const std::string& get_value() const: Retrieves the current value of the text input field.

  • void set_value(const std::string& value): Sets the value of the text input field to the specified value.

  • const std::pair<color, color>& get_colors() const: Retrieves the colors of the text input field (background, border).

  • void set_colors(const std::pair<color, color>& colors): Sets the colors of the text input field (background, border).

  • unsigned get_size_limit() const: Retrieves the maximum number of character that can enter the user.

  • void set_size_limit(unsigned sizeLimit): Sets the maximum number of character that can enter the user.

  • const std::pair<std::string, color>& get_place_holder() const: Retrieves the placeholder text and color.

  • void set_place_holder(const std::pair<std::string, color>& placeHolder): Sets the placeholder text and color.

  • const std::tuple<Function, Function, Function>& get_on_changes_functions() const: Retrieves the functions callback (new typed text, on focus, on unfocus).

using Function = std::variant<std::function<void()>, std::function<void(const std::shared_ptr<TextInput>&)>>

  • void set_on_changes_functions(const std::tuple<Function, Function, Function>& onChanges): Sets the functions callback (new typed text, on focus, on unfocus).

using Function = std::variant<std::function<void()>, std::function<void(const std::shared_ptr<TextInput>&)>>

  • unsigned get_caret_position() const: Retrieves the caret position within the text input field.

  • void set_caret_position(unsigned caretPosition): Sets the caret position within the text input field.

  • const std::shared_ptr<Font>& get_font() const: Retrieves the font used for rendering text in the text input field.

  • void set_font(const std::shared_ptr<Font>& font) const: Sets the font used for rendering text in the text input field.

  • const color& get_text_color() const: Retrieves the text color of the text input field.

  • void set_text_color(const color& textColor): Sets the text color of the text input field.

  • const std::pair<color, std::chrono::milliseconds>& get_caret() const: Retrieves the caret color and blink duration.

  • void set_caret(const std::pair<color, std::chrono::milliseconds>& caret): Sets the caret color and blink duration.

Example Usage

#include <hgui/kernel/TextInput.h>

// Example Usage of TextInput class
std::shared_ptr<hgui::kernel::Shader> shader = /* initialize shader */;
hgui::size inputSize(400, 50);
hgui::point inputPosition(150, 150);
std::shared_ptr<hgui::kernel::Label> labelText = /* initialize label */;
std::pair<hgui::color, hgui::color> inputColors(hgui::color(0.8f, 0.8f, 0.8f), hgui::color(0.2f, 0.2f, 0.2f));
std::pair<std::string, hgui::color> placeholder("Enter text here...", hgui::color(0.5f, 0.5f, 0.5f));
std::pair<hgui::color, std::chrono::milliseconds> caretColor(hgui::color(0.2f, 0.2f, 0.2f), std::chrono::milliseconds(500));
std::tuple<TextInput::Function, TextInput::Function, TextInput::Function> onChangeFunction = /* initialize onChanges functions */;
HGUI_PRECISION cornerRadius = 5.0f;
unsigned borderWidth = 1;

hgui::kernel::TextInput textInput(shader, inputSize, inputPosition, labelText, inputColors, placeholder, caretColor, onChangeFunction, cornerRadius, borderWidth);

// Set the position of the TextInput
hgui::point newPosition(200, 200);
textInput.set_position(newPosition);

// Get the current value of the TextInput
std::string value = textInput.get_value();

// Set a new value for the TextInput
textInput.set_value("New Text Value");

// Set the font for the TextInput
std::shared_ptr<hgui::kernel::Font> font = /* initialize font */;
textInput.set_font(font);

// Set the text color for the TextInput
hgui::color textColor(1.0f, 1.0f, 1.0f);
textInput.set_text_color(textColor);

// Draw the TextInput
textInput.draw();

Note: In the "Example Usage" section, we demonstrate how to create a TextInput object with a shader, size, position, label, colors, placeholder text, caret color, onChange function, corner radius, and border width. You can customize various aspects of the TextInput, such as its appearance, text content, font, and colors, and interact with it to retrieve or set its value. The TextInput class provides essential functionality for integrating text input fields into your graphical user interface applications.

⚠️ **GitHub.com Fallback** ⚠️