Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drawing_manipulator.cpp
1 
2 /***************************************************************************
3  * drawing_manipulator.cpp - Manipulates things like point size, line
4  * width, etc.
5  *
6  * Created: Fri Oct 10 18:20:02 2008
7  * Copyright 2008 Daniel Beck
8  *
9  ****************************************************************************/
10 
11 /* This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version. A runtime exception applies to
15  * this software (see LICENSE.GPL_WRE file mentioned below for details).
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Library General Public License for more details.
21  *
22  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
23  */
24 
25 #include <geometry/gtk/drawing_manipulator.h>
26 
27 /** @class fawkes::DrawingManipulator <geometry/gtk/drawing_manipulator.h>
28  * Allows to control some aspects of the rendering of objects.
29  * @author Daniel Beck
30  */
31 
32 /** @enum fawkes::DrawingManipulator::Color
33  * Some pre-defined colors
34  */
35 
36 namespace fawkes {
37 
38 /** Constructor. */
40 {
41  m_line_width = -1.0;
42  m_color_r = -1.0;
43  m_color_g = -1.0;
44  m_color_b = -1.0;
45 
46  m_line_width_set = false;
47  m_point_size_set = false;
48  m_color_set = false;
49 }
50 
51 /** Desctructor. */
53 {
54 }
55 
56 /** Integrates the parameters of another manipulator.
57  * If a certain field in this manipulator is not set it is assigned
58  * the respective value from the specified manipualator.
59  * @param m the manipulator to integrate
60  */
61 void
63 {
64  if ( !m_line_width_set && m->m_line_width_set )
65  { m_line_width = m->m_line_width; }
66 
67  if ( !m_point_size_set && m->m_point_size_set)
68  { m_point_size = m->m_point_size; }
69 
70  if ( !m_color_set && m->m_color_set)
71  {
72  m_color_r = m->m_color_r;
73  m_color_g = m->m_color_g;
74  m_color_b = m->m_color_b;
75  }
76 }
77 
78 /** Set the line width.
79  * @param w the line width
80  */
81 void
83 {
84  if (0 > w)
85  { return; }
86 
87  m_line_width = w;
88  m_line_width_set = true;
89 }
90 
91 /** Get the line width.
92  * @return the line width
93  */
94 float
96 {
97  return m_line_width;
98 }
99 
100 /** Set the point size.
101  * @param s the point size
102  */
103 void
105 {
106  if (0 > s)
107  { return; }
108 
109  m_point_size = s;
110  m_point_size_set = true;
111 }
112 
113 /** Get the point size.
114  * @return the point size
115  */
116 float
118 {
119  return m_point_size;
120 }
121 
122 /** Set the color.
123  * @param c the color
124  */
125 void
127 {
128  switch (c)
129  {
130  case BLACK:
131  m_color_r = 0.0;
132  m_color_g = 0.0;
133  m_color_b = 0.0;
134  m_color_set = true;
135  break;
136 
137  case WHITE:
138  m_color_r = 1.0;
139  m_color_g = 1.0;
140  m_color_b = 1.0;
141  m_color_set = true;
142  break;
143 
144  case RED:
145  m_color_r = 1.0;
146  m_color_g = 0.0;
147  m_color_b = 0.0;
148  m_color_set = true;
149  break;
150 
151  case GREEN:
152  m_color_r = 0.0;
153  m_color_g = 1.0;
154  m_color_b = 0.0;
155  m_color_set = true;
156  break;
157 
158  case BLUE:
159  m_color_r = 0.0;
160  m_color_g = 0.0;
161  m_color_b = 1.0;
162  m_color_set = true;
163  break;
164 
165  default:
166  break;
167  }
168 }
169 
170 /** Set the color specified in RGB.
171  * @param r the R value of the color
172  * @param g the G value of the color
173  * @param b the B value of the color
174  */
175 void
176 DrawingManipulator::set_color(float r, float g, float b)
177 {
178  if ( 0 > r || 1 < r || 0 > g || 1 < g || 0 > b || 1 < b )
179  { return; }
180 
181  m_color_r = r;
182  m_color_g = g;
183  m_color_b = b;
184 
185  m_color_set = true;
186 }
187 
188 /** Get the color.
189  * @param r reference to a variable where the R value of the current color is written to
190  * @param g reference to a variable where the G value of the current color is written to
191  * @param b reference to a variable where the B value of the current color is written to
192  */
193 void
194 DrawingManipulator::get_color(float& r, float& g, float& b) const
195 {
196  r = m_color_r;
197  g = m_color_g;
198  b = m_color_b;
199 }
200 
201 void
202 DrawingManipulator::draw(Cairo::RefPtr<Cairo::Context>& context)
203 {
204  if (m_line_width_set)
205  { context->set_line_width(m_line_width); }
206 
207  if (m_color_set)
208  { context->set_source_rgb(m_color_r, m_color_g, m_color_b); }
209 }
210 
211 /** Creates a drawing manipulator which sets the given line width.
212  * @param w the line width
213  * @return pointer to the newly created drawing manipulator
214  */
216 {
218  m->set_line_width(w);
219  return m;
220 }
221 
222 /** Creates a drawing manipulator which sets the given color.
223  * @param r the R value of the color
224  * @param g the G value of the color
225  * @param b the B value of the color
226  * @return pointer to the newly created drawing manipulator
227  */
228 DrawingManipulator* set_color(float r, float g, float b)
229 {
231  m->set_color(r, g, b);
232  return m;
233 }
234 
235 /** Creates a drawing manipulator which sets the given color.
236  * @param c one of the colors defined in the DrawingManipulator class
237  * @return pointer to the newly created drawing manipulator
238  */
240 {
242  m->set_color(c);
243  return m;
244 }
245 
246 /** Creates a drawing manipulator which sets the given point size.
247  * @param s the point size
248  * @return pointer to the newly created drawing manipulator
249  */
251 {
253  m->set_point_size(s);
254  return m;
255 }
256 
257 } // end namespace fawkes