Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreGuiManager.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2002 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #include "OgreStableHeaders.h"
00026 
00027 #include "OgreString.h"
00028 #include "OgreGuiManager.h"
00029 #include "OgreGuiElement.h"
00030 #include "OgreGuiContainer.h"
00031 #include "OgreGuiElementFactory.h"
00032 #include "OgreException.h"
00033 #include "OgreLogManager.h"
00034 
00035 namespace Ogre {
00036     //-----------------------------------------------------------------------
00037     template<> GuiManager* Singleton<GuiManager>::ms_Singleton = 0;
00038     //---------------------------------------------------------------------
00039     GuiManager::GuiManager()
00040     {
00041     }
00042     //---------------------------------------------------------------------
00043     GuiManager::~GuiManager()
00044     {
00045         destroyAllGuiElements(false);
00046         destroyAllGuiElements(true);
00047     }
00048 
00049     //---------------------------------------------------------------------
00050     GuiManager::ElementMap& GuiManager::getElementMap(bool isTemplate)
00051     {
00052         return (isTemplate)?mTemplates:mInstances;
00053     }
00054 
00055     //---------------------------------------------------------------------
00056     GuiElement* GuiManager::createGuiElementFromTemplate(const String& templateName, const String& typeName, const String& instanceName, bool isTemplate)
00057     {
00058 
00059         GuiElement* newObj  = NULL;
00060 
00061         if (templateName == "")
00062         {
00063             newObj = createGuiElement(typeName, instanceName, isTemplate);
00064         }
00065         else
00066         {
00067             // no template 
00068             GuiElement* templateGui = getGuiElement(templateName, true);
00069     
00070             String typeNameToCreate;
00071             if (typeName == "")
00072             {
00073                 typeNameToCreate = templateGui->getTypeName();
00074             }
00075             else
00076             {
00077                 typeNameToCreate = typeName;
00078             }
00079 
00080             newObj = createGuiElement(typeNameToCreate, instanceName, isTemplate);
00081 
00082             ((GuiContainer*)newObj)->copyFromTemplate(templateGui);
00083         }
00084         
00085         return newObj;
00086     }
00087 
00088     //---------------------------------------------------------------------
00089     GuiElement* GuiManager::createGuiElement(const String& typeName, const String& instanceName, bool isTemplate)
00090     {
00091         return createGuiElementImpl(typeName, instanceName, getElementMap(isTemplate));
00092     }
00093 
00094     //---------------------------------------------------------------------
00095     GuiElement* GuiManager::createGuiElementImpl(const String& typeName, const String& instanceName, ElementMap& elementMap)
00096     {
00097         // Check not duplicated
00098         ElementMap::iterator ii = elementMap.find(instanceName);
00099         if (ii != elementMap.end())
00100         {
00101             Except(Exception::ERR_DUPLICATE_ITEM, "GuiElement with name " + instanceName +
00102                 " already exists.", "GuiManager::createGuiElement" );
00103         }
00104         GuiElement* newElem = createGuiElementFromFactory(typeName, instanceName);
00105         newElem->initialise();
00106 
00107         // Register
00108         elementMap.insert(ElementMap::value_type(instanceName, newElem));
00109 
00110         return newElem;
00111 
00112 
00113     }
00114 
00115     //---------------------------------------------------------------------
00116     GuiElement* GuiManager::createGuiElementFromFactory(const String& typeName, const String& instanceName)
00117     {
00118         // Look up factory
00119         FactoryMap::iterator fi = mFactories.find(typeName);
00120         if (fi == mFactories.end())
00121         {
00122             Except(Exception::ERR_ITEM_NOT_FOUND, "Cannot locate factory for element type " + typeName,
00123                 "GuiManager::createGuiElement");
00124         }
00125 
00126         // create
00127         return fi->second->createGuiElement(instanceName);
00128     }
00129 
00130     //---------------------------------------------------------------------
00131     GuiElement* GuiManager::getGuiElement(const String& name, bool isTemplate)
00132     {
00133         return getGuiElementImpl(name, getElementMap(isTemplate));
00134     }
00135     //---------------------------------------------------------------------
00136     GuiElement* GuiManager::getGuiElementImpl(const String& name, ElementMap& elementMap)
00137     {
00138         // Locate instance
00139         ElementMap::iterator ii = elementMap.find(name);
00140         if (ii == elementMap.end())
00141         {
00142             Except(Exception::ERR_ITEM_NOT_FOUND, "GuiElement with name " + name +
00143                 " not found.", "GuiManager::destroyGugetGuiElementiElement" );
00144         }
00145 
00146         return ii->second;
00147     }
00148     //---------------------------------------------------------------------
00149     void GuiManager::destroyGuiElement(const String& instanceName, bool isTemplate)
00150     {
00151         destroyGuiElementImpl(instanceName, getElementMap(isTemplate));
00152     }
00153 
00154     //---------------------------------------------------------------------
00155     void GuiManager::destroyGuiElement(GuiElement* pInstance, bool isTemplate)
00156     {
00157         destroyGuiElementImpl(pInstance->getName(), getElementMap(isTemplate));
00158     }
00159 
00160     //---------------------------------------------------------------------
00161     void GuiManager::destroyGuiElementImpl(const String& instanceName, ElementMap& elementMap)
00162     {
00163         // Locate instance
00164         ElementMap::iterator ii = elementMap.find(instanceName);
00165         if (ii == elementMap.end())
00166         {
00167             Except(Exception::ERR_ITEM_NOT_FOUND, "GuiElement with name " + instanceName +
00168                 " not found.", "GuiManager::destroyGuiElement" );
00169         }
00170         // Look up factory
00171         const String& typeName = ii->second->getTypeName();
00172         FactoryMap::iterator fi = mFactories.find(typeName);
00173         if (fi == mFactories.end())
00174         {
00175             Except(Exception::ERR_ITEM_NOT_FOUND, "Cannot locate factory for element type " + typeName,
00176                 "GuiManager::destroyGuiElement");
00177         }
00178 
00179         fi->second->destroyGuiElement(ii->second);
00180         elementMap.erase(ii);
00181     }
00182     //---------------------------------------------------------------------
00183     void GuiManager::destroyAllGuiElements(bool isTemplate)
00184     {
00185         destroyAllGuiElementsImpl(getElementMap(isTemplate));
00186     }
00187     //---------------------------------------------------------------------
00188     void GuiManager::destroyAllGuiElementsImpl(ElementMap& elementMap)
00189     {
00190         ElementMap::iterator i, iend;
00191         iend = elementMap.end();
00192         for (i = elementMap.begin(); i != iend; ++i)
00193         {
00194             // Get factory to delete
00195             FactoryMap::iterator fi = mFactories.find(i->second->getTypeName());
00196             if (fi == mFactories.end())
00197             {
00198                 Except(Exception::ERR_ITEM_NOT_FOUND, "Cannot locate factory for element " 
00199                     + i->second->getName(),
00200                     "GuiManager::destroyAllGuiElements");
00201             }
00202 
00203             // Destroy
00204             fi->second->destroyGuiElement(i->second);
00205         }
00206         elementMap.clear();
00207 
00208     }
00209     //---------------------------------------------------------------------
00210     void GuiManager::addGuiElementFactory(GuiElementFactory* elemFactory)
00211     {
00212         // Add / replace
00213         mFactories[elemFactory->getTypeName()] = elemFactory;
00214 
00215         LogManager::getSingleton().logMessage("GuiElementFactory for type " + elemFactory->getTypeName()
00216             + " registered.");
00217     }
00218     //---------------------------------------------------------------------
00219     GuiManager& GuiManager::getSingleton(void)
00220     {
00221         return Singleton<GuiManager>::getSingleton();
00222     }
00223 
00224 
00225 }

Copyright © 2002-2003 by The OGRE Team
Last modified Wed Jan 21 00:10:13 2004