MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
horizontalcontainer.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 _uihorizontalcontainer_cpp
41 #define _uihorizontalcontainer_cpp
42 
43 #include "UI/horizontalcontainer.h"
44 #include "UI/horizontallayoutstrategy.h"
45 
46 #include "UI/horizontalscrollbar.h"
47 #include "UI/spinner.h"
48 
49 namespace Mezzanine
50 {
51  namespace UI
52  {
53  const String HorizontalContainer::TypeName = "HorizontalContainer";
54 
55  ///////////////////////////////////////////////////////////////////////////////
56  // HorizontalContainer Methods
57 
59  LinearContainer(Parent)
60  { this->LayoutStrat = new LayoutStrategy(); }
61 
63  LinearContainer(RendName,Parent)
64  { this->LayoutStrat = new LayoutStrategy(); }
65 
66  HorizontalContainer::HorizontalContainer(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 HorizontalContainer::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.X );
88  Real XTarget = ( this->XProvider->GetCurrentXPage() - 1 ) * ActWorkArea.X;
89  Real CurrXPos = 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() && CurrXPos < XTarget )
95  {
96  this->UpdateInvisibleChild( OldSelfRect, NewSelfRect, (*ChildUpdateIt) );
97  // Increment our 'cursor' position
98  CurrXPos += ( (*ChildUpdateIt)->GetActualSize().X + 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  CurrXPos = ActPadding * 0.5;
111  XTarget = NewSelfRect.Size.X;
112 
113  while( ChildUpdateIt != this->ChildWidgets.end() /*&& CurrXPos < XTarget */ )
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( CurrXPos + ChildSize.X < XTarget ) {
120  // Clamp the perpendicular axis, at least until we get proper expanding widgets
121  if( ChildSize.Y > NewSelfRect.Size.Y ) {
122  ChildSize.Y = NewSelfRect.Size.Y;
123  }
124  ChildSizeCache.push_back( ChildSize );
125  this->VisibleChildren.push_back( (*ChildUpdateIt) );
126  CurrXPos += ( ChildSize.X + ActPadding );
127  TotalLinearSize += ( ChildSize.X + 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: CurrXPos = ActPadding * 0.5;
146  case UI::LA_Center: CurrXPos = ( NewSelfRect.Position.X + ( NewSelfRect.Size.X * 0.5 ) ) - ( TotalLinearSize * 0.5 );
147  case UI::LA_BottomRight: CurrXPos = ( NewSelfRect.Position.X + NewSelfRect.Size.X ) - ( TotalLinearSize + ActPadding * 0.5);
148  }
149 
150  for( Whole X = 0 ; X < this->VisibleChildren.size() ; ++X )
151  {
152  // Setup child transform data
153  const Rect OldChildRect = this->VisibleChildren.at(X)->GetRect();
154  Rect NewChildRect;
155  // Set the Size
156  NewChildRect.Size = ChildSizeCache.at(X);
157  // Set the Position
158  NewChildRect.Position.X = CurrXPos;
159  NewChildRect.Position.Y = this->LayoutStrat->HandleChildVerticalPositioning( OldSelfRect, NewSelfRect, NewChildRect.Size, this->VisibleChildren.at(X) );
160  // Perform the update
161  this->VisibleChildren.at(X)->UpdateDimensions(OldChildRect,NewChildRect);
162  // Increment the cursor
163  CurrXPos += ( NewChildRect.Size.X + ActPadding );
164  }
165  ChildSizeCache.clear();
166  }
167  }
168 
169  ///////////////////////////////////////////////////////////////////////////////
170  // Utility
171 
174 
176  { this->SetXProvider(XProv); }
177 
179  {
180  if( this->XProvider != NULL && this->XProvider != XProv ) {
181  this->UnbindProvider(this->XProvider);
182  }
183  this->XProvider = XProv;
184  this->XProvider->_SetContainer(this);
186  }
187 
189  { /* Do Nothing */ }
190 
192  { this->SetXProvider(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  // HorizontalContainerFactory Methods
223 
226 
228  { return new HorizontalContainer(RendName,Parent); }
229 
231  { return new HorizontalContainer(RendName,RendRect,Parent); }
232 
234  { return new HorizontalContainer(XMLNode,Parent); }
235 
237  { return new HorizontalContainer(Parent); }
238 
240  { return this->CreateHorizontalContainer(RendName,Parent); }
241 
242  Widget* HorizontalContainerFactory::CreateWidget(const String& RendName, const UnifiedRect& RendRect, const NameValuePairMap& Params, Screen* Parent)
243  { return this->CreateHorizontalContainer(RendName,RendRect,Parent); }
244 
246  { return this->CreateHorizontalContainer(XMLNode,Parent); }
247 
249  { delete static_cast<HorizontalContainer*>( ToBeDestroyed ); }
250  }//UI
251 }//Mezzanine
252 
253 #endif