Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
slider.hpp
Go to the documentation of this file.
1 
23 #ifndef __SLIDER__
24 #define __SLIDER__
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 
39 class Slider : public Element {
40 
41 protected:
42 
45 
47  function< void(float) > callback;
48 
50  float value = 0.5;
51 
53  float min_value = 0.0;
54  float max_value = 1.0;
55 
57  Eigen::Affine3f slider_model_matrix = Eigen::Affine3f::Identity();
58 
60  bool sliding = false;
61 
63  Eigen::Vector2i slider_pos = Eigen::Vector2i::Zero();
64 
66  Eigen::Vector2i slider_dim = Eigen::Vector2i::Zero();
67 
68 public:
69 
73  Slider (void)
74  {
75  Slider(10, 100, 0, 0);
76  }
77 
85  Slider (int w, int h, int x, int y)
86  {
87  dimensions << w, h;
88  position << x, y;
89  setValue(value);
91  num_params = 1;
92  }
93 
102  Slider (int w, int h, int x, int y, string bar_tex, string slider_tex)
103  {
104  Slider (w, h, x, y);
105  setTexture (bar_tex, slider_tex);
106  }
107 
115  void setMinMaxValues (float minv, float maxv)
116  {
117  min_value = minv;
118  max_value = maxv;
119  }
120 
121 
126  void moveSlider (float v)
127  {
128  setValue ((v - min_value)/(max_value - min_value));
129  }
130 
135  void setValue (float normalized_pos)
136  {
137  // clamp in range [0,1]
138  value = min( max (0.0f, normalized_pos), 1.0f);
139  slider_pos << position[0] + dimensions[0]*value, position[1];
141  }
142 
150  bool isInside (const Eigen::Vector2i& pos)
151  {
152  // check if clicked on bar
154  {
155  float relative_pos = (pos[0] - position[0])/(float)dimensions[0];
156  // check if clicked on slider handle, we leave an extra margin to facilita clicking
157  if (fabs(relative_pos - value) <= 1.5*slider_dim[0]/(float)dimensions[0])
158  {
159  setValue(relative_pos);
160  sliding = true;
161  }
162  return true;
163  }
164  sliding = false;
165  return false;
166  }
167 
173  void cursorMove (int x, int y)
174  {
175  if (sliding)
176  {
177  setValue((x - position[0])/(float)dimensions[0]);
178  }
179  }
180 
181 
185  void release (void)
186  {
187  sliding = false;
188  }
189 
190 
191  int getType (void)
192  {
193  return Tucano::GUI::SLIDER;
194  }
195 
200  void setTexture (string bar_file, string slider_file)
201  {
203  texture.setTexParameters( GL_REPEAT, GL_REPEAT, GL_LINEAR, GL_LINEAR );
204  Tucano::ImageImporter::loadImage(slider_file, &slider_texture);
205  slider_texture.setTexParameters( GL_REPEAT, GL_REPEAT, GL_LINEAR, GL_LINEAR );
206 
207  slider_dim << slider_texture.getWidth() * (dimensions[1] / (float) texture.getHeight()), dimensions[1];
208 
209  setValue(value);
210  }
211 
216  void onValueChanged (function<void(float)> f)
217  {
218  callback = f;
219  }
220 
221 
226  float getValue (void)
227  {
228  return (value)*(max_value-min_value) + min_value;
229  }
230 
231 
235  void valueChanged (void)
236  {
237  callback(getValue());
238  }
239 
244  void setColor (const Eigen::Vector4f& c)
245  {
246  color = c;
247  }
248 
252  void setModelMatrix (void)
253  {
256  }
257 
262  {
263  slider_model_matrix = Eigen::Affine3f::Identity();
264  // place the slider at the horizontal center of the value, and a little below the bar
265  slider_model_matrix.translate (Eigen::Vector3f ((float)slider_pos[0], (float)slider_pos[1], 0.0));
266  // scale slider to same scale as bar (in pixels slider should have twice the height of the bar)
267  slider_model_matrix.scale (Eigen::Vector3f( (float)slider_dim[0], (float)slider_dim[1], 1.0) );
268  // set y origin to top corner
269  slider_model_matrix.translate (Eigen::Vector3f (0.0, 0.5, 0.0) );
270  }
271 
272  void render ( Tucano::Camera &camera_2d, Shader &shader )
273  {
274  if (!visible)
275  return;
276 
277  shader.bind();
278 
279  shader.setUniform("modelMatrix", model_matrix);
280  shader.setUniform("viewMatrix", camera_2d.getViewMatrix());
281  shader.setUniform("projectionMatrix", camera_2d.getProjectionMatrix());
282  shader.setUniform("in_Color", color);
283  shader.setUniform("shapetex", texture.bind());
284 
285  quad.setAttributeLocation(shader);
286  quad.bindBuffers();
287 
290  texture.unbind();
291 
292  shader.setUniform("modelMatrix", slider_model_matrix);
293  shader.setUniform("shapetex", slider_texture.bind());
294 
295  quad.setAttributeLocation(shader);
296  quad.bindBuffers();
298 
300  slider_texture.unbind();
301  shader.unbind();
302  }
303 
304 };
305 }
306 }
307 #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
function< void(float) > callback
Slider has one callback with the slider position.
Definition: slider.hpp:47
float min_value
Min max values.
Definition: slider.hpp:53
void setModelMatrix(void)
Sets the slider model matrix.
Definition: slider.hpp:252
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 setColor(const Eigen::Vector4f &c)
Sets the slider color.
Definition: slider.hpp:244
void getProjectionMatrix(GLdouble *matrix)
Return the projection matrix as a GLdouble array.
Definition: camera.hpp:142
Definition: bufferobject.hpp:34
bool sliding
is holding slider
Definition: slider.hpp:60
static bool loadImage(string filename, Tucano::Texture *tex) __attribute__((unused))
Definition: imageIO.hpp:45
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 cursorMove(int x, int y)
Set behavior when mouse is released after clicking this element.
Definition: slider.hpp:173
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
Eigen::Affine3f slider_model_matrix
Model matrix for the slider (not constant)
Definition: slider.hpp:57
void render(Tucano::Camera &camera_2d, Shader &shader)
Definition: slider.hpp:272
float getValue(void)
Returns the value in range [min, max].
Definition: slider.hpp:226
void setValue(float normalized_pos)
Move slider to relative normalized position on bar [0,1].
Definition: slider.hpp:135
A Shader object represents one GLSL program.
Definition: shader.hpp:45
int getWidth(void)
Returns the texture width.
Definition: texture.hpp:102
void moveSlider(float v)
Move slider to relative position on bar [min,max].
Definition: slider.hpp:126
void setSliderModelMatrix(void)
Sets the model matrix for the slider with the respective position.
Definition: slider.hpp:261
bool visible
Flag to show/hide element.
Definition: element.hpp:98
virtual void callback(void)
Callback with no parameters.
Definition: element.hpp:319
Slider(int w, int h, int x, int y)
Overload Constructor.
Definition: slider.hpp:85
float value
Slider current value.
Definition: slider.hpp:50
void release(void)
Stop sliding when mouse is released.
Definition: slider.hpp:185
void setMinMaxValues(float minv, float maxv)
Sets min max values for the slide All operations are treated in range [0,1] and only when responding ...
Definition: slider.hpp:115
void valueChanged(void)
Callback when value changes.
Definition: slider.hpp:235
Eigen::Vector2i slider_dim
slider dimensions
Definition: slider.hpp:66
Eigen::Vector2i slider_pos
position of slider
Definition: slider.hpp:63
int num_params
Number of parameters for the callback.
Definition: element.hpp:60
void setTexture(string bar_file, string slider_file)
Loads the slider texture file.
Definition: slider.hpp:200
void unbind(void)
Disables the shader program.
Definition: shader.hpp:1184
Eigen::Affine3f model_matrix
Model Matrix for placing element.
Definition: element.hpp:63
Slider(int w, int h, int x, int y, string bar_tex, string slider_tex)
Overload constructor that receives dimensions and texture file.
Definition: slider.hpp:102
Eigen::Vector4f color
Color.
Definition: element.hpp:90
int getHeight(void)
Returns the texture height.
Definition: texture.hpp:111
virtual void unbindBuffers(void)
Unbinds all buffers.
Definition: mesh.hpp:702
int getType(void)
Returns the element type.
Definition: slider.hpp:191
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
int element_type
Type of element.
Definition: element.hpp:57
void bind(void)
Enables the shader program for usage.
Definition: shader.hpp:1176
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 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
float max_value
Definition: slider.hpp:54
Tucano::Texture slider_texture
Slider texture.
Definition: slider.hpp:44
Shapes::Quad quad
Quad to hold texture.
Definition: element.hpp:72
bool isInside(const Eigen::Vector2i &pos)
Returns if clicked on slider handle If clicked on handle start slidigin while button is pressed If cl...
Definition: slider.hpp:150
Definition: element.hpp:41
Slider(void)
Default constructor.
Definition: slider.hpp:73
void unbind(void)
Unbinds this texture and frees the texture unit.
Definition: texture.hpp:367
The slide class draws a dragable cursor over a bar.
Definition: slider.hpp:39
void onValueChanged(function< void(float)> f)
Definition: slider.hpp:216
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