Skylicht Engine
Loading...
Searching...
No Matches
CUIBase.h
1/*
2!@
3MIT License
4
5Copyright (c) 2023 Skylicht Technology CO., LTD
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
8(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
9merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20This file is part of the "Skylicht Engine".
21https://github.com/skylicht-lab/skylicht-engine
22!#
23*/
24
25#pragma once
26
27#include "Graphics2D/GUI/CGUIElement.h"
28#include "Graphics2D/CCanvas.h"
29
30#include "Motion/CMotion.h"
31#include "Motion/CColorMotion.h"
32#include "Motion/CAlphaMotion.h"
33#include "Motion/CPositionMotion.h"
34#include "Motion/CScaleMotion.h"
35#include "Motion/CRotationMotion.h"
36#include "Motion/CFrameMotion.h"
37#include "Motion/CVisibleMotion.h"
38
39namespace Skylicht
40{
42 namespace UI
43 {
44 class CUIContainer;
45
62 class CUIBase
63 {
64 protected:
65 CUIContainer* m_container;
66 CGUIElement* m_element;
67
68 core::vector3df m_rectTransform[4];
69
70 std::vector<CMotion*> m_motions[(int)EMotionEvent::NumEvent];
71
72 bool m_enable;
73 bool m_visible;
74
75 bool m_isPointerHover;
76 bool m_isPointerDown;
77
78 bool m_skipPointerEventWhenDrag;
79 bool m_continueGameEvent;
80 bool m_continueKeyEvent;
81
82 int m_pointerId;
83 float m_pointerDownX;
84 float m_pointerDownY;
85
86 int m_soundId[4];
87
88 void* m_userData;
89
90 public:
91 std::function<void(int, float, float)> OnPointerHover;
92 std::function<void(int, float, float)> OnPointerOut;
93 std::function<void(int, float, float)> OnPointerDown;
94 std::function<void(int, float, float)> OnPointerUp;
95 std::function<void(int, float, float, bool)> OnPointerMove;
96
97 std::function<void(CUIBase*)> OnPressed;
98 std::function<void(CUIBase*)> OnFocus;
99 std::function<void(CUIBase*)> OnLostFocus;
100 std::function<void(CUIBase*, const SEvent&)> OnKeyEvent;
101
102 std::function<void(CUIBase*, EMotionEvent)> OnMotionFinish[(int)EMotionEvent::NumEvent];
103
104 public:
110 CUIBase(CUIContainer* container, CGUIElement* element);
111
113 virtual ~CUIBase();
114
120 void remove();
121
122 virtual void update();
123
128 virtual void setEnable(bool b);
129
134 virtual void setVisible(bool b);
135
137 inline bool isEnable()
138 {
139 return m_enable;
140 }
141
143 inline bool isVisible()
144 {
145 return m_visible;
146 }
147
149 inline int getPointerId()
150 {
151 return m_pointerId;
152 }
153
155 inline void setPointerId(int id)
156 {
157 m_pointerId = id;
158 }
159
162
167 inline void setContinueGameEvent(bool b)
168 {
169 m_continueGameEvent = b;
170 }
171
174 {
175 return m_continueGameEvent;
176 }
177
178 inline bool isContinueKeyEvent()
179 {
180 return m_continueKeyEvent;
181 }
182
187 inline void setSkipPointerEventWhenDrag(bool b)
188 {
189 m_skipPointerEventWhenDrag = b;
190 }
191
193 inline bool isPointerHover()
194 {
195 return m_isPointerHover;
196 }
197
199 inline bool isPointerDown()
200 {
201 return m_isPointerDown;
202 }
203
206 {
207 return m_element;
208 }
209
214 {
215 if (m_element)
216 return m_element->getCanvas();
217 return NULL;
218 }
219
222 {
223 return m_container;
224 }
225
228
230
237 virtual void onPointerHover(int pointerId, float pointerX, float pointerY);
238
245 virtual void onPointerOut(int pointerId, float pointerX, float pointerY);
246
253 virtual void onPointerDown(int pointerId, float pointerX, float pointerY);
254
261 virtual void onPointerUp(int pointerId, float pointerX, float pointerY);
262
269 virtual void onPointerMove(int pointerId, float pointerX, float pointerY);
270
275 virtual void onPressed();
276
278 virtual void onFocus();
279
281 virtual void onLostFocus();
282
287 virtual void onKeyEvent(const SEvent& event);
288
293 void startMotion(EMotionEvent event);
294
298 void stopMotion(EMotionEvent event);
299
305 bool isMotionPlaying(EMotionEvent event);
306
314 virtual CMotion* addMotion(EMotionEvent event, CMotion* motion);
315
323 virtual CMotion* addMotion(EMotionEvent event, CGUIElement* element, CMotion* motion);
324
331 bool removeMotion(EMotionEvent event, CMotion* motion);
332
337 void removeMotions(EMotionEvent event);
338
344 std::vector<CMotion*>& getMotions(EMotionEvent event)
345 {
346 return m_motions[(int)event];
347 }
348
354 void convertToUICoordinate(float& pointerX, float& pointerY);
355
362 void convertWorldToLocal(CGUIElement* element, float& x, float& y);
363
370 void convertLocalToWorld(CGUIElement* element, float& x, float& y);
371
385 virtual bool acceptDragFocus()
386 {
387 return false;
388 }
389
399 inline void setSoundId(int slot, int soundId)
400 {
401 m_soundId[slot] = soundId;
402 }
403
409 inline int getSoundId(int slot)
410 {
411 return m_soundId[slot];
412 }
413
423 inline void setUserData(void* data)
424 {
425 m_userData = data;
426 }
427
434 {
435 return m_userData;
436 }
437 };
438 }
439}
This class manages GUI components, including creating and deleting images and sprites.
Definition CCanvas.h:79
This is the base object class from which other GUIs inherit. It's an empty GUI and can contain child ...
Definition CGUIElement.h:51
Definition CMotion.h:49
virtual CMotion * addMotion(EMotionEvent event, CGUIElement *element, CMotion *motion)
Add a CMotion instance bound to a specific CGUIElement for the given event.
void convertToUICoordinate(float &pointerX, float &pointerY)
Convert screen pointer coordinates to this UI object's local UI coordinates.
virtual void onPointerDown(int pointerId, float pointerX, float pointerY)
Called when pointer is pressed down on the element.
virtual void onPressed()
Called when the element is considered "pressed" (high-level event). Subclasses may trigger OnPressed ...
virtual ~CUIBase()
Virtual destructor.
virtual void setVisible(bool b)
Set visibility of this UI object.
int getSoundId(int slot)
Get the sound id associated with this UI object.
Definition CUIBase.h:409
CUIBase(CUIContainer *container, CGUIElement *element)
Constructor.
virtual void onPointerMove(int pointerId, float pointerX, float pointerY)
Called when pointer moves while over or dragging the element.
virtual bool acceptDragFocus()
Whether this UI element should accept drag-focus when the pointer is already down and the user drags ...
Definition CUIBase.h:385
CUIContainer * getContainer()
Get parent container.
Definition CUIBase.h:221
void removeMotions(EMotionEvent event)
Remove all motions attached to the specified event.
void convertWorldToLocal(CGUIElement *element, float &x, float &y)
Convert world coordinates to the local coordinate space of a CGUIElement.
void remove()
Remove this UI object from its container and perform cleanup.
void startMotion(EMotionEvent event)
Start motions associated with a specific motion event.
bool isVisible()
Returns true if the object is visible.
Definition CUIBase.h:143
virtual void onFocus()
Called when element receives focus.
void resetTouch()
Reset pointer internal state (hover/down).
void setSoundId(int slot, int soundId)
Set the sound id associated with this UI object.
Definition CUIBase.h:399
virtual void onLostFocus()
Called when element loses focus.
int getPointerId()
Get the pointer ID associated with this UI object.
Definition CUIBase.h:149
void convertLocalToWorld(CGUIElement *element, float &x, float &y)
Convert local coordinates of a CGUIElement to world coordinates.
core::vector3df * getRectTransform()
Returns a pointer to the internal rect transform (4 corners).
virtual void onPointerHover(int pointerId, float pointerX, float pointerY)
Called when pointer enters or hovers over the element.
bool isMotionPlaying(EMotionEvent event)
Query whether any motion is currently playing for the given event.
bool isEnable()
Returns true if the object is enabled.
Definition CUIBase.h:137
void stopMotion(EMotionEvent event)
Stop motions associated with a specific motion event.
std::vector< CMotion * > & getMotions(EMotionEvent event)
Access the motion vector for a specific event.
Definition CUIBase.h:344
bool removeMotion(EMotionEvent event, CMotion *motion)
Remove a motion instance from the given event list.
void setSkipPointerEventWhenDrag(bool b)
When true pointer events will be skipped while dragging.
Definition CUIBase.h:187
bool isPointerHover()
Returns true if pointer is hovering this object.
Definition CUIBase.h:193
CGUIElement * getElement()
Get the underlying CGUIElement pointer.
Definition CUIBase.h:205
void setUserData(void *data)
Attach an application-defined pointer to this UI object.
Definition CUIBase.h:423
virtual void setEnable(bool b)
Enable or disable this UI object.
bool isPointerDown()
Returns true if pointer is down on this object.
Definition CUIBase.h:199
void setPointerId(int id)
Set the pointer ID for this UI object.
Definition CUIBase.h:155
CCanvas * getCanvas()
Retrieve the canvas that the underlying element belongs to.
Definition CUIBase.h:213
virtual void onPointerUp(int pointerId, float pointerX, float pointerY)
Called when pointer is released over the element.
void setContinueGameEvent(bool b)
Control whether input events continue to the game layer.
Definition CUIBase.h:167
virtual void onPointerOut(int pointerId, float pointerX, float pointerY)
Called when pointer leaves the element.
void * getUserData()
Retrieve the pointer previously set via setUserData.
Definition CUIBase.h:433
virtual void onKeyEvent(const SEvent &event)
Called when a keyboard event is delivered to this element.
virtual CMotion * addMotion(EMotionEvent event, CMotion *motion)
Add a CMotion instance to the list for the given event. The motion will be initialized and managed by...
bool isContinueGameEvent()
Returns true when events are allowed to continue to the game layer.
Definition CUIBase.h:173
Top-level UI container and event router.
Definition CUIContainer.h:59
Classes used to build the User Interface (UI).
Definition CAlphaMotion.h:32
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29
vector3d< f32 > vector3df
Typedef for a f32 3d vector.
Definition vector3d.h:445
SEvents hold information about an event. See irr::IEventReceiver for details on event handling.
Definition IEventReceiver.h:273