Skip to content

Commit

Permalink
Update button with toggle state
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Aug 15, 2024
1 parent c498952 commit 86e2cb4
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ namespace Skylicht
CUIButton::CUIButton(CUIContainer* container, CGUIElement* element) :
CUIBase(container, element),
m_background(NULL),
m_text(NULL)
m_text(NULL),
m_isToggle(false),
m_toggleStatus(false)
{
CCanvas* canvas = getCanvas();
if (canvas)
Expand All @@ -58,5 +60,47 @@ namespace Skylicht
if (m_text)
m_text->setText(string);
}

void CUIButton::setToggle(bool b, bool invokeEvent)
{
m_toggleStatus = b;
if (invokeEvent && OnToggle != nullptr)
OnToggle(this, b);
}

void CUIButton::onPressed()
{
CUIBase::onPressed();
if (m_isToggle)
setToggle(!m_toggleStatus);
}

void CUIButton::onPointerHover(float pointerX, float pointerY)
{
if (m_isToggle && m_toggleStatus)
{
m_isPointerHover = true;
if (OnPointerHover != nullptr)
OnPointerHover(pointerX, pointerY);
}
else
{
CUIBase::onPointerHover(pointerX, pointerY);
}
}

void CUIButton::onPointerOut(float pointerX, float pointerY)
{
if (m_isToggle && m_toggleStatus)
{
m_isPointerHover = false;
if (OnPointerOut != nullptr)
OnPointerOut(pointerX, pointerY);
}
else
{
CUIBase::onPointerOut(pointerX, pointerY);
}
}
}
}
29 changes: 29 additions & 0 deletions Projects/Skylicht/UserInterface/Source/UserInterface/CUIButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ namespace Skylicht
CGUIElement* m_background;
CGUIText* m_text;

bool m_isToggle;
bool m_toggleStatus;

public:
std::function<void(CUIBase*, bool)> OnToggle;

public:
CUIButton(CUIContainer* container, CGUIElement* element);

Expand All @@ -45,6 +51,29 @@ namespace Skylicht

void setLabel(const wchar_t* string);

virtual void onPointerHover(float pointerX, float pointerY);

virtual void onPointerOut(float pointerX, float pointerY);

virtual void onPressed();

inline void setToggleButton(bool b)
{
m_isToggle = b;
}

inline bool isToggleButton(bool b)
{
return m_isToggle;
}

virtual void setToggle(bool b, bool invokeEvent = true);

inline bool isToggle()
{
return m_toggleStatus;
}

inline CGUIElement* getBackground()
{
return m_background;
Expand Down
53 changes: 47 additions & 6 deletions Samples/GUI/Source/CViewDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
#include "CImguiManager.h"

#include "Context/CContext.h"

#include "Graphics2D/CGUIImporter.h"

CViewDemo::CViewDemo()
CViewDemo::CViewDemo() :
m_uiContainer(NULL),
m_listUniform(NULL),
m_canvas(NULL)
{

}
Expand All @@ -35,9 +37,9 @@ void CViewDemo::onInit()
m_canvas->applyScaleGUI(1.0f);
m_canvas->setSortDepth(0);

UI::CUIContainer* uiContainer = leftPanel->addComponent<UI::CUIContainer>();
m_uiContainer = leftPanel->addComponent<UI::CUIContainer>();

m_listUniform = new UI::CUIListView(uiContainer,
m_listUniform = new UI::CUIListView(m_uiContainer,
m_canvas->getGUIByPath("Canvas/Container/ListItems"),
m_canvas->getGUIByPath("Canvas/Container/ListItems/Item"));

Expand All @@ -51,6 +53,11 @@ void CViewDemo::onInit()
}
}

void CViewDemo::onDestroy()
{

}

void CViewDemo::addListIconItem(SFrame* frame)
{
if (frame)
Expand All @@ -59,12 +66,46 @@ void CViewDemo::addListIconItem(SFrame* frame)
CGUISprite* iconGUI = (CGUISprite*)m_canvas->getGUIByPath(element, "Icon");
if (iconGUI)
iconGUI->setFrame(frame);

CGUIElement* bg = m_canvas->getGUIByPath(element, "Background");
if (bg)
{
UI::CUIButton* button = new UI::CUIButton(m_uiContainer, element);
button->addMotion(UI::EMotionEvent::PointerHover, bg, new UI::CColorMotion(SColor(0xfffe7200)))->setTime(0.0f, 50.0f);
button->addMotion(UI::EMotionEvent::PointerOut, bg, new UI::CColorMotion())->setTime(0.0f, 50.0f);
button->setToggleButton(true);
button->OnToggle = [bg, view = this](UI::CUIBase* button, bool status)
{
if (status)
{
bg->setColor(SColor(0xff0072fe));
button->setEnable(false);
view->onChangeUniform(button);
}
else
{
bg->setColor(SColor(0xff201a2b));
}
};

if (m_listUniformBtn.size() == 0)
button->setToggle(true);

m_listUniformBtn.push_back(button);
}
}
}

void CViewDemo::onDestroy()
void CViewDemo::onChangeUniform(UI::CUIBase* btn)
{

for (UI::CUIButton* b : m_listUniformBtn)
{
if (b != btn)
{
b->setToggle(false);
b->setEnable(true);
}
}
}

void CViewDemo::onUpdate()
Expand Down
5 changes: 5 additions & 0 deletions Samples/GUI/Source/CViewDemo.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
#include "UserInterface/CUIContainer.h"
#include "UserInterface/CUIListView.h"
#include "UserInterface/CUIGridView.h"
#include "UserInterface/CUIButton.h"

class CViewDemo : public CView
{
protected:
UI::CUIContainer* m_uiContainer;
UI::CUIListView* m_listUniform;
std::vector<UI::CUIButton*> m_listUniformBtn;
CCanvas* m_canvas;

public:
Expand All @@ -33,4 +36,6 @@ class CViewDemo : public CView
void onGUI();

void addListIconItem(SFrame* frame);

void onChangeUniform(UI::CUIBase* btn);
};

0 comments on commit 86e2cb4

Please sign in to comment.