Enfisk

GUIManager

Feb 20th, 2014
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. //GUIManager.h
  2. #pragma once
  3. #include "stdafx.h"
  4. #include "GUIBase.h"
  5.  
  6. class DrawManager;
  7.  
  8. class GUIManager {
  9. public:
  10.     GUIManager();
  11.     ~GUIManager();
  12.  
  13.     void Draw(const std::string &p_identifier, DrawManager *p_drawManager);
  14.     void DrawAll(DrawManager *p_drawManager);
  15.    
  16.     void UpdatePlayerHealth(int p_currentHP, int p_maxHP);
  17.  
  18.     void Add(const std::string &p_identifier, GUIBase *p_GUIElement);
  19.     GUIBase* GetGUI(const std::string &p_identifier);
  20.     void Remove(const std::string &p_identifier);
  21.  
  22. private:
  23.     std::map<std::string, GUIBase*> m_guiElements;
  24. };
  25.  
  26.  
  27. //GUIManager.cpp
  28. #include "stdafx.h"
  29. #include "GUIManager.h"
  30. #include "DrawManager.h"
  31. #include "PlayerHealthBar.h"
  32.  
  33.  
  34. GUIManager::GUIManager()
  35. {
  36. }
  37.  
  38. GUIManager::~GUIManager()
  39. {
  40.     auto it = m_guiElements.begin();
  41.     while (it != m_guiElements.end()) {
  42.         delete it->second;
  43.         it = m_guiElements.erase(it);
  44.     }
  45.  
  46.     m_guiElements.clear();          //Redundant?
  47. }
  48.  
  49. void GUIManager::Draw(const std::string &p_identifier, DrawManager *p_drawManager)          //This function might also be redundant >.>
  50. {
  51.     auto it = m_guiElements.find(p_identifier);
  52.     if (it == m_guiElements.end()) {
  53.         printf("Can't find GUI Element %s!", p_identifier.c_str());
  54.         return;
  55.     }
  56.     else {
  57.         it->second->draw(p_drawManager->GetWindow());
  58.     }
  59. }
  60.  
  61. void GUIManager::DrawAll(DrawManager *p_drawManager)
  62. {
  63.     std::map<std::string, GUIBase*> tempStorage = m_guiElements;
  64.     auto it = tempStorage.begin();
  65.     for (int i = 0; i < GUIBase::Strata::StrataAmount; ++i) {
  66.         while (it != tempStorage.end())
  67.         {
  68.             if (it->second->GetStrata() == i) {
  69.                 it->second->draw(p_drawManager->GetWindow());
  70.                 //it = tempStorage.erase(it);
  71.             }
  72.             //else {
  73.             //  it++;
  74.             //}
  75.             ++it;
  76.         }
  77.         it = tempStorage.begin();
  78.     }
  79. }
  80.  
  81. void GUIManager::UpdatePlayerHealth(int p_currentHP, int p_maxHP)
  82. {
  83.     auto it = m_guiElements.begin();
  84.     while (it != m_guiElements.end()) {
  85.         PlayerHealthBar* HPBar = dynamic_cast<PlayerHealthBar*>(it->second);        //Need the checks from dynamic to make sure it actually is the health bar
  86.         if (HPBar == nullptr) {
  87.             continue;
  88.         }
  89.         else {
  90.             HPBar->Update(p_currentHP, p_maxHP);
  91.             break;
  92.         }
  93.  
  94.         ++it;
  95.     }
  96. }
  97.  
  98. void GUIManager::Add(const std::string &p_identifier, GUIBase *p_GUIElement)
  99. {
  100.     auto it = m_guiElements.find(p_identifier);
  101.     if (m_guiElements.size() > 0) {
  102.         if (it == m_guiElements.end()) {
  103.             printf("%s already in the GUIManager! Choose another identifier!", p_identifier);
  104.             return;
  105.         }
  106.         else {
  107.             m_guiElements.insert(std::make_pair(p_identifier, p_GUIElement));
  108.         }
  109.     }
  110.     else {
  111.         m_guiElements.insert(std::make_pair(p_identifier, p_GUIElement));
  112.     }
  113. }
  114.  
  115. GUIBase* GUIManager::GetGUI(const std::string &p_identifier)
  116. {
  117.     auto it = m_guiElements.find(p_identifier);
  118.     if (it != m_guiElements.end()) {
  119.         return it->second;
  120.     }
  121.     else {
  122.         printf("Can't find GUI Element %s!", p_identifier.c_str());
  123.         return nullptr;
  124.     }
  125. }
  126.  
  127. void GUIManager::Remove(const std::string &p_identifier)
  128. {
  129.     auto it = m_guiElements.find(p_identifier);
  130.     if (it == m_guiElements.end()) {
  131.         printf("Can't find %s in the GUI Map!", p_identifier);
  132.         return;
  133.     }
  134.     else {
  135.         delete it->second;
  136.         m_guiElements.erase(it);
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment