1 /* === S Y N F I G ========================================================= */
2 /*! \file valuedescset.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 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 ======================================================= */
33 #include "layerparamset.h"
34 #include "valuenodeconstset.h"
35 #include "valuedescconnect.h"
36 #include "waypointsetsmart.h"
38 #include "valuedescset.h"
39 #include <synfigapp/canvasinterface.h>
40 #include <synfig/valuenode_composite.h>
41 #include <synfig/valuenode_radialcomposite.h>
42 #include <synfig/valuenode_reference.h>
43 #include <synfigapp/main.h>
45 #include <synfigapp/general.h>
51 using namespace synfig;
52 using namespace synfigapp;
53 using namespace Action;
55 /* === M A C R O S ========================================================= */
57 ACTION_INIT_NO_GET_LOCAL_NAME(Action::ValueDescSet);
58 ACTION_SET_NAME(Action::ValueDescSet,"ValueDescSet");
59 ACTION_SET_LOCAL_NAME(Action::ValueDescSet,N_("Set ValueDesc"));
60 ACTION_SET_TASK(Action::ValueDescSet,"set");
61 ACTION_SET_CATEGORY(Action::ValueDescSet,Action::CATEGORY_VALUEDESC);
62 ACTION_SET_PRIORITY(Action::ValueDescSet,0);
63 ACTION_SET_VERSION(Action::ValueDescSet,"0.0");
64 ACTION_SET_CVS_ID(Action::ValueDescSet,"$Id$");
66 /* === G L O B A L S ======================================================= */
68 /* === P R O C E D U R E S ================================================= */
70 /* === M E T H O D S ======================================================= */
72 Action::ValueDescSet::ValueDescSet():
78 Action::ValueDescSet::get_local_name()const
80 return strprintf(_("Set %s"),
82 ? value_desc.get_description().c_str()
87 Action::ValueDescSet::get_param_vocab()
89 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
91 ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
92 .set_local_name(_("ValueDesc"))
95 ret.push_back(ParamDesc("new_value",Param::TYPE_VALUE)
96 .set_local_name(_("ValueBase"))
99 ret.push_back(ParamDesc("time",Param::TYPE_TIME)
100 .set_local_name(_("Time"))
108 Action::ValueDescSet::is_candidate(const ParamList &x)
110 return candidate_check(get_param_vocab(),x);
114 Action::ValueDescSet::set_param(const synfig::String& name, const Action::Param ¶m)
116 if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
118 value_desc=param.get_value_desc();
123 if(name=="new_value" && param.get_type()==Param::TYPE_VALUE)
125 value=param.get_value();
130 if(name=="time" && param.get_type()==Param::TYPE_TIME)
132 time=param.get_time();
137 return Action::CanvasSpecific::set_param(name,param);
141 Action::ValueDescSet::is_ready()const
143 if(!value_desc || !value.is_valid())
145 return Action::CanvasSpecific::is_ready();
149 Action::ValueDescSet::prepare()
153 // If our tangents are merged, and
154 // our first tangent is being manipulated,
155 // then we also need to adjust the other
157 if( value_desc.parent_is_value_node() &&
158 value_desc.get_parent_value_node()->get_type()==ValueBase::TYPE_BLINEPOINT &&
159 (value_desc.get_name()=="t1" || value_desc.get_name()=="t2") &&
160 //(value_desc.get_index()==4 || value_desc.get_index()==5) &&
161 (*value_desc.get_parent_value_node())(time).get(BLinePoint()).get_split_tangent_flag()==false)
164 ValueNode_Composite::Handle parent_value_node;
165 parent_value_node=parent_value_node.cast_dynamic(value_desc.get_parent_value_node());
166 assert(parent_value_node);
168 Vector t1((*parent_value_node->get_link("t1"))(time));
169 Vector t2((*parent_value_node->get_link("t2"))(time));
172 //if (value_desc.get_index()==4) {
173 if (value_desc.get_name()=="t1") {
174 ValueNode_Composite::Handle parent_value_node;
175 parent_value_node=parent_value_node.cast_dynamic(value_desc.get_parent_value_node());
177 assert(parent_value_node);
179 Action::Handle action(Action::create("ValueDescSet"));
182 throw Error(_("Unable to find action ValueDescSet (bug)"));
184 action->set_param("canvas",get_canvas());
185 action->set_param("canvas_interface",get_canvas_interface());
186 action->set_param("time",time);
187 action->set_param("new_value",value);
188 action->set_param("value_desc",ValueDesc(parent_value_node, parent_value_node->get_link_index_from_name("t2")));
190 if(!action->is_ready())
191 throw Error(Error::TYPE_NOTREADY);
197 // If we are a reference value node, then
198 // we need to distribute the changes to the
199 // referenced value node
200 if(value_desc.is_value_node() && ValueNode_Reference::Handle::cast_dynamic(value_desc.get_value_node()))
202 ValueDesc reference_value_desc(ValueNode_Reference::Handle::cast_dynamic(value_desc.get_value_node()),0);
204 Action::Handle action(Action::create("ValueDescSet"));
207 throw Error(_("Unable to find action ValueDescSet (bug)"));
209 action->set_param("canvas",get_canvas());
210 action->set_param("canvas_interface",get_canvas_interface());
211 action->set_param("time",time);
212 action->set_param("new_value",value);
213 action->set_param("value_desc",reference_value_desc);
215 if(!action->is_ready())
216 throw Error(Error::TYPE_NOTREADY);
223 // If we are a composite value node, then
224 // we need to distribute the changes to the
226 if(value_desc.is_value_node() && ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()))
228 ValueBase components[6];
230 switch(value.get_type())
232 case ValueBase::TYPE_VECTOR:
233 components[0]=value.get(Vector())[0];
234 components[1]=value.get(Vector())[1];
237 case ValueBase::TYPE_COLOR:
238 components[0]=value.get(Color()).get_r();
239 components[1]=value.get(Color()).get_g();
240 components[2]=value.get(Color()).get_b();
241 components[3]=value.get(Color()).get_a();
244 case ValueBase::TYPE_SEGMENT:
245 components[0]=value.get(Segment()).p1;
246 components[1]=value.get(Segment()).t1;
247 components[2]=value.get(Segment()).p2;
248 components[3]=value.get(Segment()).t2;
251 case ValueBase::TYPE_BLINEPOINT:
253 BLinePoint bline_point(value);
254 components[0]=bline_point.get_vertex();
255 components[1]=bline_point.get_width();
256 components[2]=bline_point.get_origin();
257 components[3]=bline_point.get_split_tangent_flag();
258 components[4]=bline_point.get_tangent1();
259 components[5]=bline_point.get_tangent2();
264 throw Error(_("Bad type for composite (%s)"),ValueBase::type_local_name(value.get_type()).c_str());
268 for(int i=0;i<n_components;i++)
270 ValueDesc component_value_desc(ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()),i);
272 Action::Handle action(Action::create("ValueDescSet"));
275 throw Error(_("Unable to find action ValueDescSet (bug)"));
277 action->set_param("canvas",get_canvas());
278 action->set_param("canvas_interface",get_canvas_interface());
279 action->set_param("time",time);
280 action->set_param("new_value",components[i]);
281 action->set_param("value_desc",component_value_desc);
283 if(!action->is_ready())
284 throw Error(Error::TYPE_NOTREADY);
293 // If we are a RADIAL composite value node, then
294 // we need to distribute the changes to the
296 if(value_desc.is_value_node() && ValueNode_RadialComposite::Handle::cast_dynamic(value_desc.get_value_node()))
298 ValueBase components[6];
300 switch(value.get_type())
302 case ValueBase::TYPE_VECTOR:
304 Angle old_angle = (*(ValueNode_RadialComposite::Handle::cast_dynamic(
305 value_desc.get_value_node())->get_link("theta")))(time).get(Angle());
306 Vector vect(value.get(Vector()));
307 components[0]=vect.mag();
308 Angle change = Angle(Angle::tan(vect[1],vect[0])) - old_angle;
309 while (change < Angle::deg(-180)) change += Angle::deg(360);
310 while (change > Angle::deg(180)) change -= Angle::deg(360);
311 components[1]=old_angle + change;
315 case ValueBase::TYPE_COLOR:
316 components[0]=value.get(Color()).get_y();
317 components[1]=value.get(Color()).get_s();
318 components[2]=value.get(Color()).get_hue();
319 components[3]=value.get(Color()).get_a();
323 throw Error(_("Bad type for radial composite (%s)"),ValueBase::type_local_name(value.get_type()).c_str());
326 for(int i=0;i<n_components;i++)
328 ValueDesc component_value_desc(ValueNode_RadialComposite::Handle::cast_dynamic(value_desc.get_value_node()),i);
330 Action::Handle action(Action::create("ValueDescSet"));
333 throw Error(_("Unable to find action ValueDescSet (bug)"));
335 action->set_param("canvas",get_canvas());
336 action->set_param("canvas_interface",get_canvas_interface());
337 action->set_param("time",time);
338 action->set_param("new_value",components[i]);
339 action->set_param("value_desc",component_value_desc);
341 if(!action->is_ready())
342 throw Error(Error::TYPE_NOTREADY);
350 // If we are merging the tangents of a BLinePoint,
351 // we must also set the second tangent for things
352 // to interpolate properly
353 if (value_desc.parent_is_value_node() &&
354 value_desc.get_parent_value_node()->get_type()==ValueBase::TYPE_BLINEPOINT &&
355 value_desc.get_index()==3)
357 ValueNode_Composite::Handle parent_value_node;
358 parent_value_node=parent_value_node.cast_dynamic(value_desc.get_parent_value_node());
360 assert(parent_value_node);
362 // are we splitting or merging the tangents?
363 if (value.get(bool()))
365 // we are splitting tangents
367 Action::Handle action(Action::create("ValueDescSet"));
370 throw Error(_("Unable to find action ValueDescSet (bug)"));
372 action->set_param("canvas",get_canvas());
373 action->set_param("canvas_interface",get_canvas_interface());
374 action->set_param("time",time);
375 action->set_param("new_value",(*parent_value_node->get_link("t1"))(time));
376 action->set_param("value_desc",ValueDesc(parent_value_node,parent_value_node->get_link_index_from_name("t2")));
378 if(!action->is_ready())
379 throw Error(Error::TYPE_NOTREADY);
385 // we are merging tangents
387 // the merged tangent should be the average of the 2 tangents we're merging
388 ValueBase average(((Vector)((*parent_value_node->get_link("t1"))(time)) +
389 (Vector)((*parent_value_node->get_link("t2"))(time))) / 2);
392 Action::Handle action(Action::create("ValueDescSet"));
395 throw Error(_("Unable to find action ValueDescSet (bug)"));
397 action->set_param("canvas",get_canvas());
398 action->set_param("canvas_interface",get_canvas_interface());
399 action->set_param("time",time);
400 action->set_param("new_value",average);
401 action->set_param("value_desc",ValueDesc(parent_value_node,parent_value_node->get_link_index_from_name("t1")));
403 if(!action->is_ready())
404 throw Error(Error::TYPE_NOTREADY);
410 Action::Handle action(Action::create("ValueDescSet"));
413 throw Error(_("Unable to find action ValueDescSet (bug)"));
415 action->set_param("canvas",get_canvas());
416 action->set_param("canvas_interface",get_canvas_interface());
417 action->set_param("time",time);
418 action->set_param("new_value",average);
419 action->set_param("value_desc",ValueDesc(parent_value_node,parent_value_node->get_link_index_from_name("t2")));
421 if(!action->is_ready())
422 throw Error(Error::TYPE_NOTREADY);
431 if( value_desc.parent_is_value_node())
434 if(value_desc.get_parent_value_node()->get_type()==ValueBase::TYPE_BLINEPOINT)
437 if(value_desc.get_index()==4)
440 if((*value_desc.get_parent_value_node())(time).get(BLinePoint()).get_split_tangent_flag()==false)
450 // If we are in animate editing mode
451 if(get_edit_mode()&MODE_ANIMATE)
454 ValueNode_Animated::Handle& value_node(value_node_animated);
456 // If this value isn't a ValueNode_Animated, but
457 // it is somewhat constant, then go ahead and convert
458 // it to a ValueNode_Animated.
459 if(!value_desc.is_value_node() || ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
462 if(value_desc.is_value_node())
463 value=ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node())->get_value();
465 value=value_desc.get_value();
467 if(!value_node)value_node=ValueNode_Animated::create(value,time);
468 //if(!value_node)value_node=ValueNode_Animated::create(value.get_type());
470 Action::Handle action;
472 if(!value_desc.is_value_node())
474 action=(ValueDescConnect::create());
475 action->set_param("dest",value_desc);
476 action->set_param("src",ValueNode::Handle(value_node));
480 action=Action::create("ValueNodeReplace");
481 action->set_param("dest",value_desc.get_value_node());
482 action->set_param("src",ValueNode::Handle(value_node));
485 action->set_param("canvas",get_canvas());
486 action->set_param("canvas_interface",get_canvas_interface());
488 if(!action->is_ready())
489 throw Error(Error::TYPE_NOTREADY);
491 add_action_front(action);
495 value_node=value_node.cast_dynamic(value_desc.get_value_node());
499 throw Error(_("Direct manipulation of this ValueNode type is not yet supported"));
501 Action::Handle action(WaypointSetSmart::create());
503 //Waypoint waypoint(value,time);
505 Waypoint waypoint(value_node->new_waypoint_at_time(time));
506 waypoint.set_value(value);
508 waypoint.set_before(synfigapp::Main::get_interpolation());
509 waypoint.set_after(synfigapp::Main::get_interpolation());
511 action->set_param("canvas",get_canvas());
512 action->set_param("canvas_interface",get_canvas_interface());
513 action->set_param("value_node",ValueNode::Handle(value_node));
514 action->set_param("waypoint",waypoint);
516 if(!action->is_ready())
517 throw Error(Error::TYPE_NOTREADY);
525 if(value_desc.is_value_node())
527 if(ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
529 Action::Handle action(ValueNodeConstSet::create());
531 action->set_param("canvas",get_canvas());
532 action->set_param("canvas_interface",get_canvas_interface());
533 action->set_param("value_node",value_desc.get_value_node());
534 action->set_param("new_value",value);
536 if(!action->is_ready())
537 throw Error(Error::TYPE_NOTREADY);
539 add_action_front(action);
543 if(ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node()))
544 throw Error(_("You must be in Animate-Editing-Mode to directly manipulate this value"));
546 throw Error(_("Direct manipulation of this ValueNode type is not yet supported"));
549 if(value_desc.parent_is_layer_param() && !value_desc.is_value_node())
551 Action::Handle layer_param_set(LayerParamSet::create());
553 layer_param_set->set_param("canvas",get_canvas());
554 layer_param_set->set_param("canvas_interface",get_canvas_interface());
555 layer_param_set->set_param("layer",value_desc.get_layer());
556 layer_param_set->set_param("param",value_desc.get_param_name());
557 layer_param_set->set_param("new_value",value);
559 if(!layer_param_set->is_ready())
560 throw Error(Error::TYPE_NOTREADY);
562 add_action_front(layer_param_set);
566 throw Error(_("Unsupported ValueDesc type"));