Skip to content

Commit b53fd23

Browse files
committed
#158 Add tool for Set Native Size for sprite/image
1 parent ff7c8bb commit b53fd23

File tree

11 files changed

+235
-6
lines changed

11 files changed

+235
-6
lines changed

Projects/Editor/Source/Editor/GUIEditor/CGUIElementEditor.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ namespace Skylicht
3333
GUI_EDITOR_REGISTER(CGUIElementEditor, CGUIElement);
3434
GUI_EDITOR_REGISTER(CGUIElementEditor, CGUIRect);
3535
GUI_EDITOR_REGISTER(CGUIElementEditor, CGUIMask);
36-
GUI_EDITOR_REGISTER(CGUIElementEditor, CGUIImage);
37-
GUI_EDITOR_REGISTER(CGUIElementEditor, CGUISprite);
3836
GUI_EDITOR_REGISTER(CGUIElementEditor, CGUIText);
3937
GUI_EDITOR_REGISTER(CGUIElementEditor, CGUILayout);
4038
GUI_EDITOR_REGISTER(CGUIElementEditor, CGUICustomSizeSprite);
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 "CGUINativeSizeEditor.h"
27+
#include "Editor/Space/Property/CSpaceProperty.h"
28+
#include "Editor/CEditor.h"
29+
30+
namespace Skylicht
31+
{
32+
namespace Editor
33+
{
34+
GUI_EDITOR_REGISTER(CGUINativeSizeEditor, CGUIImage);
35+
GUI_EDITOR_REGISTER(CGUINativeSizeEditor, CGUISprite);
36+
37+
CGUINativeSizeEditor::CGUINativeSizeEditor()
38+
{
39+
40+
}
41+
42+
CGUINativeSizeEditor::~CGUINativeSizeEditor()
43+
{
44+
}
45+
46+
void CGUINativeSizeEditor::initGUI(CGUIElement* gui, CSpaceProperty* ui)
47+
{
48+
CGUIElementEditor::initGUI(gui, ui);
49+
50+
GUI::CCollapsibleGroup* group = ui->addGroup("Native Size", this);
51+
GUI::CBoxLayout* layout = ui->createBoxLayout(group);
52+
ui->addButton(layout, L"Set native size")->OnPress = [&, gui](GUI::CBase* button)
53+
{
54+
core::rectf nativeRect = gui->getNativeRect();
55+
gui->setRect(nativeRect);
56+
updateProperty();
57+
};
58+
59+
ui->addButton(layout, L"Set center position")->OnPress = [&, gui](GUI::CBase* button)
60+
{
61+
core::rectf nativeRect = gui->getNativeRect();
62+
nativeRect -= core::vector2df(nativeRect.getWidth() * 0.5f, nativeRect.getHeight() * 0.5f);
63+
gui->setRect(nativeRect);
64+
updateProperty();
65+
};
66+
group->setExpand(true);
67+
}
68+
69+
void CGUINativeSizeEditor::updateProperty()
70+
{
71+
CObjectSerializable* data = getData();
72+
if (data)
73+
{
74+
CVector3Property* p = dynamic_cast<CVector3Property*>(data->getProperty("position"));
75+
p->set(m_gui->getPosition());
76+
p->OnChanged();
77+
CVector3Property* s = dynamic_cast<CVector3Property*>(data->getProperty("scale"));
78+
s->set(m_gui->getScale());
79+
s->OnChanged();
80+
CVector3Property* r = dynamic_cast<CVector3Property*>(data->getProperty("rotation"));
81+
r->set(m_gui->getRotation());
82+
r->OnChanged();
83+
84+
core::rectf rect = m_gui->getRect();
85+
86+
CFloatProperty* rx = dynamic_cast<CFloatProperty*>(data->getProperty("rectX"));
87+
rx->set(rect.UpperLeftCorner.X);
88+
CFloatProperty* ry = dynamic_cast<CFloatProperty*>(data->getProperty("rectY"));
89+
ry->set(rect.UpperLeftCorner.Y);
90+
CFloatProperty* rw = dynamic_cast<CFloatProperty*>(data->getProperty("rectW"));
91+
rw->set(rect.getWidth());
92+
CFloatProperty* rh = dynamic_cast<CFloatProperty*>(data->getProperty("rectH"));
93+
rh->set(rect.getHeight());
94+
95+
rx->OnChanged();
96+
ry->OnChanged();
97+
rw->OnChanged();
98+
rh->OnChanged();
99+
}
100+
101+
// refresh
102+
CEditor::getInstance()->refresh();
103+
}
104+
}
105+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 "CGUIEditor.h"
28+
#include "CGUIElementEditor.h"
29+
#include "Activator/CEditorActivator.h"
30+
31+
namespace Skylicht
32+
{
33+
namespace Editor
34+
{
35+
class CGUINativeSizeEditor : public CGUIElementEditor
36+
{
37+
protected:
38+
39+
public:
40+
CGUINativeSizeEditor();
41+
42+
virtual ~CGUINativeSizeEditor();
43+
44+
virtual void initGUI(CGUIElement* gui, CSpaceProperty* ui);
45+
46+
protected:
47+
void updateProperty();
48+
};
49+
}
50+
}

Projects/Editor/Source/Editor/Gizmos/GUITransform/CGUITransformGizmos.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ namespace Skylicht
226226

227227
void CGUITransformGizmos::refresh()
228228
{
229-
229+
CGUIHandles::getInstance()->end();
230+
m_selectID = "";
230231
}
231232

232233
void CGUITransformGizmos::onNotify(ISubject* subject, IObserver* from)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ namespace Skylicht
149149

150150
void CSpaceGUIDesign::refresh()
151151
{
152+
m_gizmos->refresh();
153+
152154
CGUIDesignController::getInstance()->refresh();
153155
}
154156

Projects/Skylicht/Engine/Source/Graphics2D/GUI/CGUIElement.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ namespace Skylicht
183183
m_childs.clear();
184184
}
185185

186+
const core::rectf CGUIElement::getNativeRect()
187+
{
188+
return getRect();
189+
}
190+
186191
void CGUIElement::update(CCamera* camera)
187192
{
188193

Projects/Skylicht/Engine/Source/Graphics2D/GUI/CGUIElement.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ namespace Skylicht
151151
return m_guiTransform->getRect();
152152
}
153153

154+
virtual const core::rectf getNativeRect();
155+
154156
inline int getDepth()
155157
{
156158
return m_transform->Depth;

Projects/Skylicht/Engine/Source/Graphics2D/GUI/CGUIImage.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ namespace Skylicht
6767
CGUIElement::render(camera);
6868
}
6969

70+
const core::rectf CGUIImage::getNativeRect()
71+
{
72+
core::rectf r = core::rectf(core::vector2df(0.0f, 0.0f), core::dimension2df(getWidth(), getHeight()));
73+
if (m_image != NULL)
74+
{
75+
core::dimension2du size = m_image->getSize();
76+
r.LowerRightCorner.X = r.UpperLeftCorner.X + size.Width;
77+
r.LowerRightCorner.Y = r.UpperLeftCorner.Y + size.Height;
78+
}
79+
return r;
80+
}
81+
7082
void CGUIImage::setImage(ITexture* texture)
7183
{
7284
m_image = texture;

Projects/Skylicht/Engine/Source/Graphics2D/GUI/CGUIImage.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ namespace Skylicht
4949

5050
virtual void render(CCamera* camera);
5151

52+
virtual const core::rectf getNativeRect();
53+
5254
void setImage(ITexture* texture);
5355

5456
inline ITexture* getImage()

Projects/Skylicht/Engine/Source/Graphics2D/GUI/CGUISprite.cpp

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ namespace Skylicht
3535
m_animationTime(0),
3636
m_autoRotate(false),
3737
m_frameSpeed(0.0f),
38-
m_frameRotate(0.0f)
38+
m_frameRotate(0.0f),
39+
m_isCenter(false),
40+
m_defaultOffsetX(0.0f),
41+
m_defaultOffsetY(0.0f)
3942
{
4043
m_enableMaterial = true;
4144
}
@@ -46,7 +49,10 @@ namespace Skylicht
4649
m_animationTime(0),
4750
m_autoRotate(false),
4851
m_frameSpeed(0.0f),
49-
m_frameRotate(0.0f)
52+
m_frameRotate(0.0f),
53+
m_isCenter(false),
54+
m_defaultOffsetX(0.0f),
55+
m_defaultOffsetY(0.0f)
5056
{
5157
m_enableMaterial = true;
5258
}
@@ -89,6 +95,18 @@ namespace Skylicht
8995
CGUIElement::render(camera);
9096
}
9197

98+
const core::rectf CGUISprite::getNativeRect()
99+
{
100+
core::rectf r = core::rectf(core::vector2df(0.0f, 0.0f), core::dimension2df(getWidth(), getHeight()));
101+
102+
if (m_frame != NULL)
103+
{
104+
r.LowerRightCorner.X = r.UpperLeftCorner.X + m_frame->getWidth();
105+
r.LowerRightCorner.Y = r.UpperLeftCorner.Y + m_frame->getHeight();
106+
}
107+
return r;
108+
}
109+
92110
void CGUISprite::setFrame(SFrame* frame)
93111
{
94112
m_frame = frame;
@@ -107,11 +125,28 @@ namespace Skylicht
107125
{
108126
if (m_frame)
109127
{
128+
if (!m_isCenter)
129+
{
130+
m_defaultOffsetX = m_frame->ModuleOffset[0].OffsetX;
131+
m_defaultOffsetY = m_frame->ModuleOffset[0].OffsetY;
132+
m_isCenter = true;
133+
}
134+
110135
m_frame->ModuleOffset[0].OffsetX = -((float)m_frame->BoudingRect.getWidth() * 0.5f);
111136
m_frame->ModuleOffset[0].OffsetY = -((float)m_frame->BoudingRect.getHeight() * 0.5f);
112137
}
113138
}
114139

140+
void CGUISprite::setAlignModuleDefault()
141+
{
142+
if (m_isCenter)
143+
{
144+
m_frame->ModuleOffset[0].OffsetX = m_defaultOffsetX;
145+
m_frame->ModuleOffset[0].OffsetY = m_defaultOffsetY;
146+
m_isCenter = false;
147+
}
148+
}
149+
115150
void CGUISprite::setOffsetModule(float x, float y)
116151
{
117152
if (m_frame)
@@ -129,8 +164,10 @@ namespace Skylicht
129164
frame->setGUID(m_guid.c_str());
130165
frame->setSprite(m_sprite.c_str());
131166
frame->setSpriteGUID(m_spriteId.c_str());
132-
133167
object->autoRelease(frame);
168+
169+
object->autoRelease(new CBoolProperty(object, "centerModule", m_isCenter));
170+
134171
return object;
135172
}
136173

@@ -139,6 +176,8 @@ namespace Skylicht
139176
CFrameSourceProperty* frame = dynamic_cast<CFrameSourceProperty*>(object->getProperty("spriteSrc"));
140177
CGUIElement::loadSerializable(object);
141178

179+
bool isCenter = object->get("centerModule", false);
180+
142181
if (frame != NULL)
143182
{
144183
if (m_guid != frame->getGUID())
@@ -161,5 +200,10 @@ namespace Skylicht
161200
}
162201
}
163202
}
203+
204+
if (isCenter)
205+
setAlignCenterModule();
206+
else
207+
setAlignModuleDefault();
164208
}
165209
}

Projects/Skylicht/Engine/Source/Graphics2D/GUI/CGUISprite.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ namespace Skylicht
4141
float m_frameSpeed;
4242
float m_animationTime;
4343

44+
bool m_isCenter;
45+
float m_defaultOffsetX;
46+
float m_defaultOffsetY;
47+
4448
std::string m_guid;
4549
std::string m_frameName;
4650
std::string m_sprite;
@@ -57,12 +61,16 @@ namespace Skylicht
5761

5862
virtual void render(CCamera* camera);
5963

64+
virtual const core::rectf getNativeRect();
65+
6066
void setFrame(SFrame* frame);
6167

6268
void setAutoRotate(bool rotate, float rotateAngle, float framePerSec);
6369

6470
void setAlignCenterModule();
6571

72+
void setAlignModuleDefault();
73+
6674
void setOffsetModule(float x, float y);
6775

6876
virtual CObjectSerializable* createSerializable();

0 commit comments

Comments
 (0)