Added copyright lines for files I've edited this year.
[synfig.git] / synfig-studio / trunk / src / gtkmm / renderer_ducks.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file renderer_ducks.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "renderer_ducks.h"
34 #include "workarea.h"
35 #include "duckmatic.h"
36 #include <ETL/bezier>
37 #include <ETL/misc>
38 #include "widget_color.h"
39 #include <synfig/distance.h>
40 #include "app.h"
41
42 #include "general.h"
43
44 #endif
45
46 /* === U S I N G =========================================================== */
47
48 using namespace std;
49 using namespace etl;
50 using namespace synfig;
51 using namespace studio;
52
53 /* === M A C R O S ========================================================= */
54
55 /* === G L O B A L S ======================================================= */
56
57 /* === P R O C E D U R E S ================================================= */
58
59 /* === M E T H O D S ======================================================= */
60
61 Renderer_Ducks::~Renderer_Ducks()
62 {
63 }
64
65 /*
66 bool
67 Renderer_Ducks::get_enabled_vfunc()const
68 {
69         return get_work_area()->grid_status();
70 }
71 */
72
73 struct ScreenDuck
74 {
75         synfig::Point pos;
76         Gdk::Color color;
77         bool selected;
78         bool hover;
79         Real width;
80
81         ScreenDuck():width(0) { }
82 };
83
84 void
85 Renderer_Ducks::render_vfunc(
86         const Glib::RefPtr<Gdk::Drawable>& drawable,
87         const Gdk::Rectangle& /*expose_area*/
88 )
89 {
90         assert(get_work_area());
91         if(!get_work_area())
92                 return;
93
94         const synfig::Vector focus_point(get_work_area()->get_focus_point());
95
96
97         int drawable_w,drawable_h;
98         drawable->get_size(drawable_w,drawable_h);
99
100         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(drawable));
101
102
103         const synfig::Vector::value_type window_startx(get_work_area()->get_window_tl()[0]);
104         const synfig::Vector::value_type window_starty(get_work_area()->get_window_tl()[1]);
105
106         const float pw(get_pw()),ph(get_ph());
107
108         const std::list<etl::handle<Duckmatic::Bezier> >& bezier_list(get_work_area()->bezier_list());
109         const bool solid_lines(get_work_area()->solid_lines);
110
111         const std::list<handle<Duckmatic::Stroke> >& stroke_list(get_work_area()->stroke_list());
112
113         Glib::RefPtr<Pango::Layout> layout(Pango::Layout::create(get_work_area()->get_pango_context()));
114
115         // Render the strokes
116         for(std::list<handle<Duckmatic::Stroke> >::const_iterator iter=stroke_list.begin();iter!=stroke_list.end();++iter)
117         {
118                 Point window_start(window_startx,window_starty);
119                 vector<Gdk::Point> points;
120                 std::list<synfig::Point>::iterator iter2;
121                 Point holder;
122
123                 for(iter2=(*iter)->stroke_data->begin();iter2!=(*iter)->stroke_data->end();++iter2)
124                 {
125                         holder=*iter2-window_start;
126                         holder[0]/=pw;holder[1]/=ph;
127                         points.push_back(Gdk::Point(round_to_int(holder[0]),round_to_int(holder[1])));
128                 }
129
130                 gc->set_rgb_fg_color(colorconv_synfig2gdk((*iter)->color));
131                 gc->set_function(Gdk::COPY);
132                 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
133
134                 // Draw the stroke
135                 drawable->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points));
136         }
137
138
139
140         // Render the beziers
141         for(std::list<handle<Duckmatic::Bezier> >::const_iterator iter=bezier_list.begin();iter!=bezier_list.end();++iter)
142         {
143                 Point window_start(window_startx,window_starty);
144                 Point p1((*iter)->p1->get_trans_point()-window_start);
145                 Point p2((*iter)->p2->get_trans_point()-window_start);
146                 Point c1((*iter)->c1->get_trans_point()-window_start);
147                 Point c2((*iter)->c2->get_trans_point()-window_start);
148                 p1[0]/=pw;p1[1]/=ph;
149                 p2[0]/=pw;p2[1]/=ph;
150                 c1[0]/=pw;c1[1]/=ph;
151                 c2[0]/=pw;c2[1]/=ph;
152                 bezier<Point> curve(p1,c1,c2,p2);
153                 vector<Gdk::Point> points;
154
155                 float f;
156                 Point pt;
157                 for(f=0;f<1.0;f+=1.0/17.0)
158                 {
159                         pt=curve(f);
160                         points.push_back(Gdk::Point(round_to_int(pt[0]),round_to_int(pt[1])));
161                 }
162                 points.push_back(Gdk::Point(round_to_int(p2[0]),round_to_int(p2[1])));
163
164                 // Draw the curve
165 /*              if(solid_lines)
166                 {
167                         gc->set_rgb_fg_color(DUCK_COLOR_BEZIER_1);
168                         gc->set_function(Gdk::COPY);
169                         gc->set_line_attributes(3,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
170                         drawable->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points));
171                         gc->set_rgb_fg_color(DUCK_COLOR_BEZIER_2);
172                         gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
173                         drawable->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points));
174                 }
175                 else
176 */
177                 {
178 //                      gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
179 //                      gc->set_function(Gdk::INVERT);
180 //                      gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
181 //                      drawable->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points));
182                         gc->set_rgb_fg_color(DUCK_COLOR_BEZIER_1);
183                         gc->set_function(Gdk::COPY);
184                         gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
185                         drawable->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points));
186                         gc->set_rgb_fg_color(DUCK_COLOR_BEZIER_2);
187                         gc->set_line_attributes(1,Gdk::LINE_ON_OFF_DASH,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
188                         drawable->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points));
189
190                 }
191         }
192
193
194         const DuckList duck_list(get_work_area()->get_duck_list());
195         //Gtk::StateType state = Gtk::STATE_ACTIVE;
196         Gtk::ShadowType shadow=Gtk::SHADOW_OUT;
197         std::list<ScreenDuck> screen_duck_list;
198         const float radius((abs(pw)+abs(ph))*4);
199
200         etl::handle<Duck> hover_duck(get_work_area()->find_duck(get_work_area()->get_cursor_pos(),radius, get_work_area()->get_type_mask()));
201
202         // Render the ducks
203         for(std::list<handle<Duck> >::const_iterator iter=duck_list.begin();iter!=duck_list.end();++iter)
204         {
205
206                 // If this type of duck has been masked, then skip it
207                 if((*iter)->get_type() && (!(get_work_area()->get_type_mask() & (*iter)->get_type())))
208                         continue;
209
210 //              Real x,y;
211         //      Gdk::Rectangle area;
212                 Point point((*iter)->get_trans_point());
213                 Point origin((*iter)->get_trans_origin());
214
215                 point[0]=(point[0]-window_startx)/pw;
216                 point[1]=(point[1]-window_starty)/ph;
217
218                 bool has_connect(false);
219                 if((*iter)->get_tangent() || (*iter)->get_type()&Duck::TYPE_ANGLE)
220                 {
221                         has_connect=true;
222                 }
223                 if((*iter)->get_connect_duck())
224                 {
225                         has_connect=true;
226                         origin=(*iter)->get_connect_duck()->get_trans_point();
227                 }
228
229                 origin[0]=(origin[0]-window_startx)/pw;
230                 origin[1]=(origin[1]-window_starty)/ph;
231
232                 if (App::restrict_radius_ducks &&
233                         (*iter)->is_radius())
234                 {
235                         if (point[0] < origin[0]) point[0] = origin[0];
236                         if (point[1] > origin[1]) point[1] = origin[1];
237                 }
238
239                 bool selected(get_work_area()->duck_is_selected(*iter));
240                 bool hover(*iter==hover_duck || (*iter)->get_hover());
241
242                 shadow = selected?Gtk::SHADOW_IN:Gtk::SHADOW_OUT;
243
244                 if(get_work_area()->get_selected_value_node())
245                 {
246                         synfigapp::ValueDesc value_desc((*iter)->get_value_desc());
247                         if(value_desc.is_valid() && value_desc.is_value_node() && get_work_area()->get_selected_value_node()==value_desc.get_value_node())
248                         {
249                                 gc->set_function(Gdk::COPY);
250                                 gc->set_rgb_fg_color(DUCK_COLOR_SELECTED);
251                                 //gc->set_line_attributes(1,Gdk::LINE_ON_OFF_DASH,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
252                                 gc->set_line_attributes(2,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
253
254                                 drawable->draw_rectangle(gc,false,
255                                         round_to_int(point[0]-5),
256                                         round_to_int(point[1]-5),
257                                         10,
258                                         10
259                                 );
260                         }
261
262                 }
263
264                 if((*iter)->get_box_duck())
265                 {
266                         Point boxpoint((*iter)->get_box_duck()->get_trans_point());
267                         boxpoint[0]=(boxpoint[0]-window_startx)/pw;
268                         boxpoint[1]=(boxpoint[1]-window_starty)/ph;
269                         Point tl(min(point[0],boxpoint[0]),min(point[1],boxpoint[1]));
270
271                         gc->set_function(Gdk::COPY);
272                         gc->set_rgb_fg_color(DUCK_COLOR_BOX_1);
273                         gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
274                         drawable->draw_rectangle(gc,false,
275                                 round_to_int(tl[0]),
276                                 round_to_int(tl[1]),
277                                 round_to_int(abs(boxpoint[0]-point[0])),
278                                 round_to_int(abs(boxpoint[1]-point[1]))
279                         );
280                         gc->set_function(Gdk::COPY);
281                         gc->set_rgb_fg_color(DUCK_COLOR_BOX_2);
282                         gc->set_line_attributes(1,Gdk::LINE_ON_OFF_DASH,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
283                         drawable->draw_rectangle(gc,false,
284                                 round_to_int(tl[0]),
285                                 round_to_int(tl[1]),
286                                 round_to_int(abs(boxpoint[0]-point[0])),
287                                 round_to_int(abs(boxpoint[1]-point[1]))
288                         );
289                 }
290
291                 ScreenDuck screen_duck;
292                 screen_duck.pos=point;
293                 screen_duck.selected=selected;
294                 screen_duck.hover=hover;
295
296                 if(!(*iter)->get_editable())
297                         screen_duck.color=(DUCK_COLOR_NOT_EDITABLE);
298                 else if((*iter)->get_tangent())
299                         screen_duck.color=((*iter)->get_scalar()<0 ? DUCK_COLOR_TANGENT_1 : DUCK_COLOR_TANGENT_2);
300                 else if((*iter)->get_type()&Duck::TYPE_VERTEX)
301                         screen_duck.color=DUCK_COLOR_VERTEX;
302                 else if((*iter)->get_type()&Duck::TYPE_RADIUS)
303                         screen_duck.color=DUCK_COLOR_RADIUS;
304                 else if((*iter)->get_type()&Duck::TYPE_WIDTH)
305                         screen_duck.color=DUCK_COLOR_WIDTH;
306                 else if((*iter)->get_type()&Duck::TYPE_ANGLE)
307                         screen_duck.color=(DUCK_COLOR_ANGLE);
308                 else
309                         screen_duck.color=DUCK_COLOR_OTHER;
310
311                 screen_duck_list.push_front(screen_duck);
312
313                 if(has_connect)
314                 {
315                         if(solid_lines)
316                         {
317                                 gc->set_line_attributes(3,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
318                                 gc->set_rgb_fg_color(Gdk::Color("#000000"));
319                                 gc->set_function(Gdk::COPY);
320                                 drawable->draw_line(gc, (int)origin[0],(int)origin[1],(int)(point[0]),(int)(point[1]));
321                                 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
322                                 gc->set_rgb_fg_color(Gdk::Color("#9fefef"));
323                                 drawable->draw_line(gc, (int)origin[0],(int)origin[1],(int)(point[0]),(int)(point[1]));
324                         }
325                         else
326                         {
327 //                              gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
328 //                              gc->set_function(Gdk::INVERT);
329 //                              drawable->draw_line(gc, (int)origin[0],(int)origin[1],(int)(point[0]),(int)(point[1]));
330                                 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
331                                 gc->set_rgb_fg_color(Gdk::Color("#000000"));
332                                 gc->set_function(Gdk::COPY);
333                                 drawable->draw_line(gc, (int)origin[0],(int)origin[1],(int)(point[0]),(int)(point[1]));
334                                 gc->set_line_attributes(1,Gdk::LINE_ON_OFF_DASH,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
335                                 gc->set_rgb_fg_color(Gdk::Color("#9fefef"));
336                                 drawable->draw_line(gc, (int)origin[0],(int)origin[1],(int)(point[0]),(int)(point[1]));
337                         }
338                 }
339
340                 if((*iter)->is_radius())
341                 {
342                         const Real mag((point-origin).mag());
343                         const int d(round_to_int(mag*2));
344                         const int x(round_to_int(origin[0]-mag));
345                         const int y(round_to_int(origin[1]-mag));
346
347                         if(solid_lines)
348                         {
349                                 gc->set_rgb_fg_color(Gdk::Color("#000000"));
350                                 gc->set_function(Gdk::COPY);
351                                 gc->set_line_attributes(3,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
352                                 drawable->draw_arc(
353                                         gc,
354                                         false,
355                                         x,
356                                         y,
357                                         d,
358                                         d,
359                                         0,
360                                         360*64
361                                 );
362                                 gc->set_rgb_fg_color(Gdk::Color("#afafaf"));
363                         }
364                         else
365                         {
366                                 gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
367                                 gc->set_function(Gdk::INVERT);
368                         }
369                         gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
370
371                         drawable->draw_arc(
372                                 gc,
373                                 false,
374                                 x,
375                                 y,
376                                 d,
377                                 d,
378                                 0,
379                                 360*64
380                         );
381
382                         if(hover)
383                         {
384                                 Real mag;
385                                 if (App::restrict_radius_ducks)
386                                 {
387                                         Point point((*iter)->get_trans_point());
388                                         Point origin((*iter)->get_trans_origin());
389
390                                         if ((point[0] - origin[0]) * pw < 0) point[0] = origin[0];
391                                         if ((point[1] - origin[1]) * ph > 0) point[1] = origin[1];
392
393                                         mag = (point-origin).mag();
394                                 }
395                                 else
396                                         mag = ((*iter)->get_trans_point()-(*iter)->get_trans_origin()).mag();
397
398                                 Distance real_mag(mag, Distance::SYSTEM_UNITS);
399                                 real_mag.convert(App::distance_system,get_work_area()->get_rend_desc());
400                                 layout->set_text(real_mag.get_string());
401
402                                 gc->set_rgb_fg_color(Gdk::Color("#000000"));
403                                 drawable->draw_layout(
404                                         gc,
405                                         round_to_int(point[0])+1+6,
406                                         round_to_int(point[1])+1-8,
407                                         layout
408                                 );
409                                 gc->set_rgb_fg_color(Gdk::Color("#FF00FF"));
410                                 drawable->draw_layout(
411                                         gc,
412                                         round_to_int(point[0])+6,
413                                         round_to_int(point[1])-8,
414                                         layout
415                                 );
416                         }
417
418                 }
419
420         }
421
422
423         for(;screen_duck_list.size();screen_duck_list.pop_front())
424         {
425                 int radius=4;
426                 int outline=1;
427                 Gdk::Color color(screen_duck_list.front().color);
428
429                 if(!screen_duck_list.front().selected)
430                 {
431                         color.set_red(color.get_red()*2/3);
432                         color.set_green(color.get_green()*2/3);
433                         color.set_blue(color.get_blue()*2/3);
434                 }
435
436                 if(screen_duck_list.front().hover && !screen_duck_list.back().hover && screen_duck_list.size()>1)
437                 {
438                         screen_duck_list.push_back(screen_duck_list.front());
439                         continue;
440                 }
441
442                 if(screen_duck_list.front().hover)
443                 {
444                         radius+=2;
445                         outline++;
446                 }
447
448                 gc->set_function(Gdk::COPY);
449                 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
450                 gc->set_rgb_fg_color(DUCK_COLOR_OUTLINE);
451                 drawable->draw_arc(
452                         gc,
453                         true,
454                         round_to_int(screen_duck_list.front().pos[0]-radius),
455                         round_to_int(screen_duck_list.front().pos[1]-radius),
456                         radius*2,
457                         radius*2,
458                         0,
459                         360*64
460                 );
461
462
463                 gc->set_rgb_fg_color(color);
464
465                 drawable->draw_arc(
466                         gc,
467                         true,
468                         round_to_int(screen_duck_list.front().pos[0]-radius+outline),
469                         round_to_int(screen_duck_list.front().pos[1]-radius+outline),
470                         radius*2-outline*2,
471                         radius*2-outline*2,
472                         0,
473                         360*64
474                 );
475         }
476 }