Merge branch 'master' into nikitakit_restructure
[synfig.git] / synfig-studio / src / gui / duckmatic.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file duckmatic.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) 2009 Nikita Kitaev
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 #include <fstream>
34 #include <iostream>
35 #include <algorithm>
36
37 #include <ETL/hermite>
38
39 #include "duckmatic.h"
40 #include <synfigapp/value_desc.h>
41 #include <synfig/general.h>
42 #include <synfig/paramdesc.h>
43 #include <synfig/valuenode_timedswap.h>
44 #include <synfig/valuenode_animated.h>
45 #include <synfig/valuenode_composite.h>
46 #include <synfig/valuenode_scale.h>
47 #include <synfig/valuenode_bline.h>
48 #include <synfig/valuenode_blinecalctangent.h>
49 #include <synfig/valuenode_blinecalcvertex.h>
50 #include <synfig/valuenode_blinecalcwidth.h>
51
52 #include <synfig/curve_helper.h>
53
54 #include <sigc++/retype_return.h>
55 #include <sigc++/retype.h>
56 #include <sigc++/hide.h>
57 #include <sigc++/bind.h>
58
59 #include "canvasview.h"
60
61 #include "onemoment.h"
62
63 #include "general.h"
64
65 #endif
66
67 /* === U S I N G =========================================================== */
68
69 using namespace std;
70 using namespace etl;
71 using namespace synfig;
72 using namespace studio;
73
74 /* === M A C R O S ========================================================= */
75
76 /* 0.33333333333333333 makes for nice short tangent handles,
77    1.0 makes them draw as their real length */
78 #define TANGENT_HANDLE_SCALE 0.33333333333333333
79
80 /* leave this alone or the bezier won't lie on top of the bline */
81 #define TANGENT_BEZIER_SCALE 0.33333333333333333
82
83 /* === G L O B A L S ======================================================= */
84
85 /* === P R O C E D U R E S ================================================= */
86
87 /* === M E T H O D S ======================================================= */
88
89 /* === E N T R Y P O I N T ================================================= */
90
91 Duckmatic::Duckmatic():
92         type_mask(Duck::TYPE_ALL-Duck::TYPE_WIDTH),
93         grid_snap(false),
94         guide_snap(false),
95         grid_size(1.0/4.0,1.0/4.0),
96         show_persistent_strokes(true)
97 {
98         axis_lock=false;
99         drag_offset_=Point(0,0);
100         clear_duck_dragger();
101 }
102
103 Duckmatic::~Duckmatic()
104 {
105         clear_ducks();
106
107         if (Duck::duck_count)
108                 synfig::error("%d ducks not yet deleted!", Duck::duck_count);
109
110         if (getenv("SYNFIG_DEBUG_DESTRUCTORS"))
111                 synfig::info("Duckmatic::~Duckmatic(): Deleted");
112 }
113
114 void
115 Duckmatic::clear_ducks()
116 {
117         duck_data_share_map.clear();
118         duck_map.clear();
119
120         //duck_list_.clear();
121         bezier_list_.clear();
122         stroke_list_.clear();
123
124         if(show_persistent_strokes)
125                 stroke_list_=persistent_stroke_list_;
126 }
127
128 //! Returns \a true if the given duck is currently selected
129 bool
130 Duckmatic::duck_is_selected(const etl::handle<Duck> &duck)const
131 {
132         return duck && selected_ducks.count(duck->get_guid());
133 }
134
135 void
136 Duckmatic::set_grid_size(const synfig::Vector &s)
137 {
138         if(grid_size!=s)
139         {
140                 grid_size=s;
141                 signal_grid_changed();
142         }
143 }
144
145 void
146 Duckmatic::set_grid_snap(bool x)
147 {
148         if(grid_snap!=x)
149         {
150                 grid_snap=x;
151                 signal_grid_changed();
152         }
153 }
154
155 void
156 Duckmatic::set_guide_snap(bool x)
157 {
158         if(guide_snap!=x)
159         {
160                 guide_snap=x;
161                 signal_grid_changed();
162         }
163 }
164
165 Duckmatic::GuideList::iterator
166 Duckmatic::find_guide_x(synfig::Point pos, float radius)
167 {
168         GuideList::iterator iter,best(guide_list_x_.end());
169         float dist(radius);
170         for(iter=guide_list_x_.begin();iter!=guide_list_x_.end();++iter)
171         {
172                 float amount(abs(*iter-pos[0]));
173                 if(amount<dist)
174                 {
175                         dist=amount;
176                         best=iter;
177                 }
178         }
179         return best;
180 }
181
182 Duckmatic::GuideList::iterator
183 Duckmatic::find_guide_y(synfig::Point pos, float radius)
184 {
185         GuideList::iterator iter,best(guide_list_y_.end());
186         float dist(radius);
187         for(iter=guide_list_y_.begin();iter!=guide_list_y_.end();++iter)
188         {
189                 float amount(abs(*iter-pos[1]));
190                 if(amount<=dist)
191                 {
192                         dist=amount;
193                         best=iter;
194                 }
195         }
196         return best;
197 }
198
199 void
200 Duckmatic::clear_selected_ducks()
201 {
202         selected_ducks.clear();
203         signal_duck_selection_changed_();
204 }
205
206 etl::handle<Duckmatic::Duck>
207 Duckmatic::get_selected_duck()const
208 {
209         if(selected_ducks.empty() || duck_map.empty())
210                 return 0;
211         return duck_map.find(*selected_ducks.begin())->second;
212 }
213
214 void
215 Duckmatic::refresh_selected_ducks()
216 {
217 /*
218         std::set<etl::handle<Duck> >::iterator iter;
219         std::set<etl::handle<Duck> > new_set;
220         if(duck_list().empty())
221         {
222                 selected_duck_list.clear();
223                 signal_duck_selection_changed_();
224                 return;
225         }
226
227         for(iter=selected_duck_list.begin();iter!=selected_duck_list.end();++iter)
228         {
229                 etl::handle<Duck> similar(find_similar_duck(*iter));
230                 if(similar)
231                 {
232                         new_set.insert(similar);
233                 }
234         }
235         selected_duck_list=new_set;
236 */
237         GUIDSet old_set(selected_ducks);
238         GUIDSet::const_iterator iter;
239
240         for(iter=old_set.begin();iter!=old_set.end();++iter)
241         {
242                 if(duck_map.count(*iter)==0)
243                         selected_ducks.erase(*iter);
244         }
245
246         signal_duck_selection_changed_();
247 }
248
249 bool
250 Duckmatic::is_duck_group_selectable(const etl::handle<Duck>& x)const
251 {
252         const Type type(get_type_mask());
253
254         if (((x->get_type() && (!(type & x->get_type()))) ||
255                  !x->get_editable()))
256                 return false;
257
258         synfigapp::ValueDesc value_desc(x->get_value_desc());
259         if(value_desc.parent_is_layer_param() && type & Duck::TYPE_POSITION)
260         {
261                 Layer::Handle layer(value_desc.get_layer());
262                 String layer_name(layer->get_name());
263
264                 if (layer_name == "outline" || layer_name == "region" || layer_name == "plant" ||
265                         layer_name == "polygon" || layer_name == "curve_gradient")
266                         return false;
267
268                 if((layer_name=="PasteCanvas"|| layer_name=="paste_canvas") &&
269                    !layer->get_param("children_lock").get(bool()))
270                         return false;
271         }
272         else if (value_desc.parent_is_value_node())
273         {
274                 if (ValueNode_BLineCalcVertex::Handle::cast_dynamic(value_desc.get_value_node()))
275                         return false;
276                 if (value_desc.parent_is_linkable_value_node())
277                 {
278                         LinkableValueNode::Handle parent_value_node(value_desc.get_parent_value_node());
279                         if (ValueNode_Composite::Handle::cast_dynamic(parent_value_node))
280                         {
281                                 if (parent_value_node->get_type() == ValueBase::TYPE_BLINEPOINT &&
282                                         ValueNode_BLineCalcVertex::Handle::cast_dynamic(
283                                                 parent_value_node->get_link("point")))
284                                         return false;
285                         }
286                         else if (ValueNode_BLine::Handle::cast_dynamic(parent_value_node))
287                         {
288                                 ValueNode_Composite::Handle composite(ValueNode_Composite::Handle::cast_dynamic(
289                                                                                                                   value_desc.get_value_node()));
290                                 if (composite &&
291                                         ValueNode_BLineCalcVertex::Handle::cast_dynamic(composite->get_link("point")))
292                                         return false;
293                         }
294                 }
295         }
296         return true;
297 }
298
299 void
300 Duckmatic::select_all_ducks()
301 {
302         DuckMap::const_iterator iter;
303         for(iter=duck_map.begin();iter!=duck_map.end();++iter)
304                 if(is_duck_group_selectable(iter->second))
305                         select_duck(iter->second);
306                 else
307                         unselect_duck(iter->second);
308 }
309
310 void
311 Duckmatic::unselect_all_ducks()
312 {
313         DuckMap::const_iterator iter;
314         for(iter=duck_map.begin();iter!=duck_map.end();++iter)
315                 unselect_duck(iter->second);
316 }
317
318 void
319 Duckmatic::toggle_select_ducks_in_box(const synfig::Vector& tl,const synfig::Vector& br)
320 {
321         Vector vmin, vmax;
322         vmin[0]=std::min(tl[0],br[0]);
323         vmin[1]=std::min(tl[1],br[1]);
324         vmax[0]=std::max(tl[0],br[0]);
325         vmax[1]=std::max(tl[1],br[1]);
326
327         DuckMap::const_iterator iter;
328         for(iter=duck_map.begin();iter!=duck_map.end();++iter)
329         {
330                 Point p(iter->second->get_trans_point());
331                 if(p[0]<=vmax[0] && p[0]>=vmin[0] && p[1]<=vmax[1] && p[1]>=vmin[1] &&
332                    is_duck_group_selectable(iter->second))
333                         toggle_select_duck(iter->second);
334         }
335 }
336
337 void
338 Duckmatic::select_ducks_in_box(const synfig::Vector& tl,const synfig::Vector& br)
339 {
340         Vector vmin, vmax;
341         vmin[0]=std::min(tl[0],br[0]);
342         vmin[1]=std::min(tl[1],br[1]);
343         vmax[0]=std::max(tl[0],br[0]);
344         vmax[1]=std::max(tl[1],br[1]);
345
346 //      Type type(get_type_mask());
347
348         DuckMap::const_iterator iter;
349         for(iter=duck_map.begin();iter!=duck_map.end();++iter)
350         {
351                 Point p(iter->second->get_trans_point());
352                 if(p[0]<=vmax[0] && p[0]>=vmin[0] && p[1]<=vmax[1] && p[1]>=vmin[1])
353                 {
354                         if(is_duck_group_selectable(iter->second))
355                                 select_duck(iter->second);
356                 }
357         }
358 }
359
360 int
361 Duckmatic::count_selected_ducks()const
362 {
363         return selected_ducks.size();
364 }
365
366 void
367 Duckmatic::select_duck(const etl::handle<Duck> &duck)
368 {
369         if(duck)
370         {
371                 selected_ducks.insert(duck->get_guid());
372                 signal_duck_selection_changed_();
373         }
374 }
375
376 DuckList
377 Duckmatic::get_selected_ducks()const
378 {
379         DuckList ret;
380         GUIDSet::const_iterator iter;
381         const Type type(get_type_mask());
382
383         for(iter=selected_ducks.begin();iter!=selected_ducks.end();++iter)
384         {
385                 const DuckMap::const_iterator d_iter(duck_map.find(*iter));
386
387                 if(d_iter==duck_map.end())
388                         continue;
389
390                 if(( d_iter->second->get_type() && (!(type & d_iter->second->get_type())) ) )
391                         continue;
392
393                 ret.push_back(d_iter->second);
394         }
395         return ret;
396 }
397
398 DuckList
399 Duckmatic::get_duck_list()const
400 {
401         DuckList ret;
402         DuckMap::const_iterator iter;
403         for(iter=duck_map.begin();iter!=duck_map.end();++iter) if (iter->second->get_type()&Duck::TYPE_POSITION) ret.push_back(iter->second);
404         for(iter=duck_map.begin();iter!=duck_map.end();++iter) if (iter->second->get_type()&Duck::TYPE_VERTEX  ) ret.push_back(iter->second);
405         for(iter=duck_map.begin();iter!=duck_map.end();++iter) if (iter->second->get_type()&Duck::TYPE_TANGENT ) ret.push_back(iter->second);
406         for(iter=duck_map.begin();iter!=duck_map.end();++iter)
407                 if (!(iter->second->get_type()&Duck::TYPE_POSITION) &&
408                         !(iter->second->get_type()&Duck::TYPE_VERTEX) &&
409                         !(iter->second->get_type()&Duck::TYPE_TANGENT))
410                         ret.push_back(iter->second);
411         return ret;
412 }
413
414 void
415 Duckmatic::unselect_duck(const etl::handle<Duck> &duck)
416 {
417         if(duck && selected_ducks.count(duck->get_guid()))
418         {
419                 selected_ducks.erase(duck->get_guid());
420                 signal_duck_selection_changed_();
421         }
422 }
423
424 void
425 Duckmatic::toggle_select_duck(const etl::handle<Duck> &duck)
426 {
427         if(duck_is_selected(duck))
428                 unselect_duck(duck);
429         else
430                 select_duck(duck);
431 }
432
433 void
434 Duckmatic::translate_selected_ducks(const synfig::Vector& vector)
435 {
436         if(duck_dragger_)
437                 duck_dragger_->duck_drag(this,vector);
438 }
439
440 void
441 Duckmatic::start_duck_drag(const synfig::Vector& offset)
442 {
443         if(duck_dragger_)
444                 duck_dragger_->begin_duck_drag(this,offset);
445
446         //drag_offset_=offset;
447         drag_offset_=find_duck(offset)->get_trans_point();
448 }
449
450 void
451 Duckmatic::update_ducks()
452 {
453         Time time(get_time());
454         DuckList duck_list(get_duck_list());
455         const DuckList selected_ducks(get_selected_ducks());
456         DuckList::const_iterator iter;
457         for (iter=selected_ducks.begin(); iter!=selected_ducks.end(); ++iter)
458         {
459                 etl::handle<Duck> duck(*iter);
460                 if (duck->get_type() == Duck::TYPE_VERTEX || duck->get_type() == Duck::TYPE_POSITION)
461                 {
462                         ValueNode_BLineCalcVertex::Handle bline_vertex;
463                         ValueNode_Composite::Handle composite;
464
465                         if ((bline_vertex = ValueNode_BLineCalcVertex::Handle::cast_dynamic(duck->get_value_desc().get_value_node())) ||
466                                 ((composite = ValueNode_Composite::Handle::cast_dynamic(duck->get_value_desc().get_value_node())) &&
467                                  composite->get_type() == ValueBase::TYPE_BLINEPOINT &&
468                                  (bline_vertex = ValueNode_BLineCalcVertex::Handle::cast_dynamic(composite->get_link("point")))))
469                         {
470                                 DuckList::iterator iter;
471                                 for (iter=duck_list.begin(); iter!=duck_list.end(); iter++)
472                                         if ( (*iter)->get_origin_duck()==duck  /*&& !duck_is_selected(*iter)*/ )
473                                         {
474                                                 synfig::Real radius = 0.0;
475                                                 ValueNode_BLine::Handle bline(ValueNode_BLine::Handle::cast_dynamic(bline_vertex->get_link(bline_vertex->get_link_index_from_name("bline"))));
476                                                 Real amount = synfig::find_closest_point((*bline)(time), duck->get_point(), radius, bline->get_loop());
477
478                                                 int vertex_amount_index(bline_vertex->get_link_index_from_name("amount"));
479                                                 ValueNode::Handle vertex_amount_value_node(bline_vertex->get_link(vertex_amount_index));
480
481
482                                                 ValueNode::Handle duck_value_node((*iter)->get_value_desc().get_value_node());
483                                                 if (ValueNode_BLineCalcTangent::Handle bline_tangent = ValueNode_BLineCalcTangent::Handle::cast_dynamic(duck_value_node))
484                                                 {
485                                                         if (bline_tangent->get_link(bline_tangent->get_link_index_from_name("amount")) == vertex_amount_value_node)
486                                                         {
487                                                                 switch (bline_tangent->get_type())
488                                                                 {
489                                                                 case ValueBase::TYPE_ANGLE:
490                                                                 {
491                                                                         Angle angle((*bline_tangent)(time, amount).get(Angle()));
492                                                                         (*iter)->set_point(Point(Angle::cos(angle).get(), Angle::sin(angle).get()));
493                                                                         (*iter)->set_rotations(Angle::deg(0)); //hack: rotations are a relative value
494                                                                         break;
495                                                                 }
496                                                                 case ValueBase::TYPE_REAL:
497                                                                         (*iter)->set_point(Point((*bline_tangent)(time, amount).get(Real()), 0));
498                                                                         break;
499                                                                 case ValueBase::TYPE_VECTOR:
500                                                                         (*iter)->set_point((*bline_tangent)(time, amount).get(Vector()));
501                                                                         break;
502                                                                 default:
503                                                                         break;
504                                                                 }
505                                                         }
506                                                 }
507                                                 else if (ValueNode_BLineCalcWidth::Handle bline_width = ValueNode_BLineCalcWidth::Handle::cast_dynamic(duck_value_node))
508                                                 {
509                                                         if (bline_width->get_link(bline_width->get_link_index_from_name("amount")) == vertex_amount_value_node)
510                                                                 (*iter)->set_point(Point((*bline_width)(time, amount).get(Real()), 0));
511                                                 }
512                                         }
513                         }
514                 }
515         }
516 }
517
518
519 bool
520 Duckmatic::end_duck_drag()
521 {
522         if(duck_dragger_)
523                 return duck_dragger_->end_duck_drag(this);
524         return false;
525 }
526
527 Point
528 Duckmatic::snap_point_to_grid(const synfig::Point& x, float radius)const
529 {
530         Point ret(x);
531
532         GuideList::const_iterator guide_x,guide_y;
533         bool has_guide_x(false), has_guide_y(false);
534
535         guide_x=find_guide_x(ret,radius);
536         if(guide_x!=guide_list_x_.end())
537                 has_guide_x=true;
538
539         guide_y=find_guide_y(ret,radius);
540         if(guide_y!=guide_list_y_.end())
541                 has_guide_y=true;
542
543         if(get_grid_snap())
544         {
545                 Point snap(
546                         floor(ret[0]/get_grid_size()[0]+0.5)*get_grid_size()[0],
547                         floor(ret[1]/get_grid_size()[1]+0.5)*get_grid_size()[1]);
548
549                 if(abs(snap[0]-ret[0])<=radius && (!has_guide_x || abs(snap[0]-ret[0])<=abs(*guide_x-ret[0])))
550                         ret[0]=snap[0],has_guide_x=false;
551                 if(abs(snap[1]-ret[1])<=radius && (!has_guide_y || abs(snap[1]-ret[1])<=abs(*guide_y-ret[1])))
552                         ret[1]=snap[1],has_guide_y=false;
553         }
554
555         if(guide_snap)
556         {
557                 if(has_guide_x)
558                         ret[0]=*guide_x;
559                 if(has_guide_y)
560                         ret[1]=*guide_y;
561         }
562
563         if(axis_lock)
564         {
565                 ret-=drag_offset_;
566                 if(abs(ret[0])<abs(ret[1]))
567                         ret[0]=0;
568                 else
569                         ret[1]=0;
570                 ret+=drag_offset_;
571         }
572
573         return ret;
574 }
575
576 void
577 DuckDrag_Translate::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
578 {
579         last_translate_=Vector(0,0);
580         {
581                 drag_offset_=duckmatic->find_duck(offset)->get_trans_point();
582
583                 snap=Vector(0,0);
584         }
585
586         const DuckList selected_ducks(duckmatic->get_selected_ducks());
587         DuckList::const_iterator iter;
588
589         positions.clear();
590         for(iter=selected_ducks.begin();iter!=selected_ducks.end();++iter)
591         {
592                 Point p((*iter)->get_trans_point());
593                 positions.push_back(p);
594         }
595 }
596
597 bool
598 DuckDrag_Translate::end_duck_drag(Duckmatic* duckmatic)
599 {
600         if(last_translate_.mag()>0.0001)
601         {
602                 duckmatic->signal_edited_selected_ducks();
603                 return true;
604         }
605         else
606         {
607                 duckmatic->signal_user_click_selected_ducks(0);
608                 return false;
609         }
610 }
611
612 void
613 DuckDrag_Translate::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
614 {
615         const DuckList selected_ducks(duckmatic->get_selected_ducks());
616         DuckList::const_iterator iter;
617
618         synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-drag_offset_);
619         int i;
620         Time time(duckmatic->get_time());
621
622         // drag the vertex and position ducks first
623         for (i=0,iter=selected_ducks.begin(); iter!=selected_ducks.end(); ++iter,i++)
624                 if((*iter)->get_type() == Duck::TYPE_VERTEX || (*iter)->get_type() == Duck::TYPE_POSITION)
625                         (*iter)->set_trans_point(positions[i]+vect, time);
626
627         // then drag the others
628         for (i=0,iter=selected_ducks.begin(); iter!=selected_ducks.end(); ++iter,i++)
629                 if ((*iter)->get_type() != Duck::TYPE_VERTEX && (*iter)->get_type() != Duck::TYPE_POSITION)
630                         (*iter)->set_trans_point(positions[i]+vect, time);
631
632         // then patch up the tangents for the vertices we've moved
633         duckmatic->update_ducks();
634
635         last_translate_=vect;
636 }
637
638 void
639 Duckmatic::signal_edited_selected_ducks()
640 {
641         const DuckList ducks(get_selected_ducks());
642         DuckList::const_iterator iter;
643
644         synfig::GUIDSet old_set(selected_ducks);
645
646         // If we have more than 20 things to move, then display
647         // something to explain that it may take a moment
648         smart_ptr<OneMoment> wait; if(ducks.size()>20)wait.spawn();
649
650         // Go ahead and call everyone's signals
651         for(iter=ducks.begin();iter!=ducks.end();++iter)
652         {
653                 if ((*iter)->get_type() == Duck::TYPE_ANGLE)
654                 {
655                         if(!(*iter)->signal_edited_angle()((*iter)->get_rotations()))
656                         {
657                                 selected_ducks=old_set;
658                                 throw String("Bad edit");
659                         }
660                 }
661                 else if (App::restrict_radius_ducks &&
662                                  (*iter)->is_radius())
663                 {
664                         Point point((*iter)->get_point());
665                         bool changed = false;
666
667                         if (point[0] < 0)
668                         {
669                                 point[0] = 0;
670                                 changed = true;
671                         }
672                         if (point[1] < 0)
673                         {
674                                 point[1] = 0;
675                                 changed = true;
676                         }
677
678                         if (changed) (*iter)->set_point(point);
679
680                         if(!(*iter)->signal_edited()(point))
681                         {
682                                 selected_ducks=old_set;
683                                 throw String("Bad edit");
684                         }
685                 }
686                 else
687                 {
688                         if(!(*iter)->signal_edited()((*iter)->get_point()))
689                         {
690                                 selected_ducks=old_set;
691                                 throw String("Bad edit");
692                         }
693                 }
694         }
695         selected_ducks=old_set;
696 }
697
698 void
699 Duckmatic::signal_user_click_selected_ducks(int button)
700 {
701         const DuckList ducks(get_selected_ducks());
702         DuckList::const_iterator iter;
703
704         for(iter=ducks.begin();iter!=ducks.end();++iter)
705         {
706                 (*iter)->signal_user_click(button)();
707         }
708 }
709
710 void
711 Duckmatic::add_duck(const etl::handle<Duck> &duck)
712 {
713         //if(!duck_map.count(duck->get_guid()))
714         {
715                 if(duck_data_share_map.count(duck->get_data_guid()))
716                 {
717                         duck->set_shared_point(duck_data_share_map[duck->get_data_guid()]);
718                 }
719                 else
720                 {
721                         etl::smart_ptr<synfig::Point> point(new Point(duck->get_point()));
722                         duck->set_shared_point(point);
723                         duck_data_share_map[duck->get_data_guid()]=point;
724                 }
725
726                 duck_map.insert(duck);
727         }
728
729         last_duck_guid=duck->get_guid();
730 }
731
732 void
733 Duckmatic::add_bezier(const etl::handle<Bezier> &bezier)
734 {
735         bezier_list_.push_back(bezier);
736 }
737
738 void
739 Duckmatic::add_stroke(etl::smart_ptr<std::list<synfig::Point> > stroke_point_list, const synfig::Color& color)
740 {
741         assert(stroke_point_list);
742
743         std::list<etl::handle<Stroke> >::iterator iter;
744
745         for(iter=stroke_list_.begin();iter!=stroke_list_.end();++iter)
746         {
747                 if((*iter)->stroke_data==stroke_point_list)
748                         return;
749         }
750
751         etl::handle<Stroke> stroke(new Stroke());
752
753         stroke->stroke_data=stroke_point_list;
754         stroke->color=color;
755
756         stroke_list_.push_back(stroke);
757 }
758
759 void
760 Duckmatic::add_persistent_stroke(etl::smart_ptr<std::list<synfig::Point> > stroke_point_list, const synfig::Color& color)
761 {
762         add_stroke(stroke_point_list,color);
763         persistent_stroke_list_.push_back(stroke_list_.back());
764 }
765
766 void
767 Duckmatic::clear_persistent_strokes()
768 {
769         persistent_stroke_list_.clear();
770 }
771
772 void
773 Duckmatic::set_show_persistent_strokes(bool x)
774 {
775         if(x!=show_persistent_strokes)
776         {
777                 show_persistent_strokes=x;
778                 if(x)
779                         stroke_list_=persistent_stroke_list_;
780                 else
781                         stroke_list_.clear();
782         }
783 }
784
785 void
786 Duckmatic::erase_duck(const etl::handle<Duck> &duck)
787 {
788         duck_map.erase(duck->get_guid());
789 }
790
791 etl::handle<Duckmatic::Duck>
792 Duckmatic::find_similar_duck(etl::handle<Duck> duck)
793 {
794         DuckMap::const_iterator iter(duck_map.find(duck->get_guid()));
795         if(iter!=duck_map.end())
796                 return iter->second;
797         return 0;
798
799 /*      std::list<handle<Duck> >::reverse_iterator iter;
800
801         for(iter=duck_list_.rbegin();iter!=duck_list_.rend();++iter)
802         {
803                 if(*iter!=duck && **iter==*duck)
804                 {
805                         //synfig::info("Found similar duck! (iter:%08x vs. duck:%08x)",iter->get(), duck.get());
806                         return *iter;
807                 }
808         }
809         return 0;
810 */
811 }
812
813 etl::handle<Duckmatic::Duck>
814 Duckmatic::add_similar_duck(etl::handle<Duck> duck)
815 {
816         etl::handle<Duck> similar(find_similar_duck(duck));
817         if(!similar)
818         {
819                 add_duck(duck);
820                 return duck;
821         }
822         return similar;
823 }
824
825 void
826 Duckmatic::erase_bezier(const etl::handle<Bezier> &bezier)
827 {
828         std::list<handle<Bezier> >::iterator iter;
829
830         for(iter=bezier_list_.begin();iter!=bezier_list_.end();++iter)
831         {
832                 if(*iter==bezier)
833                 {
834                         bezier_list_.erase(iter);
835                         return;
836                 }
837         }
838         synfig::warning("Unable to find bezier to erase!");
839 }
840
841 etl::handle<Duckmatic::Duck>
842 Duckmatic::last_duck()const
843 {
844         DuckMap::const_iterator iter(duck_map.find(last_duck_guid));
845         if(iter!=duck_map.end())
846                 return iter->second;
847         return 0;
848 }
849
850 etl::handle<Duckmatic::Bezier>
851 Duckmatic::last_bezier()const
852 {
853         return bezier_list_.back();
854 }
855
856 etl::handle<Duckmatic::Duck>
857 Duckmatic::find_duck(synfig::Point point, synfig::Real radius, Duck::Type type)
858 {
859         if(radius==0)radius=10000000;
860
861         if(type==Duck::TYPE_DEFAULT)
862                 type=get_type_mask();
863
864         Real closest(10000000);
865         etl::handle<Duck> ret;
866
867         DuckMap::const_iterator iter;
868
869         for(iter=duck_map.begin();iter!=duck_map.end();++iter)
870         {
871                 const Duck::Handle& duck(iter->second);
872
873                 if(duck->get_ignore() ||
874                    (duck->get_type() && !(type & duck->get_type())))
875                         continue;
876
877                 Real dist((duck->get_trans_point()-point).mag_squared());
878
879                 if(duck->get_type()&Duck::TYPE_VERTEX)
880                         dist*=1.0001;
881                 else if(duck->get_type()&Duck::TYPE_TANGENT && duck->get_scalar()>0)
882                         dist*=1.00005;
883                 else if(duck->get_type()&Duck::TYPE_RADIUS)
884                         dist*=0.9999;
885
886                 if(dist<=closest)
887                 {
888                         closest=dist;
889                         ret=duck;
890                 }
891         }
892
893         if(radius==0 || closest<radius*radius)
894                 return ret;
895
896         return 0;
897 }
898
899 etl::handle<Duckmatic::Bezier>
900 Duckmatic::find_bezier(synfig::Point point, synfig::Real radius,float* location)
901 {
902         return find_bezier(point,radius,radius,location);
903 }
904
905 etl::handle<Duckmatic::Bezier>
906 Duckmatic::find_bezier(synfig::Point pos, synfig::Real scale, synfig::Real radius, float* location)
907 {
908         if(radius==0)radius=10000000;
909         Real closest(10000000);
910         etl::handle<Bezier> ret;
911
912         bezier<Point>   curve;
913
914         Real    d,step;
915         float   time = 0;
916         float   best_time = 0;
917
918         for(std::list<handle<Bezier> >::const_iterator iter=bezier_list().begin();iter!=bezier_list().end();++iter)
919         {
920                 curve[0] = (*iter)->p1->get_trans_point();
921                 curve[1] = (*iter)->c1->get_trans_point();
922                 curve[2] = (*iter)->c2->get_trans_point();
923                 curve[3] = (*iter)->p2->get_trans_point();
924                 curve.sync();
925
926 #if 0
927                 // I don't know why this doesn't work
928                 time=curve.find_closest(pos,6);
929                 d=((curve(time)-pos).mag_squared());
930
931 #else
932                 //set the step size based on the size of the picture
933                 d = (curve[1] - curve[0]).mag() + (curve[2]-curve[1]).mag()     + (curve[3]-curve[2]).mag();
934
935                 step = d/(2*scale); //want to make the distance between lines happy
936
937                 step = max(step,0.01); //100 samples should be plenty
938                 step = min(step,0.1); //10 is minimum
939
940                 d = find_closest(curve,pos,step,&closest,&time);
941 #endif
942
943                 if(d < closest)
944                 {
945                         closest = d;
946                         ret = *iter;
947                         best_time=time;
948                 }
949         }
950
951         if(closest < radius*radius)
952         {
953                 if(location)
954                         *location = best_time;  // We need to square-root this because we were dealing with squared distances
955
956                 return ret;
957         }
958
959         return 0;
960 }
961
962 bool
963 Duckmatic::save_sketch(const synfig::String& filename)const
964 {
965         ChangeLocale change_locale(LC_NUMERIC, "C");
966         std::ofstream file(filename.c_str());
967
968         if(!file)return false;
969
970         file<<"SKETCH"<<endl;
971
972         std::list<etl::handle<Stroke> >::const_iterator iter;
973
974         for(iter=persistent_stroke_list_.begin();iter!=persistent_stroke_list_.end();++iter)
975         {
976                 file<<"C "
977                         <<(*iter)->color.get_r()<<' '
978                         <<(*iter)->color.get_g()<<' '
979                         <<(*iter)->color.get_b()
980                 <<endl;
981                 std::list<synfig::Point>::const_iterator viter;
982                 for(viter=(*iter)->stroke_data->begin();viter!=(*iter)->stroke_data->end();++viter)
983                 {
984                         file<<"V "
985                                 <<(*viter)[0]<<' '
986                                 <<(*viter)[1]
987                         <<endl;
988                 }
989         }
990         if(!file)return false;
991         sketch_filename_=filename;
992         signal_sketch_saved_();
993         return true;
994 }
995
996 bool
997 Duckmatic::load_sketch(const synfig::String& filename)
998 {
999         ChangeLocale change_locale(LC_NUMERIC, "C");
1000         std::ifstream file(filename.c_str());
1001
1002         if(!file)
1003                 return false;
1004
1005         std::string line;
1006         getline(file,line);
1007
1008         if(line!="SKETCH")
1009         {
1010                 synfig::error("Not a sketch");
1011                 return false;
1012         }
1013
1014         etl::smart_ptr<std::list<synfig::Point> > stroke_data;
1015
1016         while(file)
1017         {
1018                 getline(file,line);
1019
1020                 if(line.empty())
1021                         continue;
1022
1023                 switch(line[0])
1024                 {
1025                 case 'C':
1026                 case 'c':
1027                         {
1028                                 stroke_data.spawn();
1029                                 float r,g,b;
1030                                 if(!strscanf(line,"C %f %f %f",&r, &g, &b))
1031                                 {
1032                                         synfig::warning("Bad color line \"%s\"",line.c_str());
1033                                         r=0;g=0;b=0;
1034                                 }
1035                                 add_persistent_stroke(stroke_data, synfig::Color(r,g,b));
1036                         }
1037                         break;
1038                 case 'V':
1039                 case 'v':
1040                         if(!stroke_data)
1041                         {
1042                                 stroke_data.spawn();
1043                                 add_persistent_stroke(stroke_data, synfig::Color(0,0,0));
1044                         }
1045                         float x,y;
1046                         if(!strscanf(line,"V %f %f",&x, &y))
1047                                 synfig::warning("Bad vertex \"%s\"",line.c_str());
1048                         else
1049                                 stroke_data->push_back(synfig::Vector(x,y));
1050                         break;
1051                 default:
1052                         synfig::warning("Unexpected sketch token '%c'",line[0]);
1053                         break;
1054                 }
1055         }
1056
1057         sketch_filename_=filename;
1058         return true;
1059 }
1060
1061 Duckmatic::Push::Push(Duckmatic *duckmatic_):
1062         duckmatic_(duckmatic_)
1063 {
1064         duck_map=duckmatic_->duck_map;
1065         bezier_list_=duckmatic_->bezier_list_;
1066         duck_data_share_map=duckmatic_->duck_data_share_map;
1067         stroke_list_=duckmatic_->stroke_list_;
1068         duck_dragger_=duckmatic_->duck_dragger_;
1069         needs_restore=true;
1070 }
1071
1072 Duckmatic::Push::~Push()
1073 {
1074         if(needs_restore)
1075                 restore();
1076 }
1077
1078 void
1079 Duckmatic::Push::restore()
1080 {
1081         duckmatic_->duck_map=duck_map;
1082         duckmatic_->bezier_list_=bezier_list_;
1083         duckmatic_->duck_data_share_map=duck_data_share_map;
1084         duckmatic_->stroke_list_=stroke_list_;
1085         duckmatic_->duck_dragger_=duck_dragger_;
1086         needs_restore=false;
1087 }
1088
1089 inline String guid_string(const synfigapp::ValueDesc& x)
1090 {
1091         if(x.parent_is_layer_param())
1092                 return strprintf("%s",x.get_layer()->get_guid().get_string().c_str())+x.get_param_name();
1093         //if(x.is_value_node())
1094                 return strprintf("%s",x.get_value_node()->get_guid().get_string().c_str());
1095 }
1096
1097 inline synfig::GUID calc_duck_guid(const synfigapp::ValueDesc& x,const synfig::TransformStack& transform_stack)
1098 {
1099         synfig::GUID ret(0);
1100
1101         if(x.parent_is_layer_param())
1102         {
1103                 ret=x.get_layer()->get_guid()^synfig::GUID::hasher(x.get_param_name());
1104         }
1105         else
1106         {
1107                 ret=x.get_value_node()->get_guid();
1108         }
1109
1110         ret^=transform_stack.get_guid();
1111         return ret;
1112 }
1113
1114 /*
1115 Duck::Handle
1116 Duckmatic::create_duck_from(const synfigapp::ValueDesc& value_desc,etl::handle<CanvasView> canvas_view, const synfig::TransformStack& transform_stack, int modifier, synfig::ParamDesc *param_desc)
1117 {
1118         synfig::GUID duck_guid(calc_duck_guid(value_desc,transform_stack)^synfig::GUID::hasher(modifier));
1119         etl::handle<Duck> duck=new Duck();
1120
1121         return duck;
1122 }
1123 */
1124
1125 bool
1126 Duckmatic::add_to_ducks(const synfigapp::ValueDesc& value_desc,etl::handle<CanvasView> canvas_view, const synfig::TransformStack& transform_stack, synfig::ParamDesc *param_desc, int multiple)
1127 {
1128         ValueBase::Type type=value_desc.get_value_type();
1129 #define REAL_COOKIE             reinterpret_cast<synfig::ParamDesc*>(28)
1130         switch(type)
1131         {
1132         case ValueBase::TYPE_REAL:
1133
1134                 if(!param_desc || param_desc==REAL_COOKIE || !param_desc->get_origin().empty())
1135                 {
1136                         etl::handle<Duck> duck=new Duck();
1137                         duck->set_transform_stack(transform_stack);
1138                         duck->set_radius(true);
1139                         duck->set_type(Duck::TYPE_RADIUS);
1140
1141                         // put the duck on the right hand side of the center
1142                         duck->set_point(Point(value_desc.get_value(get_time()).get(Real()), 0));
1143                         duck->set_name(guid_string(value_desc));
1144                         if(value_desc.is_value_node())
1145                         {
1146                                 // If the ValueNode can be directly manipulated,
1147                                 // then set it as so.
1148                                 duck->set_editable(synfigapp::is_editable(value_desc.get_value_node()));
1149                         }
1150                         else
1151                         {
1152                                 duck->set_editable(true);
1153                         }
1154
1155                         if(param_desc && param_desc!=REAL_COOKIE)
1156                         {
1157                                 if(!param_desc->get_origin().empty())
1158                                 {
1159                                         synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1160                                         /*
1161                                         duck->set_origin(value_desc_origin.get_value(get_time()).get(synfig::Point()));
1162                                         */
1163                                         add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1164                                         duck->set_origin(last_duck());
1165                                 }
1166                                 duck->set_scalar(param_desc->get_scalar());
1167                         }
1168
1169                         duck->signal_edited().clear(); // value_desc.get_value_type() == ValueBase::TYPE_REAL:
1170                         duck->signal_edited().connect(
1171                                 sigc::bind(
1172                                         sigc::mem_fun(
1173                                                 *canvas_view,
1174                                                 &studio::CanvasView::on_duck_changed),
1175                                         value_desc));
1176                         duck->set_value_desc(value_desc);
1177
1178                         duck->signal_user_click(2).connect(
1179                                 sigc::bind(
1180                                         sigc::bind(
1181                                                 sigc::bind(
1182                                                         sigc::mem_fun(
1183                                                                 *canvas_view,
1184                                                                 &studio::CanvasView::popup_param_menu),
1185                                                         false),
1186                                                 0.0f),
1187                                         value_desc));
1188
1189                         duck->set_guid(calc_duck_guid(value_desc,transform_stack)^synfig::GUID::hasher(multiple));
1190
1191                         add_duck(duck);
1192
1193                         return true;
1194                 }
1195                 break;
1196
1197         case ValueBase::TYPE_ANGLE:
1198
1199                 if(!param_desc || param_desc==REAL_COOKIE || !param_desc->get_origin().empty())
1200                 {
1201                         etl::handle<Duck> duck=new Duck();
1202                         duck->set_type(Duck::TYPE_ANGLE);
1203                         duck->set_transform_stack(transform_stack);
1204                         synfig::Angle angle;
1205
1206                         angle=value_desc.get_value(get_time()).get(Angle());
1207                         duck->set_point(Point(Angle::cos(angle).get(),Angle::sin(angle).get()));
1208                         duck->set_name(guid_string(value_desc));
1209                         if(value_desc.is_value_node())
1210                         {
1211                                 ValueNode::Handle value_node=value_desc.get_value_node();
1212                                 //duck->set_name(strprintf("%x",value_node.get()));
1213
1214                                 // If the ValueNode can be directly manipulated,
1215                                 // then set it as so.
1216                                 duck->set_editable(synfigapp::is_editable(value_desc.get_value_node()));
1217                         }
1218                         else
1219                         {
1220                                 //angle=(value_desc.get_value().get(Angle()));
1221                                 //duck->set_point(Point(Angle::cos(angle).get(),Angle::sin(angle).get()));
1222                                 //duck->set_name(strprintf("%x",value_desc.get_layer().get())+value_desc.get_param_name());
1223                                 duck->set_editable(true);
1224                         }
1225
1226                         if(param_desc && param_desc!=REAL_COOKIE)
1227                         {
1228                                 if(!param_desc->get_origin().empty())
1229                                 {
1230                                         synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1231                                         /*
1232                                         duck->set_origin(value_desc_origin.get_value(get_time()).get(synfig::Point()));
1233                                         */
1234                                         add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1235                                         duck->set_origin(last_duck());
1236                                 }
1237                                 duck->set_scalar(param_desc->get_scalar());
1238                         }
1239
1240                         duck->signal_edited().clear(); // value_desc.get_value_type() == ValueBase::TYPE_ANGLE:
1241                         duck->signal_edited_angle().clear();
1242                         duck->signal_edited_angle().connect(
1243                                 sigc::bind(
1244                                         sigc::mem_fun(
1245                                                 *canvas_view,
1246                                                 &studio::CanvasView::on_duck_angle_changed),
1247                                         value_desc));
1248                         duck->set_value_desc(value_desc);
1249
1250                         duck->signal_user_click(2).connect(
1251                                 sigc::bind(
1252                                         sigc::bind(
1253                                                 sigc::bind(
1254                                                         sigc::mem_fun(
1255                                                                 *canvas_view,
1256                                                                 &studio::CanvasView::popup_param_menu),
1257                                                         false),
1258                                                 0.0f),
1259                                         value_desc));
1260                         duck->set_guid(calc_duck_guid(value_desc,transform_stack)^synfig::GUID::hasher(multiple));
1261
1262                         add_duck(duck);
1263
1264                         return true;
1265                 }
1266                 break;
1267
1268         case ValueBase::TYPE_VECTOR:
1269                 {
1270                         etl::handle<Duck> duck=new Duck();
1271                         duck->set_transform_stack(transform_stack);
1272
1273                         duck->set_point(value_desc.get_value(get_time()).get(Point()));
1274                         duck->set_name(guid_string(value_desc));
1275                         if(value_desc.is_value_node())
1276                         {
1277                                 //duck->set_name(strprintf("%x",value_desc.get_value_node().get()));
1278
1279                                 // If the ValueNode can be directly manipulated,
1280                                 // then set it as so.
1281                                 duck->set_editable(synfigapp::is_editable(value_desc.get_value_node()));
1282                         }
1283                         else
1284                         {
1285                                 //duck->set_point(value_desc.get_value().get(Point()));
1286                                 //duck->set_name(strprintf("%x",value_desc.get_layer().get())+value_desc.get_param_name());
1287                                 duck->set_editable(true);
1288                         }
1289
1290                         // If we were passed a parameter description
1291                         if(param_desc)
1292                         {
1293                                 if(!param_desc->get_connect().empty())
1294                                 {
1295                                         synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_connect());
1296                                         Duck::Handle connect_duck;
1297                                         if(duck_map.find(calc_duck_guid(value_desc_origin,transform_stack)^synfig::GUID::hasher(0))!=duck_map.end())
1298                                         {
1299                                                 connect_duck=duck_map[calc_duck_guid(value_desc_origin,transform_stack)^synfig::GUID::hasher(0)];
1300                                         }
1301                                         else
1302                                         {
1303                                                 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1304                                                 connect_duck=last_duck();
1305                                         }
1306                                         duck->set_connect_duck(connect_duck);
1307                                 }
1308                                 if(!param_desc->get_box().empty())
1309                                 {
1310                                         synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_box());
1311                                         add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1312                                         duck->set_box_duck(last_duck());
1313                                 }
1314
1315                                 // If we have an origin
1316                                 if(!param_desc->get_origin().empty())
1317                                 {
1318                                         synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1319                                         /*
1320                                         duck->set_origin(value_desc_origin.get_value(get_time()).get(synfig::Point()));
1321                                         */
1322                                         add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1323                                         duck->set_origin(last_duck());
1324                                         duck->set_type(Duck::TYPE_VERTEX);
1325                                 }
1326                                 else
1327                                         duck->set_type(Duck::TYPE_POSITION);
1328
1329                                 duck->set_scalar(param_desc->get_scalar());
1330                         }
1331                         else
1332                                 duck->set_type(Duck::TYPE_POSITION);
1333
1334                         duck->signal_edited().clear(); // value_desc.get_value_type() == ValueBase::TYPE_VECTOR:
1335                         duck->signal_edited().connect(
1336                                 sigc::bind(
1337                                         sigc::mem_fun(
1338                                                 *canvas_view,
1339                                                 &studio::CanvasView::on_duck_changed),
1340                                         value_desc));
1341                         duck->set_value_desc(value_desc);
1342
1343                         duck->signal_user_click(2).connect(
1344                                 sigc::bind(
1345                                         sigc::bind(
1346                                                 sigc::bind(
1347                                                         sigc::mem_fun(
1348                                                                 *canvas_view,
1349                                                                 &studio::CanvasView::popup_param_menu),
1350                                                         false),
1351                                                 1.0f),
1352                                         value_desc));
1353                         duck->set_guid(calc_duck_guid(value_desc,transform_stack)^synfig::GUID::hasher(multiple));
1354                         add_duck(duck);
1355
1356                         return true;
1357                 }
1358                 break;
1359         case ValueBase::TYPE_SEGMENT:
1360                 {
1361                         etl::handle<Bezier> bezier(new Bezier());
1362                         ValueNode_Composite::Handle value_node;
1363
1364                         if(value_desc.is_value_node() &&
1365                                 (value_node=ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node())))
1366                         {
1367                                 if(!add_to_ducks(synfigapp::ValueDesc(value_node,0),canvas_view,transform_stack))
1368                                         return false;
1369                                 bezier->p1=last_duck();
1370                                 bezier->p1->set_type(Duck::TYPE_VERTEX);
1371                                 if(!add_to_ducks(synfigapp::ValueDesc(value_node,1),canvas_view,transform_stack))
1372                                         return false;
1373                                 bezier->c1=last_duck();
1374                                 bezier->c1->set_type(Duck::TYPE_TANGENT);
1375                                 bezier->c1->set_origin(bezier->p1);
1376                                 bezier->c1->set_scalar(TANGENT_BEZIER_SCALE);
1377                                 bezier->c1->set_tangent(true);
1378
1379                                 if(!add_to_ducks(synfigapp::ValueDesc(value_node,2),canvas_view,transform_stack))
1380                                         return false;
1381                                 bezier->p2=last_duck();
1382                                 bezier->p2->set_type(Duck::TYPE_VERTEX);
1383                                 if(!add_to_ducks(synfigapp::ValueDesc(value_node,3),canvas_view,transform_stack))
1384                                         return false;
1385                                 bezier->c2=last_duck();
1386                                 bezier->c2->set_type(Duck::TYPE_TANGENT);
1387                                 bezier->c2->set_origin(bezier->p2);
1388                                 bezier->c2->set_scalar(-TANGENT_BEZIER_SCALE);
1389                                 bezier->c2->set_tangent(true);
1390
1391                                 bezier->signal_user_click(2).connect(
1392                                         sigc::bind(
1393                                                 sigc::mem_fun(
1394                                                         *canvas_view,
1395                                                         &studio::CanvasView::popup_param_menu_bezier),
1396                                                 value_desc));
1397
1398                                 add_bezier(bezier);
1399                         }
1400                         else if(value_desc.get_value().is_valid())
1401                         {
1402                                 Segment segment=value_desc.get_value();
1403                                 etl::handle<Duck> duck_p,duck_c;
1404                                 synfig::String name;
1405                                 if(param_desc)
1406                                 {
1407                                         name=param_desc->get_local_name();
1408                                 }
1409                                 else
1410                                 {
1411                                         name=guid_string(value_desc);
1412                                 }
1413
1414                                 duck_p=new Duck(segment.p1);
1415                                 duck_p->set_name(name+".P1");
1416                                 duck_p->set_type(Duck::TYPE_VERTEX);
1417                                 add_duck(duck_p);
1418
1419                                 duck_c=new Duck(segment.t1);
1420                                 duck_c->set_name(name+".T1");
1421                                 duck_c->set_type(Duck::TYPE_TANGENT);
1422                                 add_duck(duck_c);
1423                                 duck_c->set_origin(duck_p);
1424                                 duck_c->set_scalar(TANGENT_HANDLE_SCALE);
1425                                 duck_c->set_tangent(true);
1426
1427                                 bezier->p1=duck_p;
1428                                 bezier->c1=duck_c;
1429
1430                                 duck_p=new Duck(segment.p2);
1431                                 duck_p->set_name(name+".P2");
1432                                 duck_p->set_type(Duck::TYPE_VERTEX);
1433                                 add_duck(duck_p);
1434
1435                                 duck_c=new Duck(segment.t2);
1436                                 duck_c->set_type(Duck::TYPE_TANGENT);
1437                                 duck_c->set_name(name+".T2");
1438                                 add_duck(duck_c);
1439                                 duck_c->set_origin(duck_p);
1440                                 duck_c->set_scalar(-TANGENT_HANDLE_SCALE);
1441                                 duck_c->set_tangent(true);
1442
1443                                 bezier->p2=duck_p;
1444                                 bezier->c2=duck_c;
1445                                 add_bezier(bezier);
1446                         }
1447
1448                         return true;
1449                 }
1450                 break;
1451         case ValueBase::TYPE_BLINEPOINT:
1452         {
1453
1454                 if(value_desc.is_value_node() &&
1455                         ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()))
1456                 {
1457                         ValueNode_Composite::Handle value_node;
1458                         value_node=ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node());
1459
1460                         if(!add_to_ducks(synfigapp::ValueDesc(value_node,0),canvas_view,transform_stack))
1461                                 return false;
1462                         etl::handle<Duck> vertex_duck(last_duck());
1463                         vertex_duck->set_type(Duck::TYPE_VERTEX);
1464                         if(!add_to_ducks(synfigapp::ValueDesc(value_node,4),canvas_view,transform_stack))
1465                                 return false;
1466                         etl::handle<Duck> t1_duck(last_duck());
1467
1468                         t1_duck->set_origin(vertex_duck);
1469                         t1_duck->set_scalar(-TANGENT_HANDLE_SCALE);
1470                         t1_duck->set_tangent(true);
1471
1472                         etl::handle<Duck> t2_duck;
1473
1474                         // If the tangents are split
1475                         if((*value_node->get_link("split"))(get_time()).get(bool()))
1476                         {
1477                                 if(!add_to_ducks(synfigapp::ValueDesc(value_node,5),canvas_view,transform_stack))
1478                                         return false;
1479                                 t2_duck=last_duck();
1480                                 t2_duck->set_origin(vertex_duck);
1481                                 t2_duck->set_scalar(TANGENT_HANDLE_SCALE);
1482                                 t2_duck->set_tangent(true);
1483                         }
1484                         else
1485                         {
1486                                 if(!add_to_ducks(synfigapp::ValueDesc(value_node,4),canvas_view,transform_stack))
1487                                         return false;
1488                                 t2_duck=last_duck();
1489                                 t2_duck->set_origin(vertex_duck);
1490                                 t2_duck->set_scalar(TANGENT_HANDLE_SCALE);
1491                                 t2_duck->set_tangent(true);
1492                         }
1493                         return true;
1494                 }
1495
1496         }
1497         break;
1498         case ValueBase::TYPE_LIST:
1499         {
1500                 // Check for BLine
1501                 if (value_desc.is_value_node() &&
1502                         ValueNode_BLine::Handle::cast_dynamic(value_desc.get_value_node()))
1503                 {
1504                         ValueNode_BLine::Handle value_node;
1505                         value_node=ValueNode_BLine::Handle::cast_dynamic(value_desc.get_value_node());
1506
1507                         int i,first=-1;
1508
1509                         etl::handle<Bezier> bezier;
1510                         etl::handle<Duck> first_duck;
1511                         etl::handle<Duck> duck, tduck;
1512
1513                         for (i = 0; i < value_node->link_count(); i++)
1514                         {
1515                                 float amount(value_node->list[i].amount_at_time(get_time()));
1516
1517                                 // skip vertices that aren't fully on
1518                                 if (amount < 0.9999f)
1519                                         continue;
1520
1521                                 // remember the index of the first vertex we didn't skip
1522                                 if (first == -1)
1523                                         first = i;
1524
1525                                 BLinePoint bline_point((*value_node->get_link(i))(get_time()));
1526
1527                                 // try casting the vertex to Composite - this tells us whether it is composite or not
1528                                 ValueNode_Composite::Handle composite_vertex_value_node(
1529                                         ValueNode_Composite::Handle::cast_dynamic(value_node->get_link(i)));
1530
1531                                 // add the vertex duck - it's a composite
1532                                 if(composite_vertex_value_node)
1533                                 {
1534                                         if (add_to_ducks(synfigapp::ValueDesc(composite_vertex_value_node,0),canvas_view,transform_stack))
1535                                         {
1536                                                 duck=last_duck();
1537                                                 if(i==first)
1538                                                         first_duck=duck;
1539                                                 duck->set_type(Duck::TYPE_VERTEX);
1540
1541                                                 duck->signal_user_click(2).clear();
1542                                                 duck->signal_user_click(2).connect(
1543                                                         sigc::bind(
1544                                                                 sigc::bind(
1545                                                                         sigc::bind(
1546                                                                                 sigc::mem_fun(
1547                                                                                         *canvas_view,
1548                                                                                         &studio::CanvasView::popup_param_menu),
1549                                                                                 false),
1550                                                                         1.0f),
1551                                                                 synfigapp::ValueDesc(value_node,i)));
1552                                                 duck->set_value_desc(synfigapp::ValueDesc(value_node,i));
1553
1554                                                 if(param_desc)
1555                                                 {
1556                                                         if(!param_desc->get_origin().empty())
1557                                                         {
1558                                                                 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1559                                                                 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1560                                                                 duck->set_origin(last_duck());
1561 /*
1562                                                                 ValueBase value(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()));
1563                                                                 if(value.same_type_as(synfig::Point()))
1564                                                                         duck->set_origin(value.get(synfig::Point()));
1565 */
1566                                                         }
1567                                                 }
1568                                         }
1569                                         else
1570                                                 return false;
1571                                 }
1572                                 // else it's not a composite
1573                                 else
1574                                 {
1575                                         duck=new Duck(bline_point.get_vertex());
1576                                         if(i==first)
1577                                                 first_duck=duck;
1578                                         duck->set_transform_stack(transform_stack);
1579                                         duck->set_editable(false);
1580                                         //duck->set_name(strprintf("%x-vertex",value_node->get_link(i).get()));
1581                                         duck->set_name(guid_string(synfigapp::ValueDesc(value_node,i))+".v");
1582
1583                                         duck->set_type(Duck::TYPE_VERTEX);
1584                                         if(param_desc)
1585                                         {
1586                                                 if(!param_desc->get_origin().empty())
1587                                                 {
1588                                                         synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1589                                                         add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1590                                                         duck->set_origin(last_duck());
1591 /*
1592                                                         ValueBase value(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()));
1593                                                         if(value.same_type_as(synfig::Point()))
1594                                                                 duck->set_origin(value.get(synfig::Point()));
1595 */
1596                                                 }
1597                                         }
1598                                         duck->set_guid(calc_duck_guid(synfigapp::ValueDesc(value_node,i),transform_stack)^synfig::GUID::hasher(".v"));
1599                                         duck=add_similar_duck(duck);
1600 //                                      add_duck(duck);
1601                                 }
1602
1603                                 // Add the width duck if it is a parameter with a hint (ie. "width") or if it isn't a parameter
1604                                 if ((param_desc && !param_desc->get_hint().empty()) ||
1605                                         !param_desc)
1606                                 {
1607                                         etl::handle<Duck> width;
1608                                         if (add_to_ducks(synfigapp::ValueDesc(composite_vertex_value_node,1),canvas_view,transform_stack,REAL_COOKIE))
1609                                         {
1610                                                 width=last_duck();
1611                                                 width->set_origin(duck);
1612                                                 width->set_type(Duck::TYPE_WIDTH);
1613                                                 width->set_name(guid_string(synfigapp::ValueDesc(value_node,i))+".w");
1614
1615                                                 // if the bline is a layer's parameter, scale the width duck by the layer's "width" parameter
1616                                                 if (param_desc)
1617                                                 {
1618                                                         ValueBase value(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_hint()).get_value(get_time()));
1619                                                         if(value.same_type_as(synfig::Real()))
1620                                                                 width->set_scalar(value.get(synfig::Real())*0.5f);
1621                                                         // if it doesn't have a "width" parameter, scale by 0.5f instead
1622                                                         else
1623                                                                 width->set_scalar(0.5f);
1624                                                 }
1625                                                 // otherwise just present the raw unscaled width
1626                                                 else
1627                                                         width->set_scalar(0.5f);
1628                                         }
1629                                         else
1630                                                 synfig::error("Unable to add width duck!");
1631                                 }
1632
1633                                 // each bezier uses t2 of one point and t1 of the next
1634                                 // the first time through this loop we won't have the t2 duck from the previous vertex
1635                                 // and so we don't make a bezier.  instead we skip on to t2 for this point
1636                                 if(bezier)
1637                                 {
1638                                         // Add the tangent1 duck
1639                                         if(composite_vertex_value_node)
1640                                         {
1641                                                 if(!add_to_ducks(synfigapp::ValueDesc(composite_vertex_value_node,4),canvas_view,transform_stack))
1642                                                         return false;
1643                                                 tduck=last_duck();
1644                                         }
1645                                         else
1646                                         {
1647                                                 tduck=new Duck(bline_point.get_tangent1());
1648                                                 tduck->set_transform_stack(transform_stack);
1649                                                 tduck->set_editable(false);
1650                                                 tduck->set_name(guid_string(synfigapp::ValueDesc(value_node,i))+".t1");
1651 //                                              tduck->set_name(strprintf("%x-tangent1",value_node->get_link(i).get()));
1652                                                 tduck->set_guid(calc_duck_guid(synfigapp::ValueDesc(value_node,i),transform_stack)^synfig::GUID::hasher(".t1"));
1653                                                 tduck=add_similar_duck(tduck);
1654 //                                              add_duck(duck);
1655                                         }
1656
1657                                         tduck->set_origin(duck);
1658                                         tduck->set_scalar(-TANGENT_BEZIER_SCALE);
1659                                         tduck->set_tangent(true);
1660
1661                                         bezier->p2=duck;
1662                                         bezier->c2=tduck;
1663
1664                                         bezier->signal_user_click(2).connect(
1665                                                 sigc::bind(
1666                                                         sigc::mem_fun(
1667                                                                 *canvas_view,
1668                                                                 &studio::CanvasView::popup_param_menu_bezier),
1669                                                         synfigapp::ValueDesc(value_node,i)));
1670
1671                                         duck->signal_user_click(2).clear();
1672                                         duck->signal_user_click(2).connect(
1673                                                 sigc::bind(
1674                                                         sigc::bind(
1675                                                                 sigc::bind(
1676                                                                         sigc::mem_fun(
1677                                                                                 *canvas_view,
1678                                                                                 &studio::CanvasView::popup_param_menu),
1679                                                                         false),
1680                                                                 1.0f),
1681                                                         synfigapp::ValueDesc(value_node,i)));
1682                                         duck->set_value_desc(synfigapp::ValueDesc(value_node,i));
1683
1684                                         add_bezier(bezier);
1685                                         bezier=0;
1686                                 }
1687
1688                                 // don't start a new bezier for the last point in the line if we're not looped
1689                                 if(i+1>=value_node->link_count() && !value_node->get_loop())
1690                                         continue;
1691
1692                                 bezier=new Bezier();
1693
1694                                 // Add the tangent2 duck
1695                                 if(composite_vertex_value_node)
1696                                 {
1697                                         int i=bline_point.get_split_tangent_flag()?5:4;
1698                                         if(!add_to_ducks(synfigapp::ValueDesc(composite_vertex_value_node,i),canvas_view,transform_stack,0,2))
1699                                                 return false;
1700                                         tduck=last_duck();
1701                                 }
1702                                 else
1703                                 {
1704                                         if(bline_point.get_split_tangent_flag())
1705                                                 tduck=new Duck(bline_point.get_tangent2());
1706                                         else
1707                                                 tduck=new Duck(bline_point.get_tangent1());
1708
1709                                         tduck->set_transform_stack(transform_stack);
1710                                         tduck->set_name(guid_string(synfigapp::ValueDesc(value_node,i))+".t2");
1711                                         tduck->set_guid(calc_duck_guid(synfigapp::ValueDesc(value_node,i),transform_stack)^synfig::GUID::hasher(".t2"));
1712                                         tduck->set_editable(false);
1713                                         tduck=add_similar_duck(tduck);
1714 //                                      add_duck(duck);
1715                                         if(param_desc)
1716                                         {
1717                                                 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1718                                                 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1719                                                 duck->set_origin(last_duck());
1720 /*
1721                                                 ValueBase value(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()));
1722                                                 if(value.same_type_as(synfig::Point()))
1723                                                         duck->set_origin(value.get(synfig::Point()));
1724 */
1725 //                                              if(!param_desc->get_origin().empty())
1726 //                                                      duck->set_origin(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()).get(synfig::Point()));
1727                                         }
1728                                         duck->signal_user_click(2).clear();
1729                                         duck->signal_user_click(2).connect(
1730                                                 sigc::bind(
1731                                                         sigc::bind(
1732                                                                 sigc::bind(
1733                                                                         sigc::mem_fun(
1734                                                                                 *canvas_view,
1735                                                                                 &studio::CanvasView::popup_param_menu),
1736                                                                         false),
1737                                                                 1.0f),
1738                                                         synfigapp::ValueDesc(value_node,i)));
1739                                         duck->set_value_desc(synfigapp::ValueDesc(value_node,i));
1740
1741                                 }
1742
1743                                 tduck->set_origin(duck);
1744                                 tduck->set_scalar(TANGENT_BEZIER_SCALE);
1745                                 tduck->set_tangent(true);
1746
1747                                 bezier->p1=duck;
1748                                 bezier->c1=tduck;
1749                         }
1750
1751                         // Loop if necessary
1752                         if(bezier && value_node->get_loop())
1753                         {
1754                                 BLinePoint bline_point((*value_node->get_link(first))(get_time()));
1755
1756                                 ValueNode_Composite::Handle composite_vertex_value_node(
1757                                         ValueNode_Composite::Handle::cast_dynamic(
1758                                                 value_node->get_link(first)));
1759
1760                                 // Add the vertex duck
1761                                 duck=first_duck;
1762
1763                                 // Add the tangent1 duck
1764                                 if(composite_vertex_value_node)
1765                                 {
1766                                         if(!add_to_ducks(synfigapp::ValueDesc(composite_vertex_value_node,4),canvas_view,transform_stack))
1767                                                 return false;
1768                                         tduck=last_duck();
1769                                 }
1770                                 else
1771                                 {
1772                                         tduck=new Duck(bline_point.get_tangent1());
1773                                         tduck->set_transform_stack(transform_stack);
1774                                         tduck->set_editable(false);
1775                                         tduck->set_name(guid_string(synfigapp::ValueDesc(value_node,first))+".t1");
1776                                         //tduck->set_name(strprintf("%x-tangent1",value_node->get_link(first).get()));
1777                                         tduck=add_similar_duck(tduck);
1778                                         tduck->set_guid(calc_duck_guid(synfigapp::ValueDesc(value_node,first),transform_stack)^synfig::GUID::hasher(".t1"));
1779                                         //add_duck(duck);
1780                                 }
1781
1782                                 tduck->set_origin(duck);
1783                                 tduck->set_scalar(-TANGENT_BEZIER_SCALE);
1784                                 tduck->set_tangent(true);
1785
1786                                 bezier->p2=duck;
1787                                 bezier->c2=tduck;
1788
1789                                 bezier->signal_user_click(2).connect(
1790                                         sigc::bind(
1791                                                 sigc::mem_fun(
1792                                                         *canvas_view,
1793                                                         &studio::CanvasView::popup_param_menu_bezier),
1794                                                 synfigapp::ValueDesc(value_node,first)));
1795
1796                                 duck->signal_user_click(2).clear();
1797                                 duck->signal_user_click(2).connect(
1798                                         sigc::bind(
1799                                                 sigc::bind(
1800                                                         sigc::bind(
1801                                                                 sigc::mem_fun(
1802                                                                         *canvas_view,
1803                                                                         &studio::CanvasView::popup_param_menu),
1804                                                                 false),
1805                                                         1.0f),
1806                                                 synfigapp::ValueDesc(value_node,first)));
1807                                 duck->set_value_desc(synfigapp::ValueDesc(value_node,first));
1808
1809                                 add_bezier(bezier);
1810                                 bezier=0;
1811                         }
1812                         return true;
1813                 }
1814                 else // Check for DynamicList
1815                 if(value_desc.is_value_node() &&
1816                         ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_value_node()))
1817                 {
1818                         ValueNode_DynamicList::Handle value_node;
1819                         value_node=ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_value_node());
1820                         int i;
1821
1822                         if(value_node->get_contained_type()==ValueBase::TYPE_VECTOR)
1823                         {
1824                                 Bezier bezier;
1825                                 etl::handle<Duck> first_duck, duck;
1826                                 int first = -1;
1827                                 for(i=0;i<value_node->link_count();i++)
1828                                 {
1829                                         if(!value_node->list[i].status_at_time(get_time()))
1830                                                 continue;
1831                                         if(!add_to_ducks(synfigapp::ValueDesc(value_node,i),canvas_view,transform_stack))
1832                                                 return false;
1833                                         duck = last_duck();
1834
1835                                         // remember the index of the first vertex we didn't skip
1836                                         if (first == -1)
1837                                         {
1838                                                 first = i;
1839                                                 first_duck = duck;
1840                                         }
1841
1842                                         if(param_desc && !param_desc->get_origin().empty())
1843                                         {
1844                                                 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1845                                                 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1846                                                 duck->set_origin(last_duck());
1847 /*
1848                                                 ValueBase value(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()));
1849                                                 if(value.same_type_as(synfig::Point()))
1850                                                         duck->set_origin(value.get(synfig::Point()));
1851 */
1852 //                                              if(!param_desc->get_origin().empty())
1853 //                                                      last_duck()->set_origin(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()).get(synfig::Point()));
1854                                         }
1855                                         duck->set_type(Duck::TYPE_VERTEX);
1856                                         bezier.p1=bezier.p2;bezier.c1=bezier.c2;
1857                                         bezier.p2=bezier.c2=duck;
1858
1859                                         if (first != i)
1860                                         {
1861                                                 handle<Bezier> bezier_(new Bezier());
1862                                                 bezier_->p1=bezier.p1;
1863                                                 bezier_->c1=bezier.c1;
1864                                                 bezier_->p2=bezier.p2;
1865                                                 bezier_->c2=bezier.c2;
1866                                                 add_bezier(bezier_);
1867                                                 last_bezier()->signal_user_click(2).connect(
1868                                                         sigc::bind(
1869                                                                 sigc::mem_fun(
1870                                                                         *canvas_view,
1871                                                                         &studio::CanvasView::popup_param_menu_bezier),
1872                                                                 synfigapp::ValueDesc(value_node,i)));
1873                                         }
1874                                 }
1875
1876                                 if (value_node->get_loop() && first != -1 && first_duck != duck)
1877                                 {
1878                                         duck = first_duck;
1879
1880                                         bezier.p1=bezier.p2;bezier.c1=bezier.c2;
1881                                         bezier.p2=bezier.c2=duck;
1882
1883                                         handle<Bezier> bezier_(new Bezier());
1884                                         bezier_->p1=bezier.p1;
1885                                         bezier_->c1=bezier.c1;
1886                                         bezier_->p2=bezier.p2;
1887                                         bezier_->c2=bezier.c2;
1888                                         add_bezier(bezier_);
1889                                         last_bezier()->signal_user_click(2).connect(
1890                                                 sigc::bind(
1891                                                         sigc::mem_fun(
1892                                                                 *canvas_view,
1893                                                                 &studio::CanvasView::popup_param_menu_bezier),
1894                                                         synfigapp::ValueDesc(value_node,first)));
1895                                 }
1896                         }
1897                         else if(value_node->get_contained_type()==ValueBase::TYPE_SEGMENT)
1898                         {
1899                                 for(i=0;i<value_node->link_count();i++)
1900                                 {
1901                                         if(!value_node->list[i].status_at_time(get_time()))
1902                                                 continue;
1903                                         if(!add_to_ducks(synfigapp::ValueDesc(value_node,i),canvas_view,transform_stack))
1904                                                 return false;
1905                                 }
1906                         }
1907                         else
1908                                 return false;
1909                 }
1910                 else
1911                 {
1912                         // WRITEME
1913                 }
1914
1915                 return true;
1916         }
1917
1918         break;
1919         default:
1920                 break;
1921         }
1922         return false;
1923 }