MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
verticalcontainer.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 #ifndef _uiverticalcontainer_cpp
41 #define _uiverticalcontainer_cpp
42 
43 #include "UI/verticalcontainer.h"
44 #include "UI/verticallayoutstrategy.h"
45 
46 #include "UI/verticalscrollbar.h"
47 #include "UI/spinner.h"
48 
49 namespace Mezzanine
50 {
51  namespace UI
52  {
53  const String VerticalContainer::TypeName = "VerticalContainer";
54 
55  ///////////////////////////////////////////////////////////////////////////////
56  // VerticalContainer Methods
57 
59  LinearContainer(Parent)
60  { this->LayoutStrat = new LayoutStrategy(); }
61 
63  LinearContainer(RendName,Parent)
64  { this->LayoutStrat = new LayoutStrategy(); }
65 
66  VerticalContainer::VerticalContainer(const String& RendName, const UnifiedRect& RendRect, Screen* Parent) :
67  LinearContainer(RendName,RendRect,Parent)
68  { this->LayoutStrat = new LayoutStrategy(); }
69 
71  LinearContainer(Parent)
72  { this->ProtoDeSerialize(XMLNode); }
73 
75  { delete this->LayoutStrat; }
76 
77  void VerticalContainer::UpdateContainerDimensionsImpl(const Rect& OldSelfRect, const Rect& NewSelfRect)
78  {
79  // Get the actual work area dimensions, which should already be updated
80  const Rect ParentRect = this->ParentQuad->GetRect();
81  const Vector2 ActWorkArea = this->WorkAreaSize.CalculateActualDimensions( ParentRect.Size );
82 
83  // Update the provider data if needed
85 
86  // Pre-check setup
87  const Real ActPadding = this->LinearPadding.CalculateActualDimension( ParentRect.Size.Y );
88  Real YTarget = ( this->YProvider->GetCurrentYPage() - 1 ) * ActWorkArea.Y;
89  Real CurrYPos = ActPadding * 0.5;
90  this->VisibleChildren.clear();
91 
92  // Find the first one to get rendered going through by ZOrder
93  ChildIterator ChildUpdateIt = this->ChildWidgets.begin();
94  while( ChildUpdateIt != this->ChildWidgets.end() && CurrYPos < YTarget )
95  {
96  this->UpdateInvisibleChild( OldSelfRect, NewSelfRect, (*ChildUpdateIt) );
97  // Increment our 'cursor' position
98  CurrYPos += ( (*ChildUpdateIt)->GetActualSize().Y + ActPadding );
99  ++ChildUpdateIt;
100  }
101 
102  // If we've reached the end of the children, there is nothing to render
103  if( ChildUpdateIt != this->ChildWidgets.end() )
104  {
105  // Setup any additional data for the next series of loops
106  std::vector<Vector2> ChildSizeCache;
107  Real TotalLinearSize = 0;
108 
109  // Also reset our cursor and target so we can better utilize the viewable space
110  CurrYPos = ActPadding * 0.5;
111  YTarget = NewSelfRect.Size.Y;
112 
113  while( ChildUpdateIt != this->ChildWidgets.end() /*&& CurrYPos < YTarget */ )
114  {
115  // Due to having to abide by certain positioning rules, we can't determine the position until we have all the visible children
116  // So just find the size, see if we're passed the mark and if not then save it for later
117  Vector2 ChildSize = this->LayoutStrat->HandleChildSizing( OldSelfRect, NewSelfRect, (*ChildUpdateIt) );
118  // See if our child can fit
119  if( CurrYPos + ChildSize.Y < YTarget ) {
120  // Clamp the perpendicular axis, at least until we get proper expanding widgets
121  if( ChildSize.X > NewSelfRect.Size.X ) {
122  ChildSize.X = NewSelfRect.Size.X;
123  }
124  ChildSizeCache.push_back( ChildSize );
125  this->VisibleChildren.push_back( (*ChildUpdateIt) );
126  CurrYPos += ( ChildSize.Y + ActPadding );
127  TotalLinearSize += ( ChildSize.Y + ActPadding );
128  }else{
129  break;
130  }
131  }
132 
133  // Set the remaining children (if any) in the corner with the rest
134  while( ChildUpdateIt != this->ChildWidgets.end() )
135  {
136  this->UpdateInvisibleChild( OldSelfRect, NewSelfRect, (*ChildUpdateIt) );
137  ++ChildUpdateIt;
138  }
139 
140  // By now all our children have been processed, just have to focus on the last couple steps for visible children
141  // Start by doing our alignment stuff, and trim off one increment of ActPadding since we were blindly applying it earlier
142  TotalLinearSize -= ActPadding;
143  switch( this->VisibleChildAlign )
144  {
145  case UI::LA_TopLeft: CurrYPos = ActPadding * 0.5;
146  case UI::LA_Center: CurrYPos = ( NewSelfRect.Position.Y + ( NewSelfRect.Size.Y * 0.5 ) ) - ( TotalLinearSize * 0.5 );
147  case UI::LA_BottomRight: CurrYPos = ( NewSelfRect.Position.Y + NewSelfRect.Size.Y ) - ( TotalLinearSize + ActPadding * 0.5);
148  }
149 
150  for( Whole Y = 0 ; Y < this->VisibleChildren.size() ; ++Y )
151  {
152  // Setup child transform data
153  const Rect OldChildRect = this->VisibleChildren.at(Y)->GetRect();
154  Rect NewChildRect;
155  // Set the Size
156  NewChildRect.Size = ChildSizeCache.at(Y);
157  // Set the Position
158  NewChildRect.Position.X = this->LayoutStrat->HandleChildHorizontalPositioning( OldSelfRect, NewSelfRect, NewChildRect.Size, this->VisibleChildren.at(Y) );
159  NewChildRect.Position.Y = CurrYPos;
160  // Perform the update
161  this->VisibleChildren.at(Y)->UpdateDimensions(OldChildRect,NewChildRect);
162  // Increment the cursor
163  CurrYPos += ( NewChildRect.Size.Y + ActPadding );
164  }
165  ChildSizeCache.clear();
166  }
167  }
168 
169  ///////////////////////////////////////////////////////////////////////////////
170  // Utility
171 
173  { return VerticalContainer::TypeName; }
174 
176  { this->SetYProvider(YProv); }
177 
179  { /* Do Nothing */ }
180 
182  {
183  if( this->YProvider != NULL && this->YProvider != YProv ) {
184  this->UnbindProvider(this->YProvider);
185  }
186  this->YProvider = YProv;
187  this->YProvider->_SetContainer(this);
189  }
190 
192  { this->SetYProvider(Prov); }
193 
195  { this->SetProviders(Prov,Prov); }
196 
197  ///////////////////////////////////////////////////////////////////////////////
198  // Child Management
199 
200  ///////////////////////////////////////////////////////////////////////////////
201  // Serialization
202 
204  {
206  }
207 
209  {
211  }
212 
214  {
216  }
217 
218  ///////////////////////////////////////////////////////////////////////////////
219  // Internal Methods
220 
221  ///////////////////////////////////////////////////////////////////////////////
222  // VerticalContainerFactory Methods
223 
225  { return VerticalContainer::TypeName; }
226 
228  { return new VerticalContainer(RendName,Parent); }
229 
231  { return new VerticalContainer(RendName,RendRect,Parent); }
232 
234  { return new VerticalContainer(XMLNode,Parent); }
235 
237  { return new VerticalContainer(Parent); }
238 
240  { return this->CreateVerticalContainer(RendName,Parent); }
241 
242  Widget* VerticalContainerFactory::CreateWidget(const String& RendName, const UnifiedRect& RendRect, const NameValuePairMap& Params, Screen* Parent)
243  { return this->CreateVerticalContainer(RendName,RendRect,Parent); }
244 
246  { return this->CreateVerticalContainer(XMLNode,Parent); }
247 
249  { delete static_cast<VerticalContainer*>( ToBeDestroyed ); }
250  }//UI
251 }//Mezzanine
252 
253 #endif