Spinning Topp Logo BlackTopp Studios
inc
tabset.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 _uitabset_cpp
41 #define _uitabset_cpp
42 
43 #include "UI/tabset.h"
44 #include "UI/stackbutton.h"
45 #include "UI/screen.h"
46 #include "UI/layoutstrategy.h"
47 
48 #include "Input/inputmanager.h"
49 #include "Input/mouse.h"
50 
51 namespace Mezzanine
52 {
53  namespace UI
54  {
55  ///////////////////////////////////////////////////////////////////////////////
56  // TabSet Static Members
57 
58  const String TabSet::TypeName = "TabSet";
59 
60  ///////////////////////////////////////////////////////////////////////////////
61  // TabSet Methods
62 
64  StackedContainer(Parent),
65  VisibleChild(NULL)
66  { }
67 
68  TabSet::TabSet(const String& RendName, Screen* Parent) :
69  StackedContainer(RendName,Parent),
70  VisibleChild(NULL)
71  { }
72 
73  TabSet::TabSet(const String& RendName, const UnifiedRect& RendRect, Screen* Parent) :
74  StackedContainer(RendName,RendRect,Parent),
75  VisibleChild(NULL)
76  { }
77 
78  TabSet::TabSet(const XML::Node& XMLNode, Screen* Parent) :
79  StackedContainer(Parent),
80  VisibleChild(NULL)
81  { this->ProtoDeSerialize(XMLNode); }
82 
84  {
85  this->VisibleChild = NULL;
86  this->SubSetBindings.clear();
87  }
88 
89  void TabSet::ProtoSerializeImpl(XML::Node& SelfRoot) const
90  {
91  this->Widget::ProtoSerializeImpl(SelfRoot);
92  this->ProtoSerializeButtonBindings(SelfRoot);
93  }
94 
96  {
97  this->Widget::ProtoDeSerializeImpl(SelfRoot);
98  this->ProtoDeSerializeButtonBindings(SelfRoot);
99  }
100 
101  ///////////////////////////////////////////////////////////////////////////////
102  // Utility Methods
103 
105  {
106  this->SetVisibleSubSet( MakeVisible->GetZOrder() );
107  }
108 
109  void TabSet::SetVisibleSubSet(const UInt16 Binding)
110  {
111  TabbedSubSet* NewVisible = this->GetChild( Binding );
112  if( this->VisibleChild != NULL ) {
113  this->VisibleChild->Hide();
114  }
115 
116  this->VisibleChild = NewVisible;
117 
118  if( this->VisibleChild != NULL ) {
119  this->VisibleChild->SetVisible( this->GetVisible() );
120  }
121  this->_MarkDirty();
122  }
123 
125  {
126  return this->VisibleChild;
127  }
128 
130  {
131  return TabSet::TypeName;
132  }
133 
134  ///////////////////////////////////////////////////////////////////////////////
135  // Visibility and Priority Methods
136 
138  {
139  if( this->Visible == CanSee )
140  return;
141  if(CanSee) {
142  this->_OnVisibilityShown();
143  if( this->VisibleChild != NULL ) {
144  this->VisibleChild->SetVisible(CanSee);
145  }
146  }else{
147  this->_OnVisibilityHidden();
148  for( ChildIterator It = this->ChildWidgets.begin() ; It != this->ChildWidgets.end() ; ++It )
149  {
150  (*It)->SetVisible(CanSee);
151  }
152  }
153  }
154 
156  {
157  if( this->Visible == true )
158  return;
159  this->_OnVisibilityShown();
160  if( this->VisibleChild != NULL ) {
161  this->VisibleChild->Show();
162  }
163  }
164 
166  {
167  if( this->Visible == false )
168  return;
169  this->_OnVisibilityHidden();
170  for( ChildIterator It = this->ChildWidgets.begin() ; It != this->ChildWidgets.end() ; ++It )
171  {
172  (*It)->Hide();
173  }
174  }
175 
176  ///////////////////////////////////////////////////////////////////////////////
177  // TabSet Properties
178 
179  ///////////////////////////////////////////////////////////////////////////////
180  // TabSet Configuration
181 
182  void TabSet::SetButtonConfig(const UInt16 Config, StackButton* ConfigButton)
183  {
184  this->SubSetBindings.insert( std::pair<StackButton*,UInt16>(ConfigButton,Config) );
185  //if( ConfigButton->GetBoundContainer() != this ) {
186  //}
187  ConfigButton->_SetBoundContainer(this);
188  }
189 
190  UInt16 TabSet::GetButtonConfig(const StackButton* ConfigButton) const
191  {
192  ConstTabbedSubSetIterator SubIt = this->SubSetBindings.find( const_cast<StackButton*>(ConfigButton) );
193  if( SubIt != this->SubSetBindings.end() ) {
194  return (*SubIt).second;
195  }else{
196  return 0;
197  }
198  }
199 
201  {
202  TabbedSubSetIterator BindIt = this->SubSetBindings.find(ConfigButton);
203  if( BindIt != this->SubSetBindings.end() ) {
204  this->SubSetBindings.erase( BindIt );
205  ConfigButton->_SetBoundContainer(NULL);
206  }
207  }
208 
210  {
211  TabbedSubSet* NewSet = this->ParentScreen->CreateWidget(Name,UnifiedRect(0,0,1,1,0,0,0,0));
212  this->AddChild(NewSet,ChildZOrder);
213  NewSet->Hide();
214  return NewSet;
215  }
216 
217  ///////////////////////////////////////////////////////////////////////////////
218  // Serialization
219 
221  {
223  }
224 
226  {
227  XML::Node BindingsNode = SelfRoot.AppendChild( "SubSetBindings" );
228 
229  if( BindingsNode.AppendAttribute("Version").SetValue("1") ) {
230  for( ConstTabbedSubSetIterator BindingIt = this->SubSetBindings.begin() ; BindingIt != this->SubSetBindings.end() ; ++BindingIt )
231  {
232  XML::Node BindingNode = BindingsNode.AppendChild( "SubSetBinding" );
233 
234  if( BindingNode.AppendAttribute("Version").SetValue("1") &&
235  BindingNode.AppendAttribute("ConfigID").SetValue( (*BindingIt).second ) &&
236  BindingNode.AppendAttribute("ButtonName").SetValue( (*BindingIt).first->GetName() ) )
237  {
238  continue;
239  }else{
240  SerializeError("Create XML Version Attribute","SubSetBinding",true);
241  }
242  }
243  }else{
244  SerializeError("Create XML Version Attribute","SubSetBindings",true);
245  }
246  }
247 
249  {
251  }
252 
254  {
255  this->SubSetBindings.clear();
256 
257  XML::Attribute CurrAttrib;
258  XML::Node BindingsNode = SelfRoot.GetChild( "SubSetBindings" );
259 
260  if( !BindingsNode.Empty() ) {
261  if( BindingsNode.GetAttribute("Version").AsInt() == 1 ) {
262  for( XML::NodeIterator BindingNodeIt = BindingsNode.begin() ; BindingNodeIt != BindingsNode.end() ; ++BindingNodeIt )
263  {
264  if( (*BindingNodeIt).GetAttribute("Version").AsInt() == 1 ) {
265  UInt16 ConfigID = 0;
266  String ButtonName;
267 
268  CurrAttrib = (*BindingNodeIt).GetAttribute("ConfigID");
269  if( !CurrAttrib.Empty() )
270  ConfigID = static_cast<UInt16>( CurrAttrib.AsUint() );
271 
272  CurrAttrib = (*BindingNodeIt).GetAttribute("ButtonName");
273  if( !CurrAttrib.Empty() )
274  ButtonName = CurrAttrib.AsString();
275 
276  if( !ButtonName.empty() ) {
277  Widget* NamedButton = this->ParentScreen->GetWidget(ButtonName);
278  if( NamedButton != NULL && NamedButton->GetTypeName() == StackButton::TypeName ) {
279  this->SetButtonConfig(ConfigID,static_cast<StackButton*>(NamedButton));
280  }else{
281  StringStream ExceptionStream;
282  ExceptionStream << "Named StackButton \"" << ButtonName << "\" not found when deserializing Widget named \"" << this->GetName() << "\".";
284  }
285  }
286  }else{
287  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + String("SubSetBindings") + ": Not Version 1.");
288  }
289  }
290  }else{
291  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + String("SubSetBindings") + ": Not Version 1.");
292  }
293  }
294  }
295 
297  {
298  return TabSet::TypeName;
299  }
300 
301  ///////////////////////////////////////////////////////////////////////////////
302  // Internal Event Methods
303 
304  ///////////////////////////////////////////////////////////////////////////////
305  // Internal Methods
306 
308  {
309  TabbedSubSetIterator SubIt = this->SubSetBindings.find(Selected);
310  if( SubIt != this->SubSetBindings.end() ) {
311  this->SetVisibleSubSet( (*SubIt).second );
312  }
313  }
314 
315  ///////////////////////////////////////////////////////////////////////////////
316  // TabSetFactory Methods
317 
319  { return TabSet::TypeName; }
320 
321  TabSet* TabSetFactory::CreateTabSet(const String& RendName, Screen* Parent)
322  {
323  TabSet* Ret = new TabSet(RendName,Parent);
324  Ret->_SetLayoutStrat( new LayoutStrategy() );
325  return Ret;
326  }
327 
328  TabSet* TabSetFactory::CreateTabSet(const String& RendName, const UnifiedRect& RendRect, Screen* Parent)
329  {
330  TabSet* Ret = new TabSet(RendName,RendRect,Parent);
331  Ret->_SetLayoutStrat( new LayoutStrategy() );
332  return Ret;
333  }
334 
336  {
337  TabSet* Ret = new TabSet(XMLNode,Parent);
338  Ret->_SetLayoutStrat( new LayoutStrategy() );
339  return Ret;
340  }
341 
343  {
344  TabSet* Ret = new TabSet(Parent);
345  Ret->_SetLayoutStrat( new LayoutStrategy() );
346  return Ret;
347  }
348 
349  Widget* TabSetFactory::CreateWidget(const String& RendName, const NameValuePairMap& Params, Screen* Parent)
350  { return this->CreateTabSet(RendName,Parent); }
351 
352  Widget* TabSetFactory::CreateWidget(const String& RendName, const UnifiedRect& RendRect, const NameValuePairMap& Params, Screen* Parent)
353  { return this->CreateTabSet(RendName,RendRect,Parent); }
354 
356  { return this->CreateTabSet(XMLNode,Parent); }
357 
359  { delete static_cast<TabSet*>( ToBeDestroyed ); }
360  }//UI
361 }//Mezzanine
362 
363 #endif
virtual void DestroyWidget(Widget *ToBeDestroyed)
Destroys a Widget created by this factory.
Definition: tabset.cpp:358
virtual ~TabSet()
Class destructor.
Definition: tabset.cpp:83
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
Definition: tabset.cpp:220
virtual UInt16 GetButtonConfig(const StackButton *ConfigButton) const
Gets the role of the specified StackedButton for this StackedContainer.
Definition: tabset.cpp:190
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
static String GetSerializableName()
Get the name of the the XML tag the Renderable class will leave behind as its instances are serialize...
Definition: tabset.cpp:296
const String & GetName() const
Gets the name of this renderable.
Definition: renderable.cpp:77
TabbedSubSet * VisibleChild
A pointer to the only visible child of this TabSet.
Definition: tabset.h:74
static const String TypeName
String containing the type name for this class: "TabSet".
Definition: tabset.h:67
A light-weight handle for manipulating attributes in DOM tree.
Definition: attribute.h:74
virtual void Show()
Forces this renderable to be shown.
Definition: widget.cpp:257
This is a base class for the algorithms used by QuadRenderables to determine how they should update t...
virtual void _NotifyButtonSelected(StackButton *Selected)
Notifies this StackedContainer a button has been selected.
Definition: tabset.cpp:307
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
virtual TabbedSubSet * GetVisibleSubSet() const
Gets a pointer to the TabbedSubSet that is currently visible within this TabSet.
Definition: tabset.cpp:124
virtual String GetWidgetTypeName() const
Gets the name of the Widget that is created by this factory.
Definition: tabset.cpp:318
virtual void SetVisibleSubSet(TabbedSubSet *MakeVisible)
Makes a child TabbedSubSet in this TabSet visible.
Definition: tabset.cpp:104
virtual void _OnVisibilityShown()
Self logic to be executed when this widget becomes visible.
Definition: widget.cpp:519
virtual const String & GetTypeName() const
Gets the type of widget this is.
Definition: widget.cpp:156
virtual const String & GetTypeName() const
Gets the type of widget this is.
Definition: tabset.cpp:129
#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
virtual void SetVisible(Boole CanSee)
Sets the visibility of this renderable.
Definition: tabset.cpp:137
const Char8 * AsString(const Char8 *def="") const
Attempts to convert the value of the attribute to a String and returns the results.
Boole Visible
Stores whether this Renderable is to be rendered (also dependent on parent visibility).
Definition: renderable.h:75
This is a widget that stores sets of renderables but only displays one at a time. ...
Definition: tabset.h:54
bool Empty() const
Is this storing anything at all?
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 RemoveButtonConfig(StackButton *ConfigButton)
Removes a button from the configuration of this TabSet.
Definition: tabset.cpp:200
virtual Widget * GetChild(const UInt16 Zorder) const
Gets a child by it's ZOrder.
std::stringstream StringStream
A Datatype used for streaming operations with strings.
Definition: datatypes.h:176
ChildContainer ChildWidgets
This is a container storing all the children that belong to this Quad.
virtual const UInt16 & GetZOrder() const
Gets the currently set ZOrder of this QuadRenderable with it's parent.
virtual TabSet * CreateTabSet(const String &RendName, Screen *Parent)
Creates a new TabSet.
Definition: tabset.cpp:321
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
uint16_t UInt16
An 16-bit unsigned integer.
Definition: datatypes.h:122
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: tabset.cpp:248
virtual void ProtoSerializeButtonBindings(XML::Node &SelfRoot) const
Convert the Button Bindings of this class to an XML::Node ready for serialization.
Definition: tabset.cpp:225
Child node iterator (a bidirectional iterator over a collection of Node)
Definition: nodeiterator.h:77
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
iterator begin() const
Get a Child node iterator that references the first child Node.
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. ...
iterator end() const
Get a Child node iterator that references one past the last child Node.
bool Empty() const
Is this storing anything at all?
This is the base class for all widgets.
Definition: widget.h:126
virtual TabbedSubSet * CreateTabbedSubSet(const String &Name, const UInt16 ChildZOrder)
Creates a new TabbedSubSet for this TabSet.
Definition: tabset.cpp:209
virtual void ProtoSerializeImpl(XML::Node &SelfRoot) const
Implementation method for serializing additional sets of data.
Definition: tabset.cpp:89
virtual void ProtoDeSerializeImpl(const XML::Node &SelfRoot)
Implementation method for deseriailizing additional sets of data.
Definition: widget.cpp:105
virtual void Hide()
Forces this renderable to hide.
Definition: tabset.cpp:165
TabbedSubSetContainer::iterator TabbedSubSetIterator
Iterator type for TabbedSubSet instances stored by this class.
Definition: tabset.h:62
virtual void Show()
Forces this renderable to be shown.
Definition: tabset.cpp:155
TabbedSubSetContainer::const_iterator ConstTabbedSubSetIterator
Const Iterator type for TabbedSubSet instances stored stored by this class.
Definition: tabset.h:64
virtual void AddChild(Widget *Child)
Adds a Widget to this as a child of this quad.
Thrown when parameters are checked at runtime and found invalid.
Definition: exception.h:108
virtual void _SetLayoutStrat(LayoutStrategy *ToSet)
Sets a new LayoutStrategy for this quad to use.
virtual void Hide()
Forces this renderable to hide.
Definition: widget.cpp:266
virtual void ProtoDeSerializeButtonBindings(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the Button Bindings of this object with it...
Definition: tabset.cpp:253
virtual Widget * GetWidget(const String &Name)
Gets a widget in this screen by name.
Definition: screen.cpp:573
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
virtual void ProtoSerializeImpl(XML::Node &SelfRoot) const
Implementation method for serializing additional sets of data.
Definition: widget.cpp:97
virtual Widget * CreateWidget(Screen *Parent)
Creates a Widget of the type represented by this factory.
Definition: tabset.cpp:342
virtual void ProtoDeSerializeImpl(const XML::Node &SelfRoot)
Implementation method for deseriailizing additional sets of data.
Definition: tabset.cpp:95
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
TabSet(Screen *Parent)
Blank constructor.
Definition: tabset.cpp:63
ChildContainer::iterator ChildIterator
Iterator type for Widget instances stored by this class.
virtual void SetVisible(Boole CanSee)
Sets the visibility of this renderable.
Definition: widget.cpp:229
virtual void _SetBoundContainer(StackedContainer *ToBeBound)
Notifies this StackButton that a StackedContainer has been bound to it.
virtual void ProtoDeSerialize(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite this object with it.
virtual Boole GetVisible() const
Gets the visibility setting of this renderable.
Definition: widget.cpp:243
virtual void SetButtonConfig(const UInt16 Config, StackButton *ConfigButton)
Binds a StackedButton to this container according to the provided config value.
Definition: tabset.cpp:182
virtual void _MarkDirty()
Marks this renderable as dirty, and informs other renderables if needed.
void SerializeError(const String &FailedTo, const String &ClassName, Boole SOrD)
Simply does some string concatenation, then throws an Exception.
virtual void _OnVisibilityHidden()
Self logic to be executed when this widget becomes invisible.
Definition: widget.cpp:528
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.
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
TabbedSubSetContainer SubSetBindings
A container storing all the bindings to.
Definition: tabset.h:71
virtual Widget * CreateWidget(const XML::Node &WidgetNode)
Creates a widget from an XML::Node.
Definition: screen.cpp:542