| 1 | /* |
|---|
| 2 | ----------------------------------------------------------------------------- |
|---|
| 3 | This source file is part of OGRE |
|---|
| 4 | (Object-oriented Graphics Rendering Engine) |
|---|
| 5 | For the latest info, see http://www.ogre3d.org/ |
|---|
| 6 | |
|---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
|---|
| 8 | Also see acknowledgements in Readme.html |
|---|
| 9 | |
|---|
| 10 | This program is free software; you can redistribute it and/or modify it under |
|---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
|---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 13 | version. |
|---|
| 14 | |
|---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
|---|
| 18 | |
|---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
|---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
|---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
|---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
|---|
| 23 | |
|---|
| 24 | You may alternatively use this source under the terms of a specific version of |
|---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from |
|---|
| 26 | Torus Knot Software Ltd. |
|---|
| 27 | ----------------------------------------------------------------------------- |
|---|
| 28 | */ |
|---|
| 29 | #ifndef __OverlayManager_H__ |
|---|
| 30 | #define __OverlayManager_H__ |
|---|
| 31 | |
|---|
| 32 | #include "OgrePrerequisites.h" |
|---|
| 33 | #include "OgreSingleton.h" |
|---|
| 34 | #include "OgreStringVector.h" |
|---|
| 35 | #include "OgreOverlay.h" |
|---|
| 36 | #include "OgreScriptLoader.h" |
|---|
| 37 | |
|---|
| 38 | namespace Ogre { |
|---|
| 39 | |
|---|
| 40 | /** Manages Overlay objects, parsing them from .overlay files and |
|---|
| 41 | storing a lookup library of them. Alo manages the creation of |
|---|
| 42 | OverlayContainers and OverlayElements, used for non-interactive 2D |
|---|
| 43 | elements such as HUDs. |
|---|
| 44 | */ |
|---|
| 45 | class _OgreExport OverlayManager : public Singleton<OverlayManager>, public ScriptLoader |
|---|
| 46 | { |
|---|
| 47 | public: |
|---|
| 48 | typedef std::map<String, Overlay*> OverlayMap; |
|---|
| 49 | typedef std::map<String, OverlayElement*> ElementMap; |
|---|
| 50 | protected: |
|---|
| 51 | OverlayMap mOverlayMap; |
|---|
| 52 | StringVector mScriptPatterns; |
|---|
| 53 | |
|---|
| 54 | void parseNewElement( DataStreamPtr& chunk, String& elemType, String& elemName, |
|---|
| 55 | bool isContainer, Overlay* pOverlay, bool isTemplate, String templateName = String(""), OverlayContainer* container = 0); |
|---|
| 56 | void parseAttrib( const String& line, Overlay* pOverlay); |
|---|
| 57 | void parseElementAttrib( const String& line, Overlay* pOverlay, OverlayElement* pElement ); |
|---|
| 58 | void skipToNextCloseBrace(DataStreamPtr& chunk); |
|---|
| 59 | void skipToNextOpenBrace(DataStreamPtr& chunk); |
|---|
| 60 | |
|---|
| 61 | int mLastViewportWidth, mLastViewportHeight; |
|---|
| 62 | bool mViewportDimensionsChanged; |
|---|
| 63 | |
|---|
| 64 | bool parseChildren( DataStreamPtr& chunk, const String& line, |
|---|
| 65 | Overlay* pOverlay, bool isTemplate, OverlayContainer* parent = NULL); |
|---|
| 66 | |
|---|
| 67 | typedef std::map<String, OverlayElementFactory*> FactoryMap; |
|---|
| 68 | FactoryMap mFactories; |
|---|
| 69 | |
|---|
| 70 | ElementMap mInstances; |
|---|
| 71 | ElementMap mTemplates; |
|---|
| 72 | |
|---|
| 73 | typedef std::set<String> LoadedScripts; |
|---|
| 74 | LoadedScripts mLoadedScripts; |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | ElementMap& getElementMap(bool isTemplate); |
|---|
| 80 | |
|---|
| 81 | OverlayElement* createOverlayElementImpl(const String& typeName, const String& instanceName, ElementMap& elementMap); |
|---|
| 82 | |
|---|
| 83 | OverlayElement* getOverlayElementImpl(const String& name, ElementMap& elementMap); |
|---|
| 84 | |
|---|
| 85 | void destroyOverlayElementImpl(const String& instanceName, ElementMap& elementMap); |
|---|
| 86 | |
|---|
| 87 | void destroyOverlayElementImpl(OverlayElement* pInstance, ElementMap& elementMap); |
|---|
| 88 | |
|---|
| 89 | void destroyAllOverlayElementsImpl(ElementMap& elementMap); |
|---|
| 90 | |
|---|
| 91 | public: |
|---|
| 92 | OverlayManager(); |
|---|
| 93 | virtual ~OverlayManager(); |
|---|
| 94 | |
|---|
| 95 | /// @copydoc ScriptLoader::getScriptPatterns |
|---|
| 96 | const StringVector& getScriptPatterns(void) const; |
|---|
| 97 | /// @copydoc ScriptLoader::parseScript |
|---|
| 98 | void parseScript(DataStreamPtr& stream, const String& groupName); |
|---|
| 99 | /// @copydoc ScriptLoader::getLoadingOrder |
|---|
| 100 | Real getLoadingOrder(void) const; |
|---|
| 101 | |
|---|
| 102 | /** Create a new Overlay. */ |
|---|
| 103 | Overlay* create(const String& name); |
|---|
| 104 | /** Retrieve an Overlay by name |
|---|
| 105 | @returns A pointer to the Overlay, or 0 if not found |
|---|
| 106 | */ |
|---|
| 107 | Overlay* getByName(const String& name); |
|---|
| 108 | /** Destroys an existing overlay by name */ |
|---|
| 109 | void destroy(const String& name); |
|---|
| 110 | /** Destroys an existing overlay */ |
|---|
| 111 | void destroy(Overlay* overlay); |
|---|
| 112 | /** Destroys all existing overlays */ |
|---|
| 113 | void destroyAll(void); |
|---|
| 114 | typedef MapIterator<OverlayMap> OverlayMapIterator; |
|---|
| 115 | OverlayMapIterator getOverlayIterator(void); |
|---|
| 116 | |
|---|
| 117 | /** Internal method for queueing the visible overlays for rendering. */ |
|---|
| 118 | void _queueOverlaysForRendering(Camera* cam, RenderQueue* pQueue, Viewport *vp); |
|---|
| 119 | |
|---|
| 120 | /** Method for determining if the viewport has changed dimensions. |
|---|
| 121 | @remarks This is used by pixel-based OverlayElements to work out if they need to |
|---|
| 122 | reclaculate their sizes. |
|---|
| 123 | */ |
|---|
| 124 | bool hasViewportChanged(void) const; |
|---|
| 125 | |
|---|
| 126 | /** Gets the height of the destination viewport in pixels. */ |
|---|
| 127 | int getViewportHeight(void) const; |
|---|
| 128 | |
|---|
| 129 | /** Gets the width of the destination viewport in pixels. */ |
|---|
| 130 | int getViewportWidth(void) const; |
|---|
| 131 | Real getViewportAspectRatio(void) const; |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | /** Creates a new OverlayElement of the type requested. |
|---|
| 135 | @remarks |
|---|
| 136 | The type of element to create is passed in as a string because this |
|---|
| 137 | allows plugins to register new types of component. |
|---|
| 138 | @param typeName The type of element to create. |
|---|
| 139 | @param instanceName The name to give the new instance. |
|---|
| 140 | */ |
|---|
| 141 | OverlayElement* createOverlayElement(const String& typeName, const String& instanceName, bool isTemplate = false); |
|---|
| 142 | |
|---|
| 143 | /** Gets a reference to an existing element. */ |
|---|
| 144 | OverlayElement* getOverlayElement(const String& name, bool isTemplate = false); |
|---|
| 145 | |
|---|
| 146 | /** Destroys a OverlayElement. |
|---|
| 147 | @remarks |
|---|
| 148 | Make sure you're not still using this in an Overlay. If in |
|---|
| 149 | doubt, let OGRE destroy elements on shutdown. |
|---|
| 150 | */ |
|---|
| 151 | void destroyOverlayElement(const String& instanceName, bool isTemplate = false); |
|---|
| 152 | |
|---|
| 153 | /** Destroys a OverlayElement. |
|---|
| 154 | @remarks |
|---|
| 155 | Make sure you're not still using this in an Overlay. If in |
|---|
| 156 | doubt, let OGRE destroy elements on shutdown. |
|---|
| 157 | */ |
|---|
| 158 | void destroyOverlayElement(OverlayElement* pInstance, bool isTemplate = false); |
|---|
| 159 | |
|---|
| 160 | /** Destroys all the OverlayElement created so far. |
|---|
| 161 | @remarks |
|---|
| 162 | Best to leave this to the engine to call internally, there |
|---|
| 163 | should rarely be a need to call it yourself. |
|---|
| 164 | */ |
|---|
| 165 | void destroyAllOverlayElements(bool isTemplate = false); |
|---|
| 166 | |
|---|
| 167 | /** Registers a new OverlayElementFactory with this manager. |
|---|
| 168 | @remarks |
|---|
| 169 | Should be used by plugins or other apps wishing to provide |
|---|
| 170 | a new OverlayElement subclass. |
|---|
| 171 | */ |
|---|
| 172 | void addOverlayElementFactory(OverlayElementFactory* elemFactory); |
|---|
| 173 | |
|---|
| 174 | OverlayElement* createOverlayElementFromTemplate(const String& templateName, const String& typeName, const String& instanceName, bool isTemplate = false); |
|---|
| 175 | /** |
|---|
| 176 | * @remarks |
|---|
| 177 | * Creates a new OverlayElement object from the specified template name. The new |
|---|
| 178 | * object's name, and all of it's children, will be instanceName/orignalName. |
|---|
| 179 | */ |
|---|
| 180 | OverlayElement* cloneOverlayElementFromTemplate(const String& templateName, const String& instanceName); |
|---|
| 181 | |
|---|
| 182 | OverlayElement* createOverlayElementFromFactory(const String& typeName, const String& instanceName); |
|---|
| 183 | |
|---|
| 184 | typedef MapIterator<ElementMap> TemplateIterator; |
|---|
| 185 | /** Returns an iterator over all templates in this manager.*/ |
|---|
| 186 | TemplateIterator getTemplateIterator () |
|---|
| 187 | { |
|---|
| 188 | return TemplateIterator (mTemplates.begin (), mTemplates.end ()) ; |
|---|
| 189 | } |
|---|
| 190 | /* Returns whether the Element with the given name is a Template */ |
|---|
| 191 | bool isTemplate (String strName) const { |
|---|
| 192 | return (mTemplates.find (strName) != mTemplates.end()) ; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | /** Override standard Singleton retrieval. |
|---|
| 197 | @remarks |
|---|
| 198 | Why do we do this? Well, it's because the Singleton |
|---|
| 199 | implementation is in a .h file, which means it gets compiled |
|---|
| 200 | into anybody who includes it. This is needed for the |
|---|
| 201 | Singleton template to work, but we actually only want it |
|---|
| 202 | compiled into the implementation of the class based on the |
|---|
| 203 | Singleton, not all of them. If we don't change this, we get |
|---|
| 204 | link errors when trying to use the Singleton-based class from |
|---|
| 205 | an outside dll. |
|---|
| 206 | @par |
|---|
| 207 | This method just delegates to the template version anyway, |
|---|
| 208 | but the implementation stays in this single compilation unit, |
|---|
| 209 | preventing link errors. |
|---|
| 210 | */ |
|---|
| 211 | static OverlayManager& getSingleton(void); |
|---|
| 212 | /** Override standard Singleton retrieval. |
|---|
| 213 | @remarks |
|---|
| 214 | Why do we do this? Well, it's because the Singleton |
|---|
| 215 | implementation is in a .h file, which means it gets compiled |
|---|
| 216 | into anybody who includes it. This is needed for the |
|---|
| 217 | Singleton template to work, but we actually only want it |
|---|
| 218 | compiled into the implementation of the class based on the |
|---|
| 219 | Singleton, not all of them. If we don't change this, we get |
|---|
| 220 | link errors when trying to use the Singleton-based class from |
|---|
| 221 | an outside dll. |
|---|
| 222 | @par |
|---|
| 223 | This method just delegates to the template version anyway, |
|---|
| 224 | but the implementation stays in this single compilation unit, |
|---|
| 225 | preventing link errors. |
|---|
| 226 | */ |
|---|
| 227 | static OverlayManager* getSingletonPtr(void); |
|---|
| 228 | }; |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | #endif |
|---|