Bubba-3D  0.9.0
Awesome game engine!
ListLayout.cpp
1 /*
2  * This file is part of Bubba-3D.
3  *
4  * Bubba-3D is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser 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  * Bubba-3D 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 Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with Bubba-3D. If not, see http://www.gnu.org/licenses/.
16  */
17 //
18 // Created by simon on 2016-02-14.
19 //
20 
21 #include <ListLayout.h>
22 #include <Dimension.h>
23 #include <GLSquare.h>
24 #include <vector>
25 #include <map>
26 #include <string>
27 #include <stdexcept>
28 #include <IHudDrawable.h>
29 
30 using namespace std;
31 
32 ListLayout::ListLayout(Orientation orientation, Dimension width, Dimension height) : width(width), height(height){
33  this->orientation = orientation;
34 }
35 
36 void ListLayout::getGLSquares(float layoutXPos,float layoutYPos, float layoutWidth,
37  float layoutHeight, map<string,IHudDrawable*>* list){
38 
39  Layout::getGLSquares(layoutXPos,layoutYPos,layoutWidth,layoutHeight,list);
40 
41  int weights = 0;
42  float unitsLeft = orientation == HORIZONTAL ? layoutWidth : layoutHeight;
43  for(Layout* child : children) {
44  Dimension dimension = orientation == HORIZONTAL ? child->getWidth() : child->getHeight();
45  if (dimension.getUnit() == Dimension::FILL)
46  weights += dimension.getWeight();
47  else if(dimension.getUnit() == Dimension::PERCENTAGE || dimension.getUnit() == Dimension::PIXELS)
48  unitsLeft -= dimension.getSize(orientation == HORIZONTAL ? layoutWidth : layoutHeight);
49  }
50 
51  float xpos = layoutXPos, ypos = layoutYPos, width,height, calculatedPos;
52  for(Layout* child : children){
53  Dimension dimension = orientation == HORIZONTAL ? child->getWidth() : child->getHeight();
54  if (dimension.getUnit() == Dimension::FILL)
55  calculatedPos = unitsLeft/(float)weights*(float)dimension.getWeight();
56  else
57  calculatedPos = dimension.getSize(orientation == HORIZONTAL ? layoutWidth : layoutHeight);
58 
59  if(orientation == HORIZONTAL){
60  width = calculatedPos;
61  Dimension heightDim = child->getHeight();
62  height = heightDim.getUnit() == Dimension::FILL ? layoutHeight : heightDim.getSize(layoutHeight);
63  }else{
64  Dimension widthDim = child->getWidth();
65  width = widthDim.getUnit() == Dimension::FILL ? layoutWidth : widthDim.getSize(layoutHeight);
66  height = calculatedPos;
67  }
68  child->getGLSquares(xpos,ypos,width,height,list);
69  if(orientation == HORIZONTAL){
70  xpos += width;
71  }else{
72  ypos += height;
73  }
74  }
75 }
76 
77 Dimension ListLayout::getWidth() {
78  if(width.getUnit() != Dimension::WRAP)
79  return width;
80  else
81  return Dimension::fromPixels(wrapSize(HORIZONTAL));
82 }
83 
84 Dimension ListLayout::getHeight() {
85  if(height.getUnit() != Dimension::WRAP)
86  return height;
87  else
88  return Dimension::fromPixels(wrapSize(VERTICAL));
89 }
90 
91 unsigned int ListLayout::wrapSize(Orientation parentOrientation) {
92  unsigned int wrapSize = 0;
93  for(Layout* child : children){
94  Dimension childDim = parentOrientation == HORIZONTAL ? child->getWidth() : child->getHeight();
95  // We know that the child dimension cannot be percentage or fill (checkChildCompatibility)
96  // and as getWidth() and getHeight() can never return a wrapping dimension, it has to be pixels
97  if(parentOrientation == orientation)
98  wrapSize += childDim.getPixels();
99  else
100  wrapSize = max(wrapSize,(unsigned int)childDim.getPixels());
101  }
102  return wrapSize;
103 }
104 
106  checkChildCompatibility(child);
107  Layout::addChild(child);
108 }
109 
110 void ListLayout::checkChildCompatibility(Layout *child) {
111  Dimension thisDim = orientation == HORIZONTAL ? width : height;
112  Dimension childDim = orientation == HORIZONTAL ? child->getWidth() : child->getHeight();
113  if(thisDim.getUnit() == Dimension::WRAP &&
114  !(childDim.getUnit() == Dimension::WRAP || childDim.getUnit() == Dimension::PIXELS)){
115  string err = string("The child of a wrapping layout can only use Dimension::WRAP or Dimension::PIXELS ") +
116  "in the same orientation as the parent as these aren't dependent on the parent size.";
117  throw invalid_argument(err.c_str());
118  }
119 
120 }
virtual std::map< std::string, IHudDrawable * > getGLSquares(float layoutXPos, float layoutYPos, float layoutWidth, float layoutHeight)
Definition: Layout.cpp:34
virtual void addChild(Layout *child)
Definition: ListLayout.cpp:105
ListLayout(Orientation orientation, Dimension width, Dimension height)
Definition: ListLayout.cpp:32
int getWeight()
Definition: Dimension.cpp:70
float getSize(float room)
Definition: Dimension.cpp:23
static Dimension fromPixels(int pixels)
Definition: Dimension.cpp:39
DimensionUnit getUnit()
Definition: Dimension.cpp:66
virtual void getGLSquares(float layoutXPos, float layoutYPos, float layoutWidth, float layoutHeight, std::map< std::string, IHudDrawable * > *map)
Definition: ListLayout.cpp:36
int getPixels()
Definition: Dimension.cpp:74
Definition: Layout.h:33