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