MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
verticallayoutstrategy.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 _uiverticallayoutstrategy_cpp
41 #define _uiverticallayoutstrategy_cpp
42 
43 #include "UI/verticallayoutstrategy.h"
44 #include "UI/widget.h"
45 #include "mathtool.h"
46 #include "exception.h"
47 
48 #include <algorithm>
49 
50 namespace Mezzanine
51 {
52  namespace UI
53  {
55  { }
56 
58  { }
59 
60  ///////////////////////////////////////////////////////////////////////////////
61  // Utility Methods
62 
63  void VerticalLayoutStrategy::Layout(const Rect& OldSelfRect, const Rect& NewSelfRect, const ChildContainer& ChildQuads)
64  {
65  // See what needs updating
66  //bool QuadPositionUpdated = (OldSelfRect.Position != NewSelfRect.Position);
67  //bool QuadSizeUpdated = (OldSelfRect.Size != NewSelfRect.Size);
68 
69  // Setup our persistent loop data
70  Real PrevBottomPos = 0;
71  Real NextTopPos = 0;
72  ConstChildIterator ChildIt = ChildQuads.begin();
73  while( ChildIt != ChildQuads.end() )
74  {
75  // Scan ahead for the next non-expander
76  UInt32 ExpandingChildCount = 0;
77  ConstChildIterator NextNonExpandingChild = ChildIt;
78  while( NextNonExpandingChild != ChildQuads.end() && !( (*NextNonExpandingChild)->GetSizingPolicy().CanExpandVertically() ) )
79  {
80  ++NextNonExpandingChild;
81  ++ExpandingChildCount;
82  }
83 
84  // if we're on top of a non-expanding child, such as starting with one or have two in a row
85  if( ExpandingChildCount == 0 ) {
86  // Do our updates and move on with the loop
87  QuadRenderable* NoExChild = (*NextNonExpandingChild);
88  const Rect OldChildRect = NoExChild->GetRect();
89  Rect NewChildRect;
90 
91  NewChildRect.Size = this->HandleChildSizing(OldSelfRect,NewSelfRect,NoExChild);
92  NewChildRect.Position = this->HandleChildPositioning(OldSelfRect,NewSelfRect,NewChildRect.Size,NoExChild);
93  NoExChild->UpdateDimensions(OldChildRect,NewChildRect);
94  PrevBottomPos = NewChildRect.Position.Y + NewChildRect.Size.Y;
95 
96  // Increment if it's not the end node and move on
97  if( NextNonExpandingChild != ChildQuads.end() )
98  ++NextNonExpandingChild;
99  ChildIt = NextNonExpandingChild;
100  continue;
101  }else{
102  // We got a range now so lets to the "fixed" child first if it is valid
103  if( NextNonExpandingChild != ChildQuads.end() ) {
104  QuadRenderable* NoExChild = (*NextNonExpandingChild);
105  const Rect OldChildRect = NoExChild->GetRect();
106  Rect NewChildRect;
107 
108  NewChildRect.Size = this->HandleChildSizing(OldSelfRect,NewSelfRect,NoExChild);
109  NewChildRect.Position = this->HandleChildPositioning(OldSelfRect,NewSelfRect,NewChildRect.Size,NoExChild);
110 
111  this->CheckChildAspectRatio(OldChildRect.Size,NewChildRect.Size,NoExChild);
112  this->ClampChildToMinSize(NewSelfRect,NewChildRect.Size,NoExChild);
113  this->ClampChildToMaxSize(NewSelfRect,NewChildRect.Size,NoExChild);
114 
115  NoExChild->UpdateDimensions(OldChildRect,NewChildRect);
116  NextTopPos = NewChildRect.Position.Y;
117  }else{
118  // Just use the bottom edge of the parent if the child isn't valid
119  NextTopPos = NewSelfRect.Position.Y + NewSelfRect.Size.Y;
120  }
121 
122  // Set up the data for the range of expanding children
123  Real YPos = PrevBottomPos + 1; // Is the +1 necessary?
124  Real YSpacePerChild = (NextTopPos - PrevBottomPos) / ExpandingChildCount;
125  // Update the expanding children in the range
126  while( ChildIt != NextNonExpandingChild )
127  {
128  QuadRenderable* ExChild = (*ChildIt);
129  const Rect OldChildRect = ExChild->GetRect();
130  Rect NewChildRect;
131 
132  NewChildRect.Size.X = ( ExChild->GetHorizontalSizingRules() == UI::SR_Fill_Available ? NewSelfRect.Size.Y : this->HandleChildHorizontalSizing(OldSelfRect,NewSelfRect,YSpacePerChild,ExChild) );
133  NewChildRect.Size.Y = YSpacePerChild;
134  NewChildRect.Position.X = this->HandleChildHorizontalPositioning(OldSelfRect,NewSelfRect,NewChildRect.Size,ExChild);
135  NewChildRect.Position.Y = YPos;
136 
137  this->CheckChildAspectRatio(OldChildRect.Size,NewChildRect.Size,ExChild);
138  this->ClampChildToMinSize(NewSelfRect,NewChildRect.Size,ExChild);
139  this->ClampChildToMaxSize(NewSelfRect,NewChildRect.Size,ExChild);
140 
141  ExChild->UpdateDimensions(OldChildRect,NewChildRect);
142  }
143  }
144  // We handled the object at the end of the range, so increment passed it and move on unless it is the end node
145  if( NextNonExpandingChild != ChildQuads.end() )
146  ++NextNonExpandingChild;
147  ChildIt = NextNonExpandingChild;
148  }// for each child
149  }
150  }//UI
151 }//Mezzanine
152 
153 #endif