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