Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_03 / synfig-studio / src / synfigapp / actions / valuedesclink.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuedesclink.cpp
3 **      \brief Template File
4 **
5 **      $Id: valuedesclink.cpp,v 1.1.1.1 2005/01/07 03:34:37 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
32 #include "valuedesclink.h"
33
34 #include <synfigapp/canvasinterface.h>
35 #include <synfig/valuenode_const.h>
36
37 #endif
38
39 using namespace std;
40 using namespace etl;
41 using namespace synfig;
42 using namespace synfigapp;
43 using namespace Action;
44
45 /* === M A C R O S ========================================================= */
46
47 ACTION_INIT(Action::ValueDescLink);
48 ACTION_SET_NAME(Action::ValueDescLink,"value_desc_link");
49 ACTION_SET_LOCAL_NAME(Action::ValueDescLink,"Link");
50 ACTION_SET_TASK(Action::ValueDescLink,"connect");
51 ACTION_SET_CATEGORY(Action::ValueDescLink,Action::CATEGORY_VALUEDESC);
52 ACTION_SET_PRIORITY(Action::ValueDescLink,0);
53 ACTION_SET_VERSION(Action::ValueDescLink,"0.0");
54 ACTION_SET_CVS_ID(Action::ValueDescLink,"$Id: valuedesclink.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $");
55
56 /* === G L O B A L S ======================================================= */
57
58 /* === P R O C E D U R E S ================================================= */
59
60 /* === M E T H O D S ======================================================= */
61
62 Action::ValueDescLink::ValueDescLink()
63 {
64         poison=false;
65 }
66
67 Action::ParamVocab
68 Action::ValueDescLink::get_param_vocab()
69 {
70         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
71         
72         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
73                 .set_local_name(_("ValueDesc to link"))
74                 .set_requires_multiple()
75         );
76         
77         return ret;
78 }
79
80 bool
81 Action::ValueDescLink::is_canidate(const ParamList &x)
82 {
83         return canidate_check(get_param_vocab(),x);
84 }
85
86 bool
87 Action::ValueDescLink::set_param(const synfig::String& name, const Action::Param &param)
88 {
89         if(name=="time" && param.get_type()==Param::TYPE_TIME)
90         {
91                 time=param.get_time();
92                 return true;
93         }
94         
95         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
96         {
97                 ValueDesc value_desc(param.get_value_desc());
98                                 
99                 if(value_desc.is_value_node() && value_desc.get_value_node()->is_exported())
100                 {
101                         if(link_value_node==value_desc.get_value_node())
102                                 return true;
103                         
104                         if(link_value_node && link_value_node->is_exported())
105                         {
106                                 poison=true;
107                                 return false;
108                         }
109                                                 
110                         link_value_node=value_desc.get_value_node();
111                 }
112                 else if(value_desc.is_value_node())
113                 {
114                         if(!link_value_node)
115                         {
116                                 link_value_node=value_desc.get_value_node();
117                         }
118
119                         // Use the one that is referenced more
120                         else if(link_value_node->rcount()<value_desc.get_value_node()->rcount())
121                         {
122                                 link_value_node=value_desc.get_value_node();
123                         }
124
125                         // If the current link value node is a constant and
126                         // this one isn't, then give preference to the exotic
127                         else if(ValueNode_Const::Handle::cast_dynamic(link_value_node) && !ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
128                         {
129                                 link_value_node=value_desc.get_value_node();
130                         }
131
132                         // If both are animated, and this one has more waypoints,
133                         // then use the one with more waypoints
134                         else if(
135                                         ValueNode_Animated::Handle::cast_dynamic(link_value_node)
136                                 &&      ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node())
137                                 && (
138                                         ValueNode_Animated::Handle::cast_dynamic(link_value_node)->waypoint_list().size()
139                                 <       ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node())->waypoint_list().size()
140                                 )
141                         )
142                         {
143                                 link_value_node=value_desc.get_value_node();
144                         }
145                         
146                         /*
147                         // Use the one that was most recently changed
148                         else if(link_value_node->get_time_last_changed()<value_desc.get_value_node()->get_time_last_changed())
149                         {
150                                 link_value_node=value_desc.get_value_node();
151                         }
152                         */
153                 }
154
155                 
156                 if(value_desc_list.size() && value_desc.get_value_type()!=value_desc_list.front().get_value_type())
157                 {
158                         // Everything must be of the same type
159                         poison=true;
160                         return false;
161                 }
162                 value_desc_list.push_back(value_desc);
163
164                 return true;
165         }
166
167         return Action::CanvasSpecific::set_param(name,param);
168 }
169
170 bool
171 Action::ValueDescLink::is_ready()const
172 {
173         if(poison || value_desc_list.size()<=1)
174                 return false;
175         return Action::CanvasSpecific::is_ready();
176 }
177
178 void
179 Action::ValueDescLink::prepare()
180 {
181         if(poison || value_desc_list.empty())
182                 throw Error(Error::TYPE_NOTREADY);
183                 
184         clear();
185
186         if(!link_value_node)
187         {
188                 ValueDesc& value_desc(value_desc_list.front());
189                 
190                 link_value_node=ValueNode_Const::create(value_desc.get_value(time));
191                 
192                 Action::Handle action(Action::create("value_desc_connect"));
193                 
194                 action->set_param("canvas",get_canvas());
195                 action->set_param("canvas_interface",get_canvas_interface());
196                 action->set_param("src",link_value_node);
197                 action->set_param("dest",value_desc);
198         
199                 assert(action->is_ready());             
200                 if(!action->is_ready())
201                         throw Error(Error::TYPE_NOTREADY);
202         
203                 add_action_front(action);               
204         }
205
206         /*
207         if(!link_value_node->is_exported())
208         {
209                 Action::Handle action(Action::create("value_node_add"));
210                 
211                 action->set_param("canvas",get_canvas());
212                 action->set_param("canvas_interface",get_canvas_interface());
213                 action->set_param("new",link_value_node);
214                 action->set_param("name",strprintf(_("Unnamed%08d"),synfig::UniqueID().get_uid()));
215         
216                 assert(action->is_ready());             
217                 if(!action->is_ready())
218                         throw Error(Error::TYPE_NOTREADY);
219         
220                 add_action_front(action);
221         }
222         */
223         
224         std::list<ValueDesc>::iterator iter;
225         for(iter=value_desc_list.begin();iter!=value_desc_list.end();++iter)
226         {
227                 ValueDesc& value_desc(*iter);
228                 
229                 if(value_desc.is_value_node() && value_desc.get_value_node()==link_value_node)
230                         continue;
231
232                 Action::Handle action(Action::create("value_desc_connect"));
233                 
234                 action->set_param("canvas",get_canvas());
235                 action->set_param("canvas_interface",get_canvas_interface());
236                 action->set_param("src",link_value_node);
237                 action->set_param("dest",value_desc);
238         
239                 assert(action->is_ready());             
240                 if(!action->is_ready())
241                         throw Error(Error::TYPE_NOTREADY);
242         
243                 add_action_front(action);               
244         }
245 }