Spinning Topp Logo BlackTopp Studios
inc
textureatlashandler.cpp
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _uitextureatlashandler_cpp
41 #define _uitextureatlashandler_cpp
42 
43 #include "UI/textureatlashandler.h"
44 #include "UI/textureatlas.h"
45 
47 #include "Resource/datastream.h"
48 #include "XML/xml.h"
49 
50 namespace Mezzanine
51 {
52  namespace UI
53  {
55  { }
56 
58  { this->DestroyAllAtlases(); }
59 
60  ///////////////////////////////////////////////////////////////////////////////
61  // Texture Atlas Management
62 
63  void TextureAtlasHandler::LoadAtlasFromFile(const String& Name, const String& Group)
64  {
65  /// @todo Update after we have refactored the resource system if needed.
66  Resource::DataStreamPtr AtlasStream = Resource::ResourceManager::GetSingletonPtr()->OpenAssetStream(Name,Group);
67  AtlasStream->SetStreamPosition(0);
68  XML::Document AtlasDoc;
69  AtlasDoc.Load( *AtlasStream.Get() );
70 
71  XML::Node RootNode = AtlasDoc.GetChild("Atlases");
72  if( !RootNode.Empty() )
73  {
74  for( XML::NodeIterator AtlasIt = RootNode.begin() ; AtlasIt != RootNode.end() ; ++AtlasIt )
75  {
76  // Parse the Atlas
77  TextureAtlas* NewAtlas = new TextureAtlas( (*AtlasIt) );
78  // Verify we don't already have one of the same name
79  AtlasIterator AtIt = this->Atlases.find( NewAtlas->GetName() );
80  if( AtIt != Atlases.end() )
81  {
82  MEZZ_EXCEPTION(ExceptionBase::II_DUPLICATE_IDENTITY_EXCEPTION,"Texture Atlas with the name \"" + NewAtlas->GetName() + "\" already exists.");
83  }
84  // Add the unique Atlas
85  this->Atlases[NewAtlas->GetName()] = NewAtlas;
86  }
87  }else{
88  MEZZ_EXCEPTION(ExceptionBase::INVALID_STATE_EXCEPTION,"Mezzanine Texture Atlas file \"" + Name + "\"does not contain expected \"Atlases\" root node. File is not valid and cannot be parsed.");
89  }
90  }
91 
93  {
94  AtlasIterator AtlasIt = this->Atlases.find(AtlasName);
95  if( AtlasIt != this->Atlases.end() ) {
96  return (*AtlasIt).second;
97  }else{
98  return NULL;
99  }
100  }
101 
103  {
104  AtlasIterator AtlasIt = this->Atlases.find(AtlasName);
105  if( AtlasIt != this->Atlases.end() ) {
106  return (*AtlasIt).second;
107  }else{
108  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,"Unabled to find Texture Atlas named \"" + AtlasName + "\".");
109  }
110  }
111 
113  {
114  AtlasIterator AtlasIt = this->Atlases.find( ToBeDestroyed->GetName() );
115  if( AtlasIt != this->Atlases.end() ) {
116  delete (*AtlasIt).second;
117  this->Atlases.erase(AtlasIt);
118  }
119  }
120 
122  {
123  for( AtlasIterator AtlasIt = this->Atlases.begin() ; AtlasIt != this->Atlases.end() ; ++AtlasIt )
124  { delete (*AtlasIt).second; }
125  this->Atlases.clear();
126  }
127  }//UI
128 }//Mezzanine
129 
130 #endif
Thrown when duplicates of teh same identity string exist.
Definition: exception.h:95
const String & GetName() const
Gets the name of this Texture Atlas.
Thrown when the requested identity could not be found.
Definition: exception.h:94
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
A simple reference counting pointer.
Definition: countedptr.h:70
This is a collection of smaller textures packed into a larger texture, intended to increase UI perfor...
Definition: textureatlas.h:71
void LoadAtlasFromFile(const String &Name, const String &Group="UI")
Loads a Mezzanine Texture Atlas file for use with UI::Screen's.
UI::TextureAtlas * GetAtlas(const String &AtlasName)
Gets a loaded Atlas being stored in this manager.
bool Empty() const
Is this storing anything at all?
ParseResult Load(std::basic_istream< char, std::char_traits< char > > &stream, unsigned int options=ParseDefault, Encoding DocumentEncoding=EncodingAuto)
Load XML from a stream.
UI::TextureAtlas * GetAtlasExcept(const String &AtlasName)
Gets a loaded Atlas being stored in this manager.
static ResourceManager * GetSingletonPtr()
Fetches a pointer to the singleton.
Definition: singleton.h:97
TypePointedTo * Get() const
Get the raw pointer to the managed object.
Definition: countedptr.h:458
Child node iterator (a bidirectional iterator over a collection of Node)
Definition: nodeiterator.h:77
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
iterator begin() const
Get a Child node iterator that references the first child Node.
void DestroyAllAtlases()
Destroys all loaded texture atlases.
iterator end() const
Get a Child node iterator that references one past the last child Node.
AtlasContainer::iterator AtlasIterator
Iterator type for TextureAtlases being stored within this class.
The defintion of the Resource Manager.
Thrown when the available information should have worked but failed for unknown reasons.
Definition: exception.h:113
The root node of any xml hierarchy is a Document.
Definition: document.h:83
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
void DestroyAtlas(TextureAtlas *ToBeDestroyed)
Destroys a loaded texture atlas.
AtlasContainer Atlases
Container storing all currently loaded texture atlases.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
Declaration of DataStream.
Node GetChild(const Char8 *Name) const
Attempt to get a child Node with a given name.