Skip to content

Commit

Permalink
#6 Add Ruler for space GUI Design
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Oct 18, 2021
1 parent 132869e commit 7ef7930
Show file tree
Hide file tree
Showing 11 changed files with 408 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Projects/Editor/Source/Editor/CEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ This file is part of the "Skylicht Engine".
#include "Space/Hierarchy/CSpaceHierarchy.h"
#include "Space/LoadScene/CSpaceLoadScene.h"
#include "Space/ProjectSettings/CSpaceProjectSettings.h"
#include "Space/GUIDesign/CSpaceGUIDesign.h"

#include "SpaceController/CSceneController.h"
#include "SpaceController/CPropertyController.h"
Expand Down Expand Up @@ -479,7 +480,7 @@ namespace Skylicht
}
else if (workspace == L"GUI Design")
{

m_workspaces.push_back(new CSpaceGUIDesign(window, this));
}
else if (workspace == L"Animation")
{
Expand Down
51 changes: 51 additions & 0 deletions Projects/Editor/Source/Editor/Space/GUIDesign/CSpaceGUIDesign.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
!@
MIT License
Copyright (c) 2020 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the Rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#include "pch.h"
#include "CSpaceGUIDesign.h"

namespace Skylicht
{
namespace Editor
{
CSpaceGUIDesign::CSpaceGUIDesign(GUI::CWindow* window, CEditor* editor) :
CSpace(window, editor)
{
m_leftRuler = new GUI::CRulerBar(window, false);
m_leftRuler->setHeight(100.0f);
m_leftRuler->setBeginOffset(40.0f);

m_topRuler = new GUI::CRulerBar(window, true);
m_topRuler->setWidth(200.0f);

m_leftRuler->setPosition(0.0f);
m_topRuler->setPosition(0.0f);
}

CSpaceGUIDesign::~CSpaceGUIDesign()
{

}
}
}
46 changes: 46 additions & 0 deletions Projects/Editor/Source/Editor/Space/GUIDesign/CSpaceGUIDesign.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
!@
MIT License
Copyright (c) 2020 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the Rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#pragma once

#include "SkylichtEngine.h"
#include "Editor/Space/CSpace.h"

namespace Skylicht
{
namespace Editor
{
class CSpaceGUIDesign : public CSpace
{
protected:
GUI::CRulerBar* m_topRuler;
GUI::CRulerBar* m_leftRuler;

public:
CSpaceGUIDesign(GUI::CWindow* window, CEditor* editor);

virtual ~CSpaceGUIDesign();
};
}
}
170 changes: 170 additions & 0 deletions Projects/Editor/Source/GUI/Controls/CRulerBar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
!@
MIT License
Copyright (c) 2020 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#include "pch.h"
#include "CRulerBar.h"
#include "GUI/Theme/CThemeConfig.h"

namespace Skylicht
{
namespace Editor
{
namespace GUI
{
CRulerBar::CRulerBar(CBase* parent, bool horizontal) :
CBase(parent),
m_isHorizontal(horizontal),
m_beginOffset(0.0f),
m_position(0.0f),
m_pixelPerUnit(5.0f),
m_textPerUnit(10)
{
if (m_isHorizontal)
{
setHeight(40.0f);
dock(EPosition::Top);
}
else
{
setWidth(40.0f);
dock(EPosition::Left);
}

enableRenderFillRect(true);
setFillRectColor(CThemeConfig::RulerBG);
}

CRulerBar::~CRulerBar()
{

}

void CRulerBar::render()
{
CBase::render();

CRenderer* renderer = CRenderer::getRenderer();

float size = 0.0f;
float size1 = 5.0f; // 1
float size2 = 10.0f; // 2
float size3 = 15.0f; // 5
float size4 = 30.0f; // 10

int unit = (int)(m_position / m_pixelPerUnit);
float offset = m_position - unit * m_pixelPerUnit;

SGUIColor& lineColor = CThemeConfig::RulerLine1;
wchar_t textValue[64];

if (m_isHorizontal)
{
float x = m_beginOffset - offset;
float w = width();

while (x < w)
{
size = size1;
lineColor = CThemeConfig::RulerLine1;

if (unit % 2 == 0)
{
size = size2;
lineColor = CThemeConfig::RulerLine1;
}
if (unit % 5 == 0)
{
size = size3;
lineColor = CThemeConfig::RulerLine2;
}
if (unit % 10 == 0)
{
size = size4;
lineColor = CThemeConfig::RulerLine3;
}

renderer->drawLineY(x, 0.0f, size, lineColor);

if (unit % m_textPerUnit == 0)
{
SRect r;
r.X = x + 2.0f;
r.Y = 15.0f;
r.Width = 9.0f * m_pixelPerUnit;
r.Height = 20.0f;
swprintf(textValue, 64, L"%d", unit);
renderer->renderText(r, SizeNormal, CThemeConfig::DefaultTextColor, std::wstring(textValue));
}

x = x + m_pixelPerUnit;
unit++;
}
}
else
{
float y = m_beginOffset - offset;
float h = height();

while (y < h)
{
size = size1;
lineColor = CThemeConfig::RulerLine1;

if (unit % 2 == 0)
{
size = size2;
lineColor = CThemeConfig::RulerLine1;
}
if (unit % 5 == 0)
{
size = size3;
lineColor = CThemeConfig::RulerLine2;
}
if (unit % 10 == 0)
{
size = size4;
lineColor = CThemeConfig::RulerLine3;
}

renderer->drawLineX(0.0f, y, size, lineColor);

if (unit % m_textPerUnit == 0)
{
SRect r;
r.X = 15.0f;
r.Y = y;
r.Width = 20.0f;
r.Height = 20.0f;
swprintf(textValue, 64, L"%d", unit);
renderer->renderText(r, SizeNormal, CThemeConfig::DefaultTextColor, std::wstring(textValue));
}

y = y + m_pixelPerUnit;
unit++;
}
}
}
}
}
}
79 changes: 79 additions & 0 deletions Projects/Editor/Source/GUI/Controls/CRulerBar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
!@
MIT License
Copyright (c) 2020 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/
#pragma once

#include "CBase.h"
#include "CScrollBarBar.h"

namespace Skylicht
{
namespace Editor
{
namespace GUI
{
class CRulerBar : public CBase
{
protected:
bool m_isHorizontal;

float m_beginOffset;
float m_position;
float m_pixelPerUnit;
int m_textPerUnit;

public:
CRulerBar(CBase* parent, bool horizontal);

virtual ~CRulerBar();

virtual void render();

inline void setBeginOffset(float offset)
{
m_beginOffset = offset;
}

inline void setPosition(float pos)
{
m_position = pos;
}

inline float getPosition()
{
return m_position;
}

inline void setTextPerUnit(int value)
{
m_textPerUnit = value;
}

inline void setPixelPerUnit(float value)
{
m_pixelPerUnit = value;
}
};
}
}
}
1 change: 1 addition & 0 deletions Projects/Editor/Source/GUI/GUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ This file is part of the "Skylicht Engine".
#include "GUI/Controls/CFlatButton.h"
#include "GUI/Controls/CDataGridView.h"
#include "GUI/Controls/COpenSaveDialog.h"
#include "GUI/Controls/CRulerBar.h"
#include "GUI/Utils/CToggleGroup.h"
4 changes: 4 additions & 0 deletions Projects/Editor/Source/GUI/Renderer/CRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ namespace Skylicht

virtual void drawFillRect(const SRect& r, const SGUIColor& color) {}

virtual void drawLineX(float x1, float y1, float width, const SGUIColor& color) {}

virtual void drawLineY(float x1, float y1, float height, const SGUIColor& color) {}

virtual void drawBorderRect(const SRect& r, const SGUIColor& color, bool left, bool top, bool right, bool bottom) {}

virtual void renderText(const SRect& r, EFontSize fontSize, const SGUIColor& textColor, const std::wstring& string) {}
Expand Down
Loading

0 comments on commit 7ef7930

Please sign in to comment.