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 
30 /** @class VisualDisplay2D "visdisplay.h"
31  * 2D visualization processor for VisualDisplay2DInterface.
32  * This class processes messages from the VisualDisplay2DInterface and
33  * issues appropriate drawing commands to a Cairo drawing context.
34  * @author Tim Niemueller
35  */
36 
37 /** Constructor. */
39 {
40  __interface = NULL;
41 }
42 
43 /** Destructor. */
45 {
46  for (__sit = __shapes.begin(); __sit != __shapes.end(); ++__sit) {
47  delete __sit->second;
48  }
49  __shapes.clear();
50 }
51 
52 
53 /** Set interface.
54  * @param interface interface to query for messages
55  */
56 void
58 {
59  __interface = interface;
60 }
61 
62 
63 /** Process messages.
64  * This processes the messages and builds up the internal object
65  * representations.
66  */
67 void
69 {
70  while (! __interface->msgq_empty()) {
71  if ( __interface->msgq_first_is<VisualDisplay2DInterface::AddCartLineMessage>() ) {
73  __shapes[m->id()] = new Line(m->x(0), m->y(0), m->x(1), m->y(1),
74  m->id(), m->sender_id(),
75  m->style(), m->color(0),
76  m->color(1), m->color(2), m->color(3));
77 
78  } else if ( __interface->msgq_first_is<VisualDisplay2DInterface::AddCartRectMessage>() ) {
80  __shapes[m->id()] = new Rectangle(m->x(), m->y(), m->width(), m->height(),
81  m->id(), m->sender_id(),
82  m->style(), m->color(0),
83  m->color(1), m->color(2), m->color(3));
84 
85  } else if ( __interface->msgq_first_is<VisualDisplay2DInterface::AddCartCircleMessage>() ) {
87  __shapes[m->id()] = new Circle(m->x(), m->y(), m->radius(),
88  m->id(), m->sender_id(),
89  m->style(), m->color(0),
90  m->color(1), m->color(2), m->color(3));
91 
92  } else if ( __interface->msgq_first_is<VisualDisplay2DInterface::AddCartTextMessage>() ) {
94  __shapes[m->id()] = new Text(m->x(), m->y(), m->text(),
95  m->anchor(), m->size(),
96  m->id(), m->sender_id(),
97  m->color(0),
98  m->color(1), m->color(2), m->color(3));
99 
100  } else if (__interface->msgq_first_is<VisualDisplay2DInterface::DeleteAllMessage>() ) {
101  for (__sit = __shapes.begin(); __sit != __shapes.end(); ++__sit) {
102  delete __sit->second;
103  }
104  __shapes.clear();
105  }
106 
107  __interface->msgq_pop();
108  }
109 }
110 
111 
112 /** Draw objects.
113  * This draws all objects currently enqueued by process_messages().
114  * @param cr Cairo context to draw to
115  */
116 void
117 VisualDisplay2D::draw(Cairo::RefPtr<Cairo::Context> cr)
118 {
119  cr->save();
120  for (__sit = __shapes.begin(); __sit != __shapes.end(); ++__sit) {
121  float r, g, b, a;
122  __sit->second->color(r, g, b, a);
123  __sit->second->apply_style(cr);
124  __sit->second->draw(cr);
125  }
126  cr->stroke();
127  cr->restore();
128 }
129 
130 
131 /** @class VisualDisplay2D::Shape "visdisplay.h"
132  * Class representing a shape.
133  * All shapes inherit from the class and provide drawing primitives. The
134  * internal object representations are instances of shapes.
135  * @author Tim Niemueller
136  *
137  * @fn VisualDisplay2D::Shape::draw(Cairo::RefPtr<Cairo::Context> &cr)
138  * Draw shape to Cairo context.
139  * This method shall be implemented by a shape to draw itself using the
140  * provided Cairo context.
141  * @param cr reference to Cairo context. Note that this is a reference
142  * bypassing the reference pointer. This is done for efficiency and with
143  * the assumption that this method is only called by VisualDisplay2D::draw()
144  * which itself has proper refptr handling.
145  *
146  * @fn inline void VisualDisplay2D::Shape::apply_style(Cairo::RefPtr<Cairo::Context> &cr)
147  * Set style on context.
148  * This method sets the style determined by the shape to the Cairo context.
149  * @param cr reference to Cairo context. Note that this is a reference
150  * bypassing the reference pointer. This is done for efficiency and with
151  * the assumption that this method is only called by VisualDisplay2D::draw()
152  * which itself has proper refptr handling.
153  *
154  * @fn inline unsigned int VisualDisplay2D::Shape::id()
155  * Get shape ID.
156  * @return shape ID
157  *
158  * @fn inline unsigned int VisualDisplay2D::Shape::owner()
159  * Get owner ID.
160  * @return owner ID
161  *
162  * @fn inline void VisualDisplay2D::Shape::color(float &r, float &g, float &b, float &a)
163  * Get shape color.
164  * @param r upon return contains red part of RGBA color
165  * @param g upon return contains green part of RGBA color
166  * @param b upon return contains blue part of RGBA color
167  * @param a upon return contains alpha part of RGBA color
168  */
169 
170 /** Constructor.
171  * @param id object ID
172  * @param owner ID of the owner of the object
173  * @param line_style drawing style of lines of shapes
174  * @param r red part of RGBA color
175  * @param g green part of RGBA color
176  * @param b blue part of RGBA color
177  * @param a alpha part of RGBA color
178  */
179 VisualDisplay2D::Shape::Shape(unsigned int id, unsigned int owner,
181  unsigned char r, unsigned char g,
182  unsigned char b, unsigned char a)
183 {
184  _id = id;
185  _owner = owner;
186  _line_style = line_style;
187  _color_r = r / 255.f;
188  _color_g = g / 255.f;
189  _color_b = b / 255.f;
190  _color_a = a / 255.f;
191 }
192 
193 
194 /** Virtual empty destructor. */
196 {
197 }
198 
199 
200 /** @class VisualDisplay2D::Line "visdisplay.h"
201  * Class representing a line.
202  * Line represented by two end points in cartesian coordinates.
203  * @author Tim Niemueller
204  */
205 
206 /** Constructor.
207  * @param x1 X coordinate of first point
208  * @param y1 Y coordinate of first point
209  * @param x2 X coordinate of second point
210  * @param y2 Y coordinate of second point
211  * @param id object ID
212  * @param owner ID of the owner of the object
213  * @param line_style drawing style of lines of shapes
214  * @param r red part of RGBA color
215  * @param g green part of RGBA color
216  * @param b blue part of RGBA color
217  * @param a alpha part of RGBA color
218  */
219 VisualDisplay2D::Line::Line(float x1, float y1, float x2, float y2,
220  unsigned int id, unsigned int owner,
222  unsigned char r, unsigned char g,
223  unsigned char b, unsigned char a)
224  : Shape(id, owner, line_style, r, g, b, a)
225 {
226  __x1 = x1;
227  __y1 = y1;
228  __x2 = x2;
229  __y2 = y2;
230 }
231 
232 
233 void
234 VisualDisplay2D::Line::draw(Cairo::RefPtr<Cairo::Context> &cr)
235 {
236  cr->move_to(__x1, __y1);
237  cr->line_to(__x2, __y2);
238  cr->stroke();
239 }
240 
241 
242 
243 /** @class VisualDisplay2D::Rectangle "visdisplay.h"
244  * Class representing a rectangle.
245  * Rectangle represented the cartesian coordinates of the lower right corner
246  * and its width and height.
247  * @author Tim Niemueller
248  */
249 
250 /** Constructor.
251  * @param x X coordinate of lower right point
252  * @param y Y coordinate of lower right point
253  * @param width width of rectangle
254  * @param height height of rectangle
255  * @param id object ID
256  * @param owner ID of the owner of the object
257  * @param line_style drawing style of lines of shapes
258  * @param r red part of RGBA color
259  * @param g green part of RGBA color
260  * @param b blue part of RGBA color
261  * @param a alpha part of RGBA color
262  */
263 VisualDisplay2D::Rectangle::Rectangle(float x, float y, float width, float height,
264  unsigned int id, unsigned int owner,
266  unsigned char r, unsigned char g,
267  unsigned char b, unsigned char a)
268  : Shape(id, owner, line_style, r, g, b, a)
269 {
270  __x = x;
271  __y = y;
272  __width = width;
273  __height = height;
274 }
275 
276 
277 void
278 VisualDisplay2D::Rectangle::draw(Cairo::RefPtr<Cairo::Context> &cr)
279 {
280  cr->rectangle(__x, __y, __width, __height);
281 }
282 
283 
284 
285 /** @class VisualDisplay2D::Circle "visdisplay.h"
286  * Class representing a circle
287  * Line represented by its center point and radius.
288  * @author Tim Niemueller
289  */
290 
291 /** Constructor.
292  * @param x X coordinate of center point
293  * @param y Y coordinate of center point
294  * @param radius radius of the circle
295  * @param id object ID
296  * @param owner ID of the owner of the object
297  * @param line_style drawing style of lines of shapes
298  * @param r red part of RGBA color
299  * @param g green part of RGBA color
300  * @param b blue part of RGBA color
301  * @param a alpha part of RGBA color
302  */
303 VisualDisplay2D::Circle::Circle(float x, float y, float radius,
304  unsigned int id, unsigned int owner,
306  unsigned char r, unsigned char g,
307  unsigned char b, unsigned char a)
308  : Shape(id, owner, line_style, r, g, b, a)
309 {
310  __x = x;
311  __y = y;
312  __radius = radius;
313 }
314 
315 
316 void
317 VisualDisplay2D::Circle::draw(Cairo::RefPtr<Cairo::Context> &cr)
318 {
319  cr->arc(__x, __y, __radius, 0, 2*M_PI);
320 }
321 
322 
323 /** @class VisualDisplay2D::Text "visdisplay.h"
324  * Class representing a text object.
325  * Text is represented by a cartesian coordinate, which denotes a specific
326  * point defined by the anchor, the text itself, and a text size.
327  * @author Tim Niemueller
328  */
329 
330 /** Constructor.
331  * @param x X coordinate of anchor point
332  * @param y Y coordinate of anchor point
333  * @param text text to display
334  * @param anchor anchor point relative to the text's bounding box
335  * @param size height of font in meters
336  * @param id object ID
337  * @param owner ID of the owner of the object
338  * @param r red part of RGBA color
339  * @param g green part of RGBA color
340  * @param b blue part of RGBA color
341  * @param a alpha part of RGBA color
342  */
343 VisualDisplay2D::Text::Text(float x, float y, std::string text,
345  float size,
346  unsigned int id, unsigned int owner,
347  unsigned char r, unsigned char g,
348  unsigned char b, unsigned char a)
349  : Shape(id, owner, fawkes::VisualDisplay2DInterface::LS_SOLID, r, g, b, a)
350 {
351  __x = x;
352  __y = y;
353  __text = text;
354  __size = size;
355  __anchor = anchor;
356 }
357 
358 
359 void
360 VisualDisplay2D::Text::draw(Cairo::RefPtr<Cairo::Context> &cr)
361 {
362  cr->save();
363  cr->scale(-1, 1);
364  cr->rotate(-0.5 * M_PI);
365  cr->set_font_size(1.36 * __size);
366 
367  Cairo::TextExtents te;
368  cr->get_text_extents(__text, te);
369 
370  float x = __x, y = __y;
371  switch (__anchor) {
372  case VisualDisplay2DInterface::CENTERED:
373  x = __x - te.width / 2.; y = __y + te.height / 2.; break;
374  case VisualDisplay2DInterface::NORTH:
375  x = __x - te.width / 2.; y = __y + te.height; break;
376  case VisualDisplay2DInterface::EAST:
377  x = __x - te.width; y = __y + te.height / 2.; break;
378  case VisualDisplay2DInterface::SOUTH:
379  x = __x - te.width / 2.; break;
380  case VisualDisplay2DInterface::WEST:
381  y = __y + te.height / 2.; break;
382  case VisualDisplay2DInterface::NORTH_EAST:
383  x = __x - te.width; y = __y + te.height; break;
384  case VisualDisplay2DInterface::SOUTH_EAST:
385  x = __x - te.width; break;
386  case VisualDisplay2DInterface::SOUTH_WEST:
387  break;
388  case VisualDisplay2DInterface::NORTH_WEST:
389  y = __y + te.height; break;
390  }
391 
392  cr->move_to(x, y);
393  cr->show_text(__text);
394  cr->restore();
395 }
AddCartRectMessage Fawkes BlackBoard Interface Message.
LineStyle
Enumeration defining the possible line styles.
void draw(Cairo::RefPtr< Cairo::Context > &cr)
Draw shape to Cairo context.
Definition: visdisplay.cpp:234
void draw(Cairo::RefPtr< Cairo::Context > &cr)
Draw shape to Cairo context.
Definition: visdisplay.cpp:278
unsigned int id() const
Get message ID.
Definition: message.cpp:197
Fawkes library namespace.
void set_interface(fawkes::VisualDisplay2DInterface *interface)
Set interface.
Definition: visdisplay.cpp:57
AddCartCircleMessage Fawkes BlackBoard Interface Message.
Anchor
Enumeration defining the possible anchor points.
~VisualDisplay2D()
Destructor.
Definition: visdisplay.cpp:44
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:219
void process_messages()
Process messages.
Definition: visdisplay.cpp:68
AddCartTextMessage Fawkes BlackBoard Interface Message.
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:179
Class representing a text object.
Definition: visdisplay.h:114
unsigned int owner()
Get owner ID.
Definition: visdisplay.h:55
unsigned int sender_id() const
Get ID of sender.
Definition: message.cpp:345
AddCartLineMessage Fawkes BlackBoard Interface Message.
Class representing a circle Line represented by its center point and radius.
Definition: visdisplay.h:100
void draw(Cairo::RefPtr< Cairo::Context > &cr)
Draw shape to Cairo context.
Definition: visdisplay.cpp:360
Class representing a shape.
Definition: visdisplay.h:43
Text(float x, float y, 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:343
Class representing a rectangle.
Definition: visdisplay.h:85
Class representing a line.
Definition: visdisplay.h:70
void draw(Cairo::RefPtr< Cairo::Context > cr)
Draw objects.
Definition: visdisplay.cpp:117
DeleteAllMessage Fawkes BlackBoard Interface Message.
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:263
virtual ~Shape()
Virtual empty destructor.
Definition: visdisplay.cpp:195
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:303
void draw(Cairo::RefPtr< Cairo::Context > &cr)
Draw shape to Cairo context.
Definition: visdisplay.cpp:317
VisualDisplay2DInterface Fawkes BlackBoard Interface.
VisualDisplay2D()
Constructor.
Definition: visdisplay.cpp:38