23 #include <HUDGraphic.h> 26 #include "InsideRoundedSquareChecker.h" 30 void Layout::addChild(
Layout* child){
31 children.push_back(child);
36 map<string,IHudDrawable*> list;
37 getGLSquares(layoutXPos,layoutYPos,layoutWidth,layoutHeight,&list);
41 void Layout::getGLSquares(
float layoutXPos,
float layoutYPos,
float layoutWidth,
float layoutHeight,
42 map<string,IHudDrawable*> *list) {
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]);
52 renderedBackground =
nullptr;
57 void Layout::invokeListeners(
int x,
int y, ListenerType listenerType) {
59 invokeListenersInternal(x,y,listenerType,
true);
63 void Layout::invokeListenersInternal(
int x,
int y, ListenerType listenerType,
bool mayBeHit) {
65 bool *wasActive = listenerType == Layout::ON_CLICK_LISTENER ? &mouseWasDown : &hoveredBackground;
66 if(insideChecker !=
nullptr && mayBeHit && insideChecker->isInside(x, y)){
68 callListeners(x,y,listenerType,
true);
70 for (
Layout *child : children) {
71 child->invokeListenersInternal(x, y, listenerType,
true);
74 if(listenerType == Layout::ON_HOVER_LISTENER || insideChecker->isInside(x,y))
75 callListeners(x,y,listenerType,
false);
77 for (
Layout *child : children) {
78 child->invokeListenersInternal(x, y, listenerType,
false);
86 void Layout::clearChildren() {
87 children = vector<Layout*>();
89 hoveredBackground =
false;
97 Layout* Layout::findById(
string id) {
100 for(
auto it : children){
101 Layout* res = it->findById(
id);
109 this->graphic = graphic;
110 if(renderedBackground !=
nullptr){
111 renderedBackground->setGraphic(graphic);
116 string Layout::getNextRandId() {
117 static string prev =
"ganwubcky";
118 char lastChar = prev.at(prev.length()-1);
121 prev.push_back(lastChar+1);
127 void Layout::clearClickListeners() {
128 clickListeners->clear();
131 void Layout::clearHoverListeners() {
132 hoverListeners->clear();
135 Layout* Layout::addClickListener(EventFunction eventFunction) {
136 clickListeners->insert(clickListeners->end(),eventFunction);
140 Layout* Layout::addHoverListener(EventFunction eventFunction) {
141 hoverListeners->insert(hoverListeners->end(),eventFunction);
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;
151 case ON_CLICK_LISTENER:
152 toCall = clickListeners;
155 for (EventFunction call : *toCall) {
156 call(x, y,
this,enteringElseLeaving);
161 if(renderedBackground ==
nullptr)
164 return renderedBackground->getGraphic();
167 void Layout::updateGraphic() {
168 if(renderedBackground !=
nullptr)
169 renderedBackground->updateGraphic();
173 return renderedBackground;
virtual std::map< std::string, IHudDrawable * > getGLSquares(float layoutXPos, float layoutYPos, float layoutWidth, float layoutHeight)
virtual Layout * setLayoutId(std::string id)
virtual Layout * setBackground(HUDGraphic *graphic)