1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2
3 #include "GuiElement.h"
4
5 #include "Rendering/GL/myGL.h"
6
7 namespace agui
8 {
9
10 int GuiElement::screensize[2];
11 int GuiElement::screenoffset[2];
12
GuiElement(GuiElement * _parent)13 GuiElement::GuiElement(GuiElement* _parent) : parent(_parent), fixedSize(false), weight(1)
14 {
15 size[0] = size[1] = 0.0f;
16 pos[0] = pos[1] = 0.0f;
17
18 if (parent)
19 parent->AddChild(this);
20 }
21
~GuiElement()22 GuiElement::~GuiElement()
23 {
24 for (ChildList::iterator it = children.begin(); it != children.end(); ++it)
25 {
26 delete *it;
27 }
28 }
29
Draw()30 void GuiElement::Draw()
31 {
32 DrawSelf();
33 for (ChildList::iterator it = children.begin(); it != children.end(); ++it)
34 {
35 (*it)->Draw();
36 }
37 }
38
HandleEvent(const SDL_Event & ev)39 bool GuiElement::HandleEvent(const SDL_Event& ev)
40 {
41 if (HandleEventSelf(ev))
42 return true;
43 for (ChildList::iterator it = children.begin(); it != children.end(); ++it)
44 {
45 if ((*it)->HandleEvent(ev))
46 return true;
47 }
48 return false;
49 }
50
MouseOver(int x,int y) const51 bool GuiElement::MouseOver(int x, int y) const
52 {
53 float mouse[2] = {PixelToGlX(x), PixelToGlY(y)};
54 return (mouse[0] >= pos[0] && mouse[0] <= pos[0]+size[0]) && (mouse[1] >= pos[1] && mouse[1] <= pos[1]+size[1]);
55 }
56
MouseOver(float x,float y) const57 bool GuiElement::MouseOver(float x, float y) const
58 {
59 return (x >= pos[0] && x <= pos[0]+size[0]) && (y >= pos[1] && y <= pos[1]+size[1]);
60 }
61
UpdateDisplayGeo(int x,int y,int xOffset,int yOffset)62 void GuiElement::UpdateDisplayGeo(int x, int y, int xOffset, int yOffset)
63 {
64 screensize[0] = x;
65 screensize[1] = y;
66 screenoffset[0] = xOffset;
67 screenoffset[1] = yOffset;
68 }
69
PixelToGlX(int x)70 float GuiElement::PixelToGlX(int x)
71 {
72 return float(x - screenoffset[0])/float(screensize[0]);
73 }
74
PixelToGlY(int y)75 float GuiElement::PixelToGlY(int y)
76 {
77 return 1.0f - float(y - screenoffset[1])/float(screensize[1]);
78 }
79
GlToPixelX(float x)80 float GuiElement::GlToPixelX(float x)
81 {
82 return x*float(screensize[0]) + float(screenoffset[0]);
83 }
84
GlToPixelY(float y)85 float GuiElement::GlToPixelY(float y)
86 {
87 return y*float(screensize[1]) + float(screenoffset[1]);
88 }
89
AddChild(GuiElement * elem)90 void GuiElement::AddChild(GuiElement* elem)
91 {
92 children.push_back(elem);
93 elem->SetPos(pos[0], pos[1]);
94 elem->SetSize(size[0], size[1]);
95 GeometryChange();
96 }
97
SetPos(float x,float y)98 void GuiElement::SetPos(float x, float y)
99 {
100 pos[0] = x;
101 pos[1] = y;
102 GeometryChange();
103 }
104
SetSize(float x,float y,bool fixed)105 void GuiElement::SetSize(float x, float y, bool fixed)
106 {
107 size[0] = x;
108 size[1] = y;
109 fixedSize = fixed;
110 GeometryChange();
111 }
112
GeometryChange()113 void GuiElement::GeometryChange()
114 {
115 GeometryChangeSelf();
116 for (ChildList::iterator it = children.begin(); it != children.end(); ++it)
117 {
118 (*it)->GeometryChange();
119 }
120 }
121
DefaultOpacity() const122 float GuiElement::DefaultOpacity() const
123 {
124 return 0.8f;
125 }
126
Move(float x,float y)127 void GuiElement::Move(float x, float y)
128 {
129 pos[0] += x;
130 pos[1] += y;
131 for (ChildList::iterator it = children.begin(); it != children.end(); ++it)
132 {
133 (*it)->Move(x, y);
134 }
135 }
136
DrawBox(int how)137 void GuiElement::DrawBox(int how)
138 {
139 glBegin(how);
140 glVertex2f(pos[0], pos[1]);
141 glVertex2f(pos[0], pos[1]+size[1]);
142 glVertex2f(pos[0]+size[0], pos[1]+size[1]);
143 glVertex2f(pos[0]+size[0], pos[1]);
144 glEnd();
145 }
146
147 }
148