Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
selectbutton.hpp
Go to the documentation of this file.
1 
23 #ifndef __SELECTBUTTON__
24 #define __SELECTBUTTON__
25 
26 #include <tucano/shapes/quad.hpp>
27 #include <tucano/gui/element.hpp>
28 #include <Eigen/Dense>
29 
30 namespace Tucano
31 {
32 
33 namespace GUI
34 {
35 
36 class SelectGroup;
37 
43 class SelectButton : public Element {
44 
45 friend SelectGroup;
46 
47 protected:
48 
50  bool selected = false;
51 
53  function< void() > callback;
54 
55 public:
56 
60  SelectButton (void)
61  {
62  SelectButton(10, 10, 0, 0);
63  }
64 
72  SelectButton (int w, int h, int x, int y)
73  {
74  dimensions << w, h;
75  position << x, y;
78  num_params = 0;
79  color = Eigen::Vector4f (0.1, 0.1, 0.1, 1.0);
80  }
81 
90  SelectButton (int w, int h, int x, int y, string texture)
91  {
92  SelectButton (w, h, x, y);
93  setTexture (texture);
94  }
95 
104  SelectButton (int w, int h, int x, int y, string texture, string alt_texture)
105  {
106  SelectButton (w, h, x, y);
107  setTexture (texture);
108  setAltTexture (alt_texture);
109  }
110 
111  int getType (void)
112  {
114  }
115 
116 
121  void onClick (function<void()> f)
122  {
123  callback = f;
124  }
125 
129  void clicked (void)
130  {
131  toggleSelected();
132  callback();
133  }
134 
139  void setColor (const Eigen::Vector4f& c)
140  {
141  color = c;
142  }
143 
147  void toggleSelected (void)
148  {
149  selected = !selected;
150 
151  // if no alternate texture, modulate the color
152  if (!has_alt_texture)
153  {
154  if (selected)
155  color = Eigen::Vector4f (1.0, 1.0, 1.0, 1.0);
156  else
157  color = Eigen::Vector4f (0.1, 0.1, 0.1, 1.0);
158  }
159  }
160 
168  void render (Camera &camera_2d, Shader &shader)
169  {
170  if (!visible)
171  return;
172 
173  shader.bind();
174 
175  shader.setUniform("modelMatrix", model_matrix);
176  shader.setUniform("viewMatrix", camera_2d.getViewMatrix());
177  shader.setUniform("projectionMatrix", camera_2d.getProjectionMatrix());
178  shader.setUniform("in_Color", color);
179  if (selected && has_alt_texture)
180  shader.setUniform("shapetex", texture_alt.bind());
181  else
182  shader.setUniform("shapetex", texture.bind());
183 
184  quad.setAttributeLocation(shader);
185 
186  quad.bindBuffers();
189 
190  shader.unbind();
191  if (selected && has_alt_texture)
193  else
194  texture.unbind();
195  }
196 
197 
198 };
199 }
200 }
201 #endif
Eigen::Vector2i dimensions
Dimensions in pixels.
Definition: element.hpp:66
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
SelectButton(int w, int h, int x, int y, string texture)
Overload constructor that receives dimensions and texture file.
Definition: selectbutton.hpp:90
Definition: element.hpp:39
int getType(void)
Returns the element type.
Definition: selectbutton.hpp:111
void getViewMatrix(GLdouble *matrix)
Return the modelview matrix as a GLdouble array.
Definition: camera.hpp:126
virtual void setModelMatrix(void)
Sets the element model matrix.
Definition: element.hpp:195
Tucano::Texture texture
Element texture.
Definition: element.hpp:75
void getProjectionMatrix(GLdouble *matrix)
Return the projection matrix as a GLdouble array.
Definition: camera.hpp:142
Definition: bufferobject.hpp:34
The button class draws a clickable rectangle on the screen to be used as a callback interface The sel...
Definition: selectbutton.hpp:43
SelectButton(int w, int h, int x, int y)
Overload Constructor.
Definition: selectbutton.hpp:72
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
A Shader object represents one GLSL program.
Definition: shader.hpp:45
void onClick(function< void()> f)
Definition: selectbutton.hpp:121
SelectButton(void)
Default constructor.
Definition: selectbutton.hpp:60
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:40
bool selected
Flag to select (pressed) / unselect button.
Definition: selectbutton.hpp:50
int num_params
Number of parameters for the callback.
Definition: element.hpp:60
void unbind(void)
Disables the shader program.
Definition: shader.hpp:1184
Eigen::Affine3f model_matrix
Model Matrix for placing element.
Definition: element.hpp:63
bool has_alt_texture
Flag to define if element has alt texture.
Definition: element.hpp:84
Eigen::Vector4f color
Color.
Definition: element.hpp:90
virtual void unbindBuffers(void)
Unbinds all buffers.
Definition: mesh.hpp:702
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
void render(Camera &camera_2d, Shader &shader)
Renders button If has alternate texture uses the one corresponding to current state, otherwise uses always main texture.
Definition: selectbutton.hpp:168
Tucano::Texture texture_alt
Alternative texture (for example, on/off buttons)
Definition: element.hpp:78
function< void() > callback
Button has one callback without parameters.
Definition: selectbutton.hpp:53
int element_type
Type of element.
Definition: element.hpp:57
void bind(void)
Enables the shader program for usage.
Definition: shader.hpp:1176
void clicked(void)
Click callback.
Definition: selectbutton.hpp:129
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
SelectButton(int w, int h, int x, int y, string texture, string alt_texture)
Overload constructor that receives dimensions and texture file.
Definition: selectbutton.hpp:104
virtual void bindBuffers(void)
Binds all buffers.
Definition: mesh.hpp:677
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
Shapes::Quad quad
Quad to hold texture.
Definition: element.hpp:72
void setColor(const Eigen::Vector4f &c)
Sets the button color.
Definition: selectbutton.hpp:139
void unbind(void)
Unbinds this texture and frees the texture unit.
Definition: texture.hpp:367
friend SelectGroup
Definition: selectbutton.hpp:45
void toggleSelected(void)
Toggles button state selected/unselected.
Definition: selectbutton.hpp:147
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