1 /* === S Y N F I G ========================================================= */
2 /*! \file duckmatic.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
36 #include <ETL/hermite>
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>
48 #include <synfig/curve_helper.h>
50 #include <sigc++/retype_return.h>
51 #include <sigc++/retype.h>
52 #include <sigc++/hide.h>
53 #include <sigc++/bind.h>
55 #include "canvasview.h"
57 #include "onemoment.h"
63 /* === U S I N G =========================================================== */
67 using namespace synfig;
68 using namespace studio;
70 /* === M A C R O S ========================================================= */
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
76 /* leave this alone or the bezier won't lie on top of the bline */
77 #define TANGENT_BEZIER_SCALE 0.33333333333333333
79 /* === G L O B A L S ======================================================= */
81 /* === P R O C E D U R E S ================================================= */
83 /* === M E T H O D S ======================================================= */
85 /* === E N T R Y P O I N T ================================================= */
87 Duckmatic::Duckmatic():
88 type_mask(Duck::TYPE_ALL-Duck::TYPE_WIDTH),
91 grid_size(1.0/4.0,1.0/4.0),
92 show_persistent_strokes(true)
95 drag_offset_=Point(0,0);
99 Duckmatic::~Duckmatic()
103 if (Duck::duck_count)
104 synfig::error("%d ducks not yet deleted!", Duck::duck_count);
106 if (getenv("SYNFIG_DEBUG_DESTRUCTORS"))
107 synfig::info("Duckmatic::~Duckmatic(): Deleted");
111 Duckmatic::clear_ducks()
113 duck_data_share_map.clear();
116 //duck_list_.clear();
117 bezier_list_.clear();
118 stroke_list_.clear();
120 if(show_persistent_strokes)
121 stroke_list_=persistent_stroke_list_;
124 //! Returns \a true if the given duck is currently selected
126 Duckmatic::duck_is_selected(const etl::handle<Duck> &duck)const
128 return duck && selected_ducks.count(duck->get_guid());
132 Duckmatic::set_grid_size(const synfig::Vector &s)
137 signal_grid_changed();
142 Duckmatic::set_grid_snap(bool x)
147 signal_grid_changed();
152 Duckmatic::set_guide_snap(bool x)
157 signal_grid_changed();
161 Duckmatic::GuideList::iterator
162 Duckmatic::find_guide_x(synfig::Point pos, float radius)
164 GuideList::iterator iter,best(guide_list_x_.end());
166 for(iter=guide_list_x_.begin();iter!=guide_list_x_.end();++iter)
168 float amount(abs(*iter-pos[0]));
178 Duckmatic::GuideList::iterator
179 Duckmatic::find_guide_y(synfig::Point pos, float radius)
181 GuideList::iterator iter,best(guide_list_y_.end());
183 for(iter=guide_list_y_.begin();iter!=guide_list_y_.end();++iter)
185 float amount(abs(*iter-pos[1]));
196 Duckmatic::clear_selected_ducks()
198 selected_ducks.clear();
199 signal_duck_selection_changed_();
202 etl::handle<Duckmatic::Duck>
203 Duckmatic::get_selected_duck()const
205 if(selected_ducks.empty() || duck_map.empty())
207 return duck_map.find(*selected_ducks.begin())->second;
211 Duckmatic::refresh_selected_ducks()
214 std::set<etl::handle<Duck> >::iterator iter;
215 std::set<etl::handle<Duck> > new_set;
216 if(duck_list().empty())
218 selected_duck_list.clear();
219 signal_duck_selection_changed_();
223 for(iter=selected_duck_list.begin();iter!=selected_duck_list.end();++iter)
225 etl::handle<Duck> similar(find_similar_duck(*iter));
228 new_set.insert(similar);
231 selected_duck_list=new_set;
233 GUIDSet old_set(selected_ducks);
234 GUIDSet::const_iterator iter;
236 for(iter=old_set.begin();iter!=old_set.end();++iter)
238 if(duck_map.count(*iter)==0)
239 selected_ducks.erase(*iter);
242 signal_duck_selection_changed_();
246 Duckmatic::is_duck_group_selectable(const etl::handle<Duck>& x)const
248 const Type type(get_type_mask());
250 if(( x->get_type() && (!(type & x->get_type())) ) )
254 if(x->get_value_desc().parent_is_layer_param() && type & Duck::TYPE_POSITION)
256 Layer::Handle layer(x->get_value_desc().get_layer());
257 String layer_name(layer->get_name());
259 layer_name=="outline" ||
260 layer_name=="region" ||
261 layer_name=="polygon" ||
262 layer_name=="curve_gradient"
265 if((layer_name=="PasteCanvas"|| layer_name=="paste_canvas") &&
266 !layer->get_param("children_lock").get(bool()))
273 Duckmatic::select_all_ducks()
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);
282 Duckmatic::toggle_select_ducks_in_box(const synfig::Vector& tl,const synfig::Vector& br)
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]);
290 DuckMap::const_iterator iter;
291 for(iter=duck_map.begin();iter!=duck_map.end();++iter)
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);
301 Duckmatic::select_ducks_in_box(const synfig::Vector& tl,const synfig::Vector& br)
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]);
309 // Type type(get_type_mask());
311 DuckMap::const_iterator iter;
312 for(iter=duck_map.begin();iter!=duck_map.end();++iter)
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])
317 if(is_duck_group_selectable(iter->second))
318 select_duck(iter->second);
324 Duckmatic::count_selected_ducks()const
326 return selected_ducks.size();
330 Duckmatic::select_duck(const etl::handle<Duck> &duck)
334 selected_ducks.insert(duck->get_guid());
335 signal_duck_selection_changed_();
340 Duckmatic::get_selected_ducks()const
343 GUIDSet::const_iterator iter;
344 const Type type(get_type_mask());
346 for(iter=selected_ducks.begin();iter!=selected_ducks.end();++iter)
348 const DuckMap::const_iterator d_iter(duck_map.find(*iter));
350 if(d_iter==duck_map.end())
353 if(( d_iter->second->get_type() && (!(type & d_iter->second->get_type())) ) )
356 ret.push_back(d_iter->second);
362 Duckmatic::get_duck_list()const
365 DuckMap::const_iterator iter;
366 for(iter=duck_map.begin();iter!=duck_map.end();++iter)
367 ret.push_back(iter->second);
372 Duckmatic::unselect_duck(const etl::handle<Duck> &duck)
374 if(duck && selected_ducks.count(duck->get_guid()))
376 selected_ducks.erase(duck->get_guid());
377 signal_duck_selection_changed_();
382 Duckmatic::toggle_select_duck(const etl::handle<Duck> &duck)
384 if(duck_is_selected(duck))
391 Duckmatic::translate_selected_ducks(const synfig::Vector& vector)
394 duck_dragger_->duck_drag(this,vector);
398 Duckmatic::start_duck_drag(const synfig::Vector& offset)
401 duck_dragger_->begin_duck_drag(this,offset);
403 //drag_offset_=offset;
404 drag_offset_=find_duck(offset)->get_trans_point();
408 Duckmatic::end_duck_drag()
411 return duck_dragger_->end_duck_drag(this);
416 Duckmatic::snap_point_to_grid(const synfig::Point& x, float radius)const
420 GuideList::const_iterator guide_x,guide_y;
421 bool has_guide_x(false), has_guide_y(false);
423 guide_x=find_guide_x(ret,radius);
424 if(guide_x!=guide_list_x_.end())
427 guide_y=find_guide_y(ret,radius);
428 if(guide_y!=guide_list_y_.end())
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]
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;
455 if(abs(ret[0])<abs(ret[1]))
466 DuckDrag_Translate::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
468 last_translate_=Vector(0,0);
470 drag_offset_=duckmatic->find_duck(offset)->get_trans_point();
475 const DuckList selected_ducks(duckmatic->get_selected_ducks());
476 DuckList::const_iterator iter;
479 for(iter=selected_ducks.begin();iter!=selected_ducks.end();++iter)
481 Point p((*iter)->get_trans_point());
482 positions.push_back(p);
487 DuckDrag_Translate::end_duck_drag(Duckmatic* duckmatic)
489 if(last_translate_.mag()>0.0001)
491 duckmatic->signal_edited_selected_ducks();
496 duckmatic->signal_user_click_selected_ducks(0);
502 DuckDrag_Translate::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
504 const DuckList selected_ducks(duckmatic->get_selected_ducks());
505 DuckList::const_iterator iter;
507 synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-drag_offset_);
510 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
512 if((*iter)->get_type()!=Duck::TYPE_VERTEX &&
513 (*iter)->get_type()!=Duck::TYPE_POSITION)
515 (*iter)->set_trans_point(positions[i]+vect);
517 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
519 if((*iter)->get_type()==Duck::TYPE_VERTEX ||
520 (*iter)->get_type()==Duck::TYPE_POSITION)
522 (*iter)->set_trans_point(positions[i]+vect);
524 last_translate_=vect;
528 Duckmatic::signal_edited_selected_ducks()
530 const DuckList ducks(get_selected_ducks());
531 DuckList::const_iterator iter;
533 synfig::GUIDSet old_set(selected_ducks);
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();
539 // Go ahead and call everyone's signals
540 for(iter=ducks.begin();iter!=ducks.end();++iter)
542 if ((*iter)->get_type() == Duck::TYPE_ANGLE)
544 if(!(*iter)->signal_edited_angle()((*iter)->get_rotations()))
546 selected_ducks=old_set;
547 throw String("Bad edit");
552 if(!(*iter)->signal_edited()((*iter)->get_point()))
554 selected_ducks=old_set;
555 throw String("Bad edit");
559 selected_ducks=old_set;
563 Duckmatic::signal_user_click_selected_ducks(int button)
565 const DuckList ducks(get_selected_ducks());
566 DuckList::const_iterator iter;
568 for(iter=ducks.begin();iter!=ducks.end();++iter)
570 (*iter)->signal_user_click(button)();
575 Duckmatic::add_duck(const etl::handle<Duck> &duck)
577 //if(!duck_map.count(duck->get_guid()))
579 if(duck_data_share_map.count(duck->get_data_guid()))
581 duck->set_shared_point(duck_data_share_map[duck->get_data_guid()]);
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;
590 duck_map.insert(duck);
593 last_duck_guid=duck->get_guid();
597 Duckmatic::add_bezier(const etl::handle<Bezier> &bezier)
599 bezier_list_.push_back(bezier);
603 Duckmatic::add_stroke(etl::smart_ptr<std::list<synfig::Point> > stroke_point_list, const synfig::Color& color)
605 assert(stroke_point_list);
607 std::list<etl::handle<Stroke> >::iterator iter;
609 for(iter=stroke_list_.begin();iter!=stroke_list_.end();++iter)
611 if((*iter)->stroke_data==stroke_point_list)
615 etl::handle<Stroke> stroke(new Stroke());
617 stroke->stroke_data=stroke_point_list;
620 stroke_list_.push_back(stroke);
624 Duckmatic::add_persistent_stroke(etl::smart_ptr<std::list<synfig::Point> > stroke_point_list, const synfig::Color& color)
626 add_stroke(stroke_point_list,color);
627 persistent_stroke_list_.push_back(stroke_list_.back());
631 Duckmatic::clear_persistent_strokes()
633 persistent_stroke_list_.clear();
637 Duckmatic::set_show_persistent_strokes(bool x)
639 if(x!=show_persistent_strokes)
641 show_persistent_strokes=x;
643 stroke_list_=persistent_stroke_list_;
645 stroke_list_.clear();
650 Duckmatic::erase_duck(const etl::handle<Duck> &duck)
652 duck_map.erase(duck->get_guid());
655 etl::handle<Duckmatic::Duck>
656 Duckmatic::find_similar_duck(etl::handle<Duck> duck)
658 DuckMap::const_iterator iter(duck_map.find(duck->get_guid()));
659 if(iter!=duck_map.end())
663 /* std::list<handle<Duck> >::reverse_iterator iter;
665 for(iter=duck_list_.rbegin();iter!=duck_list_.rend();++iter)
667 if(*iter!=duck && **iter==*duck)
669 //synfig::info("Found similar duck! (iter:%08x vs. duck:%08x)",iter->get(), duck.get());
677 etl::handle<Duckmatic::Duck>
678 Duckmatic::add_similar_duck(etl::handle<Duck> duck)
680 etl::handle<Duck> similar(find_similar_duck(duck));
690 Duckmatic::erase_bezier(const etl::handle<Bezier> &bezier)
692 std::list<handle<Bezier> >::iterator iter;
694 for(iter=bezier_list_.begin();iter!=bezier_list_.end();++iter)
698 bezier_list_.erase(iter);
702 synfig::warning("Unable to find bezier to erase!");
705 etl::handle<Duckmatic::Duck>
706 Duckmatic::last_duck()const
708 DuckMap::const_iterator iter(duck_map.find(last_duck_guid));
709 if(iter!=duck_map.end())
714 etl::handle<Duckmatic::Bezier>
715 Duckmatic::last_bezier()const
717 return bezier_list_.back();
720 etl::handle<Duckmatic::Duck>
721 Duckmatic::find_duck(synfig::Point point, synfig::Real radius, Duck::Type type)
723 if(radius==0)radius=10000000;
725 if(type==Duck::TYPE_DEFAULT)
726 type=get_type_mask();
728 Real closest(10000000);
729 etl::handle<Duck> ret;
731 DuckMap::const_iterator iter;
733 for(iter=duck_map.begin();iter!=duck_map.end();++iter)
735 const Duck::Handle& duck(iter->second);
737 if(!duck->get_editable())
739 Real dist((duck->get_trans_point()-point).mag_squared());
741 if(duck->get_type()&Duck::TYPE_VERTEX)
744 if(dist<=closest && !( duck->get_type() && (!(type & duck->get_type())) ) )
753 if(radius==0 || closest<radius*radius)
759 etl::handle<Duckmatic::Bezier>
760 Duckmatic::find_bezier(synfig::Point point, synfig::Real radius,float* location)
762 return find_bezier(point,radius,radius,location);
765 etl::handle<Duckmatic::Bezier>
766 Duckmatic::find_bezier(synfig::Point pos, synfig::Real scale, synfig::Real radius, float* location)
768 if(radius==0)radius=10000000;
769 Real closest(10000000);
770 etl::handle<Bezier> ret;
778 for(std::list<handle<Bezier> >::const_iterator iter=bezier_list().begin();iter!=bezier_list().end();++iter)
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();
787 // I don't know why this doesn't work
788 time=curve.find_closest(pos,6);
789 d=((curve(time)-pos).mag_squared());
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();
795 step = d/(2*scale); //want to make the distance between lines happy
797 step = max(step,0.01); //100 samples should be plenty
798 step = min(step,0.1); //10 is minimum
800 d = find_closest(curve,pos,step,&closest,&time);
811 if(closest < radius*radius)
814 *location = best_time; // We need to square-root this because we were dealing with squared distances
823 Duckmatic::save_sketch(const synfig::String& filename)const
825 ChangeLocale change_locale(LC_NUMERIC, "C");
826 std::ofstream file(filename.c_str());
828 if(!file)return false;
830 file<<"SKETCH"<<endl;
832 std::list<etl::handle<Stroke> >::const_iterator iter;
834 for(iter=persistent_stroke_list_.begin();iter!=persistent_stroke_list_.end();++iter)
837 <<(*iter)->color.get_r()<<' '
838 <<(*iter)->color.get_g()<<' '
839 <<(*iter)->color.get_b()
841 std::list<synfig::Point>::const_iterator viter;
842 for(viter=(*iter)->stroke_data->begin();viter!=(*iter)->stroke_data->end();++viter)
850 if(!file)return false;
851 sketch_filename_=filename;
852 signal_sketch_saved_();
857 Duckmatic::load_sketch(const synfig::String& filename)
859 ChangeLocale change_locale(LC_NUMERIC, "C");
860 std::ifstream file(filename.c_str());
870 synfig::error("Not a sketch");
874 etl::smart_ptr<std::list<synfig::Point> > stroke_data;
890 if(!strscanf(line,"C %f %f %f",&r, &g, &b))
892 synfig::warning("Bad color line \"%s\"",line.c_str());
895 add_persistent_stroke(stroke_data, synfig::Color(r,g,b));
903 add_persistent_stroke(stroke_data, synfig::Color(0,0,0));
906 if(!strscanf(line,"V %f %f",&x, &y))
907 synfig::warning("Bad vertex \"%s\"",line.c_str());
909 stroke_data->push_back(synfig::Vector(x,y));
912 synfig::warning("Unexpected sketch token '%c'",line[0]);
917 sketch_filename_=filename;
921 Duckmatic::Push::Push(Duckmatic *duckmatic_):
922 duckmatic_(duckmatic_)
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_;
932 Duckmatic::Push::~Push()
939 Duckmatic::Push::restore()
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_;
949 inline String guid_string(const synfigapp::ValueDesc& x)
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());
957 inline GUID calc_duck_guid(const synfigapp::ValueDesc& x,const synfig::TransformStack& transform_stack)
961 if(x.parent_is_layer_param())
963 ret=x.get_layer()->get_guid()^GUID::hasher(x.get_param_name());
967 ret=x.get_value_node()->get_guid();
970 ret^=transform_stack.get_guid();
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)
978 GUID duck_guid(calc_duck_guid(value_desc,transform_stack)^GUID::hasher(modifier));
979 etl::handle<Duck> duck=new Duck();
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)
988 ValueBase::Type type=value_desc.get_value_type();
989 #define REAL_COOKIE reinterpret_cast<synfig::ParamDesc*>(28)
992 case ValueBase::TYPE_REAL:
994 if(!param_desc || param_desc==REAL_COOKIE || !param_desc->get_origin().empty())
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);
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())
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()));
1011 duck->set_editable(true);
1014 if(param_desc && param_desc!=REAL_COOKIE)
1016 if(!param_desc->get_origin().empty())
1018 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1020 duck->set_origin(value_desc_origin.get_value(get_time()).get(synfig::Point()));
1022 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1023 duck->set_origin(last_duck());
1025 duck->set_scalar(param_desc->get_scalar());
1028 duck->signal_edited().clear(); // value_desc.get_value_type() == ValueBase::TYPE_REAL:
1029 duck->signal_edited().connect(
1033 &studio::CanvasView::on_duck_changed
1038 duck->set_value_desc(value_desc);
1040 duck->signal_user_click(2).connect(
1045 &studio::CanvasView::popup_param_menu
1053 duck->set_guid(calc_duck_guid(value_desc,transform_stack)^GUID::hasher(multiple));
1061 case ValueBase::TYPE_ANGLE:
1063 if(!param_desc || param_desc==REAL_COOKIE || !param_desc->get_origin().empty())
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;
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())
1075 ValueNode::Handle value_node=value_desc.get_value_node();
1076 //duck->set_name(strprintf("%x",value_node.get()));
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()));
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);
1090 if(param_desc && param_desc!=REAL_COOKIE)
1092 if(!param_desc->get_origin().empty())
1094 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1096 duck->set_origin(value_desc_origin.get_value(get_time()).get(synfig::Point()));
1098 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1099 duck->set_origin(last_duck());
1101 duck->set_scalar(param_desc->get_scalar());
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(
1110 &studio::CanvasView::on_duck_angle_changed
1115 duck->set_value_desc(value_desc);
1117 duck->signal_user_click(2).connect(
1122 &studio::CanvasView::popup_param_menu
1129 duck->set_guid(calc_duck_guid(value_desc,transform_stack)^GUID::hasher(multiple));
1137 case ValueBase::TYPE_VECTOR:
1139 etl::handle<Duck> duck=new Duck();
1140 duck->set_transform_stack(transform_stack);
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())
1146 //duck->set_name(strprintf("%x",value_desc.get_value_node().get()));
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()));
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);
1159 // If we were passed a parameter description
1162 if(!param_desc->get_connect().empty())
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())
1168 connect_duck=duck_map[calc_duck_guid(value_desc_origin,transform_stack)^GUID::hasher(0)];
1172 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1173 connect_duck=last_duck();
1175 duck->set_connect_duck(connect_duck);
1177 if(!param_desc->get_box().empty())
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());
1184 // If we have an origin
1185 if(!param_desc->get_origin().empty())
1187 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1189 duck->set_origin(value_desc_origin.get_value(get_time()).get(synfig::Point()));
1191 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1192 duck->set_origin(last_duck());
1193 duck->set_type(Duck::TYPE_VERTEX);
1196 duck->set_type(Duck::TYPE_POSITION);
1198 duck->set_scalar(param_desc->get_scalar());
1201 duck->set_type(Duck::TYPE_POSITION);
1203 duck->signal_edited().clear(); // value_desc.get_value_type() == ValueBase::TYPE_VECTOR:
1204 duck->signal_edited().connect(
1208 &studio::CanvasView::on_duck_changed
1213 duck->set_value_desc(value_desc);
1215 duck->signal_user_click(2).connect(
1220 &studio::CanvasView::popup_param_menu
1227 duck->set_guid(calc_duck_guid(value_desc,transform_stack)^GUID::hasher(multiple));
1233 /* case ValueBase::TYPE_SEGMENT:
1235 etl::handle<Bezier> bezier(new Bezier());
1236 ValueNode_Composite::Handle value_node;
1238 if(value_desc.is_value_node() &&
1239 (value_node=ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()))
1242 if(!add_to_ducks(synfigapp::ValueDesc(value_node,0),canvas_view,transform_stack))
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))
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);
1254 if(!add_to_ducks(synfigapp::ValueDesc(value_node,2),canvas_view,transform_stack))
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))
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);
1266 bezier->signal_user_click(2).connect(
1270 &studio::CanvasView::popup_param_menu_bezier
1278 else if(value_desc.get_value().is_valid())
1280 Segment segment=value_desc.get_value();
1281 etl::handle<Duck> duck_p,duck_c;
1282 synfig::String name;
1285 name=param_desc->get_local_name();
1289 name=guid_string(value_desc);
1292 duck_p=new class Duck(segment.p1);
1293 duck_p->set_name(name+".P1");
1294 duck_p->set_type(Duck::TYPE_VERTEX);
1297 duck_c=new class Duck(segment.t1);
1298 duck_c->set_name(name+".T1");
1299 duck_c->set_type(Duck::TYPE_TANGENT);
1301 duck_c->set_origin(duck_p);
1302 duck_c->set_scalar(TANGENT_HANDLE_SCALE);
1303 duck_c->set_tangent(true);
1308 duck_p=new class Duck(segment.p2);
1309 duck_p->set_name(name+".P2");
1310 duck_p->set_type(Duck::TYPE_VERTEX);
1313 duck_c=new class Duck(segment.t2);
1314 duck_c->set_type(Duck::TYPE_TANGENT);
1315 duck_c->set_name(name+".T2");
1317 duck_c->set_origin(duck_p);
1318 duck_c->set_scalar(-TANGENT_HANDLE_SCALE);
1319 duck_c->set_tangent(true);
1330 case ValueBase::TYPE_BLINEPOINT:
1333 if(value_desc.is_value_node() &&
1334 ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node())
1337 ValueNode_Composite::Handle value_node;
1338 value_node=ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node());
1340 if(!add_to_ducks(synfigapp::ValueDesc(value_node,0),canvas_view,transform_stack))
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))
1346 etl::handle<Duck> t1_duck(last_duck());
1348 t1_duck->set_origin(vertex_duck);
1349 t1_duck->set_scalar(-TANGENT_HANDLE_SCALE);
1350 t1_duck->set_tangent(true);
1352 etl::handle<Duck> t2_duck;
1354 // If the tangents are split
1355 if((*value_node->get_link("split"))(get_time()).get(bool()))
1357 if(!add_to_ducks(synfigapp::ValueDesc(value_node,5),canvas_view,transform_stack))
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);
1366 if(!add_to_ducks(synfigapp::ValueDesc(value_node,4),canvas_view,transform_stack))
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);
1378 case ValueBase::TYPE_LIST:
1381 if(value_desc.is_value_node() &&
1382 ValueNode_BLine::Handle::cast_dynamic(value_desc.get_value_node())
1385 ValueNode_BLine::Handle value_node;
1386 value_node=ValueNode_BLine::Handle::cast_dynamic(value_desc.get_value_node());
1390 etl::handle<Bezier> bezier;
1391 etl::handle<Duck> first_duck;
1392 etl::handle<Duck> duck, tduck;
1394 for(i=0;i<value_node->link_count();i++)
1396 float amount(value_node->list[i].amount_at_time(get_time()));
1399 if(first==-1)first=i;
1401 BLinePoint bline_point((*value_node->get_link(i))(get_time()));
1403 ValueNode_Composite::Handle vertex_value_node(
1404 ValueNode_Composite::Handle::cast_dynamic(
1405 value_node->get_link(i)
1409 // Add the vertex duck
1410 if(vertex_value_node)
1412 if(add_to_ducks(synfigapp::ValueDesc(vertex_value_node,0),canvas_view,transform_stack))
1417 duck->set_type(Duck::TYPE_VERTEX);
1419 duck->signal_user_click(2).clear();
1420 duck->signal_user_click(2).connect(
1425 &studio::CanvasView::popup_param_menu
1429 synfigapp::ValueDesc(value_node,i)
1432 duck->set_value_desc(synfigapp::ValueDesc(value_node,i));
1436 if(!param_desc->get_origin().empty())
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());
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()));
1454 duck=new Duck(bline_point.get_vertex());
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");
1462 duck->set_type(Duck::TYPE_VERTEX);
1465 if(!param_desc->get_origin().empty())
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());
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()));
1477 duck->set_guid(calc_duck_guid(synfigapp::ValueDesc(value_node,i),transform_stack)^GUID::hasher(".v"));
1478 duck=add_similar_duck(duck);
1482 // Add the width duck only if we have a hint of scale
1483 if(param_desc && !param_desc->get_hint().empty())
1485 etl::handle<Duck> width;
1486 if(add_to_ducks(synfigapp::ValueDesc(vertex_value_node,1),canvas_view,transform_stack,REAL_COOKIE))
1489 width->set_origin(duck);
1490 width->set_type(Duck::TYPE_WIDTH);
1491 width->set_name(guid_string(synfigapp::ValueDesc(value_node,i))+".w");
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);
1499 synfig::error("Unable to add width duck!");
1504 // Add the tangent1 duck
1505 if(vertex_value_node)
1507 if(!add_to_ducks(synfigapp::ValueDesc(vertex_value_node,4),canvas_view,transform_stack))
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);
1523 tduck->set_origin(duck);
1524 tduck->set_scalar(-TANGENT_BEZIER_SCALE);
1525 tduck->set_tangent(true);
1530 bezier->signal_user_click(2).connect(
1534 &studio::CanvasView::popup_param_menu_bezier
1536 synfigapp::ValueDesc(value_node,i)
1540 duck->signal_user_click(2).clear();
1541 duck->signal_user_click(2).connect(
1546 &studio::CanvasView::popup_param_menu
1550 synfigapp::ValueDesc(value_node,i)
1553 duck->set_value_desc(synfigapp::ValueDesc(value_node,i));
1559 if(i+1>=value_node->link_count() && !value_node->get_loop())
1562 bezier=new Bezier();
1564 // Add the tangent2 duck
1565 if(vertex_value_node)
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))
1574 if(bline_point.get_split_tangent_flag())
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"));
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"));
1590 tduck->set_editable(false);
1591 tduck=add_similar_duck(tduck);
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());
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()));
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()));
1606 duck->signal_user_click(2).clear();
1607 duck->signal_user_click(2).connect(
1612 &studio::CanvasView::popup_param_menu
1616 synfigapp::ValueDesc(value_node,0)
1619 duck->set_value_desc(synfigapp::ValueDesc(value_node,0));
1623 tduck->set_origin(duck);
1624 tduck->set_scalar(TANGENT_BEZIER_SCALE);
1625 tduck->set_tangent(true);
1631 // Loop if necessary
1632 if(bezier && value_node->get_loop())
1634 BLinePoint bline_point((*value_node->get_link(first))(get_time()));
1636 ValueNode_Composite::Handle vertex_value_node(
1637 ValueNode_Composite::Handle::cast_dynamic(
1638 value_node->get_link(first)
1642 // Add the vertex duck
1645 if(vertex_value_node)
1647 if(!add_to_ducks(synfigapp::ValueDesc(vertex_value_node,0),canvas_view,transform_stack))
1652 if(!param_desc->get_origin().empty())
1653 duck->set_origin(value_desc.get_layer()->get_param(param_desc->get_origin()).get(synfig::Point()));
1655 duck->set_type(Duck::TYPE_VERTEX);
1659 duck=new Duck(bline_point.get_vertex());
1660 duck->set_transform_stack(transform_stack);
1663 if(!param_desc->get_origin().empty())
1664 duck->set_origin(value_desc.get_layer()->get_param(param_desc->get_origin()).get(synfig::Point()));
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);
1673 // Add the tangent1 duck
1674 if(vertex_value_node)
1676 if(!add_to_ducks(synfigapp::ValueDesc(vertex_value_node,4),canvas_view,transform_stack))
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"));
1692 tduck->set_origin(duck);
1693 tduck->set_scalar(-TANGENT_BEZIER_SCALE);
1694 tduck->set_tangent(true);
1698 bezier->signal_user_click(2).connect(
1702 &studio::CanvasView::popup_param_menu_bezier
1704 synfigapp::ValueDesc(value_node,first)
1707 duck->signal_user_click(2).clear();
1708 duck->signal_user_click(2).connect(
1713 &studio::CanvasView::popup_param_menu
1717 synfigapp::ValueDesc(value_node,first)
1720 duck->set_value_desc(synfigapp::ValueDesc(value_node,first));
1728 else // Check for DynamicList
1729 if(value_desc.is_value_node() &&
1730 ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_value_node())
1733 ValueNode_DynamicList::Handle value_node;
1734 value_node=ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_value_node());
1737 if(value_node->get_contained_type()==ValueBase::TYPE_VECTOR)
1740 for(i=0;i<value_node->link_count();i++)
1742 if(!value_node->list[i].status_at_time(get_time()))
1744 if(!add_to_ducks(synfigapp::ValueDesc(value_node,i),canvas_view,transform_stack))
1746 etl::handle<Duck> duck(last_duck());
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());
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()));
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()));
1761 duck->set_type(Duck::TYPE_VERTEX);
1762 bezier.p1=bezier.p2;bezier.c1=bezier.c2;
1763 bezier.p2=bezier.c2=duck;
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(
1777 &studio::CanvasView::popup_param_menu_bezier
1779 synfigapp::ValueDesc(value_node,i)
1785 /*else if(value_node->get_contained_type()==ValueBase::TYPE_SEGMENT)
1787 for(i=0;i<value_node->link_count();i++)
1789 if(!value_node->list[i].status_at_time(get_time()))
1791 if(!add_to_ducks(synfigapp::ValueDesc(value_node,i),canvas_view,transform_stack))