Skip to content

Commit

Permalink
Add name property to GUI Object (iqXTrGl9V7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neko-Box-Coder committed May 9, 2023
1 parent ddde9ce commit 1c28cbb
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 2 deletions.
24 changes: 24 additions & 0 deletions Include/ssGUI/DataClasses/Hierarchy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,30 @@ namespace ssGUI
//To preserve the current children iterator, use <StashChildrenIterator>.
virtual bool FindChild(ssGUI::GUIObject* child);

//function: GetChild
//Gets the first child with specified name. Nullptr if not found.
virtual ssGUI::GUIObject* GetChild(std::string childName, bool recursive = false) const;

//function: GetChild
//See above
template<typename T>
T* GetChild(std::string childName, bool recursive = false) const
{
if(std::is_base_of<ssGUI::GUIObject, T>::value)
return static_cast<T*>(GetChild(childName, recursive));
else
{
ssGUI_WARNING(ssGUI_DATA_TAG, "You cannot add non GUI object");
return nullptr;
}
}

//function: GetChildrenWithTag
//Gets all the children that have the specified tag
virtual void GetChildrenWithTag(std::string tag,
std::vector<ssGUI::GUIObject*>& foundChildren,
bool recursive = false) const;

//function: GetCurrentChild
//Returns the object the children iterator is currently pointing to. This will remove nullptr if it is not pointing at any child.
//To check if the current children iterator is valid, use <IsChildrenIteratorEnd>.
Expand Down
11 changes: 10 additions & 1 deletion Include/ssGUI/GUIObjectClasses/GUIObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ namespace ssGUI
glm::vec2 LastGlobalPosition; //(Internal variable) Used for cache rendering
std::unordered_set<std::string> CurrentTags; //See <HasTag>
ssGUI::ssGUIObjectIndex RightClickMenuId; //See <RegisterRightClickMenu>
std::string CurrentName; //See <GetName>
=================================================================
============================== C++ ==============================
GUIObject::GUIObject() : LastGlobalPosition(),
CurrentTags(),
RightClickMenuId(-1)
RightClickMenuId(-1),
CurrentName()
{
SetupComponents();
}
Expand All @@ -49,6 +51,7 @@ namespace ssGUI
glm::vec2 LastGlobalPosition; //(Internal variable) Used for cache rendering
std::unordered_set<std::string> CurrentTags; //See <HasTag>
ssGUI::ssGUIObjectIndex RightClickMenuId; //See <RegisterRightClickMenu>
std::string CurrentName; //See <GetName>

GUIObject(GUIObject const& other);

Expand Down Expand Up @@ -83,6 +86,12 @@ namespace ssGUI
//Returns true if the tag exists on this GUI Object
virtual bool HasTag(std::string tag) const;

//function: SetName
virtual void SetName(std::string name);

//function: GetName
virtual std::string GetName() const;

//function: RegisterRightClickMenu
//Register this GUI Object to a menu that can be triggered by right click
virtual void RegisterRightClickMenu(ssGUI::Menu* menu);
Expand Down
78 changes: 78 additions & 0 deletions Src/ssGUI/DataClasses/Hierarchy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,84 @@ namespace ssGUI
CurrentChildIteratorBackEnd = false;
return found;
}

ssGUI::GUIObject* Hierarchy::GetChild(std::string childName, bool recursive) const
{
std::vector<ssGUI::GUIObject*> children = GetListOfChildren();

int nonRecursiveChildrenCount = children.size();
for(int i = 0; i < nonRecursiveChildrenCount; i++)
{
if(children[i]->GetName() == childName)
return children[i];

if(recursive)
{
std::vector<ssGUI::GUIObject*> recursiveChildren = children[i]->GetListOfChildren();

for(int j = 0; j < recursiveChildren.size(); j++)
children.push_back(recursiveChildren[j]);
}
}

if(!recursive)
return nullptr;

int currentIndex = nonRecursiveChildrenCount;
while(currentIndex < children.size())
{
if(children[currentIndex]->GetName() == childName)
return children[currentIndex];

std::vector<ssGUI::GUIObject*> recursiveChildren = children[currentIndex]->GetListOfChildren();

for(int j = 0; j < recursiveChildren.size(); j++)
children.push_back(recursiveChildren[j]);

currentIndex++;
}

return nullptr;
}

void Hierarchy::GetChildrenWithTag( std::string tag,
std::vector<ssGUI::GUIObject*>& foundChildren,
bool recursive) const
{
std::vector<ssGUI::GUIObject*> children = GetListOfChildren();

int nonRecursiveChildrenCount = children.size();
for(int i = 0; i < nonRecursiveChildrenCount; i++)
{
if(children[i]->HasTag(tag))
foundChildren.push_back(children[i]);

if(recursive)
{
std::vector<ssGUI::GUIObject*> recursiveChildren = children[i]->GetListOfChildren();

for(int j = 0; j < recursiveChildren.size(); j++)
children.push_back(recursiveChildren[j]);
}
}

if(!recursive)
return;

int currentIndex = nonRecursiveChildrenCount;
while(currentIndex < children.size())
{
if(children[currentIndex]->HasTag(tag))
foundChildren.push_back(children[currentIndex]);

std::vector<ssGUI::GUIObject*> recursiveChildren = children[currentIndex]->GetListOfChildren();

for(int j = 0; j < recursiveChildren.size(); j++)
children.push_back(recursiveChildren[j]);

currentIndex++;
}
}

ssGUI::GUIObject* Hierarchy::GetCurrentChild()
{
Expand Down
14 changes: 13 additions & 1 deletion Src/ssGUI/GUIObjectClasses/GUIObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace ssGUI
LastGlobalPosition = other.LastGlobalPosition;
CurrentTags = other.CurrentTags;// std::unordered_set<std::string>();
RightClickMenuId = other.RightClickMenuId;
CurrentName = other.CurrentName;

SetupComponents();

Expand Down Expand Up @@ -214,7 +215,8 @@ namespace ssGUI

GUIObject::GUIObject() : LastGlobalPosition(),
CurrentTags(),
RightClickMenuId(-1)
RightClickMenuId(-1),
CurrentName()
{
SetupComponents();
}
Expand Down Expand Up @@ -260,6 +262,16 @@ namespace ssGUI
{
return CurrentTags.find(tag) != CurrentTags.end();
}

void GUIObject::SetName(std::string name)
{
CurrentName = name;
}

std::string GUIObject::GetName() const
{
return CurrentName;
}

void GUIObject::RegisterRightClickMenu(ssGUI::Menu* menu)
{
Expand Down

0 comments on commit 1c28cbb

Please sign in to comment.