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