Spinning Topp Logo BlackTopp Studios
inc
stackbutton.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 _uistackbutton_cpp
41 #define _uistackbutton_cpp
42 
43 #include "UI/stackbutton.h"
44 #include "UI/stackedcontainer.h"
45 #include "UI/screen.h"
46 
47 #include "stringtool.h"
48 #include "exception.h"
49 #include "serialization.h"
50 
51 namespace Mezzanine
52 {
53  namespace UI
54  {
55  ///////////////////////////////////////////////////////////////////////////////
56  // StackButton Static Members
57 
58  const String StackButton::TypeName = "StackButton";
59 
60  ///////////////////////////////////////////////////////////////////////////////
61  // StackButton Methods
62 
64  Button(Parent),
65  BoundContainer(NULL)
66  { }
67 
68  StackButton::StackButton(const String& RendName, Screen* Parent) :
69  Button(RendName,Parent),
70  BoundContainer(NULL)
71  { }
72 
73  StackButton::StackButton(const String& RendName, const UnifiedRect& RendRect, Screen* Parent) :
74  Button(RendName,RendRect,Parent),
75  BoundContainer(NULL)
76  { }
77 
78  StackButton::StackButton(const XML::Node& XMLNode, Screen* Parent) :
79  Button(Parent),
80  BoundContainer(NULL)
81  { this->ProtoDeSerialize(XMLNode); }
82 
84  { }
85 
86  ///////////////////////////////////////////////////////////////////////////////
87  // Utility Methods
88 
90  { return this->BoundContainer; }
91 
93  { return StackButton::TypeName; }
94 
95  ///////////////////////////////////////////////////////////////////////////////
96  // Serialization
97 
99  {
100  this->Button::ProtoSerializeProperties(SelfRoot);
101 
102  XML::Node PropertiesNode = SelfRoot.AppendChild( StackButton::GetSerializableName() + "Properties" );
103 
104  if( PropertiesNode.AppendAttribute("Version").SetValue("1") )
105  {
106  // Only if we have a valid binding
107  if( this->BoundContainer != NULL ) {
108  if( PropertiesNode.AppendAttribute("ContainerName").SetValue( this->BoundContainer->GetName() ) &&
109  PropertiesNode.AppendAttribute("ButtonConfig").SetValue( this->BoundContainer->GetButtonConfig(this) ) )
110  {
111  return;
112  }else{
113  SerializeError("Create XML Attribute Values",StackButton::GetSerializableName() + "Properties",true);
114  }
115  }
116  }else{
117  SerializeError("Create XML Attribute Values",StackButton::GetSerializableName() + "Properties",true);
118  }
119  }
120 
122  {
123  this->Button::ProtoDeSerializeProperties(SelfRoot);
124 
125  XML::Attribute CurrAttrib;
126  XML::Node PropertiesNode = SelfRoot.GetChild( StackButton::GetSerializableName() + "Properties" );
127 
128  if( !PropertiesNode.Empty() ) {
129  if(PropertiesNode.GetAttribute("Version").AsInt() == 1) {
130  String ContainerName;
131  UInt16 ContainerConfig;
132 
133  CurrAttrib = PropertiesNode.GetAttribute("ContainerName");
134  if( !CurrAttrib.Empty() )
135  ContainerName = CurrAttrib.AsString();
136 
137  CurrAttrib = PropertiesNode.GetAttribute("ButtonConfig");
138  if( !CurrAttrib.Empty() )
139  ContainerConfig = static_cast<UInt16>( CurrAttrib.AsUint() );
140 
141  if( !ContainerName.empty() ) {
142  Widget* UncastedMenu = this->ParentScreen->GetWidget(ContainerName);
143  if( UncastedMenu->GetTypeName() == StackedContainer::TypeName ) {
144  static_cast<StackedContainer*>( UncastedMenu )->SetButtonConfig(ContainerConfig,this);
145  }else{
146  MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION,"Named Widget that was expected to be a MenuEntry is not a MenuEntry.");
147  }
148  }
149  }else{
150  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + (StackButton::GetSerializableName() + "Properties") + ": Not Version 1.");
151  }
152  }else{
153  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,StackButton::GetSerializableName() + "Properties" + " was not found in the provided XML node, which was expected.");
154  }
155  }
156 
158  { return StackButton::TypeName; }
159 
160  ///////////////////////////////////////////////////////////////////////////////
161  // Internal Event Methods
162 
164  {
165  this->Button::_OnActivate();
166  // Currently this needs nothing, may change
167  }
168 
170  {
171  this->Button::_OnDeactivate();
172 
173  if( this->IsHovered() && this->BoundContainer ) {
175  }
176  }
177 
178  ///////////////////////////////////////////////////////////////////////////////
179  // Internal Methods
180 
182  { this->BoundContainer = ToBeBound; }
183 
184  ///////////////////////////////////////////////////////////////////////////////
185  // StackButtonFactory Methods
186 
188  { return StackButton::TypeName; }
189 
191  { return new StackButton(RendName,Parent); }
192 
193  StackButton* StackButtonFactory::CreateStackButton(const String& RendName, const UnifiedRect& RendRect, Screen* Parent)
194  { return new StackButton(RendName,RendRect,Parent); }
195 
197  { return new StackButton(XMLNode,Parent); }
198 
200  { return new StackButton(Parent); }
201 
202  Widget* StackButtonFactory::CreateWidget(const String& RendName, const NameValuePairMap& Params, Screen* Parent)
203  { return this->CreateStackButton(RendName,Parent); }
204 
205  Widget* StackButtonFactory::CreateWidget(const String& RendName, const UnifiedRect& RendRect, const NameValuePairMap& Params, Screen* Parent)
206  { return this->CreateStackButton(RendName,RendRect,Parent); }
207 
209  { return this->CreateStackButton(XMLNode,Parent); }
210 
212  { delete static_cast<StackButton*>( ToBeDestroyed ); }
213  }//UI
214 }//Mezzanine
215 
216 #endif
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
const String & GetName() const
Gets the name of this renderable.
Definition: renderable.cpp:77
virtual void ProtoDeSerializeProperties(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the properties of this object with it...
Definition: button.cpp:296
A light-weight handle for manipulating attributes in DOM tree.
Definition: attribute.h:74
virtual void _OnDeactivate()
Self logic to be executed when this button is deactivated.
Definition: button.cpp:364
static String GetSerializableName()
Get the name of the the XML tag the Renderable class will leave behind as its instances are serialize...
virtual const String & GetTypeName() const
Gets the type of widget this is.
Definition: widget.cpp:156
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
This is a button with additional data used to track the binding to a StackedContainer which can be se...
Definition: stackbutton.h:54
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
const Char8 * AsString(const Char8 *def="") const
Attempts to convert the value of the attribute to a String and returns the results.
virtual void _OnDeactivate()
Self logic to be executed when this button is deactivated.
virtual Boole IsHovered() const
Gets the result of the last mouse hover check.
Definition: widget.cpp:159
bool Empty() const
Is this storing anything at all?
This implements the exception hiearchy for Mezzanine.
virtual void ProtoDeSerializeProperties(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the properties of this object with it...
virtual void _OnActivate()
Self logic to be executed when this button is activated.
StackButton(Screen *Parent)
Blank constructor.
Definition: stackbutton.cpp:63
The interface for serialization.
bool SetValue(const Char8 *rhs)
Set the value of this.
static const String TypeName
String containing the type name for this class: "StackedContainer".
virtual void DestroyWidget(Widget *ToBeDestroyed)
Destroys a Widget created by this factory.
This class represents a 2D rect which can express the size and position of a renderable on screen...
Definition: unifieddim.h:661
uint16_t UInt16
An 16-bit unsigned integer.
Definition: datatypes.h:122
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
static const String TypeName
String containing the type name for this class: "StackButton".
Definition: stackbutton.h:58
This is the base class for containers that stack their children one on top of the other...
unsigned int AsUint(unsigned int def=0) const
Attempts to convert the value of the attribute to an unsigned int and returns the results...
int AsInt(int def=0) const
Attempts to convert the value of the attribute to an int and returns the results. ...
bool Empty() const
Is this storing anything at all?
virtual StackedContainer * GetBoundContainer() const
Gets the container that is currently bound to this button.
Definition: stackbutton.cpp:89
This is the base class for all widgets.
Definition: widget.h:126
virtual ~StackButton()
Standard destructor.
Definition: stackbutton.cpp:83
virtual void _OnActivate()
Self logic to be executed when this button is activated.
Definition: button.cpp:352
StackedContainer * BoundContainer
A pointer storing the StackedContainer this button is bound to.
Definition: stackbutton.h:62
virtual StackButton * CreateStackButton(const String &RendName, Screen *Parent)
Creates a new StackButton.
Thrown when parameters are checked at runtime and found invalid.
Definition: exception.h:108
virtual const String & GetTypeName() const
Gets the type of widget this is.
Definition: stackbutton.cpp:92
virtual Widget * GetWidget(const String &Name)
Gets a widget in this screen by name.
Definition: screen.cpp:573
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
Definition: stackbutton.cpp:98
virtual void _SetBoundContainer(StackedContainer *ToBeBound)
Notifies this StackButton that a StackedContainer has been bound to it.
This class is a helper class, specifically for use as a button.
Definition: button.h:66
virtual void ProtoDeSerialize(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite this object with it.
virtual UInt16 GetButtonConfig(const StackButton *ConfigButton) const =0
Gets the role of the specified StackedButton for this StackedContainer.
void SerializeError(const String &FailedTo, const String &ClassName, Boole SOrD)
Simply does some string concatenation, then throws an Exception.
virtual void _NotifyButtonSelected(StackButton *Selected)=0
Notifies this StackedContainer a button has been selected.
Node AppendChild(NodeType Type=NodeElement)
Creates a Node and makes it a child of this one.
std::map< String, String > NameValuePairMap
This is a datatype mostly used for describing settings or parameters that can't be declared in advanc...
Definition: datatypes.h:209
This class is a helper class for creating UI's. It is responsible for storing and keeping track of al...
Definition: screen.h:142
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.
virtual Widget * CreateWidget(Screen *Parent)
Creates a Widget of the type represented by this factory.
Node GetChild(const Char8 *Name) const
Attempt to get a child Node with a given name.
Screen * ParentScreen
A pointer to the Screen that created this Renderable.
Definition: renderable.h:72
virtual String GetWidgetTypeName() const
Gets the name of the Widget that is created by this factory.
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
Definition: button.cpp:269