Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
base.hpp
Go to the documentation of this file.
1 
23 #ifndef __GUIBASE__
24 #define __GUIBASE__
25 
26 #include <tucano/camera.hpp>
28 #include <tucano/gui/button.hpp>
30 #include <tucano/gui/slider.hpp>
31 #include <tucano/gui/label.hpp>
32 #include <tucano/gui/groupbox.hpp>
34 #include <functional>
35 
36 namespace Tucano
37 {
38 
39 namespace GUI
40 {
41 
45 class Base {
46 
47 protected:
48 
50  Eigen::Vector2i viewport_size;
51 
54 
57 
59  vector < Tucano::GUI::Element* > elements;
60 
63 
64 public:
65 
69  Base (void)
70  {
71  gui_shader.setShaderName("gui_shader");
73  }
74 
78  void add (Element *el)
79  {
80  elements.push_back(el);
81  }
82 
87  void setViewportSize (const Eigen::Vector2i & vs)
88  {
89  camera_2d.reset();
90  camera_2d.setOrthographicMatrix(0.0, (float)vs[0], (float)vs[1], 0.0, 0.0, 1.0);
91  }
92 
98  void setViewportSize (int w, int h)
99  {
100  setViewportSize (Eigen::Vector2i (w, h) );
101  }
102 
106  void render (void)
107  {
108  glDepthMask(GL_FALSE);
109  glDisable(GL_DEPTH_TEST);
110  for (unsigned int i = 0; i < elements.size(); ++i)
111  {
112  elements[i]->render(camera_2d, gui_shader);
113  }
114  glDepthMask(GL_TRUE);
115  }
116 
124  bool checkClicked (int x, int y, vector <Tucano::GUI::Element *> * list)
125  {
126  for (unsigned int i = 0; i < list->size(); ++i)
127  {
128  Tucano::GUI::Element* element = list->at(i);
129  if (element->isVisible() && element->isInside(x, y))
130  {
131  last_clicked = element;
132  // if element was a groupbox, check elements inside group
133  if (element->getType() == Tucano::GUI::GROUPBOX)
134  {
135  return checkClicked (x , y, ((Tucano::GUI::GroupBox*)element)->elementList());
136  }
137 
138  // if element is a select group call internal routine for checking selection
139  if (element->getType() == Tucano::GUI::SELECTGROUP)
140  {
141  return static_cast <Tucano::GUI::SelectGroup*> (element)->checkClicked(x, y);
142  }
143 
144  // clicked element is a button
145  if (element->getType() == Tucano::GUI::BUTTON)
146  {
147  ((Tucano::GUI::Button*)element)->clicked();
148  }
149 
150  // clicked element is a select button
151  if (element->getType() == Tucano::GUI::SELECTBUTTON)
152  {
153  static_cast <Tucano::GUI::SelectButton*> (element)->clicked();
154  }
155  return true;
156  }
157  }
158  return false;
159  }
160 
167  bool leftButtonPressed (int x, int y)
168  {
169  last_clicked = NULL;
170  return checkClicked (x, y, &elements);
171  }
172 
179  bool leftButtonReleased (int x, int y)
180  {
181  if (last_clicked)
182  {
183  last_clicked->release();
184  last_clicked = 0;
185  return true;
186  }
187  return false;
188  }
189 
196  bool cursorMove (int x, int y)
197  {
198  if (last_clicked)
199  {
200  last_clicked->cursorMove(x,y);
201  if (last_clicked->getType() == Tucano::GUI::SLIDER)
202  {
203  ((Tucano::GUI::Slider*)last_clicked)->valueChanged();
204  }
205  return true;
206  }
207  return false;
208  }
209 
210 };
211 }
212 }
213 #endif
void setViewportSize(int w, int h)
Set viewport size.
Definition: base.hpp:98
virtual void cursorMove(int x, int y)
Set behavior when mouse is released after clicking this element.
Definition: element.hpp:260
Base class for all GUI elements (buttons, sliders ...)
Definition: element.hpp:52
Definition: element.hpp:39
const string gui_vertex_code
Default vertex shader for rendering gui elements.
Definition: guishaders.hpp:50
bool isVisible(void)
Return whether element is visible or not.
Definition: element.hpp:359
Definition: bufferobject.hpp:34
bool leftButtonReleased(int x, int y)
Treats mouse left release callback for last clicked element.
Definition: base.hpp:179
Tucano::GUI::Element * last_clicked
Last clicked element.
Definition: base.hpp:62
The button class draws a clickable rectangle on the screen to be used as a callback interface The sel...
Definition: selectbutton.hpp:43
void setShaderName(string name)
Sets the shader name, very useful for debugging.
Definition: shader.hpp:329
Base(void)
Default constructor.
Definition: base.hpp:69
Tucano::Shader gui_shader
Shader for rendering gui elements.
Definition: base.hpp:56
A Shader object represents one GLSL program.
Definition: shader.hpp:45
The gui select group is a placeholder for many selection buttons, so only one can be active at a time...
Definition: selectgroup.hpp:39
void add(Element *el)
Adds an element to the gui.
Definition: base.hpp:78
The gui group box is just a placeholder for other gui elements. It can be used to group elements...
Definition: groupbox.hpp:41
void initializeFromStrings(string in_vertex_code, string in_fragment_code, string in_geometry_code="", string in_tessellation_evaluation_code="", string in_tessellation_control_code="")
Initializes shader directly from string, no files.
Definition: shader.hpp:589
Eigen::Vector2i viewport_size
Viewport size.
Definition: base.hpp:50
Definition: element.hpp:44
Tucano::Camera camera_2d
2D camera
Definition: base.hpp:53
The button class draws a clickable rectangle on the screen to be used as a callback interface...
Definition: button.hpp:40
void render(void)
renders the 2D gui interface
Definition: base.hpp:106
Definition: element.hpp:40
Definition: element.hpp:43
const string gui_fragment_code
Default fragment shader for rendering gui elments.
Definition: guishaders.hpp:35
bool cursorMove(int x, int y)
Treats mouse mouvement.
Definition: base.hpp:196
virtual void release(void)
Set behavior when mouse is released after clicking this element.
Definition: element.hpp:253
bool checkClicked(int x, int y, vector< Tucano::GUI::Element * > *list)
Check if any element in a given list is inside clicked coordinates.
Definition: base.hpp:124
void setViewportSize(const Eigen::Vector2i &vs)
Set viewport size.
Definition: base.hpp:87
Base class for GUI, contains all elements and handles viewport and rendering transformations.
Definition: base.hpp:45
bool isInside(int x, int y)
Overloads the isInside method. Queries if a point is inside the button.
Definition: element.hpp:233
void reset(void)
Resets trackball to initial position and orientation.
Definition: camera.hpp:104
virtual int getType(void)
Returns the element type.
Definition: element.hpp:265
bool leftButtonPressed(int x, int y)
Treats mouse left click callback for all elements.
Definition: base.hpp:167
Defines an abstract camera with a projection and view matrices.
Definition: camera.hpp:37
Definition: element.hpp:41
Eigen::Matrix4f setOrthographicMatrix(float left, float right, float bottom, float top, float near_plane, float far_plane)
Sets the projection matrix as a orthographic matrix.
Definition: camera.hpp:465
vector< Tucano::GUI::Element * > elements
Vector containing gui elements.
Definition: base.hpp:59
The slide class draws a dragable cursor over a bar.
Definition: slider.hpp:39