Skip to content

Commit

Permalink
Added function for printing GUI Objects tree
Browse files Browse the repository at this point in the history
  • Loading branch information
Neko-Box-Coder committed Jul 22, 2023
1 parent 9122c3e commit 5cae4ba
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
57 changes: 56 additions & 1 deletion Include/ssGUI/Enums/GUIObjectType.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#ifndef SSGUI_OBJECT_TYPE_H
#define SSGUI_OBJECT_TYPE_H

#include "ssGUI/HelperClasses/EnumToStringMacro.hpp"
#include <cstdint>
#include <string>

namespace ssGUI
{

Expand Down Expand Up @@ -66,7 +69,8 @@ namespace Enums
SLIDER = 1 << 13,
SCROLLBAR = 1 << 14,
IMAGE_CANVAS = 1 << 15,
STANDARD_SLIDER = 1 << 16
STANDARD_SLIDER = 1 << 16,
COUNT = 1 << 17
};

inline ssGUI::Enums::GUIObjectType operator|(ssGUI::Enums::GUIObjectType a, ssGUI::Enums::GUIObjectType b)
Expand All @@ -88,6 +92,57 @@ namespace Enums
{
return !(a==b);
};

namespace
{
inline std::string InternalGUIObjectTypeToString(GUIObjectType guiObjectType)
{
static_assert((int)GUIObjectType::COUNT == 1 << 17, "ToString");
switch(guiObjectType)
{
RETURN_ENUM_STRING(GUIObjectType::WINDOW);
RETURN_ENUM_STRING(GUIObjectType::WIDGET);
RETURN_ENUM_STRING(GUIObjectType::MAIN_WINDOW);
RETURN_ENUM_STRING(GUIObjectType::IMAGE);
RETURN_ENUM_STRING(GUIObjectType::TEXT);
RETURN_ENUM_STRING(GUIObjectType::BUTTON);
RETURN_ENUM_STRING(GUIObjectType::BASE_OBJECT);
RETURN_ENUM_STRING(GUIObjectType::STANDARD_WINDOW);
RETURN_ENUM_STRING(GUIObjectType::STANDARD_BUTTON);
RETURN_ENUM_STRING(GUIObjectType::MENU);
RETURN_ENUM_STRING(GUIObjectType::MENU_ITEM);
RETURN_ENUM_STRING(GUIObjectType::DROPDOWN);
RETURN_ENUM_STRING(GUIObjectType::TEXT_FIELD);
RETURN_ENUM_STRING(GUIObjectType::SLIDER);
RETURN_ENUM_STRING(GUIObjectType::SCROLLBAR);
RETURN_ENUM_STRING(GUIObjectType::IMAGE_CANVAS);
RETURN_ENUM_STRING(GUIObjectType::STANDARD_SLIDER);
RETURN_ENUM_STRING(GUIObjectType::COUNT);
}

return "";
}
}

//function: GUIObjectTypeToString
inline std::string GUIObjectTypeToString(GUIObjectType guiObjectType)
{
static_assert((int)GUIObjectType::COUNT == 1 << 17, "ToString");

std::string curString;
for(int i = 0; i < 18; ++i)
{
std::string returnString = InternalGUIObjectTypeToString(guiObjectType & (GUIObjectType)(1 << i));
if(!returnString.empty())
curString += returnString + ", ";
}

//Remove last comma
if(!curString.empty())
curString.erase(curString.begin() + curString.size() - 2);

return curString;
}
}

}
Expand Down
2 changes: 2 additions & 0 deletions Include/ssGUI/ssGUIManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ namespace ssGUI
//function: GetElapsedTimeInMillisecond
//See <ssGUI::Backend::BackendSystemInputInterface::GetElapsedTime>
uint64_t GetElapsedTimeInMillisecond() const;

void PrintGUIObjectTree() const;

//function: Clear
//Clears the console
Expand Down
35 changes: 35 additions & 0 deletions Src/ssGUI/ssGUIManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -915,5 +915,40 @@ namespace ssGUI
return BackendInput->GetElapsedTime();
}

void ssGUIManager::PrintGUIObjectTree() const
{
using depth = int;
using lastSpaces = std::string;

std::list<std::tuple<ssGUI::GUIObject*, depth, lastSpaces>> childrenToPrint;

for(auto it = MainWindowPList.begin(); it != MainWindowPList.end(); --it)
childrenToPrint.push_back(std::make_tuple(*it, 0, ""));

while(!childrenToPrint.empty())
{
ssGUI::GUIObject* curObj = std::get<0>(childrenToPrint.front());
int curDepth = std::get<1>(childrenToPrint.front());
std::string lastSpaces = std::get<2>(childrenToPrint.front());
childrenToPrint.pop_front();

bool hasNextWithSameDepth = childrenToPrint.empty() ? false : (std::get<1>(childrenToPrint.front()) == curDepth);

ssLOG_LINE(lastSpaces << "| ");
ssLOG_LINE(lastSpaces << "|---" << "GUI Object: " << curObj);
ssLOG_LINE(lastSpaces << (hasNextWithSameDepth ? "| " : " " ) << "Type: " << ssGUI::Enums::GUIObjectTypeToString(curObj->GetType()));

//Add the children to the list
std::vector<ssGUI::GUIObject*> curChildren = curObj->GetListOfChildren();

std::string nextSpaces = lastSpaces;
if(hasNextWithSameDepth)
nextSpaces += "| ";
else
nextSpaces += " ";

for(int i = curChildren.size() - 1; i >= 0; --i)
childrenToPrint.push_front(std::make_tuple(curChildren.at(i), curDepth + 1, nextSpaces));
}
}
}

0 comments on commit 5cae4ba

Please sign in to comment.