MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
menubutton.cpp
1 //© Copyright 2010 - 2013 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 _uimenubutton_cpp
41 #define _uimenubutton_cpp
42 
43 #include "UI/menubutton.h"
44 #include "UI/menuentry.h"
45 #include "UI/screen.h"
46 #include "stringtool.h"
47 #include "exception.h"
48 #include "serialization.h"
49 
50 namespace Mezzanine
51 {
52  namespace UI
53  {
54  const String MenuButton::TypeName = "MenuButton";
55 
56  ///////////////////////////////////////////////////////////////////////////////
57  // MenuButton Methods
58 
60  Button(Parent),
61  BoundMenu(NULL)
62  { }
63 
64  MenuButton::MenuButton(const String& RendName, Screen* Parent) :
65  Button(RendName,Parent),
66  BoundMenu(NULL)
67  { }
68 
69  MenuButton::MenuButton(const String& RendName, const UnifiedRect& RendRect, Screen* Parent) :
70  Button(RendName,RendRect,Parent),
71  BoundMenu(NULL)
72  { }
73 
74  MenuButton::MenuButton(const XML::Node& XMLNode, Screen* Parent) :
75  Button(Parent),
76  BoundMenu(NULL)
77  { this->ProtoDeSerialize(XMLNode); }
78 
80  { }
81 
82  ///////////////////////////////////////////////////////////////////////////////
83  // Serialization
84 
86  {
87  this->Button::ProtoSerializeProperties(SelfRoot);
88 
89  XML::Node PropertiesNode = SelfRoot.AppendChild( MenuButton::GetSerializableName() + "Properties" );
90 
91  if( PropertiesNode.AppendAttribute("Version").SetValue("1") )
92  {
93  // Only if we have a valid binding
94  if( this->BoundMenu != NULL ) {
95  if( PropertiesNode.AppendAttribute("MenuEntryName").SetValue( this->BoundMenu->GetName() ) &&
96  PropertiesNode.AppendAttribute("ButtonConfig").SetValue( static_cast<Whole>( this->BoundMenu->GetButtonConfig(this) ) ) )
97  {
98  return;
99  }else{
100  SerializeError("Create XML Attribute Values",MenuButton::GetSerializableName() + "Properties",true);
101  }
102  }
103  }else{
104  SerializeError("Create XML Attribute Values",MenuButton::GetSerializableName() + "Properties",true);
105  }
106  }
107 
109  {
110  this->Button::ProtoDeSerializeProperties(SelfRoot);
111 
112  XML::Attribute CurrAttrib;
113  XML::Node PropertiesNode = SelfRoot.GetChild( MenuButton::GetSerializableName() + "Properties" );
114 
115  if( !PropertiesNode.Empty() ) {
116  if(PropertiesNode.GetAttribute("Version").AsInt() == 1) {
117  String MenuName;
119 
120  CurrAttrib = PropertiesNode.GetAttribute("MenuEntryName");
121  if( !CurrAttrib.Empty() )
122  MenuName = CurrAttrib.AsString();
123 
124  CurrAttrib = PropertiesNode.GetAttribute("IsPushButton");
125  if( !CurrAttrib.Empty() )
126  MenuConfig = static_cast<MenuEntry::ButtonConfig>( CurrAttrib.AsWhole() );
127 
128  if( !MenuName.empty() ) {
129  Widget* UncastedMenu = this->ParentScreen->GetWidget(MenuName);
130  if( UncastedMenu->GetTypeName() == "MenuEntry" ) {
131  MenuEntry* CastedMenu = static_cast<MenuEntry*>( UncastedMenu );
132  switch( MenuConfig )
133  {
134  case MenuEntry::BC_PushButton: CastedMenu->SetPushButton(this); break;
135  case MenuEntry::BC_PopButton: CastedMenu->SetPopButton(this); break;
136  case MenuEntry::BC_ToggleButton: CastedMenu->SetToggleButton(this); break;
137  default: /* Do Nothing */ break;
138  }
139  }else{
140  MEZZ_EXCEPTION(Exception::PARAMETERS_EXCEPTION,"Named Widget that was expected to be a MenuEntry is not a MenuEntry.");
141  }
142  }
143  }else{
144  MEZZ_EXCEPTION(Exception::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + (MenuButton::GetSerializableName() + "Properties") + ": Not Version 1.");
145  }
146  }else{
147  MEZZ_EXCEPTION(Exception::II_IDENTITY_NOT_FOUND_EXCEPTION,MenuButton::GetSerializableName() + "Properties" + " was not found in the provided XML node, which was expected.");
148  }
149  }
150 
152  { return MenuButton::TypeName; }
153 
154  ///////////////////////////////////////////////////////////////////////////////
155  // Internal Methods
156 
158  { this->BoundMenu = ToBeBound; }
159 
160  ///////////////////////////////////////////////////////////////////////////////
161  // MenuButtonFactory Methods
162 
164  { return MenuButton::TypeName; }
165 
167  { return new MenuButton(RendName,Parent); }
168 
169  MenuButton* MenuButtonFactory::CreateMenuButton(const String& RendName, const UnifiedRect& RendRect, Screen* Parent)
170  { return new MenuButton(RendName,RendRect,Parent); }
171 
173  { return new MenuButton(XMLNode,Parent); }
174 
176  { return new MenuButton(Parent); }
177 
178  Widget* MenuButtonFactory::CreateWidget(const String& RendName, const NameValuePairMap& Params, Screen* Parent)
179  { return this->CreateMenuButton(RendName,Parent); }
180 
181  Widget* MenuButtonFactory::CreateWidget(const String& RendName, const UnifiedRect& RendRect, const NameValuePairMap& Params, Screen* Parent)
182  { return this->CreateMenuButton(RendName,RendRect,Parent); }
183 
185  { return this->CreateMenuButton(XMLNode,Parent); }
186 
188  { delete static_cast<MenuButton*>( ToBeDestroyed ); }
189  }//UI
190 }//Mezzanine
191 
192 #endif