Skip to content

Commit

Permalink
Add Event Callback for text change (L0hp5jTY7Y)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neko-Box-Coder committed Jul 30, 2023
1 parent 24ba27d commit 4bd3a30
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 47 deletions.
7 changes: 5 additions & 2 deletions Include/ssGUI/Enums/EventType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ namespace ssGUI
WINDOW_DRAG_STATE_CHANGED - Triggered *after* this GUI object (window) drag state is changed. Window(Container) will be the source for triggering this event callback.
OBJECT_UPDATED - Triggered *after* this GUI object is being updated. Container will be the source for triggering this event callback.
<ssGUI::EventInfo::CustomInfo> will be pointer to <ssGUI::ObjectUpdateInfo> struct.
TEXT_CONTENT_CHANGED - Triggered *shortly after* there's a text content change (Not visual change such as underline or bold).
The text GUI object that has the text being changed will be the source for triggering this event callback.
COUNT - Count
SCROLLBAR_VALUE_CHANGED - Same as <SLIDER_VALUE_CHANGED>
Expand Down Expand Up @@ -90,6 +91,7 @@ namespace ssGUI
ITEM_SELECTED,
MIN_MAX_SIZE_CHANGED,
OBJECT_RENDERED,
TEXT_CONTENT_CHANGED,
RECURSIVE_CHILD_ADDED,
RECURSIVE_CHILD_REMOVED,
SIZE_CHANGED,
Expand All @@ -109,7 +111,7 @@ namespace ssGUI
//function: EventTypeToString
inline std::string EventTypeToString(EventType event)
{
static_assert((int)EventType::COUNT == 26, "ToString");
static_assert((int)EventType::COUNT == 27, "ToString");
switch(event)
{
RETURN_ENUM_STRING(EventType::NONE);
Expand All @@ -132,6 +134,7 @@ namespace ssGUI
RETURN_ENUM_STRING(EventType::ITEM_SELECTED);
RETURN_ENUM_STRING(EventType::MIN_MAX_SIZE_CHANGED);
RETURN_ENUM_STRING(EventType::OBJECT_RENDERED);
RETURN_ENUM_STRING(EventType::TEXT_CONTENT_CHANGED);
RETURN_ENUM_STRING(EventType::RECURSIVE_CHILD_ADDED);
RETURN_ENUM_STRING(EventType::RECURSIVE_CHILD_REMOVED);
RETURN_ENUM_STRING(EventType::SIZE_CHANGED);
Expand Down
7 changes: 6 additions & 1 deletion Include/ssGUI/GUIObjectClasses/Text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ namespace ssGUI
uint32_t LastDefaultFontsID; //(Internal variable) Used to keep track if there's any changes to the default fonts
bool TextContentChanged; //(Internal variable) Used to keep track if there's any changes to the text content
static int TextObjectCount; //(Internal variable) Used for deallocating default resources
static std::vector<ssGUI::Font*> DefaultFonts; //See <GetDefaultFont>
static uint32_t DefaultFontsChangeID; //(Internal variable) Used to track default font changes
Expand Down Expand Up @@ -86,7 +88,8 @@ namespace ssGUI
DeselectWhenFocusLost(true),
SelectionColor(51, 153, 255, 255),
TextSelectedColor(255, 255, 255, 255),
LastDefaultFontsID(0)
LastDefaultFontsID(0),
TextContentChanged(false)
{
SetBackgroundColor(glm::ivec4(255, 255, 255, 0));
SetBlockInput(false);
Expand Down Expand Up @@ -164,6 +167,8 @@ namespace ssGUI

uint32_t LastDefaultFontsID; //(Internal variable) Used to keep track if there's any changes to the default fonts

bool TextContentChanged; //(Internal variable) Used to keep track if there's any changes to the text content

static int TextObjectCount; //(Internal variable) Used for deallocating default resources
static std::vector<ssGUI::Font*> DefaultFonts; //See <GetDefaultFont>
static uint32_t DefaultFontsChangeID; //(Internal variable) Used to track default font changes
Expand Down
32 changes: 32 additions & 0 deletions Src/Examples/Playground/TextfieldPlayground.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "ssGUI/HeaderGroups/StandardGroup.hpp"

#include "ssLogger/ssLog.hpp"
#include "ExamplesResources.h"

int main()
{
ssGUI::MainWindow mainWindow;

//Creating ssGUIManager and run it
ssGUI::ssGUIManager guiManager;
auto* textfield = mainWindow.AddChild<ssGUI::TextField>();

textfield->SetSize(glm::vec2(500, 250));
textfield->SetPosition(glm::vec2(50, 50));
textfield->SetWrappingMode(ssGUI::Enums::TextWrapping::WORD_WRAPPING);

textfield->AddEventCallback(ssGUI::Enums::EventType::TEXT_CONTENT_CHANGED)->AddEventListener
(
"Key",
[](ssGUI::EventInfo& info)
{
ssLOG_LINE("Changing...");
}
);

guiManager.AddRootGUIObject((ssGUI::GUIObject*)&mainWindow);

guiManager.StartRunning();

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ void DrawFontTest()
}
}

//NOTE: Skipping AddImageCache for now, it is used internally anyway so it should be working.
//TODO: Need to test AddImageCache

void RemoveImageCacheTest()
{
Expand Down
25 changes: 20 additions & 5 deletions Src/ssGUI/GUIObjectClasses/Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,12 @@ namespace ssGUI
inputStatus.KeyInputBlockedObject = this;
}
}

//Handle event callbacks
if(IsEventCallbackExist(ssGUI::Enums::EventType::TEXT_CONTENT_CHANGED) && TextContentChanged)
GetEventCallback(ssGUI::Enums::EventType::TEXT_CONTENT_CHANGED)->Notify(this);

TextContentChanged = false;
}

const std::string Text::ListenerKey = "Text";
Expand Down Expand Up @@ -1293,7 +1299,8 @@ namespace ssGUI
DeselectWhenFocusLost(true),
SelectionColor(51, 153, 255, 255),
TextSelectedColor(255, 255, 255, 255),
LastDefaultFontsID(0)
LastDefaultFontsID(0),
TextContentChanged(false)
{
SetBackgroundColor(glm::ivec4(255, 255, 255, 0));
SetBlockInput(false);
Expand Down Expand Up @@ -1383,8 +1390,7 @@ namespace ssGUI
{
RecalculateTextNeeded = true;

CurrentCharactersDetails.Clear();
ProcessedCharacterDetails.clear();
ClearAllCharacterDetails();
std::vector<ssGUI::CharacterDetails> newDetails;
ConstructCharacterDetails(text, newDetails);
AddCharacterDetails(newDetails);
Expand All @@ -1395,8 +1401,7 @@ namespace ssGUI
{
RecalculateTextNeeded = true;

CurrentCharactersDetails.Clear();
ProcessedCharacterDetails.clear();
ClearAllCharacterDetails();
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring currentText = converter.from_bytes(text);
std::vector<ssGUI::CharacterDetails> newDetails;
Expand Down Expand Up @@ -1492,6 +1497,7 @@ namespace ssGUI
CurrentCharactersDetails[index] = details;

RecalculateTextNeeded = true;
TextContentChanged = true;
RedrawObject();
}

Expand All @@ -1516,13 +1522,15 @@ namespace ssGUI
CurrentCharactersDetails.Add(details, index);

RecalculateTextNeeded = true;
TextContentChanged = true;
RedrawObject();
}

void Text::AddCharacterDetails(ssGUI::CharacterDetails details)
{
CurrentCharactersDetails.Add(details);
RecalculateTextNeeded = true;
TextContentChanged = true;
RedrawObject();
}

Expand All @@ -1534,12 +1542,16 @@ namespace ssGUI
CurrentCharactersDetails.Add(details, index);

RecalculateTextNeeded = true;
TextContentChanged = true;
RedrawObject();
}

void Text::AddCharacterDetails(std::vector<ssGUI::CharacterDetails>& details)
{
CurrentCharactersDetails.Add(details);
RecalculateTextNeeded = true;
TextContentChanged = true;
RedrawObject();
}

void Text::RemoveCharacterDetails(int index)
Expand All @@ -1549,6 +1561,7 @@ namespace ssGUI

CurrentCharactersDetails.Remove(index);
RecalculateTextNeeded = true;
TextContentChanged = true;
RedrawObject();
}

Expand All @@ -1562,13 +1575,15 @@ namespace ssGUI

CurrentCharactersDetails.Remove(startIndex, exclusiveEndIndex);
RecalculateTextNeeded = true;
TextContentChanged = true;
RedrawObject();
}

void Text::ClearAllCharacterDetails()
{
CurrentCharactersDetails.Clear();
RecalculateTextNeeded = true;
TextContentChanged = true;
RedrawObject();
}

Expand Down
Loading

0 comments on commit 4bd3a30

Please sign in to comment.