Skip to content

Commit

Permalink
Update GUIText
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Aug 15, 2024
1 parent 09479d8 commit a7db7e4
Show file tree
Hide file tree
Showing 17 changed files with 197 additions and 24 deletions.
14 changes: 10 additions & 4 deletions Projects/Skylicht/Engine/Source/Graphics2D/GUI/CGUIText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,11 +475,17 @@ namespace Skylicht
{
int lastLine = numLine - 1;
ArrayInt& line = m_arrayCharId[lastLine];

if (line.back() == m_setCaret)
m_caret.set((int)line.size() - 1, lastLine);
if (line.size() == 0)
{
m_caret.set(0, lastLine);
}
else
m_caret.set((int)line.size(), lastLine);
{
if (line.back() == m_setCaret)
m_caret.set((int)line.size() - 1, lastLine);
else
m_caret.set((int)line.size(), lastLine);
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions Projects/Skylicht/Engine/Source/Graphics2D/GUI/CGUIText.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ namespace Skylicht
return m_textwId.c_str();
}

inline int getTextLength()
{
return (int)m_textw.size();
}

inline void setEnableTextFormnat(bool b)
{
m_enableTextFormat = b;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ namespace Skylicht

CUIBase::~CUIBase()
{
CUIEventManager* eventMgr = CUIEventManager::getInstance();
if (eventMgr->getFocus() == this)
eventMgr->setFocus(NULL);

for (int i = 0, n = (int)EMotionEvent::NumEvent; i < n; i++)
removeMotions((EMotionEvent)i);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ namespace Skylicht
CUIBase(container, element),
m_background(NULL),
m_text(NULL),
m_editable(true)
m_editable(true),
m_maxLength(64)
{
CCanvas* canvas = getCanvas();
if (canvas)
Expand All @@ -49,9 +50,40 @@ namespace Skylicht

CUITextBox::~CUITextBox()
{
CUIEventManager* eventMgr = CUIEventManager::getInstance();
if (eventMgr->getFocus() == this)
eventMgr->setFocus(NULL);

}

void CUITextBox::setText(const char* text)
{
if (m_text)
m_text->setText(text);
}

void CUITextBox::setText(const wchar_t* text)
{
if (m_text)
m_text->setText(text);
}

const char* CUITextBox::getText()
{
if (m_text)
return m_text->getText();
return "";
}

const wchar_t* CUITextBox::getTextW()
{
if (m_text)
return m_text->getTextW();
return L"";
}

int CUITextBox::getTextLength()
{
if (m_text)
return m_text->getTextLength();
return 0;
}

void CUITextBox::onPointerHover(float pointerX, float pointerY)
Expand Down Expand Up @@ -293,7 +325,8 @@ namespace Skylicht
}
else if (onChar && event.KeyInput.Char > 0)
{
m_text->insert(event.KeyInput.Char);
if (m_text->getTextLength() < m_maxLength)
m_text->insert(event.KeyInput.Char);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ namespace Skylicht

bool m_editable;

int m_maxLength;

public:
std::function<void(CUIBase*)> OnTextChanged;

Expand All @@ -51,11 +53,21 @@ namespace Skylicht
return m_background;
}

inline CGUIText* getText()
inline CGUIText* getTextGUI()
{
return m_text;
}

void setText(const char* text);

void setText(const wchar_t* text);

const char* getText();

const wchar_t* getTextW();

int getTextLength();

inline void setEditable(bool b)
{
m_editable = b;
Expand All @@ -66,6 +78,16 @@ namespace Skylicht
return m_editable;
}

inline void setMaxLength(int l)
{
m_maxLength = l;
}

inline int getMaxLength()
{
return m_maxLength;
}

virtual void onPointerHover(float pointerX, float pointerY);

virtual void onPointerOut(float pointerX, float pointerY);
Expand Down
14 changes: 14 additions & 0 deletions Samples/GUI/Source/CProfileData.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "pch.h"
#include "CProfileData.h"

IMPLEMENT_SINGLETON(CProfileData);

CProfileData::CProfileData()
{
Name = L"No Name";
}

CProfileData::~CProfileData()
{

}
16 changes: 16 additions & 0 deletions Samples/GUI/Source/CProfileData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "Utils/CSingleton.h"

class CProfileData
{
public:
DECLARE_SINGLETON(CProfileData)

CProfileData();

~CProfileData();

public:

std::wstring Name;

};
13 changes: 11 additions & 2 deletions Samples/GUI/Source/CViewHeader.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "pch.h"
#include "CProfileData.h"
#include "CViewHeader.h"
#include "Context/CContext.h"
#include "ViewManager/CViewManager.h"
Expand All @@ -11,7 +12,8 @@
#include "UserInterface/CUIContainer.h"
#include "UserInterface/CUIButton.h"

CViewHeader::CViewHeader()
CViewHeader::CViewHeader() :
m_txtUserName(NULL)
{

}
Expand All @@ -35,7 +37,8 @@ void CViewHeader::onInit()

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

UI::CUIBase* btnUserName = new UI::CUIBase(uiContainer, canvas->getGUIByPath("Canvas/Header/txtUserName"));
m_txtUserName = dynamic_cast<CGUIText*>(canvas->getGUIByPath("Canvas/Header/txtUserName"));
UI::CUIBase* btnUserName = new UI::CUIBase(uiContainer, m_txtUserName);
btnUserName->addMotion(UI::EMotionEvent::PointerHover, new UI::CColorMotion(SColor(255, 230, 90, 30)));
btnUserName->addMotion(UI::EMotionEvent::PointerOut, new UI::CColorMotion());
btnUserName->addMotion(UI::EMotionEvent::PointerDown, new UI::CAlphaMotion(0.8f))->setTime(0.0f, 0.0f);
Expand All @@ -59,6 +62,12 @@ void CViewHeader::onDestroy()

}

void CViewHeader::onData()
{
if (m_txtUserName)
m_txtUserName->setText(CProfileData::getInstance()->Name.c_str());
}

void CViewHeader::onUpdate()
{

Expand Down
4 changes: 4 additions & 0 deletions Samples/GUI/Source/CViewHeader.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#pragma once

#include "Graphics2D/GUI/CGUIText.h"
#include "ViewManager/CView.h"

class CViewHeader : public CView
{
protected:
CGUIText* m_txtUserName;

public:
CViewHeader();
Expand All @@ -13,6 +15,8 @@ class CViewHeader : public CView

virtual void onInit();

virtual void onData();

virtual void onDestroy();

virtual void onUpdate();
Expand Down
30 changes: 27 additions & 3 deletions Samples/GUI/Source/CViewPopupEnterName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "Context/CContext.h"
#include "ViewManager/CViewManager.h"

#include "CProfileData.h"

#include "Graphics2D/CGUIImporter.h"
#include "GameObject/CGameObject.h"

Expand All @@ -11,7 +13,8 @@
#include "UserInterface/CUITextBox.h"

CViewPopupEnterName::CViewPopupEnterName() :
m_popup(NULL)
m_popup(NULL),
m_textBox(NULL)
{

}
Expand Down Expand Up @@ -43,7 +46,7 @@ void CViewPopupEnterName::onInit()
dialog->addMotion(UI::EMotionEvent::In, new UI::CAlphaMotion(0.0f))->setInverse(true)->setTime(0.0f, 250.0f);
dialog->addMotion(UI::EMotionEvent::Out, new UI::CAlphaMotion(0.0f))->setTime(0.0f, 250.0f);

UI::CUITextBox* txtBox = new UI::CUITextBox(uiContainer, canvas->getGUIByPath("Canvas/Dialog/InputName"));
m_textBox = new UI::CUITextBox(uiContainer, canvas->getGUIByPath("Canvas/Dialog/InputName"));

UI::CUIBase* btnClose = new UI::CUIBase(uiContainer, canvas->getGUIByPath("Canvas/Dialog/btnClose"));
btnClose->addMotion(UI::EMotionEvent::PointerHover, new UI::CScaleMotion(1.2f, 1.2f, 1.0f));
Expand Down Expand Up @@ -72,7 +75,8 @@ void CViewPopupEnterName::onInit()
btnOk->addMotion(UI::EMotionEvent::PointerUp, new UI::CScaleMotion())->setTime(0.0f, 100.0f);
btnOk->OnPressed = [&](UI::CUIBase* base)
{
close();
if (onOK())
close();
};

uiContainer->startInMotion();
Expand All @@ -89,6 +93,26 @@ void CViewPopupEnterName::close()
};
}

bool CViewPopupEnterName::onOK()
{
wchar_t name[64];
CStringImp::copy(name, m_textBox->getTextW());
CStringImp::trim(name);

if (CStringImp::length(name) > 0)
{
CProfileData::getInstance()->Name = name;
CViewManager::getInstance()->onData();
return true;
}
return false;
}

void CViewPopupEnterName::onData()
{
m_textBox->setText(CProfileData::getInstance()->Name.c_str());
}

void CViewPopupEnterName::onDestroy()
{

Expand Down
7 changes: 7 additions & 0 deletions Samples/GUI/Source/CViewPopupEnterName.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
#include "ViewManager/CView.h"
#include "GameObject/CGameObject.h"

#include "UserInterface/CUITextBox.h"

class CViewPopupEnterName : public CView
{
protected:
CGameObject* m_popup;
UI::CUITextBox* m_textBox;

public:
CViewPopupEnterName();
Expand All @@ -15,6 +18,8 @@ class CViewPopupEnterName : public CView

virtual void onInit();

virtual void onData();

virtual void onDestroy();

virtual void onUpdate();
Expand All @@ -27,6 +32,8 @@ class CViewPopupEnterName : public CView

void close();

bool onOK();

void onGUI();

};
6 changes: 6 additions & 0 deletions Samples/GUI/Source/SampleGUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "CImguiManager.h"

#include "CViewInit.h"
#include "CProfileData.h"
#include "UserInterface/CUIEventManager.h"

void installApplication(const std::vector<std::string>& argv)
Expand All @@ -17,6 +18,9 @@ void installApplication(const std::vector<std::string>& argv)
SampleGUI::SampleGUI()
{
CContext::createGetInstance();

CProfileData::createGetInstance();

// layer 0: Main GUI
// layer 1: Header
// layer 2: Popup
Expand All @@ -32,6 +36,8 @@ SampleGUI::~SampleGUI()
CViewManager::releaseInstance();
CContext::releaseInstance();

CProfileData::releaseInstance();

UI::CUIEventManager::releaseInstance();
CImguiManager::releaseInstance();
CLightmapper::releaseInstance();
Expand Down
5 changes: 5 additions & 0 deletions Samples/SampleFW/ViewManager/CView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ void CView::onInit()

}

void CView::onData()
{

}

void CView::onDestroy()
{

Expand Down
2 changes: 2 additions & 0 deletions Samples/SampleFW/ViewManager/CView.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class CView

virtual void onInit();

virtual void onData();

virtual void onDestroy();

virtual void onActive();
Expand Down
Loading

0 comments on commit a7db7e4

Please sign in to comment.