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