Spinning Topp Logo BlackTopp Studios
inc
checkbox.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 _uicheckbox_cpp
41 #define _uicheckbox_cpp
42 
43 #include "UI/uimanager.h"
44 #include "UI/checkbox.h"
45 #include "UI/screen.h"
46 
47 #include "stringtool.h"
48 #include "exception.h"
49 
50 namespace Mezzanine
51 {
52  namespace UI
53  {
54  ///////////////////////////////////////////////////////////////////////////////
55  // CheckBox Static Members
56 
57  const String CheckBox::TypeName = "CheckBox";
58 
59  const String CheckBox::EventSelected = "Selected";
60  const String CheckBox::EventDeselected = "Deselected";
61 
62  ///////////////////////////////////////////////////////////////////////////////
63  // CheckBox Methods
64 
66  Button(Parent),
67  SelectLock(false)
68  { }
69 
70  CheckBox::CheckBox(const String& RendName, Screen* Parent) :
71  Button(RendName,Parent),
72  SelectLock(false)
73  { this->ConstructCheckbox(); }
74 
75  CheckBox::CheckBox(const String& RendName, const UnifiedRect& RendRect, Screen* Parent) :
76  Button(RendName,RendRect,Parent),
77  SelectLock(false)
78  { this->ConstructCheckbox(); }
79 
80  CheckBox::CheckBox(const XML::Node& XMLNode, Screen* Parent) :
81  Button(Parent),
82  SelectLock(false)
83  { this->ProtoDeSerialize(XMLNode); }
84 
86  { }
87 
89  {
90  // Add our new events
91  this->AddEvent(CheckBox::EventSelected);
92  this->AddEvent(CheckBox::EventDeselected);
93  // Add some more render layer groups
94  RenderLayerGroup* SelectedNormalGroup = this->CreateRenderLayerGroup(CheckBox::WG_SelectedNormal);
95  RenderLayerGroup* SelectedHoveredGroup = this->CreateRenderLayerGroup(CheckBox::WG_SelectedHovered);
96 
97  this->BindGroupToState( WS_Selected, SelectedNormalGroup );
98  this->BindGroupToState( WS_Selected | WS_Hovered, SelectedHoveredGroup );
99  this->BindGroupToState( WS_Selected | WS_Focused, SelectedNormalGroup );
100  this->BindGroupToState( WS_Selected | WS_Dragged, SelectedNormalGroup );
101  this->BindGroupToState( WS_Selected | WS_Hovered | WS_Focused, SelectedHoveredGroup );
102  this->BindGroupToState( WS_Selected | WS_Focused | WS_Dragged, SelectedNormalGroup );
103  this->BindGroupToState( WS_Selected | WS_Dragged | WS_Hovered, SelectedHoveredGroup );
104  this->BindGroupToState( WS_Selected | WS_Hovered | WS_Focused | WS_Dragged, SelectedHoveredGroup );
105  }
106 
107  ///////////////////////////////////////////////////////////////////////////////
108  // Utility Methods
109 
111  {
112  return (this->State & WS_Selected);
113  }
114 
116  {
117  return this->SelectLock;
118  }
119 
121  {
122  if( this->IsSelected() != Select ) {
123  Boole NewState = !this->IsSelected();
124  if( NewState ) this->_OnSelected();
125  else this->_OnDeselected();
126  }
127  }
128 
130  {
131  this->SelectLock = Lock;
132  }
133 
135  {
136  return CheckBox::TypeName;
137  }
138 
139  ///////////////////////////////////////////////////////////////////////////////
140  // Serialization
141 
143  {
144  this->Button::ProtoSerializeProperties(SelfRoot);
145 
146  XML::Node PropertiesNode = SelfRoot.AppendChild( CheckBox::GetSerializableName() + "Properties" );
147 
148  if( PropertiesNode.AppendAttribute("Version").SetValue("1") &&
149  PropertiesNode.AppendAttribute("SelectLock").SetValue( this->SelectLock ? "true" : "false" ) )
150  {
151  return;
152  }else{
153  SerializeError("Create XML Attribute Values",CheckBox::GetSerializableName() + "Properties",true);
154  }
155  }
156 
158  {
159  this->Button::ProtoDeSerializeProperties(SelfRoot);
160 
161  XML::Attribute CurrAttrib;
162  XML::Node PropertiesNode = SelfRoot.GetChild( CheckBox::GetSerializableName() + "Properties" );
163 
164  if( !PropertiesNode.Empty() ) {
165  if(PropertiesNode.GetAttribute("Version").AsInt() == 1) {
166  CurrAttrib = PropertiesNode.GetAttribute("LockoutTime");
167  if( !CurrAttrib.Empty() )
168  this->SelectLock = StringTools::ConvertToBool( CurrAttrib.AsString() );
169  }else{
170  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + (CheckBox::GetSerializableName() + "Properties") + ": Not Version 1.");
171  }
172  }else{
173  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,CheckBox::GetSerializableName() + "Properties" + " was not found in the provided XML node, which was expected.");
174  }
175  }
176 
178  {
179  return CheckBox::TypeName;
180  }
181 
182  ///////////////////////////////////////////////////////////////////////////////
183  // Internal Event Methods
184 
186  {
187  this->Button::_OnActivate();
188  // Currently this needs nothing, may change
189  }
190 
192  {
193  this->Button::_OnDeactivate();
194 
195  if( this->IsHovered() && !this->SelectLock ) {
196  Boole NewState = !this->IsSelected();
197  if( NewState ) this->_OnSelected();
198  else this->_OnDeselected();
199  }
200  }
201 
203  {
204  this->State |= WS_Selected;
205  this->SetGroupFromState(this->State);
206 
207  EventArgumentsPtr Args( new WidgetEventArguments(CheckBox::EventSelected,this->Name) );
208  this->FireEvent(Args);
209  }
210 
212  {
213  this->State &= ~WS_Selected;
214  this->SetGroupFromState(this->State);
215 
216  EventArgumentsPtr Args( new WidgetEventArguments(CheckBox::EventDeselected,this->Name) );
217  this->FireEvent(Args);
218  }
219 
220  ///////////////////////////////////////////////////////////////////////////////
221  // CheckBoxFactory Methods
222 
224  { return CheckBox::TypeName; }
225 
227  { return new CheckBox(RendName,Parent); }
228 
229  CheckBox* CheckBoxFactory::CreateCheckBox(const String& RendName, const UnifiedRect& RendRect, Screen* Parent)
230  { return new CheckBox(RendName,RendRect,Parent); }
231 
233  { return new CheckBox(XMLNode,Parent); }
234 
236  { return new CheckBox(Parent); }
237 
238  Widget* CheckBoxFactory::CreateWidget(const String& RendName, const NameValuePairMap& Params, Screen* Parent)
239  { return this->CreateCheckBox(RendName,Parent); }
240 
241  Widget* CheckBoxFactory::CreateWidget(const String& RendName, const UnifiedRect& RendRect, const NameValuePairMap& Params, Screen* Parent)
242  { return this->CreateCheckBox(RendName,RendRect,Parent); }
243 
245  { return this->CreateCheckBox(XMLNode,Parent); }
246 
248  { delete static_cast<CheckBox*>( ToBeDestroyed ); }
249  }//UT
250 }//Mezzanine
251 
252 #endif
static const String TypeName
String containing the type name for this class: "CheckBox".
Definition: checkbox.h:71
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
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
virtual void DestroyWidget(Widget *ToBeDestroyed)
Destroys a Widget created by this factory.
Definition: checkbox.cpp:247
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
virtual void SetSelectLock(Boole Lock)
Locks (or unlocks) the current state of this checkbox.
Definition: checkbox.cpp:129
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
virtual void _OnDeselected()
Self logic to be executed when this checkbox is deselected.
Definition: checkbox.cpp:211
virtual Boole IsLocked()
Gets wether this checkbox is locked into it's current state.
Definition: checkbox.cpp:115
virtual Boole IsSelected()
Gets whether this checkbox is selected or not.
Definition: checkbox.cpp:110
Boole SelectLock
Stores whether or not the current state of this CheckBox is locked.
Definition: checkbox.h:79
UInt32 State
UInt32 describing the current state of this widget.
Definition: widget.h:204
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
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: checkbox.cpp:157
A simple reference counting pointer.
Definition: countedptr.h:70
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
virtual void BindGroupToState(const UInt32 BindState, RenderLayerGroup *ToBind)
Binds a RenderLayerGroup to a WidgetState.
Definition: widget.cpp:189
const Char8 * AsString(const Char8 *def="") const
Attempts to convert the value of the attribute to a String and returns the results.
virtual ~CheckBox()
Class destructor.
Definition: checkbox.cpp:85
virtual const String & GetTypeName() const
Gets the type of widget this is.
Definition: checkbox.cpp:134
static String GetSerializableName()
Get the name of the the XML tag the Renderable class will leave behind as its instances are serialize...
Definition: checkbox.cpp:177
virtual Boole IsHovered() const
Gets the result of the last mouse hover check.
Definition: widget.cpp:159
RenderLayerGroup * CreateRenderLayerGroup(const UInt16 GroupID)
Creates a new RenderLayerGroup that can have. function will throw an exception if a group already exi...
virtual void _OnActivate()
Self logic to be executed when this button is activated.
Definition: checkbox.cpp:185
bool Empty() const
Is this storing anything at all?
This implements the exception hiearchy for Mezzanine.
static const String EventSelected
Event name for when this checkbox is Selected.
Definition: checkbox.h:73
virtual Boole SetGroupFromState(const UInt32 BindState)
Sets the group bound to the specified WidgetState as active.
Definition: widget.cpp:201
virtual String GetWidgetTypeName() const
Gets the name of the Widget that is created by this factory.
Definition: checkbox.cpp:223
virtual void _OnDeactivate()
Self logic to be executed when this button is deactivated.
Definition: checkbox.cpp:191
bool SetValue(const Char8 *rhs)
Set the value of this.
This class represents a 2D rect which can express the size and position of a renderable on screen...
Definition: unifieddim.h:661
Boole ConvertToBool(const String &ToConvert, const Boole Default)
Converts a string into a Boole.
Definition: stringtool.cpp:379
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
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?
This is the base class for all widgets.
Definition: widget.h:126
virtual void _OnActivate()
Self logic to be executed when this button is activated.
Definition: button.cpp:352
Event * AddEvent(const String &EventName)
Creates a new event this Publisher can fire.
virtual Widget * CreateWidget(Screen *Parent)
Creates a Widget of the type represented by this factory.
Definition: checkbox.cpp:235
String Name
The unique name of this Renderable.
Definition: renderable.h:81
void FireEvent(EventArgumentsPtr Args)
Fires an event.
This class stores a group of render layers that can be set to be rendered.
static const String EventDeselected
Event name for when this checkbox is Deselected.
Definition: checkbox.h:75
virtual CheckBox * CreateCheckBox(const String &RendName, Screen *Parent)
Creates a new CheckBox.
Definition: checkbox.cpp:226
void ConstructCheckbox()
Contains all the common necessary startup initializations for this class.
Definition: checkbox.cpp:88
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
virtual void ManualSelect(Boole Select)
Manually select or deselect this checkbox.
Definition: checkbox.cpp:120
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 void _OnSelected()
Self logic to be executed when this checkbox is selected.
Definition: checkbox.cpp:202
This is a simple widget for storing a bool value.
Definition: checkbox.h:55
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.
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
Definition: checkbox.cpp:142
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.
CheckBox(Screen *Parent)
Blank constructor.
Definition: checkbox.cpp:65
Node GetChild(const Char8 *Name) const
Attempt to get a child Node with a given name.
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
Definition: button.cpp:269
This is the base class for widget specific event arguments.
Definition: widget.h:60