Fawkes API Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * drawing_manipulator.cpp - Manipulates things like point size, line 00004 * width, etc. 00005 * 00006 * Created: Fri Oct 10 18:20:02 2008 00007 * Copyright 2008 Daniel Beck 00008 * 00009 ****************************************************************************/ 00010 00011 /* This program is free software; you can redistribute it and/or modify 00012 * it under the terms of the GNU General Public License as published by 00013 * the Free Software Foundation; either version 2 of the License, or 00014 * (at your option) any later version. A runtime exception applies to 00015 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00016 * 00017 * This program is distributed in the hope that it will be useful, 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 * GNU Library General Public License for more details. 00021 * 00022 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00023 */ 00024 00025 #include <geometry/gtk/drawing_manipulator.h> 00026 00027 /** @class fawkes::DrawingManipulator <geometry/gtk/drawing_manipulator.h> 00028 * Allows to control some aspects of the rendering of objects. 00029 * @author Daniel Beck 00030 */ 00031 00032 /** @enum fawkes::DrawingManipulator::Color 00033 * Some pre-defined colors 00034 */ 00035 00036 namespace fawkes { 00037 00038 /** Constructor. */ 00039 DrawingManipulator::DrawingManipulator() 00040 { 00041 m_line_width = -1.0; 00042 m_color_r = -1.0; 00043 m_color_g = -1.0; 00044 m_color_b = -1.0; 00045 00046 m_line_width_set = false; 00047 m_point_size_set = false; 00048 m_color_set = false; 00049 } 00050 00051 /** Desctructor. */ 00052 DrawingManipulator::~DrawingManipulator() 00053 { 00054 } 00055 00056 /** Integrates the parameters of another manipulator. 00057 * If a certain field in this manipulator is not set it is assigned 00058 * the respective value from the specified manipualator. 00059 * @param m the manipulator to integrate 00060 */ 00061 void 00062 DrawingManipulator::integrate(const DrawingManipulator* m) 00063 { 00064 if ( !m_line_width_set && m->m_line_width_set ) 00065 { m_line_width = m->m_line_width; } 00066 00067 if ( !m_point_size_set && m->m_point_size_set) 00068 { m_point_size = m->m_point_size; } 00069 00070 if ( !m_color_set && m->m_color_set) 00071 { 00072 m_color_r = m->m_color_r; 00073 m_color_g = m->m_color_g; 00074 m_color_b = m->m_color_b; 00075 } 00076 } 00077 00078 /** Set the line width. 00079 * @param w the line width 00080 */ 00081 void 00082 DrawingManipulator::set_line_width(float w) 00083 { 00084 if (0 > w) 00085 { return; } 00086 00087 m_line_width = w; 00088 m_line_width_set = true; 00089 } 00090 00091 /** Get the line width. 00092 * @return the line width 00093 */ 00094 float 00095 DrawingManipulator::get_line_width() const 00096 { 00097 return m_line_width; 00098 } 00099 00100 /** Set the point size. 00101 * @param s the point size 00102 */ 00103 void 00104 DrawingManipulator::set_point_size(float s) 00105 { 00106 if (0 > s) 00107 { return; } 00108 00109 m_point_size = s; 00110 m_point_size_set = true; 00111 } 00112 00113 /** Get the point size. 00114 * @return the point size 00115 */ 00116 float 00117 DrawingManipulator::get_point_size() const 00118 { 00119 return m_point_size; 00120 } 00121 00122 /** Set the color. 00123 * @param c the color 00124 */ 00125 void 00126 DrawingManipulator::set_color(Color c) 00127 { 00128 switch (c) 00129 { 00130 case BLACK: 00131 m_color_r = 0.0; 00132 m_color_g = 0.0; 00133 m_color_b = 0.0; 00134 m_color_set = true; 00135 break; 00136 00137 case WHITE: 00138 m_color_r = 1.0; 00139 m_color_g = 1.0; 00140 m_color_b = 1.0; 00141 m_color_set = true; 00142 break; 00143 00144 case RED: 00145 m_color_r = 1.0; 00146 m_color_g = 0.0; 00147 m_color_b = 0.0; 00148 m_color_set = true; 00149 break; 00150 00151 case GREEN: 00152 m_color_r = 0.0; 00153 m_color_g = 1.0; 00154 m_color_b = 0.0; 00155 m_color_set = true; 00156 break; 00157 00158 case BLUE: 00159 m_color_r = 0.0; 00160 m_color_g = 0.0; 00161 m_color_b = 1.0; 00162 m_color_set = true; 00163 break; 00164 00165 default: 00166 break; 00167 } 00168 } 00169 00170 /** Set the color specified in RGB. 00171 * @param r the R value of the color 00172 * @param g the G value of the color 00173 * @param b the B value of the color 00174 */ 00175 void 00176 DrawingManipulator::set_color(float r, float g, float b) 00177 { 00178 if ( 0 > r || 1 < r || 0 > g || 1 < g || 0 > b || 1 < b ) 00179 { return; } 00180 00181 m_color_r = r; 00182 m_color_g = g; 00183 m_color_b = b; 00184 00185 m_color_set = true; 00186 } 00187 00188 /** Get the color. 00189 * @param r reference to a variable where the R value of the current color is written to 00190 * @param g reference to a variable where the G value of the current color is written to 00191 * @param b reference to a variable where the B value of the current color is written to 00192 */ 00193 void 00194 DrawingManipulator::get_color(float& r, float& g, float& b) const 00195 { 00196 r = m_color_r; 00197 g = m_color_g; 00198 b = m_color_b; 00199 } 00200 00201 void 00202 DrawingManipulator::draw(Cairo::RefPtr<Cairo::Context>& context) 00203 { 00204 if (m_line_width_set) 00205 { context->set_line_width(m_line_width); } 00206 00207 if (m_color_set) 00208 { context->set_source_rgb(m_color_r, m_color_g, m_color_b); } 00209 } 00210 00211 /** Creates a drawing manipulator which sets the given line width. 00212 * @param w the line width 00213 * @return pointer to the newly created drawing manipulator 00214 */ 00215 DrawingManipulator* set_line_width(float w) 00216 { 00217 DrawingManipulator* m = new DrawingManipulator(); 00218 m->set_line_width(w); 00219 return m; 00220 } 00221 00222 /** Creates a drawing manipulator which sets the given color. 00223 * @param r the R value of the color 00224 * @param g the G value of the color 00225 * @param b the B value of the color 00226 * @return pointer to the newly created drawing manipulator 00227 */ 00228 DrawingManipulator* set_color(float r, float g, float b) 00229 { 00230 DrawingManipulator* m = new DrawingManipulator(); 00231 m->set_color(r, g, b); 00232 return m; 00233 } 00234 00235 /** Creates a drawing manipulator which sets the given color. 00236 * @param c one of the colors defined in the DrawingManipulator class 00237 * @return pointer to the newly created drawing manipulator 00238 */ 00239 DrawingManipulator* set_color(DrawingManipulator::Color c) 00240 { 00241 DrawingManipulator* m = new DrawingManipulator(); 00242 m->set_color(c); 00243 return m; 00244 } 00245 00246 /** Creates a drawing manipulator which sets the given point size. 00247 * @param s the point size 00248 * @return pointer to the newly created drawing manipulator 00249 */ 00250 DrawingManipulator* set_point_size(float s) 00251 { 00252 DrawingManipulator* m = new DrawingManipulator(); 00253 m->set_point_size(s); 00254 return m; 00255 } 00256 00257 } // end namespace fawkes