Skip to content

Commit

Permalink
Fix Graphics Mask (inherit parent Mask)
Browse files Browse the repository at this point in the history
Fix Resize GUI
Update Skylicht Editor (create Icon Button, setting Outline)
Update SampleGUI
  • Loading branch information
ducphamhong committed Aug 21, 2024
1 parent b53eedb commit b40ae55
Show file tree
Hide file tree
Showing 24 changed files with 331 additions and 83 deletions.
Binary file added Assets/SampleGUI/!Sprites/triangle-d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/SampleGUI/!Sprites/triangle-d.png.meta
Binary file not shown.
Binary file added Assets/SampleGUI/!Sprites/triangle-l.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/SampleGUI/!Sprites/triangle-l.png.meta
Binary file not shown.
Binary file added Assets/SampleGUI/!Sprites/triangle-r.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/SampleGUI/!Sprites/triangle-r.png.meta
Binary file not shown.
Binary file added Assets/SampleGUI/!Sprites/triangle-u.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/SampleGUI/!Sprites/triangle-u.png.meta
Binary file not shown.
Binary file modified Assets/SampleGUI/SampleGUI.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions Assets/SampleGUI/SampleGUI.spritedata
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
<module id="31" x="334.000000" y="246.000000" w="124.000000" h="64.000000"/>
<module id="32" x="460.000000" y="246.000000" w="124.000000" h="64.000000"/>
<module id="33" x="586.000000" y="246.000000" w="124.000000" h="64.000000"/>
<module id="34" x="172.000000" y="72.000000" w="19.000000" h="14.000000"/>
<module id="35" x="812.000000" y="0.000000" w="14.000000" h="18.000000"/>
<module id="36" x="828.000000" y="0.000000" w="14.000000" h="18.000000"/>
<module id="37" x="193.000000" y="72.000000" w="18.000000" h="14.000000"/>
</modules>
<frames>
<frame id="abd38ad8c2e594ed" name="button-disable" page="0">
Expand Down Expand Up @@ -142,5 +146,17 @@
<frame id="dbbc6b190569ce4a" name="switch-on" page="0">
<fmodule id="33" x="0.000000" y="0.000000" flipX="0" flipY="0"/>
</frame>
<frame id="5916f1c284b74f6f" name="triangle-d" page="0">
<fmodule id="34" x="0.000000" y="0.000000" flipX="0" flipY="0"/>
</frame>
<frame id="f75ba76731d50f99" name="triangle-l" page="0">
<fmodule id="35" x="0.000000" y="0.000000" flipX="0" flipY="0"/>
</frame>
<frame id="2812c836e798a15f" name="triangle-r" page="0">
<fmodule id="36" x="0.000000" y="0.000000" flipX="0" flipY="0"/>
</frame>
<frame id="22910e3eb1f489c8" name="triangle-u" page="0">
<fmodule id="37" x="0.000000" y="0.000000" flipX="0" flipY="0"/>
</frame>
</frames>
</sprite>
Binary file modified Assets/SampleGUIDemo/Main.gui
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,20 @@ namespace Skylicht
m_inputHeight->setValue(1080.0f, false);
m_inputHeight->OnTextChanged = BIND_LISTENER(&CCanvasSettingController::onChanged, this);
boxLayout->endVertical();


boxLayout->addSpace(5.0f);

layout = boxLayout->beginVertical();
label = new GUI::CLabel(layout);
label->setPadding(GUI::SMargin(0.0f, 2.0, 0.0f, 0.0f));
label->setString("Draw GUI outline");
label->setTextAlignment(GUI::TextRight);

GUI::CBase* checkBoxContainer = new GUI::CBase(layout);
m_checkboxOutline = new GUI::CCheckBox(checkBoxContainer);
m_checkboxOutline->OnChanged = BIND_LISTENER(&CCanvasSettingController::onCheckBoxOutline, this);
boxLayout->endVertical();

boxLayout->addSpace(20.0f);

layout = boxLayout->beginVertical();
Expand Down Expand Up @@ -101,15 +114,20 @@ namespace Skylicht
{
CScene* scene = CSceneController::getInstance()->getScene();
CGameObject* canvasObj = scene->searchObjectInChild(L"GUICanvas");

CCanvas* canvas = canvasObj->getComponent<CCanvas>();
CGUIElement* root = canvas->getRootElement();
float w = root->getWidth();
float h = root->getHeight();

m_inputWidth->setValue(w, false);
m_inputHeight->setValue(h, false);

if (canvasObj)
{
CCanvas* canvas = canvasObj->getComponent<CCanvas>();
if (canvas)
{
CGUIElement* root = canvas->getRootElement();
float w = root->getWidth();
float h = root->getHeight();

m_inputWidth->setValue(w, false);
m_inputHeight->setValue(h, false);
m_checkboxOutline->setToggle(canvas->DrawOutline);
}
}
enableTabGroup();
}

Expand All @@ -119,14 +137,14 @@ namespace Skylicht
tabGroup.add(m_inputWidth);
tabGroup.add(m_inputHeight);
}

void CCanvasSettingController::clearTabGroup()
{
GUI::CTabableGroup& tabGroup = m_menu->getCanvas()->TabableGroup;
tabGroup.remove(m_inputWidth);
tabGroup.remove(m_inputHeight);
}

void CCanvasSettingController::onCancel(GUI::CBase* base)
{
m_menu->close();
Expand All @@ -137,22 +155,39 @@ namespace Skylicht
{
float w = m_inputWidth->getValue();
float h = m_inputHeight->getValue();

CScene* scene = CSceneController::getInstance()->getScene();
CGameObject* canvasObj = scene->searchObjectInChild(L"GUICanvas");

CCanvas* canvas = canvasObj->getComponent<CCanvas>();
CGUIElement* root = canvas->getRootElement();

root->setRect(core::rectf(0.0f, 0.0f, w, h));

m_menu->close();
clearTabGroup();
}

void CCanvasSettingController::onChanged(GUI::CBase* base)
{

CScene* scene = CSceneController::getInstance()->getScene();
CGameObject* canvasObj = scene->searchObjectInChild(L"GUICanvas");

CCanvas* canvas = canvasObj->getComponent<CCanvas>();
}

void CCanvasSettingController::onCheckBoxOutline(GUI::CBase* base)
{
CScene* scene = CSceneController::getInstance()->getScene();
CGameObject* canvasObj = scene->searchObjectInChild(L"GUICanvas");
if (canvasObj)
{
CCanvas* canvas = canvasObj->getComponent<CCanvas>();
if (canvas)
{
canvas->DrawOutline = m_checkboxOutline->getToggle();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace Skylicht

GUI::CNumberInput* m_inputWidth;
GUI::CNumberInput* m_inputHeight;
GUI::CCheckBox* m_checkboxOutline;

public:
CCanvasSettingController(CEditor* editor, GUI::CMenu* menu);
Expand All @@ -54,11 +55,13 @@ namespace Skylicht
void onOK(GUI::CBase* base);

void onChanged(GUI::CBase* base);


void onCheckBoxOutline(GUI::CBase* base);

private:

void enableTabGroup();

void clearTabGroup();
};
}
Expand Down
1 change: 1 addition & 0 deletions Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ namespace Skylicht

CCanvas* canvas = guiCanvas->addComponent<CCanvas>();
canvas->IsInEditor = true;
canvas->DrawOutline = true;

/*
CGUIRect* rect = canvas->createRect(SColor(255, 0, 0, 0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ namespace Skylicht
addMenu->addSeparator();
addMenu->addItem(L"UI Button");
addMenu->addItem(L"UI Button (Shiny)");
addMenu->addItem(L"UI Icon Button");
addMenu->addItem(L"UI Slider");
addMenu->addItem(L"UI Input");
addMenu->addItem(L"UI List");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,32 @@ namespace Skylicht
text->setTextAlign(EGUIHorizontalAlign::Center, EGUIVerticalAlign::Middle);
text->setFontSource("SampleGUI/Fonts/Roboto-Bold.font");
}
else if (command == L"UI Icon Button")
{
r.LowerRightCorner.X = 64.0f;
r.LowerRightCorner.Y = 64.0f;

nameHint = L"Btn";
newNode = parentNode->getCanvas()->createElement(parentNode, r);

CGUISprite* bg = parentNode->getCanvas()->createSprite(newNode, r, NULL);
bg->setName(L"Background");
bg->setFrameSource(
"SampleGUI/SampleGUI.spritedata",
"switch-circle-disable",
"SampleGUI/!Sprites/switch-circle-disable.png");
bg->setRect(core::rectf(0.0f, 0.0f, 46.0f, 46.0f));
bg->setAlign(EGUIHorizontalAlign::Center, EGUIVerticalAlign::Middle);

CGUISprite* icon = parentNode->getCanvas()->createSprite(newNode, r, NULL);
icon->setName(L"Icon");
icon->setFrameSource(
"SampleGUI/SampleGUI.spritedata",
"triangle-l",
"SampleGUI/!Sprites/triangle-l.png");
icon->setRect(core::rectf(0.0f, 0.0f, 14.0f, 18.0f));
icon->setAlign(EGUIHorizontalAlign::Center, EGUIVerticalAlign::Middle);
}
else if (command == L"UI Slider")
{

Expand Down Expand Up @@ -306,12 +332,6 @@ namespace Skylicht
bg->setDock(EGUIDock::DockFill);
bg->setAnchor(CGUIFitSprite::AnchorAll, 15.0f, 15.0f, 15.0f, 15.0f);

r.UpperLeftCorner.X = 0.0f;
r.UpperLeftCorner.Y = 0.0f;
r.LowerRightCorner.X = 110.0f;
CGUIMask* mask = parentNode->getCanvas()->createMask(newNode, r);
mask->setName(L"Mask");

CGUIFitSprite* progress = parentNode->getCanvas()->createFitSprite(newNode, r, NULL);
progress->setName(L"Loading");
progress->setDock(EGUIDock::DockFill);
Expand Down
63 changes: 48 additions & 15 deletions Projects/Skylicht/Engine/Source/Graphics2D/CCanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ namespace Skylicht
{
CCanvas::CCanvas() :
m_sortDepth(0),
m_scaleGUI(1.0f),
m_haveScaleGUI(false),
m_enable3DBillboard(false),
m_renderCamera(NULL),
IsInEditor(false)
m_currentMask(NULL),
IsInEditor(false),
DrawOutline(false)
{
CGraphics2D* g = CGraphics2D::getInstance();
float w = (float)g->getScreenSize().Width;
Expand Down Expand Up @@ -102,6 +106,10 @@ namespace Skylicht

// update the root rect
m_rect = core::rectf(0.0f, 0.0f, w, h);
m_root->setRect(m_rect);

if (m_haveScaleGUI)
applyScaleGUI(m_scaleGUI);
}
}

Expand Down Expand Up @@ -160,41 +168,52 @@ namespace Skylicht
void CCanvas::render(CCamera* camera)
{
m_renderCamera = camera;
m_currentMask = NULL;

// render
std::stack<CGUIElement*> renderEntity;
renderEntity.push(m_root);

CGUIMask* parentMask = NULL;
CGUIElement* entity = NULL;
CGUIElement* parent = NULL;

while (renderEntity.size() > 0)
{
CGUIElement* entity = renderEntity.top();
entity = renderEntity.top();
renderEntity.pop();

// skip the invisible
if (entity->isVisible() == false)
continue;

// update the entity
entity->update(camera);

// apply mask
entity->applyCurrentMask(entity->getMask());

// try set the parrent mask
CGUIElement* parent = entity->getParent();
parent = entity->getParent();
if (parent != NULL)
{
CGUIMask* mask = parent->getMask();
if (mask == NULL)
mask = parent->getCurrentMask();

if (mask != NULL)
entity->applyCurrentMask(mask);
parentMask = parent->getMask();
if (parentMask == NULL)
parentMask = parent->getCurrentMask();
}

if (entity->getMask() != NULL)
entity->applyCurrentMask(entity->getMask());
else
entity->applyCurrentMask(parentMask);

CGUIMask* mask = entity->getCurrentMask();
if (mask != NULL)
{
if (m_currentMask != mask)
{
// clear last mask to apply new mask
if (m_currentMask)
m_currentMask->clearMask();
m_currentMask = mask;
}
mask->applyParentClip(parentMask);
mask->beginMaskTest(camera);
}

entity->render(camera);

Expand Down Expand Up @@ -349,6 +368,8 @@ namespace Skylicht
return;

float s = core::clamp(widthOrHeight, 0.0f, 1.0f);
m_scaleGUI = s;
m_haveScaleGUI = true;

CGraphics2D* g = CGraphics2D::getInstance();
float screenW = (float)g->getScreenSize().Width;
Expand All @@ -371,6 +392,18 @@ namespace Skylicht
m_root->setScale(core::vector3df(scale, scale, 1.0f));
}

void CCanvas::resetScaleGUI()
{
m_haveScaleGUI = false;

CGraphics2D* g = CGraphics2D::getInstance();
float w = (float)g->getScreenSize().Width;
float h = (float)g->getScreenSize().Height;
m_rect = core::rectf(0.0f, 0.0f, w, h);
m_root->setRect(m_rect);
m_root->setScale(core::vector3df(1.0f, 1.0f, 1.0f));
}

const core::vector3df& CCanvas::getRootScale()
{
return m_root->getScale();
Expand Down
8 changes: 6 additions & 2 deletions Projects/Skylicht/Engine/Source/Graphics2D/CCanvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,19 @@ namespace Skylicht
{
protected:
core::rectf m_rect;

core::rectf m_defaultRect;

CGUIElement* m_root;

int m_sortDepth;

float m_scaleGUI;
bool m_haveScaleGUI;
bool m_enable3DBillboard;

core::matrix4 m_renderWorldTransform;

CCamera* m_renderCamera;
CGUIMask* m_currentMask;

// ECS System
CEntityPrefab* m_entityMgr;
Expand All @@ -70,6 +71,7 @@ namespace Skylicht
public:

bool IsInEditor;
bool DrawOutline;

public:
CCanvas();
Expand Down Expand Up @@ -122,6 +124,8 @@ namespace Skylicht

void applyScaleGUI(float widthOrHeight = 1.0f);

void resetScaleGUI();

const core::vector3df& getRootScale();

inline CGUIElement* getRootElement()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ namespace Skylicht
inline void setRect(const core::rectf& r)
{
Rect = r;
HasChanged = true;
}

DECLARE_GETTYPENAME(CGUITransformData)
Expand Down
Loading

0 comments on commit b40ae55

Please sign in to comment.