MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
checkbox.cpp
1 // © Copyright 2010 - 2014 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(true)
68  { }
69 
70  CheckBox::CheckBox(const String& RendName, Screen* Parent) :
71  Button(RendName,Parent),
72  SelectLock(true)
73  { this->ConstructCheckbox(); }
74 
75  CheckBox::CheckBox(const String& RendName, const UnifiedRect& RendRect, Screen* Parent) :
76  Button(RendName,RendRect,Parent),
77  SelectLock(true)
78  { this->ConstructCheckbox(); }
79 
80  CheckBox::CheckBox(const XML::Node& XMLNode, Screen* Parent) :
81  Button(Parent),
82  SelectLock(true)
83  { this->ProtoDeSerialize(XMLNode); }
84 
86  { }
87 
89  {
90  // Add our new events
93  // Add some more render layer groups
94  RenderLayerGroup* SelectedNormalGroup = this->CreateRenderLayerGroup("SelectedNormal");
95  RenderLayerGroup* SelectedHoveredGroup = this->CreateRenderLayerGroup("SelectedHovered");
96 
97  this->StateGroupBindings[ WS_Selected ] = SelectedNormalGroup;
98  this->StateGroupBindings[ WS_Selected | WS_Hovered ] = SelectedHoveredGroup;
99  this->StateGroupBindings[ WS_Selected | WS_Focused ] = SelectedNormalGroup;
100  this->StateGroupBindings[ WS_Selected | WS_Dragged ] = SelectedNormalGroup;
101  this->StateGroupBindings[ WS_Selected | WS_Hovered | WS_Focused ] = SelectedHoveredGroup;
102  this->StateGroupBindings[ WS_Selected | WS_Focused | WS_Dragged ] = SelectedNormalGroup;
103  this->StateGroupBindings[ WS_Selected | WS_Dragged | WS_Hovered ] = SelectedHoveredGroup;
104  this->StateGroupBindings[ 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 
120  void CheckBox::ManualSelect(Boolean Select)
121  {
122  if( this->IsSelected() != Select ) {
123  Boolean NewState = !this->IsSelected();
124  if( NewState ) this->_OnSelected();
125  else this->_OnDeselected();
126  }
127  }
128 
129  void CheckBox::SetSelectLock(Boolean Lock)
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(Exception::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + (CheckBox::GetSerializableName() + "Properties") + ": Not Version 1.");
171  }
172  }else{
173  MEZZ_EXCEPTION(Exception::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  Boolean 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 
208  this->FireEvent(Args);
209  }
210 
212  {
213  this->State &= ~WS_Selected;
214  this->SetGroupFromState(this->State);
215 
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