Skip to content

Commit

Permalink
Fixed Text losing focus should deselect text (j6bwJJhdu1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neko-Box-Coder committed May 25, 2023
1 parent 281fac0 commit cff48c6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Include/ssGUI/GUIObjectClasses/Text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ namespace ssGUI
bool SelectionAllowed; //See <IsTextSelectionAllowed>
int StartSelectionIndex; //See <GetStartSelectionIndex>
int EndSelectionIndex; //See <GetEndSelectionIndex>
bool DeselectWhenFocusLost; //See <IsDeselectWhenFocusLost>
glm::u8vec4 SelectionColor; //See <GetSelectionColor>
glm::u8vec4 TextSelectedColor; //See <GetTextSelectedColor>
Expand Down Expand Up @@ -82,6 +83,7 @@ namespace ssGUI
SelectionAllowed(true),
StartSelectionIndex(-1),
EndSelectionIndex(-1),
DeselectWhenFocusLost(true),
SelectionColor(51, 153, 255, 255),
TextSelectedColor(255, 255, 255, 255),
LastDefaultFontsID(0)
Expand Down Expand Up @@ -155,6 +157,7 @@ namespace ssGUI
bool SelectionAllowed; //See <IsTextSelectionAllowed>
int StartSelectionIndex; //See <GetStartSelectionIndex>
int EndSelectionIndex; //See <GetEndSelectionIndex>
bool DeselectWhenFocusLost; //See <IsDeselectWhenFocusLost>

glm::u8vec4 SelectionColor; //See <GetSelectionColor>
glm::u8vec4 TextSelectedColor; //See <GetTextSelectedColor>
Expand Down Expand Up @@ -486,6 +489,14 @@ namespace ssGUI
//Gets the text color when being selected
virtual glm::u8vec4 GetTextSelectedColor() const;

//function: SetDeselectWhenFocusLost
//Sets the text to deselect when its focus is lost or not
virtual void SetDeselectWhenFocusLost(bool deselectWhenFocusLost);

//function: IsDeselectWhenFocusLost
//Returns the text to deselect when its focus is lost or not
virtual bool IsDeselectWhenFocusLost() const;

//function: GetContainedCharacterIndexFromPos
//Gets the character index if the passed in position is contained inside a character
virtual int GetContainedCharacterIndexFromPos(glm::vec2 pos);
Expand Down
11 changes: 11 additions & 0 deletions Src/Tests/ManualTests/GUIObjectTests/TextTest_Manual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ void TextSelectionTest(bool firstRun) //7
ssLOG_SIMPLE("Press E to GetSelectionColor");
ssLOG_SIMPLE("Press R to SetTextSelectedColor to Green");
ssLOG_SIMPLE("Press T to GetTextSelectedColor");
ssLOG_SIMPLE("Press Y to Toggle SetDeselectWhenFocusLost");
ssLOG_SIMPLE("Press U to IsDeselectWhenFocusLost");
}

if(Manager->IsButtonOrKeyDown(ssGUI::Enums::NumberKey::EIGHT))
Expand Down Expand Up @@ -292,6 +294,15 @@ void TextSelectionTest(bool firstRun) //7
auto testColor = TestText->GetTextSelectedColor();
ssLOG_SIMPLE("GetSelectionColor called: "<<(int)testColor.r<<", "<<(int)testColor.g<<", "<<(int)testColor.b<<", "<<(int)testColor.a);
}
else if(Manager->IsButtonOrKeyDown(ssGUI::Enums::LetterKey::Y))
{
TestText->SetDeselectWhenFocusLost(!TestText->IsDeselectWhenFocusLost());
ssLOG_SIMPLE("SetDeselectWhenFocusLost called: "<<TestText->IsDeselectWhenFocusLost());
}
else if(Manager->IsButtonOrKeyDown(ssGUI::Enums::LetterKey::U))
{
ssLOG_SIMPLE("IsDeselectWhenFocusLost called: "<<TestText->IsDeselectWhenFocusLost());
}
}

void CharacterPosTest(bool firstRun) //8
Expand Down
20 changes: 20 additions & 0 deletions Src/ssGUI/GUIObjectClasses/Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace ssGUI
SelectionAllowed = other.IsTextSelectionAllowed();
StartSelectionIndex = other.GetStartSelectionIndex();
EndSelectionIndex = other.GetEndSelectionIndex();
DeselectWhenFocusLost = other.IsDeselectWhenFocusLost();
SelectionColor = other.GetSelectionColor();
TextSelectedColor = other.GetTextSelectedColor();
LastDefaultFontsID = other.LastDefaultFontsID;
Expand Down Expand Up @@ -1228,6 +1229,14 @@ namespace ssGUI
}
}

//Deselect when focus is lost
if( IsTextSelectionAllowed() && IsDeselectWhenFocusLost() && GetStartSelectionIndex() != GetEndSelectionIndex() &&
GetStartSelectionIndex() != -1 && !IsFocused())
{
SetStartSelectionIndex(GetStartSelectionIndex());
SetEndSelectionIndex(GetStartSelectionIndex());
}

if(inputStatus.KeyInputBlockedObject == nullptr)
{
//Text copying when ctrl+c is pressed and there is something highlighted
Expand Down Expand Up @@ -1282,6 +1291,7 @@ namespace ssGUI
SelectionAllowed(true),
StartSelectionIndex(-1),
EndSelectionIndex(-1),
DeselectWhenFocusLost(true),
SelectionColor(51, 153, 255, 255),
TextSelectedColor(255, 255, 255, 255),
LastDefaultFontsID(0)
Expand Down Expand Up @@ -1864,6 +1874,16 @@ namespace ssGUI
{
return TextSelectedColor;
}

void Text::SetDeselectWhenFocusLost(bool deselectWhenFocusLost)
{
DeselectWhenFocusLost = deselectWhenFocusLost;
}

bool Text::IsDeselectWhenFocusLost() const
{
return DeselectWhenFocusLost;
}

int Text::GetContainedCharacterIndexFromPos(glm::vec2 pos)
{
Expand Down

0 comments on commit cff48c6

Please sign in to comment.