Skip to content

Commit bd2e4a7

Browse files
committed
#158 Add base code for GUI Handles & GUI Gizmos
1 parent 7b49572 commit bd2e4a7

File tree

9 files changed

+485
-15
lines changed

9 files changed

+485
-15
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
!@
3+
MIT License
4+
5+
Copyright (c) 2023 Skylicht Technology CO., LTD
6+
7+
Permission 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,
9+
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19+
20+
This file is part of the "Skylicht Engine".
21+
https://github.com/skylicht-lab/skylicht-engine
22+
!#
23+
*/
24+
25+
#include "pch.h"
26+
#include "CGUIGizmos.h"
27+
#include "Handles/CGUIHandles.h"
28+
#include "Editor/SpaceController/CSceneController.h"
29+
#include "Selection/CSelection.h"
30+
31+
namespace Skylicht
32+
{
33+
namespace Editor
34+
{
35+
CGUIGizmos::CGUIGizmos()
36+
{
37+
38+
}
39+
40+
CGUIGizmos::~CGUIGizmos()
41+
{
42+
43+
}
44+
45+
void CGUIGizmos::onGizmos()
46+
{
47+
CGUIHandles* handle = CGUIHandles::getInstance();
48+
49+
CSelectObject* selectObject = CSelection::getInstance()->getLastSelected();
50+
if (selectObject == NULL)
51+
{
52+
handle->end();
53+
return;
54+
}
55+
56+
if (selectObject->getID() != m_selectID)
57+
{
58+
m_selectID = selectObject->getID();
59+
60+
CSceneController* sceneController = CSceneController::getInstance();
61+
CScene* scene = sceneController->getScene();
62+
63+
if (selectObject->getType() == CSelectObject::GUIElement)
64+
{
65+
// Show GUI Property
66+
CGameObject* guiCanvas = scene->searchObjectInChild(L"GUICanvas");
67+
CCanvas* canvas = guiCanvas->getComponent<CCanvas>();
68+
CGUIElement* gui = canvas->getGUIByID(selectObject->getID().c_str());
69+
if (gui)
70+
{
71+
onEnable();
72+
}
73+
}
74+
75+
// handle->setWorld(m_parentWorld);
76+
handle->end();
77+
}
78+
}
79+
80+
void CGUIGizmos::onEnable()
81+
{
82+
83+
}
84+
85+
void CGUIGizmos::onRemove()
86+
{
87+
88+
}
89+
90+
void CGUIGizmos::refresh()
91+
{
92+
93+
}
94+
}
95+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
!@
3+
MIT License
4+
5+
Copyright (c) 2023 Skylicht Technology CO., LTD
6+
7+
Permission 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,
9+
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19+
20+
This file is part of the "Skylicht Engine".
21+
https://github.com/skylicht-lab/skylicht-engine
22+
!#
23+
*/
24+
25+
#pragma once
26+
27+
#include "Reactive/CSubject.h"
28+
29+
namespace Skylicht
30+
{
31+
namespace Editor
32+
{
33+
class CGUIGizmos
34+
{
35+
protected:
36+
std::string m_selectID;
37+
38+
public:
39+
CGUIGizmos();
40+
41+
virtual ~CGUIGizmos();
42+
43+
virtual void onGizmos();
44+
45+
virtual void onEnable();
46+
47+
virtual void onRemove();
48+
49+
virtual void refresh();
50+
};
51+
}
52+
}

Projects/Editor/Source/Editor/Space/GUIDesign/CSpaceGUIDesign.cpp

Lines changed: 87 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ This file is part of the "Skylicht Engine".
2929
#include "Editor/SpaceController/CSceneController.h"
3030
#include "Editor/SpaceController/CGUIDesignController.h"
3131

32+
#include "Handles/CGUIHandles.h"
33+
34+
using namespace std::placeholders;
35+
3236
namespace Skylicht
3337
{
3438
namespace Editor
@@ -46,6 +50,8 @@ namespace Skylicht
4650
m_pressY(0.0f),
4751
m_mouseGUIX(0.0f),
4852
m_mouseGUIY(0.0f),
53+
m_leftMouseDown(false),
54+
m_rightMouseDown(false),
4955
m_middleDrag(false),
5056
m_scene(NULL),
5157
m_guiCamera(NULL)
@@ -99,40 +105,63 @@ namespace Skylicht
99105
m_view->dock(GUI::EPosition::Fill);
100106
m_view->enableRenderFillRect(true);
101107
m_view->setFillRectColor(GUI::CThemeConfig::WindowBackgroundColor);
102-
m_view->OnMouseWheeled = std::bind(&CSpaceGUIDesign::onMouseWheel, this,
103-
std::placeholders::_1,
104-
std::placeholders::_2);
105-
m_view->OnMouseMoved = std::bind(&CSpaceGUIDesign::onMouseMoved, this,
106-
std::placeholders::_1,
107-
std::placeholders::_2,
108-
std::placeholders::_3,
109-
std::placeholders::_4,
110-
std::placeholders::_5);
111-
m_view->OnMiddleMouseClick = std::bind(&CSpaceGUIDesign::onMiddleMouseClick, this,
112-
std::placeholders::_1,
113-
std::placeholders::_2,
114-
std::placeholders::_3,
115-
std::placeholders::_4);
108+
109+
m_view->OnMouseWheeled = std::bind(&CSpaceGUIDesign::onMouseWheel, this, _1, _2);
110+
m_view->OnMouseMoved = std::bind(&CSpaceGUIDesign::onMouseMoved, this, _1, _2, _3, _4, _5);
111+
m_view->OnMiddleMouseClick = std::bind(&CSpaceGUIDesign::onMiddleMouseClick, this, _1, _2, _3, _4);
112+
m_view->OnLeftMouseClick = std::bind(&CSpaceGUIDesign::onLeftMouseClick, this, _1, _2, _3, _4);
113+
m_view->OnRightMouseClick = std::bind(&CSpaceGUIDesign::onRightMouseClick, this, _1, _2, _3, _4);
114+
m_view->OnKeyPress = std::bind(&CSpaceGUIDesign::onKeyPressed, this, _1, _2, _3);
115+
116116
m_view->OnRender = BIND_LISTENER(&CSpaceGUIDesign::onRender, this);
117117

118+
m_gizmos = new CGUIGizmos();
119+
m_handlesRenderer = new CGUIHandlesRenderer();
120+
118121
CGUIDesignController::getInstance()->setSpaceDesign(this);
119122
}
120123

121124
CSpaceGUIDesign::~CSpaceGUIDesign()
122125
{
126+
delete m_handlesRenderer;
127+
delete m_gizmos;
128+
123129
CGUIDesignController* controller = CGUIDesignController::getInstance();
124130
if (controller->getSpaceDesign() == this)
125131
controller->setSpaceDesign(NULL);
126132
}
127133

134+
void CSpaceGUIDesign::update()
135+
{
136+
CSpace::update();
137+
m_gizmos->onGizmos();
138+
}
139+
128140
void CSpaceGUIDesign::openGUI(const char* path)
129141
{
130142
if (m_scene)
131143
{
132144
CGameObject* canvasObj = m_scene->searchObjectInChild(L"GUICanvas");
145+
m_gizmos->onRemove();
133146
}
134147
}
135148

149+
void CSpaceGUIDesign::onKeyPressed(GUI::CBase* base, int key, bool down)
150+
{
151+
GUI::CInput* input = GUI::CInput::getInput();
152+
153+
SEvent event;
154+
event.EventType = EET_KEY_INPUT_EVENT;
155+
event.KeyInput.Key = (irr::EKEY_CODE)key;
156+
event.KeyInput.PressedDown = down;
157+
event.KeyInput.Control = input->IsControlDown();
158+
event.KeyInput.Shift = input->IsShiftDown();
159+
160+
event.GameEvent.Sender = m_scene;
161+
162+
CGUIHandles::getInstance()->OnEvent(event);
163+
}
164+
136165
void CSpaceGUIDesign::onMiddleMouseClick(GUI::CBase* view, float x, float y, bool down)
137166
{
138167
m_middleDrag = down;
@@ -197,6 +226,8 @@ namespace Skylicht
197226

198227
m_topRuler->setCursorPosition(mx);
199228
m_leftRuler->setCursorPosition(my);
229+
230+
postMouseEventToHandles(EMIE_MOUSE_MOVED);
200231
}
201232

202233
void CSpaceGUIDesign::onMouseWheel(GUI::CBase* scroll, int delta)
@@ -225,6 +256,45 @@ namespace Skylicht
225256
}
226257
}
227258

259+
void CSpaceGUIDesign::onLeftMouseClick(GUI::CBase* base, float x, float y, bool down)
260+
{
261+
m_leftMouseDown = down;
262+
if (down)
263+
postMouseEventToHandles(EMIE_LMOUSE_PRESSED_DOWN);
264+
else
265+
postMouseEventToHandles(EMIE_LMOUSE_LEFT_UP);
266+
}
267+
268+
void CSpaceGUIDesign::onRightMouseClick(GUI::CBase* base, float x, float y, bool down)
269+
{
270+
m_rightMouseDown = down;
271+
if (down)
272+
postMouseEventToHandles(EMIE_RMOUSE_PRESSED_DOWN);
273+
else
274+
postMouseEventToHandles(EMIE_RMOUSE_LEFT_UP);
275+
}
276+
277+
void CSpaceGUIDesign::postMouseEventToHandles(EMOUSE_INPUT_EVENT eventType)
278+
{
279+
SEvent event;
280+
event.EventType = EET_MOUSE_INPUT_EVENT;
281+
event.MouseInput.Event = eventType;
282+
event.MouseInput.X = (int)m_mouseGUIX;
283+
event.MouseInput.Y = (int)m_mouseGUIY;
284+
event.MouseInput.Wheel = 0.0f;
285+
event.MouseInput.ButtonStates = 0;
286+
287+
if (m_leftMouseDown)
288+
event.MouseInput.ButtonStates |= EMBSM_LEFT;
289+
290+
if (m_rightMouseDown)
291+
event.MouseInput.ButtonStates |= EMBSM_RIGHT;
292+
293+
event.GameEvent.Sender = m_scene;
294+
295+
CGUIHandles::getInstance()->OnEvent(event);
296+
}
297+
228298
void CSpaceGUIDesign::onZoomIn(GUI::CBase* base)
229299
{
230300
doZoomIn(0.0f, 0.0f);
@@ -347,6 +417,9 @@ namespace Skylicht
347417

348418
// render GUI
349419
CGraphics2D::getInstance()->render(m_guiCamera);
420+
421+
// render Gizmos
422+
m_handlesRenderer->render(m_guiCamera, m_guiScale);
350423
}
351424
}
352425

Projects/Editor/Source/Editor/Space/GUIDesign/CSpaceGUIDesign.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ This file is part of the "Skylicht Engine".
2626

2727
#include "SkylichtEngine.h"
2828
#include "Editor/Space/CSpace.h"
29+
#include "EditorComponents/Handles/CGUIHandlesRenderer.h"
30+
#include "Editor/Gizmos/CGUIGizmos.h"
2931

3032
namespace Skylicht
3133
{
@@ -53,36 +55,51 @@ namespace Skylicht
5355

5456
float m_mouseGUIX;
5557
float m_mouseGUIY;
58+
bool m_leftMouseDown;
59+
bool m_rightMouseDown;
5660

5761
bool m_middleDrag;
5862

5963
CScene* m_scene;
6064
CCamera* m_guiCamera;
6165

66+
CGUIHandlesRenderer* m_handlesRenderer;
67+
CGUIGizmos* m_gizmos;
68+
6269
public:
6370
CSpaceGUIDesign(GUI::CWindow* window, CEditor* editor);
6471

6572
virtual ~CSpaceGUIDesign();
6673

74+
virtual void update();
75+
6776
void openGUI(const char* path);
6877

6978
protected:
7079

80+
void onKeyPressed(GUI::CBase* base, int key, bool down);
81+
7182
void onMiddleMouseClick(GUI::CBase* view, float x, float y, bool down);
7283

7384
void onMouseMoved(GUI::CBase* view, float x, float y, float deltaX, float deltaY);
7485

7586
void onMouseWheel(GUI::CBase* view, int delta);
7687

88+
void onLeftMouseClick(GUI::CBase* base, float x, float y, bool down);
89+
90+
void onRightMouseClick(GUI::CBase* base, float x, float y, bool down);
91+
7792
void onZoomIn(GUI::CBase* base);
7893

7994
void onZoomOut(GUI::CBase* base);
8095

8196
void doZoomIn(float dx, float dy);
8297

83-
void doZoomOut(float dx, float dy);
98+
void doZoomOut(float dx, float dy);
8499

85100
void onRender(GUI::CBase* base);
101+
102+
void postMouseEventToHandles(EMOUSE_INPUT_EVENT eventType);
86103
};
87104
}
88105
}

0 commit comments

Comments
 (0)