Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
element.hpp
Go to the documentation of this file.
1 
23 #ifndef __GUIELEMENT__
24 #define __GUIELEMENT__
25 
27 #include <tucano/utils/imageIO.hpp>
28 #include <tucano/texture.hpp>
29 
30 namespace Tucano
31 {
32 
33 namespace GUI
34 {
35 
36 enum ElementType : signed int
37 {
45 };
46 
52 class Element {
53 
54 protected:
55 
58 
60  int num_params = 0;
61 
63  Eigen::Affine3f model_matrix;
64 
66  Eigen::Vector2i dimensions;
67 
69  Eigen::Vector2i position;
70 
73 
76 
79 
82 
84  bool has_alt_texture = 0;
85 
87  bool has_alt_texture2 = 0;
88 
90  Eigen::Vector4f color = Eigen::Vector4f (1.0, 1.0, 1.0, 1.0);
91 
92 
93  function< void() > callback_noparam;
94  function< void(int) > callback_1i;
95  function< void(float) > callback_1f;
96 
98  bool visible = true;
99 
101  bool hover = false;
102 
103 public:
104 
108  Element (void)
109  {
110  model_matrix = Eigen::Affine3f::Identity();
111  callback_noparam = NULL;
112  callback_1i = NULL;
113  callback_1f = NULL;
114  }
115 
116 
121  Eigen::Vector2i getDimensions (void)
122  {
123  return dimensions;
124  }
125 
130  Eigen::Vector2i getPosition (void)
131  {
132  return position;
133  }
134 
139  void setDimensions (const Eigen::Vector2i& dim)
140  {
141  dimensions = dim;
142  setModelMatrix();
143  }
144 
150  void setDimensions (int w, int h)
151  {
152  setDimensions(Eigen::Vector2i(w,h));
153  }
154 
160  virtual void setDimensionsFromHeight (int h)
161  {
162  if (texture.getWidth() != 0)
163  {
164  dimensions << texture.getWidth() * h/(float)texture.getHeight(), h;
165  setModelMatrix();
166  }
167  }
168 
173  virtual void setTexture (string file)
174  {
175  Tucano::ImageImporter::loadImage(file, &texture);
176  texture.setTexParameters( GL_REPEAT, GL_REPEAT, GL_LINEAR, GL_LINEAR );
177  }
178 
183  virtual void setAltTexture (string file)
184  {
185  Tucano::ImageImporter::loadImage(file, &texture_alt);
186  texture_alt.setTexParameters( GL_REPEAT, GL_REPEAT, GL_LINEAR, GL_LINEAR );
187  has_alt_texture = true;
188  color = Eigen::Vector4f (1.0, 1.0, 1.0, 1.0); // wont modulate color, set it to white
189  }
190 
191 
195  virtual void setModelMatrix (void)
196  {
197  model_matrix = Eigen::Affine3f::Identity();
198 
199  // translates to position
200  model_matrix.translate (Eigen::Vector3f ((float)position[0], (float)position[1], 0.0) );
201  // scales to fit required dimensions, invert y axis to get appropriate texcoords
202  model_matrix.scale (Eigen::Vector3f ((float)dimensions[0], (float)dimensions[1], 1.0));
203  // sets the quad to range (0, 1) instead of (-0.5, 0.5)
204  model_matrix.translate (Eigen::Vector3f (0.5, 0.5, 0.0) );
205  }
206 
207 
208 
213  void setPosition (const Eigen::Vector2i& pos)
214  {
215  position = pos;
216  setModelMatrix();
217  }
218 
224  void setPosition (int x, int y)
225  {
226  setPosition (Eigen::Vector2i(x,y));
227  }
233  bool isInside (int x, int y)
234  {
235  return isInside (Eigen::Vector2i(x,y));
236  }
237 
242  virtual bool isInside (const Eigen::Vector2i& pos)
243  {
244  if (pos[0] >= position[0] && pos[0] <= position[0] + dimensions[0] &&
245  pos[1] >= position[1] && pos[1] <= position[1] + dimensions[1])
246  return true;
247  return false;
248  }
249 
253  virtual void release (void) {}
254 
260  virtual void cursorMove (int x, int y) {}
261 
265  virtual int getType (void)
266  {
267  return element_type;
268  }
269 
270  virtual void render (Camera &camera_2d, Shader &shader)
271  {
272  if (!visible)
273  return;
274 
275  shader.bind();
276 
277  shader.setUniform("modelMatrix", model_matrix);
278  shader.setUniform("viewMatrix", camera_2d.getViewMatrix());
279  shader.setUniform("projectionMatrix", camera_2d.getProjectionMatrix());
280  shader.setUniform("in_Color", color);
281  shader.setUniform("shapetex", texture.bind());
282 
283  quad.setAttributeLocation(shader);
284 
285  quad.bindBuffers();
286  quad.renderElements();
287  quad.unbindBuffers();
288 
289  shader.unbind();
290  texture.unbind();
291  }
292 
293 
294 
295  virtual void setCallback (function<void()> f)
296  {
297  callback_noparam = f;
298  }
299 
303  virtual void setCallback1i (function< void(int) > f)
304  {
305  callback_1i = f;
306  }
307 
311  virtual void setCallback1f (function< void(float) > f)
312  {
313  callback_1f = f;
314  }
315 
319  virtual void callback (void)
320  {
321  callback();
322  }
323 
327  virtual void callback (int p)
328  {
329  callback_1i(p);
330  }
331 
335  virtual void callback (float p)
336  {
337  callback_1f(p);
338  }
339 
343  void show(void) { visible = true; }
344 
348  void hide(void) { visible = false; }
349 
353  void toggleDisplay(void) { visible = !visible;}
354 
359  bool isVisible (void) { return visible; }
360 
361 };
362 }
363 }
364 #endif
Eigen::Vector2i dimensions
Dimensions in pixels.
Definition: element.hpp:66
virtual void cursorMove(int x, int y)
Set behavior when mouse is released after clicking this element.
Definition: element.hpp:260
Eigen::Vector2i position
Position of top-left corner in pixels.
Definition: element.hpp:69
Base class for all GUI elements (buttons, sliders ...)
Definition: element.hpp:52
Definition: element.hpp:39
void getViewMatrix(GLdouble *matrix)
Return the modelview matrix as a GLdouble array.
Definition: camera.hpp:126
A simple unitary quad.
Definition: quad.hpp:66
virtual void setModelMatrix(void)
Sets the element model matrix.
Definition: element.hpp:195
Tucano::Texture texture
Element texture.
Definition: element.hpp:75
Tucano::Texture texture_alt2
2nd Alternative texture (for example, hover on)
Definition: element.hpp:81
ElementType
Definition: element.hpp:36
void getProjectionMatrix(GLdouble *matrix)
Return the projection matrix as a GLdouble array.
Definition: camera.hpp:142
virtual bool isInside(const Eigen::Vector2i &pos)
Returns if a point is inside the element controller.
Definition: element.hpp:242
bool isVisible(void)
Return whether element is visible or not.
Definition: element.hpp:359
Definition: bufferobject.hpp:34
static bool loadImage(string filename, Tucano::Texture *tex) __attribute__((unused))
Definition: imageIO.hpp:45
virtual void callback(int p)
Callback with 1 integer.
Definition: element.hpp:327
virtual void setCallback(function< void()> f)
Definition: element.hpp:295
void hide(void)
Hides element.
Definition: element.hpp:348
void setDimensions(const Eigen::Vector2i &dim)
Sets the button dimensions.
Definition: element.hpp:139
void bind(int texture_unit)
Binds the texture to a given unit. Note that if there is another texture already binded to this unit...
Definition: texture.hpp:285
void setTexParameters(GLenum wraps=GL_CLAMP, GLenum wrapt=GL_CLAMP, GLenum magfilter=GL_NEAREST, GLenum minfilter=GL_NEAREST)
Deletes the texture.
Definition: texture.hpp:221
function< void(float) > callback_1f
Definition: element.hpp:95
A Shader object represents one GLSL program.
Definition: shader.hpp:45
int getWidth(void)
Returns the texture width.
Definition: texture.hpp:102
void setDimensions(int w, int h)
Sets the button dimensions.
Definition: element.hpp:150
bool hover
Flag to enable/disable hover on functionality.
Definition: element.hpp:101
bool visible
Flag to show/hide element.
Definition: element.hpp:98
virtual void callback(void)
Callback with no parameters.
Definition: element.hpp:319
Definition: element.hpp:44
Definition: element.hpp:40
bool has_alt_texture2
Flag to define if element has second alt texture.
Definition: element.hpp:87
Definition: element.hpp:43
int num_params
Number of parameters for the callback.
Definition: element.hpp:60
Eigen::Vector2i getPosition(void)
Returns coordinates of elements top left corner.
Definition: element.hpp:130
void unbind(void)
Disables the shader program.
Definition: shader.hpp:1184
virtual void release(void)
Set behavior when mouse is released after clicking this element.
Definition: element.hpp:253
Eigen::Affine3f model_matrix
Model Matrix for placing element.
Definition: element.hpp:63
function< void(int) > callback_1i
Definition: element.hpp:94
bool has_alt_texture
Flag to define if element has alt texture.
Definition: element.hpp:84
virtual void render(Camera &camera_2d, Shader &shader)
Definition: element.hpp:270
virtual void setDimensionsFromHeight(int h)
Sets the width proportinal to given height Uses texture dimensions as parameter, it must be already s...
Definition: element.hpp:160
function< void() > callback_noparam
Definition: element.hpp:93
Element(void)
Default constructor.
Definition: element.hpp:108
Eigen::Vector4f color
Color.
Definition: element.hpp:90
Definition: element.hpp:42
void setPosition(int x, int y)
Sets the button top left position.
Definition: element.hpp:224
int getHeight(void)
Returns the texture height.
Definition: texture.hpp:111
virtual void unbindBuffers(void)
Unbinds all buffers.
Definition: mesh.hpp:702
virtual void setCallback1f(function< void(float) > f)
Sets the callback from an method.
Definition: element.hpp:311
void show(void)
Shows element.
Definition: element.hpp:343
virtual void setAltTexture(string file)
Loads the button texture file.
Definition: element.hpp:183
virtual void setTexture(string file)
Loads the button texture file.
Definition: element.hpp:173
bool isInside(int x, int y)
Overloads the isInside method. Queries if a point is inside the button.
Definition: element.hpp:233
An OpenGL texture. It can be a simple texture or an FBO texture.
Definition: texture.hpp:41
Tucano::Texture texture_alt
Alternative texture (for example, on/off buttons)
Definition: element.hpp:78
int element_type
Type of element.
Definition: element.hpp:57
void bind(void)
Enables the shader program for usage.
Definition: shader.hpp:1176
Definition: element.hpp:38
virtual void renderElements(void)
Call the draw method for rendering triangles. This method requires that a index buffer has been creat...
Definition: mesh.hpp:726
virtual int getType(void)
Returns the element type.
Definition: element.hpp:265
virtual void bindBuffers(void)
Binds all buffers.
Definition: mesh.hpp:677
void setPosition(const Eigen::Vector2i &pos)
Sets the button top left position.
Definition: element.hpp:213
Defines an abstract camera with a projection and view matrices.
Definition: camera.hpp:37
void setAttributeLocation(Shader *shader)
Automatically sets the attribute locations for a given Shader.
Definition: mesh.hpp:541
void toggleDisplay(void)
Toggles show/hide flag.
Definition: element.hpp:353
virtual void setCallback1i(function< void(int) > f)
Sets the callback from an method.
Definition: element.hpp:303
Shapes::Quad quad
Quad to hold texture.
Definition: element.hpp:72
Definition: element.hpp:41
virtual void callback(float p)
Callback with 1 integer.
Definition: element.hpp:335
void unbind(void)
Unbinds this texture and frees the texture unit.
Definition: texture.hpp:367
Eigen::Vector2i getDimensions(void)
Returns the element&#39;s dimensions.
Definition: element.hpp:121
void setUniform(GLint location, GLint a, GLint b, GLint c, GLint d)
Sets an uniform integer 4D vector (ivec4) given a location and the vector values. ...
Definition: shader.hpp:1258