Skip to content

Commit

Permalink
Update UITextBox & key event
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Aug 14, 2024
1 parent 153c8e3 commit 34fe3f3
Show file tree
Hide file tree
Showing 11 changed files with 639 additions and 88 deletions.
363 changes: 306 additions & 57 deletions Projects/Skylicht/Engine/Source/Graphics2D/GUI/CGUIText.cpp

Large diffs are not rendered by default.

55 changes: 50 additions & 5 deletions Projects/Skylicht/Engine/Source/Graphics2D/GUI/CGUIText.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ namespace Skylicht
std::wstring m_textw;
ArrayInt m_textFormat;

bool m_enableTextFormat;

std::string m_textId;
std::wstring m_textwId;

Expand All @@ -73,6 +75,7 @@ namespace Skylicht

std::vector<ArrayModuleOffset> m_arrayCharRender;
std::vector<ArrayInt> m_arrayCharFormat;
std::vector<ArrayInt> m_arrayCharId;

std::string m_fontSource;
std::string m_fontGUID;
Expand All @@ -86,6 +89,7 @@ namespace Skylicht
#ifdef HAVE_CARET
bool m_showCaret;
int m_caretShader;
int m_setCaret;
float m_caretBlink;
float m_caretBlinkSpeed;
core::vector2di m_caret;
Expand All @@ -104,8 +108,6 @@ namespace Skylicht

virtual void render(CCamera* camera);

void splitText(std::vector<ArrayModuleOffset>& split, std::vector<ArrayInt>& format, int width);

void setTextAlign(EGUIHorizontalAlign h, EGUIVerticalAlign v)
{
TextVertical = v;
Expand All @@ -114,7 +116,7 @@ namespace Skylicht

/*
* Example:
* <1>PLAYERA <0>kill <1>PLAYERB <0>with <2>AK47
* <1>PLAYERA <0>gives <2>a heart <0>to <1>PLAYERB
* With format:
* <1> -> RED
* <0> -> WHITE
Expand Down Expand Up @@ -161,6 +163,16 @@ namespace Skylicht
return m_textwId.c_str();
}

inline void setEnableTextFormnat(bool b)
{
m_enableTextFormat = b;
}

inline bool isEnableTextFormat()
{
return m_enableTextFormat;
}

inline void setCharPadding(int charPadding)
{
m_charPadding = charPadding;
Expand Down Expand Up @@ -193,8 +205,6 @@ namespace Skylicht
void getClosestCharacter(float posX, float posY, int& line, int& character);

#ifdef HAVE_CARET
void updateCaret();

inline void showCaret(bool b)
{
m_showCaret = b;
Expand All @@ -205,10 +215,45 @@ namespace Skylicht
m_caret.set(character, line);
m_caretBlink = 0.0f;
}

void setCaret(int charPos);

inline void getCaretPosition(int& line, int& character)
{
character = m_caret.X;
line = m_caret.Y;
}

inline int getNumLine()
{
return (int)m_arrayCharRender.size();
}

int getNumCharacter(int line);

bool getWordAtPosition(int line, int charPosition, int& from, int& to);

bool isCharacter(wchar_t c);

void doBackspace();

void doDelete();

void insert(wchar_t c);
#endif

protected:

#ifdef HAVE_CARET
void updateCaret();

void updateSetCaret();
#endif

void updateSplitText();

void splitText(std::vector<ArrayModuleOffset>& split, std::vector<ArrayInt>& format, std::vector<ArrayInt>& id, int width);

void getClosestCharacter(float posX, int x, int y, int line, const core::matrix4& world, int& character);

void initFont(IFont* font);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,17 @@ This file is part of the "Skylicht Engine".

namespace Skylicht
{
SModuleOffset::SModuleOffset()
SModuleOffset::SModuleOffset() :
OffsetX(0),
OffsetY(0),
FlipX(false),
FlipY(false),
XAdvance(0.0f),
Character(0),
Frame(NULL),
Module(NULL)
{
OffsetX = 0;
OffsetY = 0;
FlipX = false;
FlipY = false;

XAdvance = 0.0f;
Character = 0;

Frame = NULL;
Module = NULL;
}

void SModuleOffset::getPositionBuffer(video::S3DVertex* vertices, u16* indices, int vertexOffset, const core::matrix4& mat, float scaleW, float scaleH)
Expand Down
2 changes: 2 additions & 0 deletions Projects/Skylicht/UserInterface/Source/Motion/CMotion.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ namespace Skylicht
PointerOut,
PointerDown,
PointerUp,
Focus,
UnFocus,
NumEvent
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace Skylicht
m_visible(true),
m_isPointerHover(false),
m_isPointerDown(false),
m_enableTouchScreen(false)
m_contineGameEvent(false)
{
m_container->addChild(this);
}
Expand Down Expand Up @@ -165,6 +165,12 @@ namespace Skylicht
OnLostFocus(this);
}

void CUIBase::onKeyEvent(const SEvent& event)
{
if (OnKeyEvent != nullptr)
OnKeyEvent(this, event);
}

void CUIBase::startMotion(EMotionEvent event)
{
if (!m_element)
Expand Down
13 changes: 8 additions & 5 deletions Projects/Skylicht/UserInterface/Source/UserInterface/CUIBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace Skylicht
bool m_isPointerHover;
bool m_isPointerDown;

bool m_enableTouchScreen;
bool m_contineGameEvent;

public:
std::function<void(float, float)> OnPointerHover;
Expand All @@ -67,6 +67,7 @@ namespace Skylicht
std::function<void(CUIBase*)> OnPressed;
std::function<void(CUIBase*)> OnFocus;
std::function<void(CUIBase*)> OnLostFocus;
std::function<void(CUIBase*, const SEvent&)> OnKeyEvent;

public:
CUIBase(CUIContainer* container, CGUIElement* element);
Expand All @@ -91,14 +92,14 @@ namespace Skylicht
return m_visible;
}

inline void setEnableTouchScreen(bool b)
inline void setContinueGameEvent(bool b)
{
m_enableTouchScreen = b;
m_contineGameEvent = b;
}

inline bool isEnableTouchScreen()
inline bool isContinueGameEvent()
{
return m_enableTouchScreen;
return m_contineGameEvent;
}

inline void setPointerHover(bool b)
Expand Down Expand Up @@ -156,6 +157,8 @@ namespace Skylicht

virtual void onLostFocus();

virtual void onKeyEvent(const SEvent& event);

void startMotion(EMotionEvent event);

bool isMotionPlaying(EMotionEvent event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ namespace Skylicht

if (m_capture)
{
base = m_capture;

CUIContainer* captureContainer = m_capture->getContainer();
captureContainer->OnProcessEvent(event, m_capture);

Expand All @@ -81,8 +83,6 @@ namespace Skylicht
if (captureContainer != ui)
ui->onPointerOut(mouseX, mouseY);
}

base = m_capture;
}
else
{
Expand All @@ -105,7 +105,15 @@ namespace Skylicht
if (!base)
return true;
else
return base->isEnableTouchScreen();
return base->isContinueGameEvent();
}
else if (event.EventType == EET_KEY_INPUT_EVENT)
{
if (m_focus)
{
m_focus->onKeyEvent(event);
return m_focus->isContinueGameEvent();
}
}

return true;
Expand Down Expand Up @@ -144,11 +152,17 @@ namespace Skylicht
return;

if (m_focus && m_focus != focus)
{
m_focus->onLostFocus();
m_focus->startMotion(UI::EMotionEvent::UnFocus);
}

m_focus = focus;
if (m_focus)
{
m_focus->onFocus();
m_focus->startMotion(UI::EMotionEvent::Focus);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ namespace Skylicht

void setFocus(CUIBase* focus);

inline CUIBase* getFocus()
{
return m_focus;
}
};
}
}
Loading

0 comments on commit 34fe3f3

Please sign in to comment.