MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
linearcontainer.cpp
1 //© Copyright 2010 - 2012 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 
41 #ifndef _uilinearcontainer_cpp
42 #define _uilinearcontainer_cpp
43 
44 #include "UI/linearcontainer.h"
45 #include "UI/layoutstrategy.h"
46 
47 namespace Mezzanine
48 {
49  namespace UI
50  {
52  PagedContainer(Parent),
53  ForcedSizingRules(SE_None),
54  VisibleChildAlign(UI::LA_TopLeft)
55  { }
56 
57  LinearContainer::LinearContainer(const String& RendName, Screen* Parent) :
58  PagedContainer(RendName,Parent),
59  ForcedSizingRules(SE_None),
60  VisibleChildAlign(UI::LA_TopLeft)
61  { }
62 
63  LinearContainer::LinearContainer(const String& RendName, const UnifiedRect& RendRect, Screen* Parent) :
64  PagedContainer(RendName,RendRect,Parent),
65  ForcedSizingRules(SE_None),
66  VisibleChildAlign(UI::LA_TopLeft)
67  { }
68 
70  { }
71 
72  void LinearContainer::UpdateInvisibleChild(const Rect& OldSelfRect, const Rect& NewSelfRect, QuadRenderable* InvisibleChild)
73  {
74  // Setup our rects for update
75  const Rect OldChildRect = InvisibleChild->GetRect();
76  Rect NewChildRect;
77  // Assign a dummy position since these will be invisible
78  NewChildRect.Position = NewSelfRect.Position;
79  // Calculate the sizing with our utility strat
80  if( this->ForcedSizingRules & SE_OnUpdate ) {
81  InvisibleChild->SetSizingPolicy( this->ChildSizing );
82  }
83  NewChildRect.Size = this->LayoutStrat->HandleChildSizing(OldSelfRect,NewSelfRect,InvisibleChild);
84  // Perform the update
85  InvisibleChild->UpdateDimensions(OldChildRect,NewChildRect);
86  InvisibleChild->Hide();
87  }
88 
89  ///////////////////////////////////////////////////////////////////////////////
90  // Utility
91 
92  void LinearContainer::SetChildSizing(const SizingInfo& ForcedSize, const Whole Enforcement)
93  {
94  this->SetChildSize(ForcedSize);
95  this->SetChildSizeEnforcement(Enforcement);
96  }
97 
98  void LinearContainer::SetChildSize(const SizingInfo& ForcedSize)
99  { this->ChildSizing = ForcedSize; }
100 
102  { return this->ChildSizing; }
103 
105  { this->ForcedSizingRules = Enforcement; }
106 
108  { return this->ForcedSizingRules; }
109 
111  { this->LinearPadding = Padding; }
112 
114  { return this->LinearPadding; }
115 
117  { this->VisibleChildAlign = ChildAlign; }
118 
120  { return this->VisibleChildAlign; }
121 
122  ///////////////////////////////////////////////////////////////////////////////
123  // Child Management
124 
126  {
127  if( this->ForcedSizingRules & SE_OnAdd ) {
128  Child->SetSizingPolicy( this->ChildSizing );
129  }
130 
131  this->QuadRenderable::AddChild(Child);
132  }
133 
134  ///////////////////////////////////////////////////////////////////////////////
135  // Serialization
136 
138  {
140  XML::Node PropertiesNode = SelfRoot.AppendChild( LinearContainer::GetSerializableName() + "Properties" );
141 
142  if( PropertiesNode.AppendAttribute("Version").SetValue("1") &&
143  PropertiesNode.AppendAttribute("ForcedSizingRules").SetValue( this->ForcedSizingRules ) &&
144  PropertiesNode.AppendAttribute("VisibleChildAlign").SetValue( static_cast<Whole>( this->VisibleChildAlign ) ) )
145  {
146  XML::Node ChildSizingNode = PropertiesNode.AppendChild("ChildSizing");
147  this->ChildSizing.ProtoSerialize( ChildSizingNode );
148  XML::Node LinearPaddingNode = PropertiesNode.AppendChild("LinearPadding");
149  this->LinearPadding.ProtoSerialize( LinearPaddingNode );
150 
151  return;
152  }else{
153  SerializeError("Create XML Attribute Values",LinearContainer::GetSerializableName() + "Properties",true);
154  }
155  }
156 
158  {
160 
161  XML::Attribute CurrAttrib;
162  XML::Node PropertiesNode = SelfRoot.GetChild( LinearContainer::GetSerializableName() + "Properties" );
163 
164  if( !PropertiesNode.Empty() ) {
165  if(PropertiesNode.GetAttribute("Version").AsInt() == 1) {
166  // Get the single data type properties
167  CurrAttrib = PropertiesNode.GetAttribute("ForcedSizingRules");
168  if( !CurrAttrib.Empty() )
169  this->ForcedSizingRules = CurrAttrib.AsWhole();
170 
171  CurrAttrib = PropertiesNode.GetAttribute("VisibleChildAlign");
172  if( !CurrAttrib.Empty() )
173  this->VisibleChildAlign = static_cast<UI::LinearAlignment>( CurrAttrib.AsWhole() );
174 
175  // Get the properties that need their own nodes
176  XML::Node ChildSizingNode = PropertiesNode.GetChild("ChildSizing").GetFirstChild();
177  if( !ChildSizingNode.Empty() )
178  this->PositioningPolicy.ProtoDeSerialize(ChildSizingNode);
179 
180  XML::Node LinearPaddingNode = PropertiesNode.GetChild("LinearPadding").GetFirstChild();
181  if( !LinearPaddingNode.Empty() )
182  this->SizingPolicy.ProtoDeSerialize(LinearPaddingNode);
183  }else{
184  MEZZ_EXCEPTION(Exception::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + (PagedContainer::GetSerializableName() + "Properties") + ": Not Version 1.");
185  }
186  }else{
187  MEZZ_EXCEPTION(Exception::II_IDENTITY_NOT_FOUND_EXCEPTION,PagedContainer::GetSerializableName() + "Properties" + " was not found in the provided XML node, which was expected.");
188  }
189  }
190 
192  {
193  return "LinearContainer";
194  }
195 
196  ///////////////////////////////////////////////////////////////////////////////
197  // Internal Methods
198  }//UI
199 }//Mezzanine
200 
201 #endif