xoreos  0.0.5
guiquad.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/common/util.h"
26 #include "src/common/ustring.h"
27 
32 
33 namespace Graphics {
34 
35 namespace Aurora {
36 
38  float x1 , float y1 , float x2 , float y2,
39  float tX1, float tY1, float tX2, float tY2) :
40  GUIElement(GUIElement::kGUIElementFront),
41  _r(1.0f), _g(1.0f), _b(1.0f), _a(1.0f), _angle(0.0f),
42  _x1 (x1) , _y1 (y1) , _x2 (x2) , _y2 (y2) ,
43  _tX1(tX1), _tY1(tY1), _tX2(tX2), _tY2(tY2),
44  _scissorX(0), _scissorY(0), _scissorWidth(0), _scissorHeight(0),
45  _xor(false), _scissor(false), _additiveBlending(false) {
46 
47  try {
48 
49  if (!texture.empty())
50  _texture = TextureMan.get(texture);
51 
52  } catch (...) {
53  _texture.clear();
54 
55  _r = _g = _b = _a = 0.0f;
56  }
57 
58  if (!_texture.empty()) {
60  }
61 
62  _distance = -FLT_MAX;
63 }
64 
66  float x1 , float y1 , float x2 , float y2,
67  float tX1, float tY1, float tX2, float tY2) :
68  GUIElement(type),
69  _r(1.0f), _g(1.0f), _b(1.0f), _a(1.0f), _angle(0.0f),
70  _x1 (x1) , _y1 (y1) , _x2 (x2) , _y2 (y2) ,
71  _tX1(tX1), _tY1(tY1), _tX2(tX2), _tY2(tY2),
72  _scissorX(0), _scissorY(0), _scissorWidth(0), _scissorHeight(0),
73  _xor(false), _scissor(false), _additiveBlending(false) {
74 
75  try {
76 
77  if (!texture.empty())
78  _texture = TextureMan.get(texture);
79 
80  } catch (...) {
81  _texture.clear();
82 
83  _r = _g = _b = _a = 0.0f;
84  }
85 
86  if (!_texture.empty()) {
88  }
89 
90  _distance = -FLT_MAX;
91 }
92 
94  float x1 , float y1 , float x2 , float y2,
95  float tX1, float tY1, float tX2, float tY2) :
96  GUIElement(GUIElement::kGUIElementFront),
97  _texture(texture), _r(1.0f), _g(1.0f), _b(1.0f), _a(1.0f), _angle(0.0f),
98  _x1 (x1) , _y1 (y1) , _x2 (x2) , _y2 (y2) ,
99  _tX1(tX1), _tY1(tY1), _tX2(tX2), _tY2(tY2),
100  _scissorX(0), _scissorY(0), _scissorWidth(0), _scissorHeight(0),
101  _xor(false), _scissor(false), _additiveBlending(false) {
102 
103  _distance = -FLT_MAX;
104 }
105 
107  hide();
108 }
109 
110 void GUIQuad::getPosition(float &x, float &y, float &z) const {
111  x = MIN(_x1, _x2);
112  y = MIN(_y1, _y2);
113  z = _distance;
114 }
115 
116 void GUIQuad::setPosition(float x, float y, float z) {
118 
119  _x2 = _x2 - _x1 + x;
120  _y2 = _y2 - _y1 + y;
121 
122  _x1 = x;
123  _y1 = y;
124 
125  _distance = z;
126  resort();
127 
129 }
130 
131 void GUIQuad::setRotation(float angle) {
132  _angle = angle;
133 }
134 
135 void GUIQuad::getColor(float& r, float& g, float& b, float& a) const {
136  r = _r;
137  g = _g;
138  b = _b;
139  a = _a;
140 }
141 
142 
143 void GUIQuad::setColor(float r, float g, float b, float a) {
145 
146  _r = r;
147  _g = g;
148  _b = b;
149  _a = a;
150 
152 }
153 
154 void GUIQuad::setTexture(const Common::UString &texture) {
156 
157  try {
158 
159  if (texture.empty())
160  _texture.clear();
161  else
162  _texture = TextureMan.get(texture);
163 
164  } catch (...) {
165  _texture.clear();
166 
167  _r = _g = _b = _a = 0.0f;
168  }
169 
171 }
172 
175 
176  _texture = texture;
177 
179 }
180 
181 void GUIQuad::setScissor(int x, int y, int width, int height) {
182  _scissorX = x;
183  _scissorY = y;
184  _scissorWidth = width;
185  _scissorHeight = height;
186 }
187 
188 float GUIQuad::getWidth() const {
189  return ABS(_x2 - _x1);
190 }
191 
192 float GUIQuad::getHeight() const {
193  return ABS(_y2 - _y1);
194 }
195 
196 void GUIQuad::setWidth(float w) {
198 
199  _x2 = _x1 + w;
200 
202 }
203 
204 void GUIQuad::setHeight(float h) {
206 
207  _y2 = _y1 + h;
208 
210 }
211 
212 void GUIQuad::setXOR(bool enabled) {
214 
215  _xor = enabled;
216 
218 }
219 
220 void GUIQuad::setScissor(bool enabled) {
222 
223  _scissor = enabled;
224 
226 }
227 
228 bool GUIQuad::isIn(float x, float y) const {
229  if ((x < _x1) || (x > _x2))
230  return false;
231  if ((y < _y1) || (y > _y2))
232  return false;
233 
234  return true;
235 }
236 
238 }
239 
241  bool isTransparent = (_a < 1.0f) || (!_texture.empty() && _texture.getTexture().hasAlpha());
242  if (((pass == kRenderPassOpaque) && isTransparent) ||
243  ((pass == kRenderPassTransparent) && !isTransparent))
244  return;
245 
246  TextureMan.set(_texture);
247 
248  glColor4f(_r, _g, _b, _a);
249 
250  if (_additiveBlending) {
251  glPushAttrib(GL_COLOR_BUFFER_BIT);
252  glBlendFunc(GL_ONE, GL_ONE);
253  }
254 
255  if (_xor) {
256  glEnable(GL_COLOR_LOGIC_OP);
257  glLogicOp(GL_XOR);
258  }
259 
260  if (_scissor) {
261  glEnable(GL_SCISSOR_TEST);
262  GLint viewport[4];
263  glGetIntegerv(GL_VIEWPORT, viewport);
264  glScissor(viewport[2]/2 + _x1 + _scissorX,
265  viewport[3]/2 + _y1 + _scissorY,
268  }
269 
270  if (_angle != 0.0f) {
271  glTranslatef(_x1 + (_x2 - _x1) / 2.0f, _y1 + (_y2 - _y1) / 2.0f, 0);
272  glRotatef(_angle, 0.0f, 0.0f, 1.0f);
273  glTranslatef(-_x1 - (_x2 - _x1) / 2.0f, -_y1 - (_y2 - _y1) / 2.0f, 0);
274  }
275 
276  glBegin(GL_QUADS);
277  glTexCoord2f(_tX1, _tY1);
278  glVertex2f(_x1, _y1);
279  glTexCoord2f(_tX2, _tY1);
280  glVertex2f(_x2, _y1);
281  glTexCoord2f(_tX2, _tY2);
282  glVertex2f(_x2, _y2);
283  glTexCoord2f(_tX1, _tY2);
284  glVertex2f(_x1, _y2);
285  glEnd();
286 
287  if (_scissor)
288  glDisable(GL_SCISSOR_TEST);
289 
290  if (_xor)
291  glDisable(GL_COLOR_LOGIC_OP);
292 
293  if (_additiveBlending) {
294  glPopAttrib();
295  }
296 
297  glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
298 }
299 
300 } // End of namespace Aurora
301 
302 } // End of namespace Graphics
void setScissor(int x, int y, int width, int height)
Set the scissor test parameters.
Definition: guiquad.cpp:181
T ABS(T x)
Definition: util.h:69
void setHeight(float h)
Set the quad&#39;s height.
Definition: guiquad.cpp:204
bool hasAlpha() const
Definition: texture.cpp:84
Only render transparent parts.
Definition: types.h:99
const TXI & getTXI() const
Return the TXI.
Definition: texture.cpp:96
virtual void hide()
Hide the object.
Definition: renderable.cpp:123
A class holding an UTF-8 string.
Definition: ustring.h:48
#define TextureMan
Shortcut for accessing the texture manager.
Definition: textureman.h:127
double _distance
The distance of the object from the viewer.
Definition: renderable.h:101
void setWidth(float w)
Set the quad&#39;s width.
Definition: guiquad.cpp:196
The Aurora texture manager.
float getWidth() const
Return the quad&#39;s width.
Definition: guiquad.cpp:188
void setColor(float r, float g, float b, float a)
Set the current color of the quad.
Definition: guiquad.cpp:143
Blending blending
Definition: txi.h:60
void setXOR(bool enabled)
Enable/Disable XOR mode.
Definition: guiquad.cpp:212
void getColor(float &r, float &g, float &b, float &a) const
Get the current color of the quad.
Definition: guiquad.cpp:135
TextureHandle _texture
Definition: guiquad.h:93
RenderPass
Definition: types.h:97
Utility templates and functions.
void setRotation(float angle)
Set the current rotation of the quad in degrees.
Definition: guiquad.cpp:131
T MIN(T a, T b)
Definition: util.h:70
GUIQuad(const Common::UString &texture, float x1, float y1, float x2, float y2, float tX1=0.0f, float tY1=0.0f, float tX2=1.0f, float tY2=1.0f)
Definition: guiquad.cpp:37
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
Texture information.
Unicode string handling.
bool isIn(float x, float y) const
Is the point within the quad?
Definition: guiquad.cpp:228
An element of the GUI.
Definition: guielement.h:33
void getPosition(float &x, float &y, float &z) const
Get the current position of the quad.
Definition: guiquad.cpp:110
A texture as used in the Aurora engines.
void setTexture(const Common::UString &texture)
Set the current texture of the quad.
Definition: guiquad.cpp:154
void setPosition(float x, float y, float z=-FLT_MAX)
Set the current position of the quad.
Definition: guiquad.cpp:116
const Features & getFeatures() const
Definition: txi.cpp:283
#define pass
Definition: fft.cpp:257
void render(RenderPass pass)
Render the object.
Definition: guiquad.cpp:240
A textured quad for a GUI element.
A handle to a texture.
Definition: texturehandle.h:51
float getHeight() const
Return the quad&#39;s height.
Definition: guiquad.cpp:192
#define FLT_MAX
Definition: maths.h:47
Only render opaque parts.
Definition: types.h:98
void calculateDistance()
Calculate the object&#39;s distance.
Definition: guiquad.cpp:237