visdisplay.cpp

00001 
00002 /***************************************************************************
00003  *  visdisplay.cpp - Visual Display to show VisualDisplay2DInterface objects
00004  *
00005  *  Created: Thu Jan 07 23:48:49 2010
00006  *  Copyright  2008-2010  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022 
00023 #include "visdisplay.h"
00024 
00025 #include <interfaces/VisualDisplay2DInterface.h>
00026 
00027 using namespace fawkes;
00028 
00029 
00030 /** @class VisualDisplay2D "visdisplay.h"
00031  * 2D visualization processor for VisualDisplay2DInterface.
00032  * This class processes messages from the VisualDisplay2DInterface and
00033  * issues appropriate drawing commands to a Cairo drawing context.
00034  * @author Tim Niemueller
00035  */
00036 
00037 /** Constructor. */
00038 VisualDisplay2D::VisualDisplay2D()
00039 {
00040   __interface = NULL;
00041 }
00042 
00043 /** Destructor. */
00044 VisualDisplay2D::~VisualDisplay2D()
00045 {
00046   for (__sit = __shapes.begin(); __sit != __shapes.end(); ++__sit) {
00047     delete __sit->second;
00048   }
00049   __shapes.clear();
00050 }
00051 
00052 
00053 /** Set interface.
00054  * @param interface interface to query for messages
00055  */
00056 void
00057 VisualDisplay2D::set_interface(fawkes::VisualDisplay2DInterface *interface)
00058 {
00059   __interface = interface;
00060 }
00061 
00062 
00063 /** Process messages.
00064  * This processes the messages and builds up the internal object
00065  * representations.
00066  */
00067 void
00068 VisualDisplay2D::process_messages()
00069 {
00070   while (! __interface->msgq_empty()) {
00071     if ( __interface->msgq_first_is<VisualDisplay2DInterface::AddCartLineMessage>() ) {
00072       VisualDisplay2DInterface::AddCartLineMessage *m = __interface->msgq_first<VisualDisplay2DInterface::AddCartLineMessage>();
00073       __shapes[m->id()] = new Line(m->x(0), m->y(0), m->x(1), m->y(1),
00074                                    m->id(), m->sender_id(),
00075                                    m->style(), m->color(0),
00076                                    m->color(1), m->color(2), m->color(3));
00077 
00078     } else if ( __interface->msgq_first_is<VisualDisplay2DInterface::AddCartRectMessage>() ) {
00079       VisualDisplay2DInterface::AddCartRectMessage *m = __interface->msgq_first<VisualDisplay2DInterface::AddCartRectMessage>();
00080       __shapes[m->id()] = new Rectangle(m->x(), m->y(), m->width(), m->height(),
00081                                         m->id(), m->sender_id(),
00082                                         m->style(), m->color(0),
00083                                         m->color(1), m->color(2), m->color(3));
00084 
00085     } else if ( __interface->msgq_first_is<VisualDisplay2DInterface::AddCartCircleMessage>() ) {
00086       VisualDisplay2DInterface::AddCartCircleMessage *m = __interface->msgq_first<VisualDisplay2DInterface::AddCartCircleMessage>();
00087       __shapes[m->id()] = new Circle(m->x(), m->y(), m->radius(),
00088                                      m->id(), m->sender_id(),
00089                                      m->style(), m->color(0),
00090                                      m->color(1), m->color(2), m->color(3));
00091 
00092     } else if ( __interface->msgq_first_is<VisualDisplay2DInterface::AddCartTextMessage>() ) {
00093       VisualDisplay2DInterface::AddCartTextMessage *m = __interface->msgq_first<VisualDisplay2DInterface::AddCartTextMessage>();
00094       __shapes[m->id()] = new Text(m->x(), m->y(), m->text(),
00095                                    m->anchor(), m->size(),
00096                                    m->id(), m->sender_id(),
00097                                    m->color(0),
00098                                    m->color(1), m->color(2), m->color(3));
00099 
00100     } else if (__interface->msgq_first_is<VisualDisplay2DInterface::DeleteAllMessage>() ) {
00101       for (__sit = __shapes.begin(); __sit != __shapes.end(); ++__sit) {
00102         delete __sit->second;
00103       }
00104       __shapes.clear();
00105     }
00106 
00107     __interface->msgq_pop();
00108   }
00109 }
00110 
00111 
00112 /** Draw objects.
00113  * This draws all objects currently enqueued by process_messages().
00114  * @param cr Cairo context to draw to
00115  */
00116 void
00117 VisualDisplay2D::draw(Cairo::RefPtr<Cairo::Context> cr)
00118 {
00119   cr->save();
00120   for (__sit = __shapes.begin(); __sit != __shapes.end(); ++__sit) {
00121     float r, g, b, a;
00122     __sit->second->color(r, g, b, a);
00123     __sit->second->apply_style(cr);
00124     __sit->second->draw(cr);
00125   }
00126   cr->stroke();
00127   cr->restore();
00128 }
00129 
00130 
00131 /** @class VisualDisplay2D::Shape "visdisplay.h"
00132  * Class representing a shape.
00133  * All shapes inherit from the class and provide drawing primitives. The
00134  * internal object representations are instances of shapes.
00135  * @author Tim Niemueller
00136  *
00137  * @fn VisualDisplay2D::Shape::draw(Cairo::RefPtr<Cairo::Context> &cr)
00138  * Draw shape to Cairo context.
00139  * This method shall be implemented by a shape to draw itself using the
00140  * provided Cairo context.
00141  * @param cr reference to Cairo context. Note that this is a reference
00142  * bypassing the reference pointer. This is done for efficiency and with
00143  * the assumption that this method is only called by VisualDisplay2D::draw()
00144  * which itself has proper refptr handling.
00145  *
00146  * @fn inline void VisualDisplay2D::Shape::apply_style(Cairo::RefPtr<Cairo::Context> &cr)
00147  * Set style on context.
00148  * This method sets the style determined by the shape to the Cairo context.
00149  * @param cr reference to Cairo context. Note that this is a reference
00150  * bypassing the reference pointer. This is done for efficiency and with
00151  * the assumption that this method is only called by VisualDisplay2D::draw()
00152  * which itself has proper refptr handling.
00153  *
00154  * @fn inline unsigned int VisualDisplay2D::Shape::id()
00155  * Get shape ID.
00156  * @return shape ID
00157  *
00158  * @fn inline unsigned int VisualDisplay2D::Shape::owner()
00159  * Get owner ID.
00160  * @return owner ID
00161  *
00162  * @fn inline void VisualDisplay2D::Shape::color(float &r, float &g, float &b, float &a)
00163  * Get shape color.
00164  * @param r upon return contains red part of RGBA color
00165  * @param g upon return contains green part of RGBA color
00166  * @param b upon return contains blue part of RGBA color
00167  * @param a upon return contains alpha part of RGBA color
00168  */
00169 
00170 /** Constructor.
00171  * @param id object ID
00172  * @param owner ID of the owner of the object
00173  * @param line_style drawing style of lines of shapes
00174  * @param r red part of RGBA color
00175  * @param g green part of RGBA color
00176  * @param b blue part of RGBA color
00177  * @param a alpha part of RGBA color
00178  */
00179 VisualDisplay2D::Shape::Shape(unsigned int id, unsigned int owner,
00180                               VisualDisplay2DInterface::LineStyle line_style,
00181                               unsigned char r, unsigned char g,
00182                               unsigned char b, unsigned char a)
00183 {
00184   _id         = id;
00185   _owner      = owner;
00186   _line_style = line_style;
00187   _color_r    = r / 255.f;
00188   _color_g    = g / 255.f;
00189   _color_b    = b / 255.f;
00190   _color_a    = a / 255.f;
00191 }
00192 
00193 
00194 /** Virtual empty destructor. */
00195 VisualDisplay2D::Shape::~Shape()
00196 {
00197 }
00198 
00199 
00200 /** @class VisualDisplay2D::Line "visdisplay.h"
00201  * Class representing a line.
00202  * Line represented by two end points in cartesian coordinates.
00203  * @author Tim Niemueller
00204  */
00205 
00206 /** Constructor.
00207  * @param x1 X coordinate of first point
00208  * @param y1 Y coordinate of first point
00209  * @param x2 X coordinate of second point
00210  * @param y2 Y coordinate of second point
00211  * @param id object ID
00212  * @param owner ID of the owner of the object
00213  * @param line_style drawing style of lines of shapes
00214  * @param r red part of RGBA color
00215  * @param g green part of RGBA color
00216  * @param b blue part of RGBA color
00217  * @param a alpha part of RGBA color
00218  */
00219 VisualDisplay2D::Line::Line(float x1, float y1, float x2, float y2,
00220                             unsigned int id, unsigned int owner,
00221                             VisualDisplay2DInterface::LineStyle line_style,
00222                             unsigned char r, unsigned char g,
00223                             unsigned char b, unsigned char a)
00224   : Shape(id, owner, line_style, r, g, b, a)
00225 {
00226   __x1 = x1;
00227   __y1 = y1;
00228   __x2 = x2;
00229   __y2 = y2;
00230 }
00231 
00232 
00233 void
00234 VisualDisplay2D::Line::draw(Cairo::RefPtr<Cairo::Context> &cr)
00235 {
00236   cr->move_to(__x1, __y1);
00237   cr->line_to(__x2, __y2);
00238   cr->stroke();
00239 }
00240 
00241 
00242 
00243 /** @class VisualDisplay2D::Rectangle "visdisplay.h"
00244  * Class representing a rectangle.
00245  * Rectangle represented the cartesian coordinates of the lower right corner
00246  * and its width and height.
00247  * @author Tim Niemueller
00248  */
00249 
00250 /** Constructor.
00251  * @param x X coordinate of lower right point
00252  * @param y Y coordinate of lower right  point
00253  * @param width width of rectangle
00254  * @param height height of rectangle
00255  * @param id object ID
00256  * @param owner ID of the owner of the object
00257  * @param line_style drawing style of lines of shapes
00258  * @param r red part of RGBA color
00259  * @param g green part of RGBA color
00260  * @param b blue part of RGBA color
00261  * @param a alpha part of RGBA color
00262  */
00263 VisualDisplay2D::Rectangle::Rectangle(float x, float y, float width, float height,
00264                                       unsigned int id, unsigned int owner,
00265                                       VisualDisplay2DInterface::LineStyle line_style,
00266                                       unsigned char r, unsigned char g,
00267                                       unsigned char b, unsigned char a)
00268   : Shape(id, owner, line_style, r, g, b, a)
00269 {
00270   __x      = x;
00271   __y      = y;
00272   __width  = width;
00273   __height = height;
00274 }
00275 
00276 
00277 void
00278 VisualDisplay2D::Rectangle::draw(Cairo::RefPtr<Cairo::Context> &cr)
00279 {
00280   cr->rectangle(__x, __y, __width, __height);
00281 }
00282 
00283 
00284 
00285 /** @class VisualDisplay2D::Circle "visdisplay.h"
00286  * Class representing a circle
00287  * Line represented by its center point and radius.
00288  * @author Tim Niemueller
00289  */
00290 
00291 /** Constructor.
00292  * @param x X coordinate of center point
00293  * @param y Y coordinate of center point
00294  * @param radius radius of the circle
00295  * @param id object ID
00296  * @param owner ID of the owner of the object
00297  * @param line_style drawing style of lines of shapes
00298  * @param r red part of RGBA color
00299  * @param g green part of RGBA color
00300  * @param b blue part of RGBA color
00301  * @param a alpha part of RGBA color
00302  */
00303 VisualDisplay2D::Circle::Circle(float x, float y, float radius,
00304                                 unsigned int id, unsigned int owner,
00305                                 VisualDisplay2DInterface::LineStyle line_style,
00306                                 unsigned char r, unsigned char g,
00307                                 unsigned char b, unsigned char a)
00308   : Shape(id, owner, line_style, r, g, b, a)
00309 {
00310   __x      = x;
00311   __y      = y;
00312   __radius = radius;
00313 }
00314 
00315 
00316 void
00317 VisualDisplay2D::Circle::draw(Cairo::RefPtr<Cairo::Context> &cr)
00318 {
00319   cr->arc(__x, __y, __radius, 0, 2*M_PI);
00320 }
00321 
00322 
00323 /** @class VisualDisplay2D::Text "visdisplay.h"
00324  * Class representing a text object.
00325  * Text is represented by a cartesian coordinate, which denotes a specific
00326  * point defined by the anchor, the text itself, and a text size.
00327  * @author Tim Niemueller
00328  */
00329 
00330 /** Constructor.
00331  * @param x X coordinate of anchor point
00332  * @param y Y coordinate of anchor point
00333  * @param text text to display
00334  * @param anchor anchor point relative to the text's bounding box
00335  * @param size height of font in meters
00336  * @param id object ID
00337  * @param owner ID of the owner of the object
00338  * @param r red part of RGBA color
00339  * @param g green part of RGBA color
00340  * @param b blue part of RGBA color
00341  * @param a alpha part of RGBA color
00342  */
00343 VisualDisplay2D::Text::Text(float x, float y, std::string text,
00344                             fawkes::VisualDisplay2DInterface::Anchor anchor,
00345                             float size,
00346                             unsigned int id, unsigned int owner,
00347                             unsigned char r, unsigned char g,
00348                             unsigned char b, unsigned char a)
00349   : Shape(id, owner, fawkes::VisualDisplay2DInterface::LS_SOLID, r, g, b, a)
00350 {
00351   __x      = x;
00352   __y      = y;
00353   __text   = text;
00354   __size   = size;
00355   __anchor = anchor;
00356 }
00357 
00358 
00359 void
00360 VisualDisplay2D::Text::draw(Cairo::RefPtr<Cairo::Context> &cr)
00361 {
00362   cr->save();
00363   cr->scale(-1, 1);
00364   cr->rotate(-0.5 * M_PI);
00365   cr->set_font_size(1.36 * __size);
00366 
00367   Cairo::TextExtents te;
00368   cr->get_text_extents(__text, te);
00369 
00370   float x = __x, y = __y;
00371   switch (__anchor) {
00372   case VisualDisplay2DInterface::CENTERED:
00373     x = __x - te.width / 2.;  y = __y + te.height / 2.; break;
00374   case VisualDisplay2DInterface::NORTH:
00375     x = __x - te.width / 2.;  y = __y + te.height; break;
00376   case VisualDisplay2DInterface::EAST:
00377     x = __x - te.width; y = __y + te.height / 2.; break;
00378   case VisualDisplay2DInterface::SOUTH:
00379     x = __x - te.width / 2.; break;
00380   case VisualDisplay2DInterface::WEST:
00381     y = __y + te.height / 2.; break;
00382   case VisualDisplay2DInterface::NORTH_EAST:
00383     x = __x - te.width;  y = __y + te.height; break;
00384   case VisualDisplay2DInterface::SOUTH_EAST:
00385     x = __x - te.width; break;
00386   case VisualDisplay2DInterface::SOUTH_WEST:
00387     break;
00388   case VisualDisplay2DInterface::NORTH_WEST:
00389     y = __y + te.height; break;
00390   }
00391 
00392   cr->move_to(x, y);
00393   cr->show_text(__text);
00394   cr->restore();
00395 }

Generated on Tue Feb 22 13:32:32 2011 for Fawkes API by  doxygen 1.4.7