Accept 1927294 (a further patch from Gerald Young): the previous patch stopped the...
[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 <synfig/valuenode_blinecalcvertex.h>
41 #include <synfig/valuenode_bline.h>
42 #include "app.h"
43
44 #include "general.h"
45
46 #endif
47
48 /* === U S I N G =========================================================== */
49
50 using namespace std;
51 using namespace etl;
52 using namespace synfig;
53 using namespace studio;
54
55 /* === M A C R O S ========================================================= */
56
57 /* === G L O B A L S ======================================================= */
58
59 /* === P R O C E D U R E S ================================================= */
60
61 /* === M E T H O D S ======================================================= */
62
63 bool
64 restrict_blinevertex_duck(etl::handle<Duck> duck, WorkArea& w_area, synfig::Point *point)
65 {
66         synfig::Point sub_trans_origin(duck->get_sub_trans_origin());
67         etl::handle<Duck> origin_duck = duck->get_origin_duck();
68         bool origin_changed = false;
69         if(origin_duck)
70                 origin_changed = restrict_blinevertex_duck(origin_duck, w_area, &sub_trans_origin);
71
72         if( ValueNode_BLineCalcVertex::Handle bline_vertex =
73                 ValueNode_BLineCalcVertex::Handle::cast_dynamic(duck->get_value_desc().get_value_node())
74         )
75         {
76                 synfig::Point closest_point = duck->get_point();
77                 synfig::Real radius = 0.0;
78                 ValueNode_BLine::Handle bline = ValueNode_BLine::Handle::cast_dynamic(bline_vertex->get_link(bline_vertex->get_link_index_from_name("bline")));
79                 synfig::find_closest_point( 
80                         (*bline)(w_area.get_time()),
81                         duck->get_point(),
82                         radius,
83                         bline->get_loop(),
84                         &closest_point
85                 );
86
87                 if(closest_point != duck->get_point())
88                 {
89                         *point = closest_point * duck->get_scalar() + sub_trans_origin;
90                         return true;
91                 }
92         }
93         
94         if(origin_changed)
95         {
96                 *point = duck->get_point() * duck->get_scalar() + sub_trans_origin;
97                 return true;
98         }
99         
100         return false;
101 }
102
103 Renderer_Ducks::~Renderer_Ducks()
104 {
105 }
106
107 /*
108 bool
109 Renderer_Ducks::get_enabled_vfunc()const
110 {
111         return get_work_area()->grid_status();
112 }
113 */
114
115 struct ScreenDuck
116 {
117         synfig::Point pos;
118         Gdk::Color color;
119         bool selected;
120         bool hover;
121         Real width;
122
123         ScreenDuck():width(0) { }
124 };
125
126 void
127 Renderer_Ducks::render_vfunc(
128         const Glib::RefPtr<Gdk::Drawable>& drawable,
129         const Gdk::Rectangle& /*expose_area*/
130 )
131 {
132         assert(get_work_area());
133         if(!get_work_area())
134                 return;
135
136         const synfig::Vector focus_point(get_work_area()->get_focus_point());
137
138
139         int drawable_w,drawable_h;
140         drawable->get_size(drawable_w,drawable_h);
141
142         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(drawable));
143
144
145         const synfig::Vector::value_type window_startx(get_work_area()->get_window_tl()[0]);
146         const synfig::Vector::value_type window_starty(get_work_area()->get_window_tl()[1]);
147
148         const float pw(get_pw()),ph(get_ph());
149
150         const std::list<etl::handle<Duckmatic::Bezier> >& bezier_list(get_work_area()->bezier_list());
151         const bool solid_lines(get_work_area()->solid_lines);
152
153         const std::list<handle<Duckmatic::Stroke> >& stroke_list(get_work_area()->stroke_list());
154
155         Glib::RefPtr<Pango::Layout> layout(Pango::Layout::create(get_work_area()->get_pango_context()));
156
157         // Render the strokes
158         for(std::list<handle<Duckmatic::Stroke> >::const_iterator iter=stroke_list.begin();iter!=stroke_list.end();++iter)
159         {
160                 Point window_start(window_startx,window_starty);
161                 vector<Gdk::Point> points;
162                 std::list<synfig::Point>::iterator iter2;
163                 Point holder;
164
165                 for(iter2=(*iter)->stroke_data->begin();iter2!=(*iter)->stroke_data->end();++iter2)
166                 {
167                         holder=*iter2-window_start;
168                         holder[0]/=pw;holder[1]/=ph;
169                         points.push_back(Gdk::Point(round_to_int(holder[0]),round_to_int(holder[1])));
170                 }
171
172                 gc->set_rgb_fg_color(colorconv_synfig2gdk((*iter)->color));
173                 gc->set_function(Gdk::COPY);
174                 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
175
176                 // Draw the stroke
177                 drawable->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points));
178         }
179
180
181
182         // Render the beziers
183         for(std::list<handle<Duckmatic::Bezier> >::const_iterator iter=bezier_list.begin();iter!=bezier_list.end();++iter)
184         {
185                 Point sub_trans_p1((*iter)->p1->get_sub_trans_point());
186                 Point sub_trans_p2((*iter)->p2->get_sub_trans_point());
187                 Point sub_trans_c1((*iter)->c1->get_sub_trans_point());
188                 Point sub_trans_c2((*iter)->c2->get_sub_trans_point());
189
190                 WorkArea* w_area = get_work_area();
191                 restrict_blinevertex_duck((*iter)->p1, *w_area, &sub_trans_p1);
192                 restrict_blinevertex_duck((*iter)->p2, *w_area, &sub_trans_p2);
193                 restrict_blinevertex_duck((*iter)->c1, *w_area, &sub_trans_c1);
194                 restrict_blinevertex_duck((*iter)->c2, *w_area, &sub_trans_c2);
195                 
196                 Point window_start(window_startx,window_starty);
197                 Point p1((*iter)->p1->get_transform_stack().perform(sub_trans_p1)-window_start);
198                 Point p2((*iter)->p2->get_transform_stack().perform(sub_trans_p2)-window_start);
199                 Point c1((*iter)->c1->get_transform_stack().perform(sub_trans_c1)-window_start);
200                 Point c2((*iter)->c2->get_transform_stack().perform(sub_trans_c2)-window_start);
201                 p1[0]/=pw;p1[1]/=ph;
202                 p2[0]/=pw;p2[1]/=ph;
203                 c1[0]/=pw;c1[1]/=ph;
204                 c2[0]/=pw;c2[1]/=ph;
205                 bezier<Point> curve(p1,c1,c2,p2);
206                 vector<Gdk::Point> points;
207
208                 float f;
209                 Point pt;
210                 for(f=0;f<1.0;f+=1.0/17.0)
211                 {
212                         pt=curve(f);
213                         points.push_back(Gdk::Point(round_to_int(pt[0]),round_to_int(pt[1])));
214                 }
215                 points.push_back(Gdk::Point(round_to_int(p2[0]),round_to_int(p2[1])));
216
217                 // Draw the curve
218 /*              if(solid_lines)
219                 {
220                         gc->set_rgb_fg_color(DUCK_COLOR_BEZIER_1);
221                         gc->set_function(Gdk::COPY);
222                         gc->set_line_attributes(3,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
223                         drawable->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points));
224                         gc->set_rgb_fg_color(DUCK_COLOR_BEZIER_2);
225                         gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
226                         drawable->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points));
227                 }
228                 else
229 */
230                 {
231 //                      gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
232 //                      gc->set_function(Gdk::INVERT);
233 //                      gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
234 //                      drawable->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points));
235                         gc->set_rgb_fg_color(DUCK_COLOR_BEZIER_1);
236                         gc->set_function(Gdk::COPY);
237                         gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
238                         drawable->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points));
239                         gc->set_rgb_fg_color(DUCK_COLOR_BEZIER_2);
240                         gc->set_line_attributes(1,Gdk::LINE_ON_OFF_DASH,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
241                         drawable->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points));
242
243                 }
244         }
245
246
247         const DuckList duck_list(get_work_area()->get_duck_list());
248         //Gtk::StateType state = Gtk::STATE_ACTIVE;
249         Gtk::ShadowType shadow=Gtk::SHADOW_OUT;
250         std::list<ScreenDuck> screen_duck_list;
251         const float radius((abs(pw)+abs(ph))*4);
252
253         etl::handle<Duck> hover_duck(get_work_area()->find_duck(get_work_area()->get_cursor_pos(),radius, get_work_area()->get_type_mask()));
254
255         // Render the ducks
256         for(std::list<handle<Duck> >::const_iterator iter=duck_list.begin();iter!=duck_list.end();++iter)
257         {
258
259                 // If this type of duck has been masked, then skip it
260                 if((*iter)->get_type() && (!(get_work_area()->get_type_mask() & (*iter)->get_type())))
261                         continue;
262
263 //              Real x,y;
264         //      Gdk::Rectangle area;
265                 Point sub_trans_point((*iter)->get_sub_trans_point());
266                 Point sub_trans_origin((*iter)->get_sub_trans_origin());
267                 etl::handle<Duck> origin_duck = (*iter)->get_origin_duck();
268
269                 bool has_connect(false);
270                 if((*iter)->get_tangent() || (*iter)->get_type()&Duck::TYPE_ANGLE)
271                 {
272                         has_connect=true;
273                 }
274                 if((*iter)->get_connect_duck())
275                 {
276                         has_connect=true;
277                         sub_trans_origin = (*iter)->get_connect_duck()->get_sub_trans_point();
278                         origin_duck = (*iter)->get_connect_duck();
279                 }
280
281
282                 if (App::restrict_radius_ducks &&
283                         (*iter)->is_radius())
284                 {
285                         if (sub_trans_point[0] < sub_trans_origin[0])
286                                 sub_trans_point[0] = sub_trans_origin[0];
287                         if (sub_trans_point[1] < sub_trans_origin[1])
288                                 sub_trans_point[1] = sub_trans_origin[1];
289                 }
290
291                 WorkArea* w_area = get_work_area();
292                 restrict_blinevertex_duck((*iter), *w_area, &sub_trans_point);
293                 if(origin_duck)
294                         restrict_blinevertex_duck(origin_duck, *w_area, &sub_trans_origin);
295
296                 Point point((*iter)->get_transform_stack().perform(sub_trans_point));
297                 Point origin((*iter)->get_transform_stack().perform(sub_trans_origin));
298
299                 point[0]=(point[0]-window_startx)/pw;
300                 point[1]=(point[1]-window_starty)/ph;
301
302                 origin[0]=(origin[0]-window_startx)/pw;
303                 origin[1]=(origin[1]-window_starty)/ph;
304
305                 bool selected(get_work_area()->duck_is_selected(*iter));
306                 bool hover(*iter==hover_duck || (*iter)->get_hover());
307
308                 shadow = selected?Gtk::SHADOW_IN:Gtk::SHADOW_OUT;
309
310                 if(get_work_area()->get_selected_value_node())
311                 {
312                         synfigapp::ValueDesc value_desc((*iter)->get_value_desc());
313                         if (value_desc.is_valid() &&
314                                 ((value_desc.is_value_node()            && get_work_area()->get_selected_value_node() == value_desc.get_value_node()) ||
315                                  (value_desc.parent_is_value_node()     && get_work_area()->get_selected_value_node() == value_desc.get_parent_value_node())))
316                         {
317                                 gc->set_function(Gdk::COPY);
318                                 gc->set_rgb_fg_color(DUCK_COLOR_SELECTED);
319                                 //gc->set_line_attributes(1,Gdk::LINE_ON_OFF_DASH,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
320                                 gc->set_line_attributes(2,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
321
322                                 drawable->draw_rectangle(gc,false,
323                                         round_to_int(point[0]-5),
324                                         round_to_int(point[1]-5),
325                                         10,
326                                         10
327                                 );
328                         }
329
330                 }
331
332                 if((*iter)->get_box_duck())
333                 {
334                         Point boxpoint((*iter)->get_box_duck()->get_trans_point());
335                         boxpoint[0]=(boxpoint[0]-window_startx)/pw;
336                         boxpoint[1]=(boxpoint[1]-window_starty)/ph;
337                         Point tl(min(point[0],boxpoint[0]),min(point[1],boxpoint[1]));
338
339                         gc->set_function(Gdk::COPY);
340                         gc->set_rgb_fg_color(DUCK_COLOR_BOX_1);
341                         gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
342                         drawable->draw_rectangle(gc,false,
343                                 round_to_int(tl[0]),
344                                 round_to_int(tl[1]),
345                                 round_to_int(abs(boxpoint[0]-point[0])),
346                                 round_to_int(abs(boxpoint[1]-point[1]))
347                         );
348                         gc->set_function(Gdk::COPY);
349                         gc->set_rgb_fg_color(DUCK_COLOR_BOX_2);
350                         gc->set_line_attributes(1,Gdk::LINE_ON_OFF_DASH,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
351                         drawable->draw_rectangle(gc,false,
352                                 round_to_int(tl[0]),
353                                 round_to_int(tl[1]),
354                                 round_to_int(abs(boxpoint[0]-point[0])),
355                                 round_to_int(abs(boxpoint[1]-point[1]))
356                         );
357                 }
358
359                 ScreenDuck screen_duck;
360                 screen_duck.pos=point;
361                 screen_duck.selected=selected;
362                 screen_duck.hover=hover;
363
364                 if(!(*iter)->get_editable())
365                         screen_duck.color=(DUCK_COLOR_NOT_EDITABLE);
366                 else if((*iter)->get_tangent())
367                         screen_duck.color=((*iter)->get_scalar()<0 ? DUCK_COLOR_TANGENT_1 : DUCK_COLOR_TANGENT_2);
368                 else if((*iter)->get_type()&Duck::TYPE_VERTEX)
369                         screen_duck.color=DUCK_COLOR_VERTEX;
370                 else if((*iter)->get_type()&Duck::TYPE_RADIUS)
371                         screen_duck.color=DUCK_COLOR_RADIUS;
372                 else if((*iter)->get_type()&Duck::TYPE_WIDTH)
373                         screen_duck.color=DUCK_COLOR_WIDTH;
374                 else if((*iter)->get_type()&Duck::TYPE_ANGLE)
375                         screen_duck.color=(DUCK_COLOR_ANGLE);
376                 else
377                         screen_duck.color=DUCK_COLOR_OTHER;
378
379                 screen_duck_list.push_front(screen_duck);
380
381                 if(has_connect)
382                 {
383                         if(solid_lines)
384                         {
385                                 gc->set_line_attributes(3,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
386                                 gc->set_rgb_fg_color(DUCK_COLOR_CONNECT_OUTSIDE);
387                                 gc->set_function(Gdk::COPY);
388                                 drawable->draw_line(gc, (int)origin[0],(int)origin[1],(int)(point[0]),(int)(point[1]));
389                                 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
390                                 gc->set_rgb_fg_color(DUCK_COLOR_CONNECT_INSIDE);
391                                 drawable->draw_line(gc, (int)origin[0],(int)origin[1],(int)(point[0]),(int)(point[1]));
392                         }
393                         else
394                         {
395 //                              gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
396 //                              gc->set_function(Gdk::INVERT);
397 //                              drawable->draw_line(gc, (int)origin[0],(int)origin[1],(int)(point[0]),(int)(point[1]));
398                                 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
399                                 gc->set_rgb_fg_color(DUCK_COLOR_CONNECT_OUTSIDE);
400                                 gc->set_function(Gdk::COPY);
401                                 drawable->draw_line(gc, (int)origin[0],(int)origin[1],(int)(point[0]),(int)(point[1]));
402                                 gc->set_line_attributes(1,Gdk::LINE_ON_OFF_DASH,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
403                                 gc->set_rgb_fg_color(DUCK_COLOR_CONNECT_INSIDE);
404                                 drawable->draw_line(gc, (int)origin[0],(int)origin[1],(int)(point[0]),(int)(point[1]));
405                         }
406                 }
407
408                 if((*iter)->is_radius())
409                 {
410                         const Real mag((point-origin).mag());
411                         const int d(round_to_int(mag*2));
412                         const int x(round_to_int(origin[0]-mag));
413                         const int y(round_to_int(origin[1]-mag));
414
415                         if(solid_lines)
416                         {
417                                 gc->set_rgb_fg_color(Gdk::Color("#000000"));
418                                 gc->set_function(Gdk::COPY);
419                                 gc->set_line_attributes(3,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
420                                 drawable->draw_arc(
421                                         gc,
422                                         false,
423                                         x,
424                                         y,
425                                         d,
426                                         d,
427                                         0,
428                                         360*64
429                                 );
430                                 gc->set_rgb_fg_color(Gdk::Color("#afafaf"));
431                         }
432                         else
433                         {
434                                 gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
435                                 gc->set_function(Gdk::INVERT);
436                         }
437                         gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
438
439                         drawable->draw_arc(
440                                 gc,
441                                 false,
442                                 x,
443                                 y,
444                                 d,
445                                 d,
446                                 0,
447                                 360*64
448                         );
449
450                         if(hover)
451                         {
452                                 Real mag;
453                                 if (App::restrict_radius_ducks)
454                                 {
455                                         Point sub_trans_point((*iter)->get_sub_trans_point());
456                                         Point sub_trans_origin((*iter)->get_sub_trans_origin());
457
458                                         if (sub_trans_point[0] < sub_trans_origin[0])
459                                                 sub_trans_point[0] = sub_trans_origin[0];
460                                         if (sub_trans_point[1] < sub_trans_origin[1])
461                                                 sub_trans_point[1] = sub_trans_origin[1];
462
463                                         Point point((*iter)->get_transform_stack().perform(sub_trans_point));
464                                         Point origin((*iter)->get_transform_stack().perform(sub_trans_origin));
465
466                                         mag = (point-origin).mag();
467                                 }
468                                 else
469                                         mag = ((*iter)->get_trans_point()-(*iter)->get_trans_origin()).mag();
470
471                                 Distance real_mag(mag, Distance::SYSTEM_UNITS);
472                                 real_mag.convert(App::distance_system,get_work_area()->get_rend_desc());
473                                 layout->set_text(real_mag.get_string());
474
475                                 gc->set_rgb_fg_color(DUCK_COLOR_WIDTH_TEXT_1);
476                                 drawable->draw_layout(
477                                         gc,
478                                         round_to_int(point[0])+1+6,
479                                         round_to_int(point[1])+1-8,
480                                         layout
481                                 );
482                                 gc->set_rgb_fg_color(DUCK_COLOR_WIDTH_TEXT_2);
483                                 drawable->draw_layout(
484                                         gc,
485                                         round_to_int(point[0])+6,
486                                         round_to_int(point[1])-8,
487                                         layout
488                                 );
489                         }
490
491                 }
492
493         }
494
495
496         for(;screen_duck_list.size();screen_duck_list.pop_front())
497         {
498                 int radius=4;
499                 int outline=1;
500                 Gdk::Color color(screen_duck_list.front().color);
501
502                 if(!screen_duck_list.front().selected)
503                 {
504                         color.set_red(color.get_red()*2/3);
505                         color.set_green(color.get_green()*2/3);
506                         color.set_blue(color.get_blue()*2/3);
507                 }
508
509                 if(screen_duck_list.front().hover && !screen_duck_list.back().hover && screen_duck_list.size()>1)
510                 {
511                         screen_duck_list.push_back(screen_duck_list.front());
512                         continue;
513                 }
514
515                 if(screen_duck_list.front().hover)
516                 {
517                         radius+=2;
518                         outline++;
519                 }
520
521                 gc->set_function(Gdk::COPY);
522                 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
523                 gc->set_rgb_fg_color(DUCK_COLOR_OUTLINE);
524                 drawable->draw_arc(
525                         gc,
526                         true,
527                         round_to_int(screen_duck_list.front().pos[0]-radius),
528                         round_to_int(screen_duck_list.front().pos[1]-radius),
529                         radius*2,
530                         radius*2,
531                         0,
532                         360*64
533                 );
534
535
536                 gc->set_rgb_fg_color(color);
537
538                 drawable->draw_arc(
539                         gc,
540                         true,
541                         round_to_int(screen_duck_list.front().pos[0]-radius+outline),
542                         round_to_int(screen_duck_list.front().pos[1]-radius+outline),
543                         radius*2-outline*2,
544                         radius*2-outline*2,
545                         0,
546                         360*64
547                 );
548         }
549 }