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