Split tangents are now red and merged tangents are yellow.
[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                 {
308                         // Check if we can reach the canvas and set the time to
309                         // evaluate the split value accordingly
310                         synfig::Canvas::Handle canvas_h(get_work_area()->get_canvas());
311                         synfig::Time time(canvas_h?canvas_h->get_time():synfig::Time(0));
312                         // Retrieve the split value of the bline point.
313                         synfigapp::ValueDesc& v_d((*iter)->get_value_desc());
314                         synfig::LinkableValueNode::Handle parent;
315                         if(v_d.parent_is_linkable_value_node())
316                         {
317                                 parent=v_d.get_parent_value_node();
318                                 bool split;
319                                 synfig::ValueNode::Handle child(parent->get_link("split"));
320                                 if(synfig::ValueNode_Animated::Handle::cast_dynamic(child))
321                                 {
322                                         synfig::ValueNode_Animated::Handle animated_child(synfig::ValueNode_Animated::Handle::cast_dynamic(child));
323                                         split=animated_child->new_waypoint_at_time(time).get_value(time).get(split);
324                                 }
325                                 else if(synfig::ValueNode_Const::Handle::cast_dynamic(child))
326                                 {
327                                         synfig::ValueNode_Const::Handle const_child(synfig::ValueNode_Const::Handle::cast_dynamic(child));
328                                         split=(const_child->get_value()).get(split);
329                                 }
330                                 screen_duck.color=(split? DUCK_COLOR_TANGENT_2 : DUCK_COLOR_TANGENT_1);
331                         }
332                         else
333                                 screen_duck.color=DUCK_COLOR_TANGENT_1;
334                 }
335                 else if((*iter)->get_type()&Duck::TYPE_VERTEX)
336                         screen_duck.color=DUCK_COLOR_VERTEX;
337                 else if((*iter)->get_type()&Duck::TYPE_RADIUS)
338                         screen_duck.color=DUCK_COLOR_RADIUS;
339                 else if((*iter)->get_type()&Duck::TYPE_WIDTH)
340                         screen_duck.color=DUCK_COLOR_WIDTH;
341                 else if((*iter)->get_type()&Duck::TYPE_ANGLE)
342                         screen_duck.color=(DUCK_COLOR_ANGLE);
343                 else
344                         screen_duck.color=DUCK_COLOR_OTHER;
345
346                 screen_duck_list.push_front(screen_duck);
347
348                 if(has_connect)
349                 {
350                         if(solid_lines)
351                         {
352                                 gc->set_line_attributes(3,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
353                                 gc->set_rgb_fg_color(DUCK_COLOR_CONNECT_OUTSIDE);
354                                 gc->set_function(Gdk::COPY);
355                                 drawable->draw_line(gc, (int)origin[0],(int)origin[1],(int)(point[0]),(int)(point[1]));
356                                 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
357                                 gc->set_rgb_fg_color(DUCK_COLOR_CONNECT_INSIDE);
358                                 drawable->draw_line(gc, (int)origin[0],(int)origin[1],(int)(point[0]),(int)(point[1]));
359                         }
360                         else
361                         {
362 //                              gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
363 //                              gc->set_function(Gdk::INVERT);
364 //                              drawable->draw_line(gc, (int)origin[0],(int)origin[1],(int)(point[0]),(int)(point[1]));
365                                 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
366                                 gc->set_rgb_fg_color(DUCK_COLOR_CONNECT_OUTSIDE);
367                                 gc->set_function(Gdk::COPY);
368                                 drawable->draw_line(gc, (int)origin[0],(int)origin[1],(int)(point[0]),(int)(point[1]));
369                                 gc->set_line_attributes(1,Gdk::LINE_ON_OFF_DASH,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
370                                 gc->set_rgb_fg_color(DUCK_COLOR_CONNECT_INSIDE);
371                                 drawable->draw_line(gc, (int)origin[0],(int)origin[1],(int)(point[0]),(int)(point[1]));
372                         }
373                 }
374
375                 if((*iter)->is_radius())
376                 {
377                         const Real mag((point-origin).mag());
378                         const int d(round_to_int(mag*2));
379                         const int x(round_to_int(origin[0]-mag));
380                         const int y(round_to_int(origin[1]-mag));
381
382                         if(solid_lines)
383                         {
384                                 gc->set_rgb_fg_color(Gdk::Color("#000000"));
385                                 gc->set_function(Gdk::COPY);
386                                 gc->set_line_attributes(3,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
387                                 drawable->draw_arc(
388                                         gc,
389                                         false,
390                                         x,
391                                         y,
392                                         d,
393                                         d,
394                                         0,
395                                         360*64
396                                 );
397                                 gc->set_rgb_fg_color(Gdk::Color("#afafaf"));
398                         }
399                         else
400                         {
401                                 gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
402                                 gc->set_function(Gdk::INVERT);
403                         }
404                         gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
405
406                         drawable->draw_arc(
407                                 gc,
408                                 false,
409                                 x,
410                                 y,
411                                 d,
412                                 d,
413                                 0,
414                                 360*64
415                         );
416
417                         if(hover)
418                         {
419                                 Real mag;
420                                 if (App::restrict_radius_ducks)
421                                 {
422                                         Point sub_trans_point((*iter)->get_sub_trans_point());
423                                         Point sub_trans_origin((*iter)->get_sub_trans_origin());
424
425                                         if (sub_trans_point[0] < sub_trans_origin[0])
426                                                 sub_trans_point[0] = sub_trans_origin[0];
427                                         if (sub_trans_point[1] < sub_trans_origin[1])
428                                                 sub_trans_point[1] = sub_trans_origin[1];
429
430                                         Point point((*iter)->get_transform_stack().perform(sub_trans_point));
431                                         Point origin((*iter)->get_transform_stack().perform(sub_trans_origin));
432
433                                         mag = (point-origin).mag();
434                                 }
435                                 else
436                                         mag = ((*iter)->get_trans_point()-(*iter)->get_trans_origin()).mag();
437
438                                 Distance real_mag(mag, Distance::SYSTEM_UNITS);
439                                 real_mag.convert(App::distance_system,get_work_area()->get_rend_desc());
440                                 layout->set_text(real_mag.get_string());
441
442                                 gc->set_rgb_fg_color(DUCK_COLOR_WIDTH_TEXT_1);
443                                 drawable->draw_layout(
444                                         gc,
445                                         round_to_int(point[0])+1+6,
446                                         round_to_int(point[1])+1-8,
447                                         layout
448                                 );
449                                 gc->set_rgb_fg_color(DUCK_COLOR_WIDTH_TEXT_2);
450                                 drawable->draw_layout(
451                                         gc,
452                                         round_to_int(point[0])+6,
453                                         round_to_int(point[1])-8,
454                                         layout
455                                 );
456                         }
457
458                 }
459
460         }
461
462
463         for(;screen_duck_list.size();screen_duck_list.pop_front())
464         {
465                 int radius=4;
466                 int outline=1;
467                 Gdk::Color color(screen_duck_list.front().color);
468
469                 if(!screen_duck_list.front().selected)
470                 {
471                         color.set_red(color.get_red()*2/3);
472                         color.set_green(color.get_green()*2/3);
473                         color.set_blue(color.get_blue()*2/3);
474                 }
475
476                 if(screen_duck_list.front().hover && !screen_duck_list.back().hover && screen_duck_list.size()>1)
477                 {
478                         screen_duck_list.push_back(screen_duck_list.front());
479                         continue;
480                 }
481
482                 if(screen_duck_list.front().hover)
483                 {
484                         radius+=2;
485                         outline++;
486                 }
487
488                 gc->set_function(Gdk::COPY);
489                 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
490                 gc->set_rgb_fg_color(DUCK_COLOR_OUTLINE);
491                 drawable->draw_arc(
492                         gc,
493                         true,
494                         round_to_int(screen_duck_list.front().pos[0]-radius),
495                         round_to_int(screen_duck_list.front().pos[1]-radius),
496                         radius*2,
497                         radius*2,
498                         0,
499                         360*64
500                 );
501
502
503                 gc->set_rgb_fg_color(color);
504
505                 drawable->draw_arc(
506                         gc,
507                         true,
508                         round_to_int(screen_duck_list.front().pos[0]-radius+outline),
509                         round_to_int(screen_duck_list.front().pos[1]-radius+outline),
510                         radius*2-outline*2,
511                         radius*2-outline*2,
512                         0,
513                         360*64
514                 );
515         }
516 }