Capycore Engine  0.1.0
A simple and lightweight game engine written in C++ based on the Unity API architecture.
Loading...
Searching...
No Matches
ui_interactable.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <functional>
6#include <vector>
7
14 bool hovered = false;
15 bool pressed = false;
16 bool held = false;
17 bool focused = false;
18 bool disabled = false;
19};
20
28class UIInteractable : public UIObject {
29 public:
32
40
43
47 void disable();
51 void enable();
52
56 virtual void on_hover() {}
57
61 virtual void on_unhover() {}
62
66 virtual void on_press() {}
67
71 virtual void on_release() {}
72
76 virtual void on_hold() {}
77
81 virtual void on_focus() {}
82
86 virtual void on_unfocus() {}
87
88 void add_on_hover(const std::function<void(UIInteractable&)>& handler);
89 void add_on_unhover(const std::function<void(UIInteractable&)>& handler);
90
91 void add_on_press(const std::function<void(UIInteractable&)>& handler);
92 void add_on_release(const std::function<void(UIInteractable&)>& handler);
93
94 void add_on_focus(const std::function<void(UIInteractable&)>& handler);
95 void add_on_unfocus(const std::function<void(UIInteractable&)>& handler);
96
97 protected:
98 [[nodiscard]] bool in_range(const Point& mouse_pos) const;
99
101
102 std::vector<std::function<void(UIInteractable&)>> on_hover_handlers_;
103 std::vector<std::function<void(UIInteractable&)>> on_unhover_handlers_;
104
105 std::vector<std::function<void(UIInteractable&)>> on_press_handlers_;
106 std::vector<std::function<void(UIInteractable&)>> on_release_handlers_;
107
108 std::vector<std::function<void(UIInteractable&)>> on_focus_handlers_;
109 std::vector<std::function<void(UIInteractable&)>> on_unfocus_handlers_;
110};
std::optional< std::reference_wrapper< T > > get_component() const noexcept
Get the first component of type T attached to this GameObject.
Definition gameObject.h:141
Scene & scene() const noexcept
Represents a point in 2D space.
Definition point.h:6
Represents a scene within the engine. A scene contains multiple GameObjects and manages their lifecyc...
Definition scene.h:19
Base class for interactive UI elements.
Definition ui_interactable.h:28
virtual void on_hover()
Event hook for when the element is hovered.
Definition ui_interactable.h:56
std::vector< std::function< void(UIInteractable &)> > on_hover_handlers_
Definition ui_interactable.h:102
std::vector< std::function< void(UIInteractable &)> > on_unhover_handlers_
Definition ui_interactable.h:103
virtual void on_unfocus()
Event hook for when the element loses focus.
Definition ui_interactable.h:86
virtual void on_unhover()
Event hook for when the element is unhovered.
Definition ui_interactable.h:61
void add_on_release(const std::function< void(UIInteractable &)> &handler)
void add_on_unhover(const std::function< void(UIInteractable &)> &handler)
UIInteractionState & state()
void add_on_press(const std::function< void(UIInteractable &)> &handler)
std::vector< std::function< void(UIInteractable &)> > on_press_handlers_
Definition ui_interactable.h:105
std::vector< std::function< void(UIInteractable &)> > on_release_handlers_
Definition ui_interactable.h:106
void add_on_focus(const std::function< void(UIInteractable &)> &handler)
UIInteractionState interaction_state_
Definition ui_interactable.h:100
UIInteractable(Scene &scene, float width, float height, Point pivot, Point anchor)
void update_interaction(float dt)
Update interaction state based on input.
void disable()
Disable the interactable element.
void enable()
Enable the interactable element.
const UIInteractionState & state() const
virtual void on_hold()
Event hook for when the element is held down.
Definition ui_interactable.h:76
std::vector< std::function< void(UIInteractable &)> > on_unfocus_handlers_
Definition ui_interactable.h:109
virtual void on_focus()
Event hook for when the element gains focus.
Definition ui_interactable.h:81
virtual void on_press()
Event hook for when the element is pressed.
Definition ui_interactable.h:66
std::vector< std::function< void(UIInteractable &)> > on_focus_handlers_
Definition ui_interactable.h:108
void add_on_hover(const std::function< void(UIInteractable &)> &handler)
virtual void on_release()
Event hook for when the element is released.
Definition ui_interactable.h:71
void add_on_unfocus(const std::function< void(UIInteractable &)> &handler)
bool in_range(const Point &mouse_pos) const
Base class for all UI objects.
Definition ui_object.h:16
Point pivot() const
float height() const
Point anchor() const
float width() const
Interaction state for UIInteractable elements.
Definition ui_interactable.h:13
bool held
Definition ui_interactable.h:16
bool pressed
Definition ui_interactable.h:15
bool focused
Definition ui_interactable.h:17
bool disabled
Definition ui_interactable.h:18
bool hovered
Definition ui_interactable.h:14