Skylicht Engine
Loading...
Searching...
No Matches
CCanvas.h
1/*
2!@
3MIT License
4
5Copyright (c) 2019 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 "Components/CComponentSystem.h"
28
29#include "Camera/CCamera.h"
30
31#include "GUI/CGUIElement.h"
32#include "GUI/CGUIImage.h"
33#include "GUI/CGUIText.h"
34#include "GUI/CGUISprite.h"
35#include "GUI/CGUIRect.h"
36#include "GUI/CGUIMask.h"
37#include "GUI/CGUILayout.h"
38#include "GUI/CGUIFitSprite.h"
39#include "GUI/CGUIElipse.h"
40
41#include "Entity/CEntityPrefab.h"
42#include "Entity/CEntityManager.h"
43
44namespace Skylicht
45{
50
78 class SKYLICHT_API CCanvas : public CComponentSystem
79 {
80 protected:
83
86
89
92
95
98
101
104
107
110
113
115 std::vector<IEntitySystem*> m_systems;
116
118 CFastArray<CEntity*> m_depth[MAX_ENTITY_DEPTH];
119
122
123 public:
126
129
130 public:
131
132 std::function<void(CGUIElement*)> OnGUILoaded;
133
134 std::function<void(CGUIText*)> OnLocalize;
135
136 public:
141
145 virtual ~CCanvas();
146
150 virtual void initComponent();
151
155 virtual void updateComponent();
156
160 virtual void onResize();
161
166
171 void render(CCamera* camera);
172
181 CGUIElement* getHitTest(CCamera* camera, float x, float y, const core::recti& viewport);
182
188 {
189 return m_entityMgr;
190 }
191
192 public:
197 inline void setSortDepth(int d)
198 {
199 m_sortDepth = d;
200 }
201
206 inline int getSortDepth()
207 {
208 return m_sortDepth;
209 }
210
215 inline void setRect(const core::rectf& r)
216 {
217 m_rect = r;
218 }
219
224 inline void setDefaultRect(const core::rectf& r)
225 {
226 m_defaultRect = r;
227 }
228
234 {
235 return m_defaultRect;
236 }
237
247 void applyGUIScale(float widthOrHeight = 1.0f);
248
253
259
265 {
266 return m_root;
267 }
268
273 inline void setEnable3DBillboard(bool b)
274 {
275 m_is3DBillboard = b;
276 }
277
283 {
284 return m_is3DBillboard;
285 }
286
291
297
304
312
313 // ---- Image ----
314
320
327
335
336 // ---- Text ----
337
344
352
360
369
370 // ---- Sprite ----
371
378
386
394
403
404 // ---- Mask ----
405
412
420
421 // ---- Rect ----
422
429
437
446
447 // ---- Elipse ----
448
455
463
472
473 // ---- Layout ----
474
480
487
495
496 // ---- Fit Sprite ----
497
504
512
520
529
530 // ---- Null element creation (for importer) ----
531
538 virtual CGUIElement* createNullElement(CGUIElement* parent, const char* type);
539
546 virtual CGUIElement* createNullElement(CGUIElement* parent, const wchar_t* type);
547
556
565
571 {
572 return m_renderCamera;
573 }
574
580 CGUIElement* getGUIByID(const char* id);
581
587 CGUIElement* getGUIByPath(const char* path);
588
594 CGUIElement* searchGUI(const char* name);
595
602 CGUIElement* getGUIByPath(CGUIElement* search, const char* path);
603
610 CGUIElement* searchGUI(CGUIElement* search, const char* name);
611
618 template<typename T>
619 std::vector<T*> getElementsInChild(bool addThis);
620
626
627 bool isClipped(CGUIElement* element);
628 };
629
630 template<typename T>
631 std::vector<T*> CCanvas::getElementsInChild(bool addThis)
632 {
633 std::vector<T*> result;
634 std::queue<CGUIElement*> queueObjs;
635
636 if (addThis == true)
637 queueObjs.push(m_root);
638 else
639 {
640 for (CGUIElement*& obj : m_root->getChilds())
641 queueObjs.push(obj);
642 }
643
644 while (queueObjs.size() != 0)
645 {
646 CGUIElement* obj = queueObjs.front();
647 queueObjs.pop();
648
649 T* type = dynamic_cast<T*>(obj);
650 if (type != NULL)
651 result.push_back(type);
652
653 for (CGUIElement*& child : obj->m_childs)
654 queueObjs.push(child);
655 }
656
657 return result;
658 }
659}
This is an object class used to set up the camera, including its position, viewing angle,...
Definition CCamera.h:70
CGUIElement * createElement(const core::rectf &r)
Create a GUI element with a specific rectangle as a child of root.
CGUIElement * searchGUI(const char *name)
Find a GUI element by name.
CGUIText * createText(const core::rectf &r, IFont *font)
Create a text element with a rectangle and font as a child of root.
void resetGUIScale()
Reset the GUI scaling to default (no scale).
CGUILayout * createLayout(const core::rectf &r)
Create a layout element with a rectangle.
CGUIFitSprite * createFitSprite(CGUIElement *e, SFrame *frame)
Create a fit sprite element with a parent and frame.
CGUIText * createText(CGUIElement *e, IFont *font)
Create a text element with a parent and font.
bool IsInEditor
True if the canvas is shown in editor mode.
Definition CCanvas.h:125
CGUIElement * getGUIByPath(CGUIElement *search, const char *path)
Find a GUI element by path starting from a specific element.
bool DrawOutline
True to draw outlines for GUI elements.
Definition CCanvas.h:128
bool m_haveScaleGUI
True if GUI scaling is applied.
Definition CCanvas.h:97
CGUIMask * createMask(CGUIElement *e, const core::rectf &r)
Create a mask element with a parent and rectangle.
CEntityPrefab * getEntityManager()
Get the entity manager.
Definition CCanvas.h:187
int getSortDepth()
Get the current sort depth value.
Definition CCanvas.h:206
virtual void initComponent()
Initialize the canvas component.
void render(CCamera *camera)
Render the GUI elements tree using the specified camera.
CGUIElement * getHitTest(CCamera *camera, float x, float y, const core::recti &viewport)
Perform hit-testing to find the GUI element under the given coordinates.
CGUIRect * createRect(CGUIElement *e, const core::rectf &r, const video::SColor &c)
Create a rectangle element with a parent, rectangle, and color.
void updateLocalizedText()
Find all CGUIText* and update localized text, the event OnLocalize must bind to the localize function...
void setRenderWorldTransform(core::matrix4 &w)
Set the world transform matrix for rendering.
Definition CCanvas.h:561
const core::matrix4 & getRenderWorldTransform()
Get the world transform matrix for rendering.
Definition CCanvas.h:552
void setRect(const core::rectf &r)
Set the canvas rectangle.
Definition CCanvas.h:215
CGUIElement * searchGUI(CGUIElement *search, const char *name)
Find a GUI element by name starting from a specific element.
CGUIElement * getRootElement()
Get the root GUI element.
Definition CCanvas.h:264
int m_sortDepth
Sorting depth for rendering.
Definition CCanvas.h:91
CGUIText * createText(CGUIElement *e, const core::rectf &r, IFont *font)
Create a text element with a parent, rectangle and font.
CGUILayout * createLayout(CGUIElement *e, const core::rectf &r)
Create a layout element with a parent and rectangle.
std::vector< IEntitySystem * > m_systems
List of entity systems for GUI logic.
Definition CCanvas.h:115
void setDefaultRect(const core::rectf &r)
Set the default rectangle value.
Definition CCanvas.h:224
void removeAllElements()
Remove all GUI elements from the canvas.
CCamera * getRenderCamera()
Get the camera currently used for rendering.
Definition CCanvas.h:570
virtual void updateComponent()
Update the canvas component.
void updateEntities()
Update internal entity lists and sorting for rendering.
virtual CGUIElement * createNullElement(CGUIElement *parent, const wchar_t *type)
Create a null GUI element by type (Unicode).
CGUISprite * createSprite(CGUIElement *e, SFrame *frame)
Create a sprite element with a parent and frame.
void setSortDepth(int d)
Set the sort depth for rendering.
Definition CCanvas.h:197
virtual CGUIElement * createNullElement(CGUIElement *parent, const char *type)
Create a null GUI element by type (UTF-8).
CGUISprite * createSprite(SFrame *frame)
Create a sprite element with a frame as a child of root.
CCanvas()
Constructor.
CGUIElement * m_root
The root GUI element of the canvas.
Definition CCanvas.h:88
CGUISprite * createSprite(const core::rectf &r, SFrame *frame)
Create a sprite element with a rectangle and frame.
CGUIFitSprite * createFitSprite(SFrame *frame)
Create a fit sprite element with a frame as a child of root.
CGUIRect * createRect(const video::SColor &c)
Create a rectangle element with a color as a child of root.
CGUIImage * createImage(CGUIElement *e, const core::rectf &r)
Create an image element with a parent and rectangle.
void applyGUIScale(float widthOrHeight=1.0f)
Apply GUI scaling based on width or height ratio.
CGUIText * createText(IFont *font)
Create a text element with a font as a child of root.
const core::rectf & getDefaultRect()
Get the default rectangle.
Definition CCanvas.h:233
CGUIElipse * createElipse(CGUIElement *e, const core::rectf &r, const video::SColor &c)
Create an elipse element with a parent, rectangle, and color.
CGUIElement * getGUIByPath(const char *path)
Find a GUI element by its path (separated by / or ).
core::rectf m_defaultRect
The default rectangle size of the canvas.
Definition CCanvas.h:85
CGUIFitSprite * createFitSprite(const core::rectf &r, SFrame *frame)
Create a fit sprite element with a rectangle and frame.
void setEnable3DBillboard(bool b)
Enable or disable 3D billboard rendering.
Definition CCanvas.h:273
CFastArray< CEntity * > m_alives
List of alive entities.
Definition CCanvas.h:121
const core::vector3df & getRootScale()
Get the root element's scale vector.
core::matrix4 m_renderWorldTransform
The world transform used for rendering.
Definition CCanvas.h:103
float m_scaleGUI
Scale factor for GUI.
Definition CCanvas.h:94
CGUIImage * createImage()
Create an image element as a child of root.
CGUIElipse * createElipse(const video::SColor &c)
Create an elipse element with a color as a child of root.
CFastArray< CEntity * > m_depth[MAX_ENTITY_DEPTH]
Entities sorted by children depth.
Definition CCanvas.h:118
CGUIMask * m_currentMask
The current mask applied during rendering.
Definition CCanvas.h:109
CGUIElement * createElement(CGUIElement *e, const core::rectf &r)
Create a GUI element with a specific parent and rectangle.
CCamera * m_renderCamera
The camera currently used for rendering.
Definition CCanvas.h:106
virtual void onResize()
Handle canvas resizing (usually called when screen size changes).
CGUILayout * createLayout()
Create a layout element as a child of root.
std::vector< T * > getElementsInChild(bool addThis)
Find all child elements of a certain type.
Definition CCanvas.h:631
core::rectf m_rect
The rectangle representing the canvas size.
Definition CCanvas.h:82
CGUIElement * createElement()
Create a generic GUI element as a child of root.
CGUIRect * createRect(const core::rectf &r, const video::SColor &c)
Create a rectangle element with a rectangle and color.
bool m_is3DBillboard
Enable 3D billboard rendering for the canvas.
Definition CCanvas.h:100
CGUIFitSprite * createFitSprite(CGUIElement *e, const core::rectf &r, SFrame *frame)
Create a fit sprite element with a parent, rectangle and frame.
CGUIMask * createMask(const core::rectf &r)
Create a mask element with a rectangle as a child of root.
CGUISprite * createSprite(CGUIElement *e, const core::rectf &r, SFrame *frame)
Create a sprite element with a parent, rectangle, and frame.
CGUIElipse * createElipse(const core::rectf &r, const video::SColor &c)
Create an elipse element with a rectangle and color.
CGUIElement * getGUIByID(const char *id)
Find a GUI element by its ID.
virtual ~CCanvas()
Destructor. Cleans up GUI elements and systems.
CGUIImage * createImage(const core::rectf &r)
Create an image element with a rectangle as a child of root.
CEntityPrefab * m_entityMgr
Entity manager for managing GUI entities.
Definition CCanvas.h:112
bool is3DBillboardEnabled()
Check if 3D billboard rendering is enabled.
Definition CCanvas.h:282
This object class is created to store data in an array of multiple CEntities.
Definition CEntityPrefab.h:38
Definition CArrayUtils.h:31
This is the base object class from which other GUIs inherit. It's an empty GUI and can contain child ...
Definition CGUIElement.h:51
This is the object class for displaying an ellipse.
Definition CGUIElipse.h:42
This is the object class for displaying a frame image in sprite. But it will scale the image size whi...
Definition CGUIFitSprite.h:49
This is the object class for displaying a texture image.
Definition CGUIImage.h:43
This is the object class that arranges other GUIs horizontally or vertically.
Definition CGUILayout.h:49
This is the object class that represents a masked rectangle, and other GUIs will only display within ...
Definition CGUIMask.h:48
This is the object class for displaying a rectangle.
Definition CGUIRect.h:42
This is the object class for displaying a frame image in sprite.
Definition CGUISprite.h:54
This is the object class for displaying a text.
Definition CGUIText.h:60
Definition IFont.h:32
Class representing a 32 bit ARGB color.
Definition SColor.h:285
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
rect< s32 > recti
Rectangle with int values.
Definition rect.h:273
CMatrix4< f32 > matrix4
Typedef for f32 matrix.
Definition matrix4.h:2241
rect< f32 > rectf
Rectangle with float values.
Definition rect.h:271
Definition CSpriteFrame.h:93