Skip to content

Commit

Permalink
#6 Improve clipping, sub window
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Oct 28, 2020
1 parent e5dfae0 commit c989b2d
Show file tree
Hide file tree
Showing 22 changed files with 164 additions and 33 deletions.
Binary file modified Assets/Editor/GUI/draw_window.psd
Binary file not shown.
Binary file modified Assets/Editor/GUI/draw_window_shadow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions Projects/Editor/Source/Editor/CEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace Skylicht

GUI::CWindow *window1 = new GUI::CWindow(canvas, 20.0f, 20.0f, 600.0f, 480.0f);
GUI::CWindow *window2 = new GUI::CWindow(canvas, 400.0f, 20.0f, 600.0f, 480.0f);
GUI::CWindow *window3 = new GUI::CWindow(window2, 20.0f, 20.0f, 300.0f, 240.0f);
}

CEditor::~CEditor()
Expand Down
50 changes: 38 additions & 12 deletions Projects/Editor/Source/GUI/Controls/CBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This file is part of the "Skylicht Engine".
#include "CBase.h"
#include "CCanvas.h"
#include "GUI/CGUIContext.h"
#include "GUI/Theme/CThemeConfig.h"
#include "GUI/Input/CInput.h"

namespace Skylicht
Expand All @@ -36,6 +37,7 @@ namespace Skylicht
{
CBase::CBase(CBase* parent, const std::string& name) :
m_parent(NULL),
m_innerPanel(NULL),
m_name(name),
m_disabled(false),
m_hidden(false),
Expand All @@ -46,9 +48,13 @@ namespace Skylicht
m_margin(0.0f, 0.0f, 0.0f, 0.0f),
m_mouseInputEnabled(true),
m_keyboardInputEnabled(true),
m_cursor(ECursorType::Normal)
m_shouldClip(false),
m_cursor(ECursorType::Normal),
m_renderFillRect(false),
m_fillRectColor(CThemeConfig::WindowInnerColor)
{
setParent(parent);
if (parent != NULL)
setParent(parent->getInnerPanel());
}

CBase::~CBase()
Expand Down Expand Up @@ -325,10 +331,10 @@ namespace Skylicht
m_renderBounds.Height = m_bounds.Height;
}

void CBase::dragTo(float x, float y, float dragPosX, float dragPosY)
void CBase::dragTo(float x, float y, float dragPosX, float dragPosY, float paddingBottom)
{
float testX = x + dragPosX;
float testY = y + dragPosY;
float testY = y;

if (m_parent)
{
Expand All @@ -341,12 +347,12 @@ namespace Skylicht
if (testX + m_padding.Right > m_parent->width() - m_parent->m_margin.Right)
testX = m_parent->width() - m_parent->m_margin.Right - m_padding.Right;

if (testY + m_padding.Bottom > m_parent->height() - m_parent->m_margin.Bottom)
testY = m_parent->height() - m_parent->m_margin.Bottom - m_padding.Bottom;
if (testY + m_padding.Bottom > m_parent->height() - m_parent->m_margin.Bottom - paddingBottom)
testY = m_parent->height() - m_parent->m_margin.Bottom - m_padding.Bottom - paddingBottom;
}

x = testX - dragPosX;
y = testY - dragPosY;
y = testY;

setBounds(x, y, width(), height());
}
Expand Down Expand Up @@ -404,7 +410,11 @@ namespace Skylicht
}

// Render this control and children controls
render->startClip();
if (shouldClip())
{
render->startClip();
}

{
this->render();

Expand All @@ -420,24 +430,40 @@ namespace Skylicht
}
}
}
render->endClip();

if (shouldClip())
{
render->endClip();
}

// Render overlay/focus
{
render->setClipRegion(oldRegion);
render->startClip();
if (shouldClip())
{
render->startClip();
}

{
renderOver();
renderFocus();
}
render->endClip();

if (shouldClip())
{
render->endClip();
}

render->setRenderOffset(oldRenderOffset);
}
}

void CBase::render()
{

if (m_renderFillRect)
{
CRenderer::getRenderer()->drawFillRect(getRenderBounds(), m_fillRectColor);
}
}

void CBase::renderFocus()
Expand Down
36 changes: 33 additions & 3 deletions Projects/Editor/Source/GUI/Controls/CBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,20 @@ namespace Skylicht
virtual CBase* getChild(u32 i);

virtual void setParent(CBase* parent);
virtual CBase* getParent() const

virtual CBase* getParent()
{
return m_parent;
}

virtual CBase* getInnerPanel()
{
if (m_innerPanel == NULL)
return this;

return m_innerPanel;
}

virtual const List& getChildren()
{
return Children;
Expand Down Expand Up @@ -177,7 +186,7 @@ namespace Skylicht
inline void setMargin(const SMargin& margin) { m_margin = margin; }

virtual void moveTo(float x, float y);
virtual void dragTo(float x, float y, float dragPosX, float dragPosY);
virtual void dragTo(float x, float y, float dragPosX, float dragPosY, float paddingBottom);
inline void moveBy(float x, float y) { moveTo(X() + x, Y() + y); }

inline const SRect& getBounds() const { return m_bounds; }
Expand All @@ -196,7 +205,22 @@ namespace Skylicht
virtual void doRender();
virtual void renderRecursive(const SRect& cliprect);

virtual bool shouldClip() { return true; }
bool shouldClip() { return m_shouldClip; }

inline void enableClip(bool b)
{
m_shouldClip = b;
}

inline void enableRenderFillRect(bool b)
{
m_renderFillRect = b;
}

inline void setFillRectColor(const SGUIColor& c)
{
m_fillRectColor = c;
}

protected:

Expand Down Expand Up @@ -301,6 +325,7 @@ namespace Skylicht

protected:
CBase *m_parent;
CBase *m_innerPanel;

std::string m_name;

Expand All @@ -315,9 +340,14 @@ namespace Skylicht
bool m_hidden;
bool m_needsLayout;

bool m_shouldClip;

bool m_mouseInputEnabled;
bool m_keyboardInputEnabled;

bool m_renderFillRect;
SGUIColor m_fillRectColor;

EPosition m_dock;

ECursorType m_cursor;
Expand Down
2 changes: 1 addition & 1 deletion Projects/Editor/Source/GUI/Controls/CDragger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace Skylicht
if (m_target->getParent() != NULL)
p = m_target->getParent()->canvasPosToLocal(p);

m_target->dragTo(p.X, p.Y, m_holdPosition.X, m_holdPosition.Y);
m_target->dragTo(p.X, p.Y, m_holdPosition.X, m_holdPosition.Y, height());
m_target->onMoved();
}

Expand Down
2 changes: 1 addition & 1 deletion Projects/Editor/Source/GUI/Controls/CResizableControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Skylicht
{
namespace GUI
{
CResizableControl::CResizableControl(CCanvas* parent, float x, float y, float w, float h) :
CResizableControl::CResizableControl(CBase* parent, float x, float y, float w, float h) :
CBase(parent)
{
setBounds(x, y, w, h);
Expand Down
2 changes: 1 addition & 1 deletion Projects/Editor/Source/GUI/Controls/CResizableControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace Skylicht
CResizer *m_resizers[8];

public:
CResizableControl(CCanvas* parent, float x, float y, float w, float h);
CResizableControl(CBase* parent, float x, float y, float w, float h);

virtual ~CResizableControl();

Expand Down
1 change: 1 addition & 0 deletions Projects/Editor/Source/GUI/Controls/CText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace Skylicht
m_color(CThemeConfig::DefaultTextColor)
{
setMouseInputEnabled(false);
enableClip(true);
}

CText::~CText()
Expand Down
1 change: 1 addition & 0 deletions Projects/Editor/Source/GUI/Controls/CTextContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace Skylicht
m_fontSize(EFontSize::SizeNormal)
{
setMouseInputEnabled(false);
enableClip(true);
}

CTextContainer::~CTextContainer()
Expand Down
22 changes: 15 additions & 7 deletions Projects/Editor/Source/GUI/Controls/CWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,38 @@ namespace Skylicht
{
namespace GUI
{
CWindow::CWindow(CCanvas* parent, float x, float y, float w, float h) :
CWindow::CWindow(CBase* parent, float x, float y, float w, float h) :
CResizableControl(parent, x, y, w, h)
{
enableClip(true);

m_titleBar = new CDragger(this);
m_titleBar->setHeight(28.0f);
m_titleBar->setHeight(22.0f);
m_titleBar->setPadding(SPadding(0.0f, 0.0f, 0.0f, 0.0f));
m_titleBar->setMargin(SMargin(0.0f, 0.0f, 0.0f, 4.0f));
m_titleBar->dock(EPosition::Top);

m_icon = new CIcon(m_titleBar, ESystemIcon::Window, false);
m_icon->dock(EPosition::Left);
m_icon->setMargin(SPadding(0.0f, -2.0f, 0.0f, 0.0f));
m_icon->setMargin(SPadding(0.0f, 0.0f, 0.0f, 0.0f));

m_title = new CLabel(m_titleBar);
m_title->setString(L"Window title");
m_title->dock(EPosition::Fill);
m_title->setPadding(SPadding(4.0f, 2.0f, 0.0f, 0.0f));
m_title->setPadding(SPadding(4.0f, 4.0f, 0.0f, 0.0f));

m_close = new CButton(m_titleBar);
m_close->setSize(18.0f, 18.0f);
m_close->setSize(20.0f, 20.0f);
m_close->dock(EPosition::Right);
m_close->setIcon(ESystemIcon::Close);
m_close->setMargin(SMargin(0.0f, 0.0f, 0.0f, 10.0f));
m_close->setIconMargin(SMargin(-1.0f, -1.0f, 0.0f, 0.0f));
m_close->setMargin(SMargin(0.0f, 0.0f, 0.0f, 2.0f));
m_close->showIcon(true);
m_close->OnPress = BIND_LISTENER(&CWindow::onCloseButtonPress, this);

m_innerPanel = new CBase(this);
m_innerPanel->dock(EPosition::Fill);
m_innerPanel->enableClip(true);
m_innerPanel->enableRenderFillRect(true);
}

CWindow::~CWindow()
Expand All @@ -78,11 +84,13 @@ namespace Skylicht
void CWindow::touch()
{
bringToFront();
CBase::touch();
}

void CWindow::onChildTouched(CBase* child)
{
bringToFront();
CBase::onChildTouched(child);
}

void CWindow::onCloseButtonPress(CBase *sender)
Expand Down
2 changes: 1 addition & 1 deletion Projects/Editor/Source/GUI/Controls/CWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace Skylicht
CButton *m_close;

public:
CWindow(CCanvas* parent, float x, float y, float w, float h);
CWindow(CBase* parent, float x, float y, float w, float h);

virtual ~CWindow();

Expand Down
2 changes: 2 additions & 0 deletions Projects/Editor/Source/GUI/Renderer/CRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ namespace Skylicht

virtual void endClip() {}

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

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

virtual SDimension measureText(EFontSize fontSize, const std::wstring& string) = 0;
Expand Down
28 changes: 28 additions & 0 deletions Projects/Editor/Source/GUI/Renderer/CSkylichtRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This file is part of the "Skylicht Engine".
#include "pch.h"
#include "CSkylichtRenderer.h"
#include "GUI/Theme/CThemeConfig.h"
#include "GUI/Theme/CSkylichtTheme.h"
#include "Graphics2D/Glyph/CGlyphFreetype.h"

namespace Skylicht
Expand Down Expand Up @@ -113,6 +114,23 @@ namespace Skylicht
}
}

void CSkylichtRenderer::drawFillRect(const SRect &r, const SGUIColor& color)
{
CGraphics2D *g = CGraphics2D::getInstance();
CSkylichtTheme *theme = (CSkylichtTheme*)CTheme::getTheme();

float invW = 1.0f / (float)theme->getAtlasWidth();
float invH = 1.0f / (float)theme->getAtlasHeight();

const core::matrix4 &world = getWorldTransform();

float offsetX = 2.0f;
float offsetY = 2.0f;
core::rectf uv(offsetX * invW, offsetY * invH, offsetX * invW, offsetY * invH);

g->addRectangleBatch(getRect(r), uv, getColor(color), world, m_materialID, NULL);
}

void CSkylichtRenderer::renderText(const SRect &r, EFontSize fontSize, const SGUIColor& textColor, const std::wstring& string)
{
CGlyphFont *font = m_fontNormal;
Expand Down Expand Up @@ -247,6 +265,16 @@ namespace Skylicht
m_fontLarge = new CGlyphFont(CThemeConfig::FontName.c_str(), fontSmall);
m_fontNormal = new CGlyphFont(CThemeConfig::FontName.c_str(), fontNormal);
}

core::rectf CSkylichtRenderer::getRect(const SRect& rect)
{
return core::rectf(rect.X, rect.Y, rect.X + rect.Width, rect.Y + rect.Height);
}

video::SColor CSkylichtRenderer::getColor(const SGUIColor& color)
{
return SColor(color.A, color.R, color.G, color.B);
}
}
}
}
6 changes: 6 additions & 0 deletions Projects/Editor/Source/GUI/Renderer/CSkylichtRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ namespace Skylicht

virtual void endClip();

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

virtual void renderText(const SRect &r, EFontSize fontSize, const SGUIColor& textColor, const std::wstring& string);

virtual SDimension measureText(EFontSize fontSize, const std::wstring& string);
Expand All @@ -80,6 +82,10 @@ namespace Skylicht
m_world.setTranslation(core::vector3df(m_renderOffset.X, m_renderOffset.Y, 0.0f));
return m_world;
}

core::rectf getRect(const SRect& rect);

video::SColor getColor(const SGUIColor& color);
};
}
}
Expand Down
Loading

0 comments on commit c989b2d

Please sign in to comment.