Spinning Topp Logo BlackTopp Studios
inc
horizontallayoutstrategy.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 _uihorizontallayoutstrategy_cpp
41 #define _uihorizontallayoutstrategy_cpp
42 
43 #include "UI/horizontallayoutstrategy.h"
44 #include "UI/widget.h"
45 #include "MathTools/mathtools.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 HorizontalLayoutStrategy::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 PrevRightPos = NewSelfRect.Position.X;
71  Real NextLeftPos = 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().CanExpandHorizontally() )
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  if( !NoExChild->GetManualTransformUpdates() ) {
89  const Rect OldChildRect = NoExChild->GetRect();
90  Rect NewChildRect;
91 
92  NewChildRect.Size = this->HandleChildSizing(OldSelfRect,NewSelfRect,NoExChild);
93  NewChildRect.Position = this->HandleChildPositioning(OldSelfRect,NewSelfRect,NewChildRect.Size,NoExChild);
94  NoExChild->UpdateDimensions(OldChildRect,NewChildRect);
95  PrevRightPos = NewChildRect.Position.X + NewChildRect.Size.X;
96  }
97 
98  // Increment if it's not the end node and move on
99  if( NextNonExpandingChild != ChildQuads.end() )
100  ++NextNonExpandingChild;
101  ChildIt = NextNonExpandingChild;
102  continue;
103  }else{
104  // Create a variable to cache the bottom position
105  Real FixedRightPos = 0;
106 
107  // We got a range now so lets process the "fixed" child first if it is valid
108  if( NextNonExpandingChild != ChildQuads.end() ) {
109  QuadRenderable* NoExChild = (*NextNonExpandingChild);
110  if( !NoExChild->GetManualTransformUpdates() ) {
111  const Rect OldChildRect = NoExChild->GetRect();
112  Rect NewChildRect;
113 
114  NewChildRect.Size = this->HandleChildSizing(OldSelfRect,NewSelfRect,NoExChild);
115  NewChildRect.Position = this->HandleChildPositioning(OldSelfRect,NewSelfRect,NewChildRect.Size,NoExChild);
116 
117  this->CheckChildAspectRatio(OldChildRect.Size,NewChildRect.Size,NoExChild);
118  this->ClampChildToMinSize(NewSelfRect,NewChildRect.Size,NoExChild);
119  this->ClampChildToMaxSize(NewSelfRect,NewChildRect.Size,NoExChild);
120 
121  NoExChild->UpdateDimensions(OldChildRect,NewChildRect);
122  NextLeftPos = NewChildRect.Position.X;
123  FixedRightPos = NewChildRect.Position.X + NewChildRect.Size.X;
124  }
125  }else{
126  // Just use the right edge of the parent if the child isn't valid
127  NextLeftPos = NewSelfRect.Position.X + NewSelfRect.Size.X;
128  }
129 
130  // Set up the data for the range of expanding children
131  Real XPos = PrevRightPos;
132  Real XSpacePerChild = (NextLeftPos - PrevRightPos) / ExpandingChildCount;
133  // Update the expanding children in the range
134  while( ChildIt != NextNonExpandingChild )
135  {
136  QuadRenderable* ExChild = (*ChildIt);
137  if( !ExChild->GetManualTransformUpdates() ) {
138  const Rect OldChildRect = ExChild->GetRect();
139  Rect NewChildRect;
140 
141  NewChildRect.Size.X = XSpacePerChild;
142  NewChildRect.Size.Y = ( ExChild->GetVerticalSizingRules() == UI::SR_Fill_Available ? NewSelfRect.Size.Y : this->HandleChildVerticalSizing(OldSelfRect,NewSelfRect,XSpacePerChild,ExChild) );
143  NewChildRect.Position.X = XPos;
144  NewChildRect.Position.Y = this->HandleChildVerticalPositioning(OldSelfRect,NewSelfRect,NewChildRect.Size,ExChild);
145 
146  this->CheckChildAspectRatio(OldChildRect.Size,NewChildRect.Size,ExChild);
147  this->ClampChildToMinSize(NewSelfRect,NewChildRect.Size,ExChild);
148  this->ClampChildToMaxSize(NewSelfRect,NewChildRect.Size,ExChild);
149 
150  ExChild->UpdateDimensions(OldChildRect,NewChildRect);
151  }
152  ++ChildIt;
153  }
154  PrevRightPos = FixedRightPos;
155  }
156  // We handled the object at the end of the range, so increment passed it and move on unless it is the end node
157  if( NextNonExpandingChild != ChildQuads.end() )
158  ++NextNonExpandingChild;
159  ChildIt = NextNonExpandingChild;
160  }// for each child
161  }
162  }//UI
163 }//Mezzanine
164 
165 #endif
virtual Rect GetRect() const
Gets this QuadRenderables' Rect.
ChildContainer::const_iterator ConstChildIterator
Const Iterator type for QuadRenderable instances stored by this class.
virtual Vector2 HandleChildSizing(const Rect &OldSelfRect, const Rect &NewSelfRect, QuadRenderable *Child)
Handles the sizing of a child that needs it's dimensions updated.
Vector2 Size
Vector2 representing the width and height of the rect.
Definition: rect.h:71
Unified dimensions are ignored and will instead us all available space.
std::vector< Widget * > ChildContainer
Basic container type for QuadRenderable storage by this class.
virtual void CheckChildAspectRatio(const Vector2 &OldChildSize, Vector2 &NewChildSize, QuadRenderable *Child)
Preserves a child's aspect ratio if it is configured to do so.
This class represents a box shaped area on the screen.
Definition: rect.h:55
This implements the exception hiearchy for Mezzanine.
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
virtual Boole GetManualTransformUpdates() const
Gets whether or not this quad will be automatically updated when parent transforms are updated...
Real Y
Coordinate on the Y vector.
Definition: vector2.h:69
Real X
Coordinate on the X vector.
Definition: vector2.h:67
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
virtual void Layout(const Rect &OldSelfRect, const Rect &NewSelfRect, const ChildContainer &ChildQuads)
Updates the dimensions of a collection of QuadRenderables.
virtual ~HorizontalLayoutStrategy()
Class destructor.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
virtual void ClampChildToMinSize(const Rect &NewSelfRect, Vector2 &NewChildSize, QuadRenderable *Child)
Clamps the updated size to the set minimum size for the child quad.
virtual Vector2 HandleChildPositioning(const Rect &OldSelfRect, const Rect &NewSelfRect, const Vector2 &NewChildSize, QuadRenderable *Child)
Handles the positioning of a child that needs it's dimensions updated.
This represents a nestable quad for an object in a GUI layout.
virtual void UpdateDimensions()
Updates the dimensions of this QuadRenderable based on the transform of it's parent.
Vector2 Position
Vector2 representing the top-left position of the rect.
Definition: rect.h:69
virtual void ClampChildToMaxSize(const Rect &NewSelfRect, Vector2 &NewChildSize, QuadRenderable *Child)
Clamps the updated size to the set maximum size for the child quad.
virtual Real HandleChildVerticalPositioning(const Rect &OldSelfRect, const Rect &NewSelfRect, const Vector2 &NewChildSize, QuadRenderable *Child)
Handles the positioning of a child on the Y axis.
virtual Whole GetVerticalSizingRules() const
Gets the current behavior this quad will follow for the Y axis when it is resized.
virtual Real HandleChildVerticalSizing(const Rect &OldSelfRect, const Rect &NewSelfRect, const Real PrevAxisResult, QuadRenderable *Child)
Handles the sizing of a child on the Y axis.