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 
00026 #include <vector>
00027 #include "OgreStableHeaders.h"
00028 
00029 #include "OgreString.h"
00030 #include "OgreGuiManager.h"
00031 #include "OgreGuiElement.h"
00032 #include "OgreGuiContainer.h"
00033 #include "OgreGuiElementFactory.h"
00034 #include "OgreException.h"
00035 #include "OgreLogManager.h"
00036 
00037 namespace Ogre {
00038     //-----------------------------------------------------------------------
00039     template<> GuiManager* Singleton<GuiManager>::ms_Singleton = 0;
00040     GuiManager* GuiManager::getSingletonPtr(void)
00041     {
00042         return ms_Singleton;
00043     }
00044     GuiManager& GuiManager::getSingleton(void)
00045     {  
00046         assert( ms_Singleton );  return ( *ms_Singleton );  
00047     }
00048     //---------------------------------------------------------------------
00049     GuiManager::GuiManager()
00050     {
00051     }
00052     //---------------------------------------------------------------------
00053     GuiManager::~GuiManager()
00054     {
00055         destroyAllGuiElements(false);
00056         destroyAllGuiElements(true);
00057     }
00058 
00059     //---------------------------------------------------------------------
00060     GuiManager::ElementMap& GuiManager::getElementMap(bool isTemplate)
00061     {
00062         return (isTemplate)?mTemplates:mInstances;
00063     }
00064 
00065     //---------------------------------------------------------------------
00066     GuiElement* GuiManager::createGuiElementFromTemplate(const String& templateName, const String& typeName, const String& instanceName, bool isTemplate)
00067     {
00068 
00069         GuiElement* newObj  = NULL;
00070 
00071         if (templateName == "")
00072         {
00073             newObj = createGuiElement(typeName, instanceName, isTemplate);
00074         }
00075         else
00076         {
00077             // no template 
00078             GuiElement* templateGui = getGuiElement(templateName, true);
00079     
00080             String typeNameToCreate;
00081             if (typeName == "")
00082             {
00083                 typeNameToCreate = templateGui->getTypeName();
00084             }
00085             else
00086             {
00087                 typeNameToCreate = typeName;
00088             }
00089 
00090             newObj = createGuiElement(typeNameToCreate, instanceName, isTemplate);
00091 
00092             ((GuiContainer*)newObj)->copyFromTemplate(templateGui);
00093         }
00094         
00095         return newObj;
00096     }
00097 
00098 
00099     //---------------------------------------------------------------------
00100     GuiElement* GuiManager::cloneGuiElementFromTemplate(const String& templateName, const String& instanceName)
00101     {
00102         GuiElement* templateGui = getGuiElement(templateName, true);
00103         return templateGui->clone(instanceName);
00104     }
00105 
00106     //---------------------------------------------------------------------
00107     GuiElement* GuiManager::createGuiElement(const String& typeName, const String& instanceName, bool isTemplate)
00108     {
00109         return createGuiElementImpl(typeName, instanceName, getElementMap(isTemplate));
00110     }
00111 
00112     //---------------------------------------------------------------------
00113     GuiElement* GuiManager::createGuiElementImpl(const String& typeName, const String& instanceName, ElementMap& elementMap)
00114     {
00115         // Check not duplicated
00116         ElementMap::iterator ii = elementMap.find(instanceName);
00117         if (ii != elementMap.end())
00118         {
00119             Except(Exception::ERR_DUPLICATE_ITEM, "GuiElement with name " + instanceName +
00120                 " already exists.", "GuiManager::createGuiElement" );
00121         }
00122         GuiElement* newElem = createGuiElementFromFactory(typeName, instanceName);
00123         newElem->initialise();
00124 
00125         // Register
00126         elementMap.insert(ElementMap::value_type(instanceName, newElem));
00127 
00128         return newElem;
00129 
00130 
00131     }
00132 
00133     //---------------------------------------------------------------------
00134     GuiElement* GuiManager::createGuiElementFromFactory(const String& typeName, const String& instanceName)
00135     {
00136         // Look up factory
00137         FactoryMap::iterator fi = mFactories.find(typeName);
00138         if (fi == mFactories.end())
00139         {
00140             Except(Exception::ERR_ITEM_NOT_FOUND, "Cannot locate factory for element type " + typeName,
00141                 "GuiManager::createGuiElement");
00142         }
00143 
00144         // create
00145         return fi->second->createGuiElement(instanceName);
00146     }
00147 
00148     //---------------------------------------------------------------------
00149     GuiElement* GuiManager::getGuiElement(const String& name, bool isTemplate)
00150     {
00151         return getGuiElementImpl(name, getElementMap(isTemplate));
00152     }
00153     //---------------------------------------------------------------------
00154     GuiElement* GuiManager::getGuiElementImpl(const String& name, ElementMap& elementMap)
00155     {
00156         // Locate instance
00157         ElementMap::iterator ii = elementMap.find(name);
00158         if (ii == elementMap.end())
00159         {
00160             Except(Exception::ERR_ITEM_NOT_FOUND, "GuiElement with name " + name +
00161                 " not found.", "GuiManager::destroyGugetGuiElementiElement" );
00162         }
00163 
00164         return ii->second;
00165     }
00166     //---------------------------------------------------------------------
00167     void GuiManager::destroyGuiElement(const String& instanceName, bool isTemplate)
00168     {
00169         destroyGuiElementImpl(instanceName, getElementMap(isTemplate));
00170     }
00171 
00172     //---------------------------------------------------------------------
00173     void GuiManager::destroyGuiElement(GuiElement* pInstance, bool isTemplate)
00174     {
00175         destroyGuiElementImpl(pInstance->getName(), getElementMap(isTemplate));
00176     }
00177 
00178     //---------------------------------------------------------------------
00179     void GuiManager::destroyGuiElementImpl(const String& instanceName, ElementMap& elementMap)
00180     {
00181         // Locate instance
00182         ElementMap::iterator ii = elementMap.find(instanceName);
00183         if (ii == elementMap.end())
00184         {
00185             Except(Exception::ERR_ITEM_NOT_FOUND, "GuiElement with name " + instanceName +
00186                 " not found.", "GuiManager::destroyGuiElement" );
00187         }
00188         // Look up factory
00189         const String& typeName = ii->second->getTypeName();
00190         FactoryMap::iterator fi = mFactories.find(typeName);
00191         if (fi == mFactories.end())
00192         {
00193             Except(Exception::ERR_ITEM_NOT_FOUND, "Cannot locate factory for element type " + typeName,
00194                 "GuiManager::destroyGuiElement");
00195         }
00196 
00197         fi->second->destroyGuiElement(ii->second);
00198         elementMap.erase(ii);
00199     }
00200     //---------------------------------------------------------------------
00201     void GuiManager::destroyAllGuiElements(bool isTemplate)
00202     {
00203         destroyAllGuiElementsImpl(getElementMap(isTemplate));
00204     }
00205     //---------------------------------------------------------------------
00206     void GuiManager::destroyAllGuiElementsImpl(ElementMap& elementMap)
00207     {
00208         ElementMap::iterator i;
00209 
00210         while ((i = elementMap.begin()) != elementMap.end())
00211         {
00212             GuiElement* element = i->second;
00213 
00214             // Get factory to delete
00215             FactoryMap::iterator fi = mFactories.find(element->getTypeName());
00216             if (fi == mFactories.end())
00217             {
00218                 Except(Exception::ERR_ITEM_NOT_FOUND, "Cannot locate factory for element " 
00219                     + element->getName(),
00220                     "GuiManager::destroyAllGuiElements");
00221             }
00222 
00223             // remove from parent, if any
00224             GuiContainer* parent;
00225             if ((parent = element->getParent()) != 0)
00226             {
00227                 parent->_removeChild(element->getName());
00228             }
00229 
00230             // children of containers will be auto-removed when container is destroyed.
00231             // destroy the element and remove it from the list
00232             fi->second->destroyGuiElement(element);
00233             elementMap.erase(i);
00234         }
00235     }
00236     //---------------------------------------------------------------------
00237     void GuiManager::addGuiElementFactory(GuiElementFactory* elemFactory)
00238     {
00239         // Add / replace
00240         mFactories[elemFactory->getTypeName()] = elemFactory;
00241 
00242         LogManager::getSingleton().logMessage("GuiElementFactory for type " + elemFactory->getTypeName()
00243             + " registered.");
00244     }
00245 
00246 
00247 }

Copyright © 2002-2003 by The OGRE Team
Last modified Fri May 14 23:22:16 2004