Bubba-3D  0.9.0
Awesome game engine!
PositioningLayout.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-21.
19 //
20 
21 #include <stdexcept>
22 #include "PositioningLayout.h"
23 #include <vector>
24 #include <map>
25 #include <IHudDrawable.h>
26 #include <iostream>
27 #include "InsideChecker.h"
28 
29 using namespace std;
30 
31 void PositioningLayout::addChild(Layout *child) {
32  checkChildCompatibility(child);
33  addChild(child,Dimension(),Dimension());
34 }
35 
36 void PositioningLayout::addChild(Layout *child, Dimension x, Dimension y) {
37  children.push_back(new PositionedLayout(child,x,y));
38 }
39 
41  return width;
42 }
43 
45  return height;
46 }
47 
48 PositioningLayout::PositioningLayout(Dimension width, Dimension height) : width(width), height(height) {
49  if(width.getUnit() == Dimension::WRAP || height.getUnit() == Dimension::WRAP)
50  throw new invalid_argument("The height or width of a PositioningLayout cannot be wrap.");
51 }
52 
53 void PositioningLayout::getGLSquares(float layoutXPos, float layoutYPos, float layoutWidth, float layoutHeight,
54  map<string,IHudDrawable*> *list) {
55 
56  Layout::getGLSquares(layoutXPos,layoutYPos,layoutWidth,layoutHeight,list);
57 
58  for(PositionedLayout* child : children){
59  float x = layoutXPos + child->x.getSize(layoutWidth);
60  float y = layoutYPos + child->y.getSize(layoutHeight);
61  float width = child->child->getWidth().getSize(layoutWidth);
62  float height = child->child->getHeight().getSize(layoutHeight);
63  child->child->getGLSquares(x,y,width,height,list);
64  }
65 }
66 
67 void PositioningLayout::checkChildCompatibility(Layout *child) {
68  if(child->getHeight().getUnit() == Dimension::FILL || child->getWidth().getUnit() == Dimension::FILL)
69  throw new invalid_argument("The width or height of a child of a PositioningLayout cannot be FILL.");
70 }
71 
72 Layout* PositioningLayout::findById(string id) {
73  if(this->id == id)
74  return this;
75  for(auto it : children){
76  Layout* res = it->child->findById(id);
77  if(res != nullptr)
78  return res;
79  }
80  return nullptr;
81 }
82 
83 void PositioningLayout::invokeListenersInternal(int x, int y, ListenerType listenerType, bool mayBeHit) {
84  bool *wasActive = listenerType == Layout::ON_CLICK_LISTENER ? &mouseWasDown : &hoveredBackground;
85  if(insideChecker != nullptr && mayBeHit && insideChecker->isInside(x, y)){
86  if(!*wasActive)
87  callListeners(x,y,listenerType,true);
88  *wasActive = true;
89  for (PositionedLayout *child : children) {
90  child->child->invokeListenersInternal(x, y, listenerType, true);
91  }
92  }else if(*wasActive){
93  if(listenerType == Layout::ON_HOVER_LISTENER || insideChecker->isInside(x,y))
94  callListeners(x,y,listenerType,false);
95  *wasActive = false;
96  for (PositionedLayout *child : children) {
97  child->child->invokeListenersInternal(x, y, listenerType, false);
98  }
99 
100  }
101 }
virtual std::map< std::string, IHudDrawable * > getGLSquares(float layoutXPos, float layoutYPos, float layoutWidth, float layoutHeight)
Definition: Layout.cpp:34
float getSize(float room)
Definition: Dimension.cpp:23
virtual void getGLSquares(float layoutXPos, float layoutYPos, float layoutWidth, float layoutHeight, std::map< std::string, IHudDrawable * > *map)
virtual Dimension getHeight()
DimensionUnit getUnit()
Definition: Dimension.cpp:66
Definition: Layout.h:33
virtual Dimension getWidth()