qtopengl_user_functions.h
Go to the documentation of this file.
1 
7 #ifndef QTOPENGL_USER_FUNCTIONS_H
8 #define QTOPENGL_USER_FUNCTIONS_H
9 
10 namespace argos {
11  class CQTOpenGLUserFunctions;
12  class CFloorEntity;
13 }
14 
15 class QPainter;
16 
17 #include <argos3/core/utility/configuration/base_configurable_resource.h>
18 #include <argos3/plugins/simulator/visualizations/qt-opengl/qtopengl_main_window.h>
19 #include <argos3/plugins/simulator/visualizations/qt-opengl/qtopengl_widget.h>
20 #include <argos3/core/utility/datatypes/color.h>
21 #include <argos3/core/utility/math/quaternion.h>
22 
23 namespace argos {
24 
75 
76  public:
77 
82 
86  virtual ~CQTOpenGLUserFunctions();
87 
88  virtual void Init(TConfigurationNode& t_tree) {}
89  virtual void Reset() {}
90  virtual void Destroy() {}
91 
104  virtual void KeyPressed(QKeyEvent* pc_event);
105 
118  virtual void KeyReleased(QKeyEvent* pc_event);
119 
133  virtual void MouseKeyPressed(QMouseEvent* pc_event) {}
134 
148  virtual void MouseKeyReleased(QMouseEvent* pc_event) {}
149 
158  virtual void MouseMoved(QMouseEvent* pc_event) {}
159 
164  virtual void EntitySelected(CEntity& c_entity) {}
165 
170  virtual void EntityDeselected(CEntity& c_entity) {}
171 
178  virtual void EntityMoved(CEntity& c_entity,
179  const CVector3& c_old_pos,
180  const CVector3& c_new_pos) {}
181 
188  virtual void EntityRotated(CEntity& c_entity,
189  const CQuaternion& c_old_orientation,
190  const CQuaternion& c_new_orientation) {}
191 
196 
205  virtual void SelectEntity(CEntity& c_entity);
206 
213  virtual void DeselectEntity();
214 
218  virtual void Draw(CFloorEntity& c_entity) {}
219 
225  virtual void DrawInWorld() {}
226 
234  virtual void DrawOverlay(QPainter& c_painter) {}
235 
241 
246  void SetMainWindow(CQTOpenGLMainWindow& c_main_win);
247 
253 
258  void SetColor(const CColor& c_color);
259 
267  void DrawPoint(const CVector3& c_position,
268  const CColor& c_color = CColor::RED,
269  Real f_diameter = 5.0);
270 
282  void DrawTriangle(const CVector3& c_position,
283  const CQuaternion& c_orientation,
284  Real f_base,
285  Real f_height,
286  const CColor& c_color = CColor::RED,
287  const bool b_fill = true);
288 
298  void DrawPolygon(const CVector3& c_position,
299  const CQuaternion& c_orientation,
300  const std::vector<CVector2>& vec_points,
301  const CColor& c_color = CColor::RED,
302  const bool b_fill = true);
303 
315  void DrawCircle(const CVector3& c_position,
316  const CQuaternion& c_orientation,
317  Real f_radius,
318  const CColor& c_color = CColor::RED,
319  const bool b_fill = true,
320  GLuint un_vertices = 20);
321 
333  void DrawCylinder(const CVector3& c_position,
334  const CQuaternion& c_orientation,
335  Real f_radius,
336  Real f_height,
337  const CColor& c_color = CColor::RED,
338  GLuint un_vertices = 20);
339 
349  void DrawBox(const CVector3& c_position,
350  const CQuaternion& c_orientation,
351  const CVector3& c_size,
352  const CColor& c_color = CColor::RED);
353 
361  void DrawRay(const CRay3& c_ray,
362  const CColor& c_color = CColor::RED,
363  Real f_width = 1.0f);
364 
373  void DrawText(const CVector3& c_position,
374  const std::string& str_text,
375  const CColor& c_color = CColor::BLACK,
376  const QFont& c_font = QFont());
377 
378  protected:
379 
385 
395  template <typename USER_IMPL, typename ENTITY>
396  void Thunk(CEntity& c_entity);
397 
402  class CFunctionHolder {};
403 
411  template <typename USER_IMPL, typename ENTITY> class CFunctionHolderImpl : public CFunctionHolder {
412  public:
413  typedef void (USER_IMPL::*TFunction)(ENTITY&);
415  CFunctionHolderImpl(TFunction t_function) : Function(t_function) {}
416  };
417 
424 
429  std::vector<CFunctionHolder*> m_vecFunctionHolders;
430 
431  public:
432 
439  template <typename USER_IMPL, typename ENTITY>
440  void RegisterUserFunction(void(USER_IMPL::*pt_function)(ENTITY&));
441 
446  virtual void Call(CEntity& c_entity);
447 
448  private:
449 
453  CQTOpenGLMainWindow* m_pcQTOpenGLMainWindow;
454 
455  };
456 
457  /****************************************/
458  /****************************************/
459 
460  template <typename USER_IMPL, typename ENTITY>
462  /*
463  * When this method is called, the static type of 'this'
464  * is CQTOpenGLUserFunctions. Since we want to call
465  * method in USER_IMPL (subclass of CQTOpenGLUserFunctions),
466  * we need a cast. The cast is static because we trust
467  * the user on not doing anything stupid.
468  * The variable cImpl can be static because the cast is necessary
469  * only the first time this function is called.
470  */
471  static USER_IMPL& cImpl = static_cast<USER_IMPL&>(*this);
472  /* Cast the argument to the right type */
473  ENTITY& cEntity = static_cast<ENTITY&>(c_entity);
474  /* Cast the function holder to its effective type */
475  CFunctionHolderImpl<USER_IMPL,ENTITY>& cFunctionHolder = static_cast<CFunctionHolderImpl<USER_IMPL,ENTITY>&>(*m_vecFunctionHolders[GetTag<ENTITY,CEntity>()]);
476  /* Call the user-defined method */
477  (cImpl.*(cFunctionHolder.Function))(cEntity);
478  }
479 
480  template <typename USER_IMPL, typename ENTITY>
481  void CQTOpenGLUserFunctions::RegisterUserFunction(void(USER_IMPL::*pt_function)(ENTITY&)) {
482  /* Add the thunk to the VTable */
483  m_cThunks.Add<ENTITY>(&CQTOpenGLUserFunctions::Thunk<USER_IMPL,ENTITY>);
484  /* Add the function holder to the vector, padding gaps with NULL pointers */
485  size_t unIdx = GetTag<ENTITY,CEntity>();
486  if(m_vecFunctionHolders.size() <= unIdx) {
487  m_vecFunctionHolders.resize(unIdx+1, NULL);
488  }
490  }
491 
492  /****************************************/
493  /****************************************/
494 
495 }
496 
497 /* Definitions useful for dynamic linking of user functions */
498 #define REGISTER_QTOPENGL_USER_FUNCTIONS(CLASSNAME, LABEL) \
499  REGISTER_SYMBOL(CQTOpenGLUserFunctions, \
500  CLASSNAME, \
501  LABEL, \
502  "undefined", \
503  "undefined", \
504  "undefined", \
505  "undefined", \
506  "undefined")
507 
508 #endif
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
The basic entity type.
Definition: entity.h:90
This class is the base of all XML-configurable ARGoS interface.
The basic color type.
Definition: color.h:25
static CColor BLACK
Definition: color.h:29
static CColor RED
Definition: color.h:31
A 3D vector class.
Definition: vector3.h:31
The actual vtable.
Definition: vtable.h:155
The QTOpenGL user functions.
virtual void EntityMoved(CEntity &c_entity, const CVector3 &c_old_pos, const CVector3 &c_new_pos)
Called every time an entity is moved.
std::vector< CFunctionHolder * > m_vecFunctionHolders
A vector of function holders.
void SetColor(const CColor &c_color)
Sets the current drawing color.
virtual void EntityDeselected(CEntity &c_entity)
Called every time an entity is deselected.
void SetMainWindow(CQTOpenGLMainWindow &c_main_win)
Sets the QTOpenGL main window for these user functions.
virtual void MouseKeyPressed(QMouseEvent *pc_event)
Called when a mouse key is pressed.
CVTable< CQTOpenGLUserFunctions, CEntity, TThunk > m_cThunks
The vtable storing the thunks.
virtual void EntityRotated(CEntity &c_entity, const CQuaternion &c_old_orientation, const CQuaternion &c_new_orientation)
Called every time an entity is rotated.
void Thunk(CEntity &c_entity)
A templetized thunk.
void DrawPoint(const CVector3 &c_position, const CColor &c_color=CColor::RED, Real f_diameter=5.0)
Draws a point.
void DrawTriangle(const CVector3 &c_position, const CQuaternion &c_orientation, Real f_base, Real f_height, const CColor &c_color=CColor::RED, const bool b_fill=true)
Draws an isosceles triangle.
virtual void Destroy()
Undoes whatever was done by Init().
virtual void SelectEntity(CEntity &c_entity)
Selects the specified entity.
virtual void MouseMoved(QMouseEvent *pc_event)
Called when the mouse is moved.
virtual void KeyPressed(QKeyEvent *pc_event)
Called when a key press event occurs.
virtual ~CQTOpenGLUserFunctions()
Class destructor.
CQTOpenGLMainWindow & GetMainWindow()
Returns the QTOpenGL main window.
void RegisterUserFunction(void(USER_IMPL::*pt_function)(ENTITY &))
Registers a user method.
virtual void Init(TConfigurationNode &t_tree)
Initializes the resource.
virtual void KeyReleased(QKeyEvent *pc_event)
Called when a key release event occurs.
virtual void Call(CEntity &c_entity)
Calls a user method for the given entity.
void DrawPolygon(const CVector3 &c_position, const CQuaternion &c_orientation, const std::vector< CVector2 > &vec_points, const CColor &c_color=CColor::RED, const bool b_fill=true)
Draws a 2D polygon.
void DrawBox(const CVector3 &c_position, const CQuaternion &c_orientation, const CVector3 &c_size, const CColor &c_color=CColor::RED)
Draws a box.
CQTOpenGLWidget & GetQTOpenGLWidget()
Returns the QTOpenGLWidget.
void DrawText(const CVector3 &c_position, const std::string &str_text, const CColor &c_color=CColor::BLACK, const QFont &c_font=QFont())
Draws a string of text.
void DrawCylinder(const CVector3 &c_position, const CQuaternion &c_orientation, Real f_radius, Real f_height, const CColor &c_color=CColor::RED, GLuint un_vertices=20)
Draws a cylinder, with the height perpendicular to the XY plane.
virtual void DrawInWorld()
Drawing hook executed after all objects have been drawn.
CEntity * GetSelectedEntity()
Returns the currently selected entity, or NULL.
virtual void EntitySelected(CEntity &c_entity)
Called every time an entity is selected.
void DrawCircle(const CVector3 &c_position, const CQuaternion &c_orientation, Real f_radius, const CColor &c_color=CColor::RED, const bool b_fill=true, GLuint un_vertices=20)
Draws a circle.
void(CQTOpenGLUserFunctions::* TThunk)(CEntity &)
Pointer-to-thunk type definition.
void DrawRay(const CRay3 &c_ray, const CColor &c_color=CColor::RED, Real f_width=1.0f)
Draws a ray, with optional endpoint markers.
virtual void Reset()
Resets the resource.
virtual void DrawOverlay(QPainter &c_painter)
Drawing hook to put graphics on top of the OpenGL window.
virtual void DeselectEntity()
Deselects the currently selected entity.
virtual void Draw(CFloorEntity &c_entity)
Drawing hook executed after the floor is drawn.
virtual void MouseKeyReleased(QMouseEvent *pc_event)
Called when a mouse key is released.