Widget - Horizon-NTH/HorizonGUI GitHub Wiki
The Widget
class in the hgui::kernel
namespace is a fundamental component designed for
creating interactive graphical widgets in your application. It serves as a base class for various UI elements,
providing features for rendering, input handling, and interactivity.
The class can be found in the header file Widget.h
.
-
Widget(const std::shared_ptr<kernel::Shader>& shader, const size& size, const point& position)
: Constructs aWidget
object with the specified rendering shader, size and a position. -
virtual ~Widget()
: Destructs theWidget
, releasing associated resources.
-
const point& get_position() const
: Retrieves the position of the widget. -
const size& get_size() const
: Retrieves the size of the widget. -
virtual void set_position(const point& newPosition)
: Sets the position of the widget to the specifiednewPosition
. -
virtual void set_size(const size& newSize)
: Sets the size of the widget to the specifiednewSize
. -
void bind(const std::variant<inputs, std::pair<buttons, actions>, std::tuple<inputs, buttons, actions>& action, const std::function<void()>& function)
: Binds an input or an action to a user-defined function, allowing interaction with the widget. -
bool is_bind(const std::variant<inputs, std::pair<buttons, actions>, std::tuple<inputs, buttons, actions>>& action)
: Check if an input or an action is already bind to the widget. -
void unbind(const std::variant<inputs, std::pair<buttons, actions>, std::tuple<inputs, buttons, actions>& action)
: Unbinds a previously bound input or an action from the widget. -
virtual void draw() const = 0
: A pure virtual function that must be implemented by derived widget classes to define the rendering behavior. -
virtual bool is_inside(const point& point) const = 0
: Indicates whether the given point is over the widget. A pure virtual function that must be implemented by derived widget classes to define the inputs behavior.
-
void bind(const std::variant<std::shared_ptr<Widget>, std::string, std::vector<std::string>>& widgets, const std::variant<inputs, std::pair<buttons, actions>, std::tuple<inputs, buttons, actions>>& action, const std::function<void()>& function)
: Binds an input or an action to a specific widget, a group of widgets, or widgets based on their tags. -
bool is_bind(const std::shared_ptr<Widget>& widget, const std::variant<inputs, std::pair<buttons, actions>, std::tuple<inputs, buttons, actions>>& action)
: Check if an input or an action is already bind to a specific widget. -
void unbind(const std::variant<std::shared_ptr<Widget>, std::string, std::vector<std::string>>& widgets, const std::variant<inputs, std. pair<buttons, actions>, std::tuple<inputs, buttons, actions>& action)
: Unbinds a previously bound input or an action from one or more widgets. -
void active(const std::vector<std::string>& tags = {})
: Activates widgets based on their tags, allowing you to specify which group of widgets should be active. -
const std::vector<std::string>& get_active_tag()
: Retrieve all tags that are actually active. -
const std::vector<std::weak_ptr<Widget>>& get_widgets(const std::string& tag)
: Return all widgets asstd::weak_ptr
that are associated to the given tag.
Note: If a widget is not activated, all the binds for this widgets will not be processed.
#include <hgui/header/Widget.h>
// Create a custom widget class that derives from Widget
class MyCustomWidget : public hgui::kernel::Widget {
public:
MyCustomWidget(const std::shared_ptr<hgui::kernel::Shader>& shader, const hgui::size& size, const hgui::point& position, HGUI_PRECISION rotation)
: hgui::kernel::Widget(shader, size, position, rotation)
{
}
void draw() const override
{
// Implement custom rendering behavior
// This function will be called for rendering the widget
}
bool is_inside(const point& point) const override
{
// Implement custom binds behavior
// This function will be called at each bind.
}
};
int main() {
// Create a shared pointer to a custom shader
std::shared_ptr<hgui::kernel::Shader> customShader = /* initialize custom shader */;
// Create a custom widget
std::shared_ptr<MyCustomWidget> customWidget = std::make_shared<MyCustomWidget>(customShader, hgui::size(100, 100), hgui::point(0, 0), 0.f);
// Bind an input action to the custom widget
customWidget->bind(hgui::inputs::OVER, [] {
// Handle when the mouse is over the widget
});
// Unbind an input action from the custom widget
customWidget->unbind(hgui::inputs::MOUSE_BUTTON_LEFT);
// Set active widgets based on tags
hgui::kernel::Widget::active({ /* current used tag */ });
return 0;
}
Note: In the "Example Usage" section, we show how to create a custom widget class, instantiate widgets, and unbind input actions, and activate widgets based on tags using the
Widget
class. Widget objects are the building blocks for creating interactive graphical elements in your application.