Bubba-3D  0.9.0
Awesome game engine!
Layout.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-06.
19 //
20 
21 #include <GLSquare.h>
22 #include <map>
23 #include <HUDGraphic.h>
24 #include <iostream>
25 #include "Layout.h"
26 #include "InsideRoundedSquareChecker.h"
27 
28 using namespace std;
29 
30 void Layout::addChild(Layout* child){
31  children.push_back(child);
32 }
33 
34 map<string,IHudDrawable*> Layout::getGLSquares(float layoutXPos, float layoutYPos, float layoutWidth,
35  float layoutHeight) {
36  map<string,IHudDrawable*> list;
37  getGLSquares(layoutXPos,layoutYPos,layoutWidth,layoutHeight,&list);
38  return list;
39 }
40 
41 void Layout::getGLSquares(float layoutXPos, float layoutYPos, float layoutWidth, float layoutHeight,
42  map<string,IHudDrawable*> *list) {
43 
44  id = id == "" ? getNextRandId() : id;
45  if(graphic != nullptr) {
46  GLSquare* renderedSquare = new GLSquare(layoutXPos, layoutYPos, layoutWidth, layoutHeight, graphic);
47  list->insert(list->end(),pair<string,GLSquare*>(id,renderedSquare));
48  renderedBackground = renderedSquare;
49  int* roundedBorders = graphic->getRoundedCorners();
50  insideChecker = new InsideRoundedSquareChecker(layoutXPos,layoutYPos,layoutWidth,layoutHeight,roundedBorders[0],roundedBorders[1],roundedBorders[2],roundedBorders[3]);
51  }else {
52  renderedBackground = nullptr;
53  insideChecker = new InsideSquareChecker(layoutXPos, layoutYPos, layoutWidth, layoutHeight);
54  }
55 }
56 
57 void Layout::invokeListeners(int x, int y, ListenerType listenerType) {
58 
59  invokeListenersInternal(x,y,listenerType,true);
60 
61 }
62 
63 void Layout::invokeListenersInternal(int x, int y, ListenerType listenerType, bool mayBeHit) {
64 
65  bool *wasActive = listenerType == Layout::ON_CLICK_LISTENER ? &mouseWasDown : &hoveredBackground;
66  if(insideChecker != nullptr && mayBeHit && insideChecker->isInside(x, y)){
67  if(!*wasActive)
68  callListeners(x,y,listenerType,true);
69  *wasActive = true;
70  for (Layout *child : children) {
71  child->invokeListenersInternal(x, y, listenerType, true);
72  }
73  }else if(*wasActive){
74  if(listenerType == Layout::ON_HOVER_LISTENER || insideChecker->isInside(x,y))
75  callListeners(x,y,listenerType,false);
76  *wasActive = false;
77  for (Layout *child : children) {
78  child->invokeListenersInternal(x, y, listenerType, false);
79  }
80 
81  }
82 
83 
84 }
85 
86 void Layout::clearChildren() {
87  children = vector<Layout*>();
88  mouseWasDown = false;
89  hoveredBackground = false;
90 }
91 
93  this->id = id;
94  return this;
95 }
96 
97 Layout* Layout::findById(string id) {
98  if(this->id == id)
99  return this;
100  for(auto it : children){
101  Layout* res = it->findById(id);
102  if(res != nullptr)
103  return res;
104  }
105  return nullptr;
106 }
107 
109  this->graphic = graphic;
110  if(renderedBackground != nullptr){
111  renderedBackground->setGraphic(graphic);
112  }
113  return this;
114 }
115 
116 string Layout::getNextRandId() {
117  static string prev = "ganwubcky";
118  char lastChar = prev.at(prev.length()-1);
119  if(lastChar < 'z') {
120  prev.pop_back();
121  prev.push_back(lastChar+1);
122  }else
123  prev.push_back('a');
124  return prev;
125 }
126 
127 void Layout::clearClickListeners() {
128  clickListeners->clear();
129 }
130 
131 void Layout::clearHoverListeners() {
132  hoverListeners->clear();
133 }
134 
135 Layout* Layout::addClickListener(EventFunction eventFunction) {
136  clickListeners->insert(clickListeners->end(),eventFunction);
137  return this;
138 }
139 
140 Layout* Layout::addHoverListener(EventFunction eventFunction) {
141  hoverListeners->insert(hoverListeners->end(),eventFunction);
142  return this;
143 }
144 
145 void Layout::callListeners(int x, int y, ListenerType listenerType, bool enteringElseLeaving) {
146  vector<EventFunction>* toCall;
147  switch (listenerType){
148  case ON_HOVER_LISTENER:
149  toCall = hoverListeners;
150  break;
151  case ON_CLICK_LISTENER:
152  toCall = clickListeners;
153  break;
154  }
155  for (EventFunction call : *toCall) {
156  call(x, y, this,enteringElseLeaving);
157  }
158 }
159 
160 HUDGraphic* Layout::getGraphic() {
161  if(renderedBackground == nullptr)
162  return graphic;
163  else
164  return renderedBackground->getGraphic();
165 }
166 
167 void Layout::updateGraphic() {
168  if(renderedBackground != nullptr)
169  renderedBackground->updateGraphic();
170 }
171 
172 IHudDrawable* Layout::getRenderedBackground() {
173  return renderedBackground;
174 }
virtual std::map< std::string, IHudDrawable * > getGLSquares(float layoutXPos, float layoutYPos, float layoutWidth, float layoutHeight)
Definition: Layout.cpp:34
virtual Layout * setLayoutId(std::string id)
Definition: Layout.cpp:92
virtual Layout * setBackground(HUDGraphic *graphic)
Definition: Layout.cpp:108
Definition: Layout.h:33