Spinning Topp Logo BlackTopp Studios
inc
charactertraits.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 
41 #ifndef _uicharactertraits_cpp
42 #define _uicharactertraits_cpp
43 
44 #include "UI/charactertraits.h"
45 #include "UI/font.h"
46 #include "UI/textureatlas.h"
47 
48 #include "UI/uimanager.h"
49 
50 namespace Mezzanine
51 {
52  namespace UI
53  {
55  CharFont(NULL),
56  CharColour(ColourValue::White())
57  { }
58 
60  CharFont(Font),
61  CharColour(Character)
62  { }
63 
65  { }
66 
67  ///////////////////////////////////////////////////////////////////////////////
68  // Operator Methods
69 
71  {
72  this->CharFont = Other.CharFont;
73  this->CharColour = Other.CharColour;
74  return *this;
75  }
76 
78  {
79  return ( this->CharFont == Other.CharFont &&
80  this->CharColour == Other.CharColour );
81  }
82 
84  {
85  return ( this->CharFont != Other.CharFont ||
86  this->CharColour != Other.CharColour );
87  }
88 
89  ///////////////////////////////////////////////////////////////////////////////
90  // Serialization
91 
93  {
95 
96  if( TraitsNode.AppendAttribute("FontName").SetValue(this->CharFont->GetName()) &&
97  TraitsNode.AppendAttribute("AtlasName").SetValue(this->CharFont->GetAtlasName()) )
98  {
99  XML::Node CharColourNode = TraitsNode.AppendChild("CharColour");
100  this->CharColour.ProtoSerialize( CharColourNode );
101 
102  return;
103  }else{
104  SerializeError("Create XML Attribute Values",CharacterTraits::GetSerializableName() + "Properties",true);
105  }
106  }
107 
109  {
110  XML::Attribute CurrAttrib;
111  if( String( SelfRoot.Name()) == this->CharacterTraits::GetSerializableName() )
112  {
113  if( SelfRoot.GetAttribute("Version").AsInt() == 1 )
114  {
115  String FontName, AtlasName;
116 
117  CurrAttrib = SelfRoot.GetAttribute("FontName");
118  if( !CurrAttrib.Empty() )
119  FontName = CurrAttrib.AsString();
120 
121  CurrAttrib = SelfRoot.GetAttribute("AtlasName");
122  if( !CurrAttrib.Empty() )
123  AtlasName = CurrAttrib.AsString();
124 
125  if( !FontName.empty() && !AtlasName.empty() ) {
126  this->CharFont = UIManager::GetSingletonPtr()->GetAtlas(AtlasName)->GetFont(FontName);
127  }else{
128  this->CharFont = NULL;
129  }
130 
131  XML::Node CharColourNode = SelfRoot.GetChild("CharColour").GetFirstChild();
132  if( !CharColourNode.Empty() )
133  this->CharColour.ProtoDeSerialize(CharColourNode);
134  }else{
135  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + (this->CharacterTraits::GetSerializableName()) + ": Not Version 1");
136  }
137  }else{
138  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_INVALID_EXCEPTION,"Attempting to deserialize a " + (this->CharacterTraits::GetSerializableName()) + ", found a " + SelfRoot.Name());
139  }
140  }
141 
143  {
144  return "CharacterTraits";
145  }
146  }//UI
147 }//Mezzanine
148 
149 #endif
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
This class represents a collection of Glyphs in a common visual style.
Definition: font.h:55
void ProtoDeSerialize(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite this object with it.
A light-weight handle for manipulating attributes in DOM tree.
Definition: attribute.h:74
virtual ~CharacterTraits()
Class Destructor.
const String & GetAtlasName() const
Gets the name of the TextureAtlas this font belongs to.
Definition: font.cpp:97
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
void ProtoSerialize(XML::Node &ParentNode) const
Convert this class to an XML::Node ready for serialization.
Node GetFirstChild() const
Get the first child Node of this Node.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
CharacterTraits()
Blank Constructor.
const Char8 * AsString(const Char8 *def="") const
Attempts to convert the value of the attribute to a String and returns the results.
This is a simple class for holding 4 reals representing the colour any give object or lightsource can...
Definition: colourvalue.h:64
bool Empty() const
Is this storing anything at all?
This class stores common data for determining the look of a Character.
FontData * CharFont
The font this Character belongs to.
bool SetValue(const Char8 *rhs)
Set the value of this.
static UIManager * GetSingletonPtr()
Fetches a pointer to the singleton.
Definition: singleton.h:97
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
This class creates and encapsultes a character that can be used in text renders.
Definition: character.h:59
int AsInt(int def=0) const
Attempts to convert the value of the attribute to an int and returns the results. ...
Boole operator!=(const CharacterTraits &Other) const
Inequality comparison operator.
const String & GetName() const
Gets the name of this font.
Definition: font.cpp:91
bool Empty() const
Is this storing anything at all?
Boole operator==(const CharacterTraits &Other) const
Equality comparison operator.
CharacterTraits & operator=(const CharacterTraits &Other)
Assignment operator.
ColourValue CharColour
The colour to render this Character as.
void ProtoDeSerialize(const XML::Node &OneNode)
Take the data stored in an XML and overwrite this instance of this object with it.
static String GetSerializableName()
Get the name of the the XML tag this class will leave behind as its instances are serialized...
Thrown when the identity string wasn't valid at all.
Definition: exception.h:93
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
void ProtoSerialize(XML::Node &CurrentRoot) const
Convert this class to an XML::Node ready for serialization.
const Char8 * Name() const
ptrdiff_tGet the name of this Node.
void SerializeError(const String &FailedTo, const String &ClassName, Boole SOrD)
Simply does some string concatenation, then throws an Exception.
Node AppendChild(NodeType Type=NodeElement)
Creates a Node and makes it a child of this one.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.
Node GetChild(const Char8 *Name) const
Attempt to get a child Node with a given name.