Button - Horizon-NTH/HorizonGUI GitHub Wiki

Button

Overview

The Button class, located in the hgui::kernel namespace, is a user interface element designed to respond to user interactions. Buttons are commonly used to trigger actions when clicked, providing interactivity within your graphical application. This class extends the Widget class and features functionalities such as setting button states, specifying text labels, assigning custom textures or images, and defining button behavior. The class can be found in the header file Button.h.

Constructors

  • Button(const std::function<void()>& function, const std::shared_ptr<Shader>& shader, const size& size, const point& position, const std::shared_ptr<Label>& text, const std::tuple<color, color, color>& colors, float cornerAngularRadius, bool blurrOnHover = true, const std::shared_ptr<Texture>& = nullptr): Constructs a Button object with the specified action function, shader, size, position, text label, colors (normal color, hover color and pressed color), corner angular radius, and optional texture and let you choose if the button will be blurred when in focus.

Member Functions

  • void press() const: Simulates a button press, invoking the assigned action function.

  • const state& get_state() const: Retrieves the current state of the button.

  • void set_state(const state& state): Sets the state of the button, which define the button's appearance and behavior.

  • const std::tuple<color, color, color>& get_color() const: Retrieves the current color of the button (normal color, hover color and pressed color).

  • void set_color(const std::tuple<color, color, color>& newColors): Sets the colors of the button (normal color, hover color and pressed color).

  • const std::string& get_text() const: Retrieves the text value drawn inside the button.

  • void set_text(const string& text): Sets the text value rendered in the of the button.

Note: To render text in the button, be sure that a label have been provided through the constructor or the set_label function..

  • const std::shared_ptr<Texture>& get_texture() const: Retrieves the texture used.

  • void set_texture(const std::shared_ptr<Texture>& texture): Sets a texture for the button, customizing its appearance.

  • const std::shared_ptr<Texture>& get_blurr_on_hover() const: Retrieves the status of the blurr on hover option.

  • void set_blurr_on_hover(bool blurrOnHover): Set the option of blurring button when in focus

  • const std::function<void()>& get_function() const: Retrieves the function called when the button is pressed.

  • void set_function(const std::function<void()>& function): Set the function called when the button is pressed.

  • const std::shared_ptr<Label>& get_label() const: Retrieves the label used to display text inside the button.

  • void set_label(const std::shared_ptr<Label>& label): Set the label used to display text inside the button.

Example Usage

#include <hgui/header/Button.h>

// Example Usage of Button class
std::shared_ptr<hgui::kernel::Shader> shader = /* initialize shader */;
std::shared_ptr<hgui::kernel::Label> label = /* initialize label */;
std::function<void()> buttonFunction = /* initialize button action */;
std::shared_ptr<hgui::Texture> texture = /* initialize button texture */;

hgui::kernel::Button button(buttonFunction, shader, hgui::size(100, 40), hgui::point(200, 200), label, hgui::color(0.2f, 0.7f, 0.3f), 0.2f, texture);

// Simulate button press
button.press();

// Set the button's state
button.set_state(hgui::state::PRESS);

// Get the current button state
hgui::state buttonState = button.get_state();

// Assign a texture to the button
button.set_textures(texture);

// Draw the button
button.draw();

Note: In the "Example Usage" section, we demonstrate how to create a Button object with an action function, shader, size, position, text label, color, and corner radius settings. You can simulate a button press, change the button's state, get the current state, and assign a texture to customize the button's appearance. The draw function renders the button, and the action function is invoked when the button is pressed. This class is ideal for implementing interactive buttons within your user interface.

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