Fawkes API  Fawkes Development Version
visdisplay.cpp
1 
2 /***************************************************************************
3  * visdisplay.cpp - Visual Display to show VisualDisplay2DInterface objects
4  *
5  * Created: Thu Jan 07 23:48:49 2010
6  * Copyright 2008-2010 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "visdisplay.h"
24 
25 #include <interfaces/VisualDisplay2DInterface.h>
26 
27 using namespace fawkes;
28 
29 /** @class VisualDisplay2D "visdisplay.h"
30  * 2D visualization processor for VisualDisplay2DInterface.
31  * This class processes messages from the VisualDisplay2DInterface and
32  * issues appropriate drawing commands to a Cairo drawing context.
33  * @author Tim Niemueller
34  */
35 
36 /** Constructor. */
38 {
39  interface_ = NULL;
40 }
41 
42 /** Destructor. */
44 {
45  for (sit_ = shapes_.begin(); sit_ != shapes_.end(); ++sit_) {
46  delete sit_->second;
47  }
48  shapes_.clear();
49 }
50 
51 /** Set interface.
52  * @param interface interface to query for messages
53  */
54 void
56 {
57  interface_ = interface;
58 }
59 
60 /** Process messages.
61  * This processes the messages and builds up the internal object
62  * representations.
63  */
64 void
66 {
67  while (!interface_->msgq_empty()) {
68  if (interface_->msgq_first_is<VisualDisplay2DInterface::AddCartLineMessage>()) {
70  interface_->msgq_first<VisualDisplay2DInterface::AddCartLineMessage>();
71  shapes_[m->id()] = new Line(m->x(0),
72  m->y(0),
73  m->x(1),
74  m->y(1),
75  m->id(),
76  m->sender_id(),
77  m->style(),
78  m->color(0),
79  m->color(1),
80  m->color(2),
81  m->color(3));
82 
83  } else if (interface_->msgq_first_is<VisualDisplay2DInterface::AddCartRectMessage>()) {
85  interface_->msgq_first<VisualDisplay2DInterface::AddCartRectMessage>();
86  shapes_[m->id()] = new Rectangle(m->x(),
87  m->y(),
88  m->width(),
89  m->height(),
90  m->id(),
91  m->sender_id(),
92  m->style(),
93  m->color(0),
94  m->color(1),
95  m->color(2),
96  m->color(3));
97 
98  } else if (interface_->msgq_first_is<VisualDisplay2DInterface::AddCartCircleMessage>()) {
100  interface_->msgq_first<VisualDisplay2DInterface::AddCartCircleMessage>();
101  shapes_[m->id()] = new Circle(m->x(),
102  m->y(),
103  m->radius(),
104  m->id(),
105  m->sender_id(),
106  m->style(),
107  m->color(0),
108  m->color(1),
109  m->color(2),
110  m->color(3));
111 
112  } else if (interface_->msgq_first_is<VisualDisplay2DInterface::AddCartTextMessage>()) {
114  interface_->msgq_first<VisualDisplay2DInterface::AddCartTextMessage>();
115  shapes_[m->id()] = new Text(m->x(),
116  m->y(),
117  m->text(),
118  m->anchor(),
119  m->size(),
120  m->id(),
121  m->sender_id(),
122  m->color(0),
123  m->color(1),
124  m->color(2),
125  m->color(3));
126 
127  } else if (interface_->msgq_first_is<VisualDisplay2DInterface::DeleteAllMessage>()) {
128  for (sit_ = shapes_.begin(); sit_ != shapes_.end(); ++sit_) {
129  delete sit_->second;
130  }
131  shapes_.clear();
132  }
133 
134  interface_->msgq_pop();
135  }
136 }
137 
138 /** Draw objects.
139  * This draws all objects currently enqueued by process_messages().
140  * @param cr Cairo context to draw to
141  */
142 void
143 VisualDisplay2D::draw(Cairo::RefPtr<Cairo::Context> cr)
144 {
145  cr->save();
146  for (sit_ = shapes_.begin(); sit_ != shapes_.end(); ++sit_) {
147  float r, g, b, a;
148  sit_->second->color(r, g, b, a);
149  sit_->second->apply_style(cr);
150  sit_->second->draw(cr);
151  }
152  cr->stroke();
153  cr->restore();
154 }
155 
156 /** @class VisualDisplay2D::Shape "visdisplay.h"
157  * Class representing a shape.
158  * All shapes inherit from the class and provide drawing primitives. The
159  * internal object representations are instances of shapes.
160  * @author Tim Niemueller
161  *
162  * @fn VisualDisplay2D::Shape::draw(Cairo::RefPtr<Cairo::Context> &cr)
163  * Draw shape to Cairo context.
164  * This method shall be implemented by a shape to draw itself using the
165  * provided Cairo context.
166  * @param cr reference to Cairo context. Note that this is a reference
167  * bypassing the reference pointer. This is done for efficiency and with
168  * the assumption that this method is only called by VisualDisplay2D::draw()
169  * which itself has proper refptr handling.
170  *
171  * @fn inline void VisualDisplay2D::Shape::apply_style(Cairo::RefPtr<Cairo::Context> &cr)
172  * Set style on context.
173  * This method sets the style determined by the shape to the Cairo context.
174  * @param cr reference to Cairo context. Note that this is a reference
175  * bypassing the reference pointer. This is done for efficiency and with
176  * the assumption that this method is only called by VisualDisplay2D::draw()
177  * which itself has proper refptr handling.
178  *
179  * @fn inline unsigned int VisualDisplay2D::Shape::id()
180  * Get shape ID.
181  * @return shape ID
182  *
183  * @fn inline unsigned int VisualDisplay2D::Shape::owner()
184  * Get owner ID.
185  * @return owner ID
186  *
187  * @fn inline void VisualDisplay2D::Shape::color(float &r, float &g, float &b, float &a)
188  * Get shape color.
189  * @param r upon return contains red part of RGBA color
190  * @param g upon return contains green part of RGBA color
191  * @param b upon return contains blue part of RGBA color
192  * @param a upon return contains alpha part of RGBA color
193  */
194 
195 /** Constructor.
196  * @param id object ID
197  * @param owner ID of the owner of the object
198  * @param line_style drawing style of lines of shapes
199  * @param r red part of RGBA color
200  * @param g green part of RGBA color
201  * @param b blue part of RGBA color
202  * @param a alpha part of RGBA color
203  */
205  unsigned int owner,
207  unsigned char r,
208  unsigned char g,
209  unsigned char b,
210  unsigned char a)
211 {
212  _id = id;
213  _owner = owner;
214  _line_style = line_style;
215  _color_r = r / 255.f;
216  _color_g = g / 255.f;
217  _color_b = b / 255.f;
218  _color_a = a / 255.f;
219 }
220 
221 /** Virtual empty destructor. */
223 {
224 }
225 
226 /** @class VisualDisplay2D::Line "visdisplay.h"
227  * Class representing a line.
228  * Line represented by two end points in cartesian coordinates.
229  * @author Tim Niemueller
230  */
231 
232 /** Constructor.
233  * @param x1 X coordinate of first point
234  * @param y1 Y coordinate of first point
235  * @param x2 X coordinate of second point
236  * @param y2 Y coordinate of second point
237  * @param id object ID
238  * @param owner ID of the owner of the object
239  * @param line_style drawing style of lines of shapes
240  * @param r red part of RGBA color
241  * @param g green part of RGBA color
242  * @param b blue part of RGBA color
243  * @param a alpha part of RGBA color
244  */
246  float y1,
247  float x2,
248  float y2,
249  unsigned int id,
250  unsigned int owner,
252  unsigned char r,
253  unsigned char g,
254  unsigned char b,
255  unsigned char a)
256 : Shape(id, owner, line_style, r, g, b, a)
257 {
258  x1_ = x1;
259  y1_ = y1;
260  x2_ = x2;
261  y2_ = y2;
262 }
263 
264 void
265 VisualDisplay2D::Line::draw(Cairo::RefPtr<Cairo::Context> &cr)
266 {
267  cr->move_to(x1_, y1_);
268  cr->line_to(x2_, y2_);
269  cr->stroke();
270 }
271 
272 /** @class VisualDisplay2D::Rectangle "visdisplay.h"
273  * Class representing a rectangle.
274  * Rectangle represented the cartesian coordinates of the lower right corner
275  * and its width and height.
276  * @author Tim Niemueller
277  */
278 
279 /** Constructor.
280  * @param x X coordinate of lower right point
281  * @param y Y coordinate of lower right point
282  * @param width width of rectangle
283  * @param height height of rectangle
284  * @param id object ID
285  * @param owner ID of the owner of the object
286  * @param line_style drawing style of lines of shapes
287  * @param r red part of RGBA color
288  * @param g green part of RGBA color
289  * @param b blue part of RGBA color
290  * @param a alpha part of RGBA color
291  */
293  float y,
294  float width,
295  float height,
296  unsigned int id,
297  unsigned int owner,
299  unsigned char r,
300  unsigned char g,
301  unsigned char b,
302  unsigned char a)
303 : Shape(id, owner, line_style, r, g, b, a)
304 {
305  x_ = x;
306  y_ = y;
307  width_ = width;
308  height_ = height;
309 }
310 
311 void
312 VisualDisplay2D::Rectangle::draw(Cairo::RefPtr<Cairo::Context> &cr)
313 {
314  cr->rectangle(x_, y_, width_, height_);
315 }
316 
317 /** @class VisualDisplay2D::Circle "visdisplay.h"
318  * Class representing a circle
319  * Line represented by its center point and radius.
320  * @author Tim Niemueller
321  */
322 
323 /** Constructor.
324  * @param x X coordinate of center point
325  * @param y Y coordinate of center point
326  * @param radius radius of the circle
327  * @param id object ID
328  * @param owner ID of the owner of the object
329  * @param line_style drawing style of lines of shapes
330  * @param r red part of RGBA color
331  * @param g green part of RGBA color
332  * @param b blue part of RGBA color
333  * @param a alpha part of RGBA color
334  */
336  float y,
337  float radius,
338  unsigned int id,
339  unsigned int owner,
341  unsigned char r,
342  unsigned char g,
343  unsigned char b,
344  unsigned char a)
345 : Shape(id, owner, line_style, r, g, b, a)
346 {
347  x_ = x;
348  y_ = y;
349  radius_ = radius;
350 }
351 
352 void
353 VisualDisplay2D::Circle::draw(Cairo::RefPtr<Cairo::Context> &cr)
354 {
355  cr->arc(x_, y_, radius_, 0, 2 * M_PI);
356 }
357 
358 /** @class VisualDisplay2D::Text "visdisplay.h"
359  * Class representing a text object.
360  * Text is represented by a cartesian coordinate, which denotes a specific
361  * point defined by the anchor, the text itself, and a text size.
362  * @author Tim Niemueller
363  */
364 
365 /** Constructor.
366  * @param x X coordinate of anchor point
367  * @param y Y coordinate of anchor point
368  * @param text text to display
369  * @param anchor anchor point relative to the text's bounding box
370  * @param size height of font in meters
371  * @param id object ID
372  * @param owner ID of the owner of the object
373  * @param r red part of RGBA color
374  * @param g green part of RGBA color
375  * @param b blue part of RGBA color
376  * @param a alpha part of RGBA color
377  */
379  float y,
380  const std::string & text,
382  float size,
383  unsigned int id,
384  unsigned int owner,
385  unsigned char r,
386  unsigned char g,
387  unsigned char b,
388  unsigned char a)
389 : Shape(id, owner, fawkes::VisualDisplay2DInterface::LS_SOLID, r, g, b, a),
390  x_(x),
391  y_(y),
392  text_(text),
393  size_(size),
394  anchor_(anchor)
395 {
396 }
397 
398 void
399 VisualDisplay2D::Text::draw(Cairo::RefPtr<Cairo::Context> &cr)
400 {
401  cr->save();
402  cr->scale(-1, 1);
403  cr->rotate(-0.5 * M_PI);
404  cr->set_font_size(1.36 * size_);
405 
406  Cairo::TextExtents te;
407  cr->get_text_extents(text_, te);
408 
409  float x = x_, y = y_;
410  switch (anchor_) {
411  case VisualDisplay2DInterface::CENTERED:
412  x = x_ - te.width / 2.;
413  y = y_ + te.height / 2.;
414  break;
415  case VisualDisplay2DInterface::NORTH:
416  x = x_ - te.width / 2.;
417  y = y_ + te.height;
418  break;
419  case VisualDisplay2DInterface::EAST:
420  x = x_ - te.width;
421  y = y_ + te.height / 2.;
422  break;
423  case VisualDisplay2DInterface::SOUTH: x = x_ - te.width / 2.; break;
424  case VisualDisplay2DInterface::WEST: y = y_ + te.height / 2.; break;
425  case VisualDisplay2DInterface::NORTH_EAST:
426  x = x_ - te.width;
427  y = y_ + te.height;
428  break;
429  case VisualDisplay2DInterface::SOUTH_EAST: x = x_ - te.width; break;
430  case VisualDisplay2DInterface::SOUTH_WEST: break;
431  case VisualDisplay2DInterface::NORTH_WEST: y = y_ + te.height; break;
432  }
433 
434  cr->move_to(x, y);
435  cr->show_text(text_);
436  cr->restore();
437 }
Class representing a circle Line represented by its center point and radius.
Definition: visdisplay.h:141
void draw(Cairo::RefPtr< Cairo::Context > &cr)
Draw shape to Cairo context.
Definition: visdisplay.cpp:353
Circle(float x, float y, float radius, unsigned int id, unsigned int owner, fawkes::VisualDisplay2DInterface::LineStyle line_style=fawkes::VisualDisplay2DInterface::LS_SOLID, unsigned char r=0, unsigned char g=0, unsigned char b=0, unsigned char a=0)
Constructor.
Definition: visdisplay.cpp:335
Class representing a line.
Definition: visdisplay.h:93
void draw(Cairo::RefPtr< Cairo::Context > &cr)
Draw shape to Cairo context.
Definition: visdisplay.cpp:265
Line(float x1, float y1, float x2, float y2, unsigned int id, unsigned int owner, fawkes::VisualDisplay2DInterface::LineStyle line_style=fawkes::VisualDisplay2DInterface::LS_SOLID, unsigned char r=0, unsigned char g=0, unsigned char b=0, unsigned char a=0)
Constructor.
Definition: visdisplay.cpp:245
Class representing a rectangle.
Definition: visdisplay.h:117
Rectangle(float x, float y, float width, float height, unsigned int id, unsigned int owner, fawkes::VisualDisplay2DInterface::LineStyle line_style=fawkes::VisualDisplay2DInterface::LS_SOLID, unsigned char r=0, unsigned char g=0, unsigned char b=0, unsigned char a=0)
Constructor.
Definition: visdisplay.cpp:292
void draw(Cairo::RefPtr< Cairo::Context > &cr)
Draw shape to Cairo context.
Definition: visdisplay.cpp:312
Class representing a shape.
Definition: visdisplay.h:44
Shape(unsigned int id, unsigned int owner, fawkes::VisualDisplay2DInterface::LineStyle line_style=fawkes::VisualDisplay2DInterface::LS_SOLID, unsigned char r=0, unsigned char g=0, unsigned char b=0, unsigned char a=0)
Constructor.
Definition: visdisplay.cpp:204
virtual ~Shape()
Virtual empty destructor.
Definition: visdisplay.cpp:222
Class representing a text object.
Definition: visdisplay.h:163
void draw(Cairo::RefPtr< Cairo::Context > &cr)
Draw shape to Cairo context.
Definition: visdisplay.cpp:399
Text(float x, float y, const std::string &text, fawkes::VisualDisplay2DInterface::Anchor anchor, float size, unsigned int id, unsigned int owner, unsigned char r=0, unsigned char g=0, unsigned char b=0, unsigned char a=0)
Constructor.
Definition: visdisplay.cpp:378
VisualDisplay2D()
Constructor.
Definition: visdisplay.cpp:37
void set_interface(fawkes::VisualDisplay2DInterface *interface)
Set interface.
Definition: visdisplay.cpp:55
void draw(Cairo::RefPtr< Cairo::Context > cr)
Draw objects.
Definition: visdisplay.cpp:143
~VisualDisplay2D()
Destructor.
Definition: visdisplay.cpp:43
void process_messages()
Process messages.
Definition: visdisplay.cpp:65
unsigned int sender_id() const
Get ID of sender.
Definition: message.cpp:316
unsigned int id() const
Get message ID.
Definition: message.cpp:180
AddCartCircleMessage Fawkes BlackBoard Interface Message.
AddCartLineMessage Fawkes BlackBoard Interface Message.
AddCartRectMessage Fawkes BlackBoard Interface Message.
AddCartTextMessage Fawkes BlackBoard Interface Message.
DeleteAllMessage Fawkes BlackBoard Interface Message.
VisualDisplay2DInterface Fawkes BlackBoard Interface.
Anchor
Enumeration defining the possible anchor points.
LineStyle
Enumeration defining the possible line styles.
Fawkes library namespace.