Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UIManager Class Reference

Draws the UI to the screen. More...

#include <UIManager.hxx>

+ Inheritance diagram for UIManager:
+ Collaboration diagram for UIManager:

Public Member Functions

void init ()
 Parses the UiLayout.json files and creates UI Elements. More...
 
struct ImFont * loadFont (const std::string &name, uint32_t size)
 
void initializeImGuiFonts ()
 
void loadSettings (json &uiLayout)
 
void parseLayouts (const json &uiLayout)
 
void drawUI ()
 Renders all UI Widgets. More...
 
void toggleDebugMenu ()
 Toggle Visibility of Debug Menu. More...
 
void setFPSCounterText (const std::string &fps)
 Helper function to update the FPS Counter. More...
 
std::unordered_map< std::string, LayoutData > & getLayouts ()
 Get the LayoutGroup container. More...
 
void setTooltip (const std::string &tooltipText)
 
void clearTooltip ()
 Hides and resets the active tooltip. More...
 
void closeOpenMenus ()
 Close all open menus but the build menu. More...
 
void openMenu (GameMenu::Ptr menuOption)
 
template<class Menu >
void openMenu ()
 
void closeMenu ()
 
bool isAnyMenuOpen () const
 
void addPersistentMenu (GameMenu::Ptr menu)
 
bool isMouseHovered () const
 
template<class Menu >
GameMenu::Ptr findMenu () const
 
template<class Menu >
void addPersistentMenu ()
 
BUILDMENU_LAYOUT buildMenuLayout () const
 
void setBuildMenuLayout (BUILDMENU_LAYOUT l)
 

Public Attributes

friend Singleton< UIManager >
 

Private Member Functions

 UIManager ()=default
 
 ~UIManager ()=default
 

Private Attributes

std::unordered_map< std::string, LayoutDatam_layouts
 map holding layput groups, accessible via the layoutgroup ID More...
 
std::string m_tooltip
 
std::unordered_map< std::string, ImFont * > m_loadedFonts
 
std::vector< GameMenu::Ptrm_menuStack
 
std::vector< GameMenu::Ptrm_persistentMenu
 
bool m_showDebugMenu = false
 visibility of the debug menu More...
 
std::string m_fpsCounter
 
bool m_showFpsCounter = true
 
struct ImFont * fontDefault = nullptr
 pointer to the default font used for in-game text More...
 
BUILDMENU_LAYOUT m_buildMenuLayout = BUILDMENU_LAYOUT::BOTTOM
 

Additional Inherited Members

- Static Public Member Functions inherited from Singleton< UIManager >
static UIManagerinstance (void)
 Get an instance of the singleton. More...
 
- Protected Member Functions inherited from Singleton< UIManager >
 Singleton () noexcept=default
 
 ~Singleton () noexcept=default
 

Detailed Description

Draws the UI to the screen.

Parses UiLayout.json file and instantiates UI widgets accordingly. Also takes care of layouting

Definition at line 39 of file UIManager.hxx.

Constructor & Destructor Documentation

◆ UIManager()

UIManager::UIManager ( )
privatedefault

◆ ~UIManager()

UIManager::~UIManager ( )
privatedefault

Member Function Documentation

◆ addPersistentMenu() [1/2]

template<class Menu >
void UIManager::addPersistentMenu ( )
inline

Definition at line 126 of file UIManager.hxx.

126 { addPersistentMenu(std::make_shared<Menu>()); }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ addPersistentMenu() [2/2]

void UIManager::addPersistentMenu ( GameMenu::Ptr  menu)

Definition at line 174 of file UIManager.cxx.

175 {
176  if (std::none_of(std::begin(m_persistentMenu), std::end(m_persistentMenu), [&](const auto &m) { return m == menu; }))
177  {
178  m_persistentMenu.push_back(menu);
179  }
180 }
+ Here is the caller graph for this function:

◆ buildMenuLayout()

BUILDMENU_LAYOUT UIManager::buildMenuLayout ( ) const
inline

Definition at line 128 of file UIManager.hxx.

128 { return m_buildMenuLayout; }

◆ clearTooltip()

void UIManager::clearTooltip ( )

Hides and resets the active tooltip.

See also
Tooltip::reset

Definition at line 224 of file UIManager.cxx.

224 { m_tooltip.clear(); }

◆ closeMenu()

void UIManager::closeMenu ( )

Definition at line 163 of file UIManager.cxx.

164 {
165  if (m_menuStack.empty())
166  return;
167 
168  m_menuStack.pop_back();
169 
172 }
+ Here is the call graph for this function:

◆ closeOpenMenus()

void UIManager::closeOpenMenus ( )

Close all open menus but the build menu.

Definition at line 144 of file UIManager.cxx.

145 {
146  for (auto &m : m_persistentMenu)
147  {
148  m->closeSubmenus();
149  }
150 }

◆ drawUI()

void UIManager::drawUI ( )

Renders all UI Widgets.

Definition at line 184 of file UIManager.cxx.

185 {
186 #ifdef MICROPROFILE_ENABLED
187  MICROPROFILE_SCOPEI("UI", "draw UI", MP_BLUE);
188 #endif
189  if (!m_menuStack.empty())
190  {
191  m_menuStack.back()->draw();
192  }
193 
194  for (const auto &m : m_persistentMenu)
195  {
196  m->draw();
197  }
198 
199  if (!m_tooltip.empty())
200  {
201  ImVec2 pos = ui::GetMousePos();
202  ui::SetCursorScreenPos(pos);
203  ui::Text(m_tooltip.c_str());
204  }
205 
206  if (m_showFpsCounter)
207  {
208  ui::SetNextWindowPos(ImVec2(0, 0));
209  ui::SetNextWindowSize(ImVec2(140, 20));
210 
211  bool open = true;
212  ui::Begin("##fpswindow", &open,
213  ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoScrollbar |
214  ImGuiWindowFlags_NoScrollWithMouse);
215  ui::Text(m_fpsCounter.c_str());
216  ui::SameLine();
217  ui::Checkbox("debug", &m_showDebugMenu);
218  ui::End();
219  }
220 }
+ Here is the caller graph for this function:

◆ findMenu()

template<class Menu >
GameMenu::Ptr UIManager::findMenu ( ) const
inline

Definition at line 105 of file UIManager.hxx.

106  {
107  for (const auto &m : m_menuStack)
108  {
109  if (dynamic_cast<Menu *>(m.get()))
110  {
111  return m;
112  }
113  }
114 
115  for (const auto &m : m_persistentMenu)
116  {
117  if (dynamic_cast<Menu *>(m.get()))
118  {
119  return m;
120  }
121  }
122 
123  return nullptr;
124  }

◆ getLayouts()

std::unordered_map<std::string, LayoutData>& UIManager::getLayouts ( )
inline

Get the LayoutGroup container.

Returns the container that holds LayoutGroups

Returns
std::unordered_map<std::string, LayoutGroup>&

Definition at line 79 of file UIManager.hxx.

79 { return m_layouts; }
+ Here is the caller graph for this function:

◆ init()

void UIManager::init ( )

Parses the UiLayout.json files and creates UI Elements.

+ Here is the caller graph for this function:

◆ initializeImGuiFonts()

void UIManager::initializeImGuiFonts ( )

Definition at line 69 of file UIManager.cxx.

70 {
71  std::string fontPath = fs::getBasePath() + Settings::instance().fontFileName.get(); // fix for macos, need to use abs path
72 
73  const auto names = {"MainMenuButtons", "PauseMenuButtons", "LoadDialogButtons", "BuildMenuButtons"};
74  for (const auto &name : names)
75  {
76  if (const auto it = m_layouts.find(name); it != m_layouts.end())
77  {
78  auto &layout = it->second;
79  layout.font = loadFont(fontPath, layout.fontSize);
80  }
81  }
82 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isAnyMenuOpen()

bool UIManager::isAnyMenuOpen ( ) const
inline

Definition at line 99 of file UIManager.hxx.

99 { return !m_menuStack.empty(); }

◆ isMouseHovered()

bool UIManager::isMouseHovered ( ) const

Definition at line 182 of file UIManager.cxx.

182 { return ImGui::IsAnyItemHovered(); }

◆ loadFont()

ImFont * UIManager::loadFont ( const std::string name,
uint32_t  size 
)

Definition at line 51 of file UIManager.cxx.

52 {
53  auto *uiFonts = ImGui::GetIO().Fonts;
54  if (!fontDefault)
55  fontDefault = uiFonts->AddFontDefault();
56 
57  std::string hashName = fontPath;
58  hashName.append(std::to_string(size));
59  if (const auto it = m_loadedFonts.find(hashName); it != m_loadedFonts.end())
60  return it->second;
61 
62  ImFont *newFont = uiFonts->AddFontFromFileTTF(fontPath.c_str(), (float)size);
63  m_loadedFonts[hashName] = newFont;
64  uiFonts->Build();
65 
66  return newFont;
67 }
+ Here is the caller graph for this function:

◆ loadSettings()

void UIManager::loadSettings ( json uiLayout)

Definition at line 84 of file UIManager.cxx.

85 {
86  std::string jsonFileContent = fs::readFileAsString(Settings::instance().uiLayoutJSONFile.get());
87  uiLayout = json::parse(jsonFileContent, nullptr, false);
88 
89  // check if json file can be parsed
90  if (uiLayout.is_discarded())
91  throw ConfigurationError(TRACE_INFO "Error parsing JSON File " + Settings::instance().uiLayoutJSONFile.get());
92 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ openMenu() [1/2]

template<class Menu >
void UIManager::openMenu ( )
inline

Definition at line 96 of file UIManager.hxx.

96 { openMenu(std::make_shared<Menu>()); }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ openMenu() [2/2]

void UIManager::openMenu ( GameMenu::Ptr  menuOption)

Definition at line 152 of file UIManager.cxx.

153 {
154  if (std::none_of(std::begin(m_menuStack), std::end(m_menuStack), [&](const auto &m) { return m == menu; }))
155  {
156  m_menuStack.push_back(menu);
157 
158  Camera::instance().canScale(false);
159  Camera::instance().canMove(false);
160  }
161 }
+ Here is the call graph for this function:

◆ parseLayouts()

void UIManager::parseLayouts ( const json uiLayout)

Definition at line 94 of file UIManager.cxx.

95 {
96  // parse Layout
97  for (const auto &it : uiLayout["LayoutGroups"].items())
98  {
99  std::string layoutGroupName;
100 
101  // prepare empty layout groups with layout information from json
102  for (size_t id = 0; id < uiLayout["LayoutGroups"][it.key()].size(); id++)
103  {
104  layoutGroupName = uiLayout["LayoutGroups"][it.key()][id].value("GroupName", "");
105 
106  if (!layoutGroupName.empty())
107  {
108  LayoutData layout;
109  layout.layoutType = uiLayout["LayoutGroups"][it.key()][id].value("LayoutType", "");
110  layout.alignment = uiLayout["LayoutGroups"][it.key()][id].value("Alignment", "");
111 
112  if (layout.layoutType.empty())
113  {
114  LOG(LOG_WARNING) << "Skipping LayoutGroup " << layoutGroupName
115  << " because it has no parameter \"LayoutType\" set. Check your UiLayout.json file.";
116  continue;
117  }
118 
119  if (layout.alignment.empty())
120  {
121  LOG(LOG_WARNING) << "Skipping LayoutGroup " << layoutGroupName
122  << " because it has no parameter \"Alignment\" set. Check your UiLayout.json file.";
123  continue;
124  }
125 
126  layout.fontSize = uiLayout["LayoutGroups"][it.key()][id].value("FontSize", Settings::instance().defaultFontSize);
127  layout.padding = uiLayout["LayoutGroups"][it.key()][id].value("Padding", 0);
128  layout.paddingToParent = uiLayout["LayoutGroups"][it.key()][id].value("PaddingToParent", 0);
129  layout.alignmentOffset = uiLayout["LayoutGroups"][it.key()][id].value("AlignmentOffset", 0.0F);
130 
131  // add layout group information to container
132  m_layouts[layoutGroupName] = layout;
133  }
134  else
135  {
136  LOG(LOG_WARNING) << "Cannot add a Layout Group without a name. Check your UiLayout.json file.";
137  }
138  }
139  }
140 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setBuildMenuLayout()

void UIManager::setBuildMenuLayout ( BUILDMENU_LAYOUT  l)
inline

Definition at line 129 of file UIManager.hxx.

129 { m_buildMenuLayout = l; }

◆ setFPSCounterText()

void UIManager::setFPSCounterText ( const std::string fps)

Helper function to update the FPS Counter.

Parameters
fps

Definition at line 142 of file UIManager.cxx.

142 { m_fpsCounter = fps; }
+ Here is the caller graph for this function:

◆ setTooltip()

void UIManager::setTooltip ( const std::string tooltipText)

Definition at line 222 of file UIManager.cxx.

222 { m_tooltip = tooltipText; }

◆ toggleDebugMenu()

void UIManager::toggleDebugMenu ( )
inline

Toggle Visibility of Debug Menu.

Definition at line 65 of file UIManager.hxx.

Member Data Documentation

◆ fontDefault

struct ImFont* UIManager::fontDefault = nullptr
private

pointer to the default font used for in-game text

Definition at line 151 of file UIManager.hxx.

◆ m_buildMenuLayout

BUILDMENU_LAYOUT UIManager::m_buildMenuLayout = BUILDMENU_LAYOUT::BOTTOM
private

Definition at line 152 of file UIManager.hxx.

◆ m_fpsCounter

std::string UIManager::m_fpsCounter
private

Definition at line 147 of file UIManager.hxx.

◆ m_layouts

std::unordered_map<std::string, LayoutData> UIManager::m_layouts
private

map holding layput groups, accessible via the layoutgroup ID

Definition at line 136 of file UIManager.hxx.

◆ m_loadedFonts

std::unordered_map<std::string, ImFont *> UIManager::m_loadedFonts
private

Definition at line 140 of file UIManager.hxx.

◆ m_menuStack

std::vector<GameMenu::Ptr> UIManager::m_menuStack
private

Definition at line 141 of file UIManager.hxx.

◆ m_persistentMenu

std::vector<GameMenu::Ptr> UIManager::m_persistentMenu
private

Definition at line 142 of file UIManager.hxx.

◆ m_showDebugMenu

bool UIManager::m_showDebugMenu = false
private

visibility of the debug menu

Definition at line 145 of file UIManager.hxx.

◆ m_showFpsCounter

bool UIManager::m_showFpsCounter = true
private

Definition at line 148 of file UIManager.hxx.

◆ m_tooltip

std::string UIManager::m_tooltip
private

Definition at line 138 of file UIManager.hxx.

◆ Singleton< UIManager >

Definition at line 42 of file UIManager.hxx.


The documentation for this class was generated from the following files:
LayoutData::layoutType
std::string layoutType
<mandatory> how to layout, default = HORIZONTAL
Definition: Layout.hxx:10
TRACE_INFO
#define TRACE_INFO
Definition: Exception.hxx:12
LayoutData::alignmentOffset
float alignmentOffset
Offset in percent to the screen point. can be negative.
Definition: Layout.hxx:12
ConfigurationError
A configuration error.
Definition: Exception.hxx:36
UIManager::openMenu
void openMenu()
Definition: UIManager.hxx:96
UIManager::m_showFpsCounter
bool m_showFpsCounter
Definition: UIManager.hxx:148
LOG
Definition: LOG.hxx:32
SettingsData::fontFileName
FilePath fontFileName
FilePath of the Font that should be used.
Definition: Settings.hxx:148
UIManager::m_loadedFonts
std::unordered_map< std::string, ImFont * > m_loadedFonts
Definition: UIManager.hxx:140
UIManager::m_fpsCounter
std::string m_fpsCounter
Definition: UIManager.hxx:147
UIManager::m_tooltip
std::string m_tooltip
Definition: UIManager.hxx:138
UIManager::fontDefault
struct ImFont * fontDefault
pointer to the default font used for in-game text
Definition: UIManager.hxx:151
UIManager::m_layouts
std::unordered_map< std::string, LayoutData > m_layouts
map holding layput groups, accessible via the layoutgroup ID
Definition: UIManager.hxx:136
readFileAsString
std::string readFileAsString(const std::string &fileName, bool binaryMode)
Read contents from a file as string.
Definition: Filesystem.cxx:12
Camera::canMove
void canMove(bool move)
Definition: Camera.hxx:49
getBasePath
std::string getBasePath()
Get base path (where Cytopia is being run)
Definition: Filesystem.cxx:77
LayoutData::fontSize
uint32_t fontSize
<internal> default font size of all elements in group
Definition: Layout.hxx:17
UIManager::m_menuStack
std::vector< GameMenu::Ptr > m_menuStack
Definition: UIManager.hxx:141
LayoutData::alignment
std::string alignment
<mandatory> where the element should be placed. e.g. CENTER
Definition: Layout.hxx:9
UIManager::m_persistentMenu
std::vector< GameMenu::Ptr > m_persistentMenu
Definition: UIManager.hxx:142
LayoutData::paddingToParent
int paddingToParent
padding between this group and the parent in pixels
Definition: Layout.hxx:14
UIManager::m_showDebugMenu
bool m_showDebugMenu
visibility of the debug menu
Definition: UIManager.hxx:145
UIManager::m_buildMenuLayout
BUILDMENU_LAYOUT m_buildMenuLayout
Definition: UIManager.hxx:152
UIManager::loadFont
struct ImFont * loadFont(const std::string &name, uint32_t size)
Definition: UIManager.cxx:51
Singleton< Camera >::instance
static Camera & instance(void)
Get an instance of the singleton.
Definition: Singleton.hxx:15
LayoutData::padding
int padding
padding between elements in pixels
Definition: Layout.hxx:13
UIManager::addPersistentMenu
void addPersistentMenu()
Definition: UIManager.hxx:126
string
std::string string
Definition: AudioConfig.hxx:14
Camera::canScale
void canScale(bool value)
Definition: Camera.hxx:55
LayoutData
Definition: Layout.hxx:7
StrongType::get
WeakType & get() noexcept
Definition: Meta.hxx:126
LOG_WARNING
@ LOG_WARNING
Definition: LOG.hxx:27