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
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include "layerparamset.h"
33 #include "valuenodeconstset.h"
34 #include "valuedescconnect.h"
35 #include "waypointsetsmart.h"
37 #include "valuedescset.h"
38 #include <synfigapp/canvasinterface.h>
39 #include <synfig/valuenode_composite.h>
40 #include <synfig/valuenode_radialcomposite.h>
41 #include <synfig/valuenode_reference.h>
42 #include <synfigapp/main.h>
48 using namespace synfig;
49 using namespace synfigapp;
50 using namespace Action;
52 /* === M A C R O S ========================================================= */
53 #define ACTION_INIT2(class) \
54 Action::Base* class::create() { return new class(); } \
55 synfig::String class::get_name()const { return name__; }
57 ACTION_INIT2(Action::ValueDescSet);
58 ACTION_SET_NAME(Action::ValueDescSet,"value_desc_set");
59 ACTION_SET_LOCAL_NAME(Action::ValueDescSet,"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 String name("ValueDesc");
85 else if(value_desc.parent_is_layer_param())
87 if(value_desc.get_layer()->get_description().empty())
88 name=value_desc.get_layer()->get_local_name();
90 name=value_desc.get_layer()->get_description();
91 name+="->"+value_desc.get_param_name();
93 else if(value_desc.parent_is_value_node())
95 synfig::LinkableValueNode::Handle value_node(synfig::LinkableValueNode::Handle::cast_reinterpret(value_desc.get_parent_value_node()));
96 name=value_node->link_local_name(value_desc.get_index());
99 for(node=value_node.get();!node->parent_set.empty() && !dynamic_cast<Layer*>(node);node=*node->parent_set.begin());
100 Layer::Handle parent_layer(dynamic_cast<Layer*>(node));
103 if(parent_layer->get_description().empty())
104 name=parent_layer->get_local_name()+"=>"+name;
106 name=parent_layer->get_description()+"=>"+name;
110 return strprintf(_("Set %s"),name.c_str());
114 Action::ValueDescSet::get_param_vocab()
116 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
118 ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
119 .set_local_name(_("ValueDesc"))
122 ret.push_back(ParamDesc("new_value",Param::TYPE_VALUE)
123 .set_local_name(_("ValueBase"))
126 ret.push_back(ParamDesc("time",Param::TYPE_TIME)
127 .set_local_name(_("Time"))
135 Action::ValueDescSet::is_candidate(const ParamList &x)
137 return candidate_check(get_param_vocab(),x);
141 Action::ValueDescSet::set_param(const synfig::String& name, const Action::Param ¶m)
143 if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
145 value_desc=param.get_value_desc();
150 if(name=="new_value" && param.get_type()==Param::TYPE_VALUE)
152 value=param.get_value();
157 if(name=="time" && param.get_type()==Param::TYPE_TIME)
159 time=param.get_time();
164 return Action::CanvasSpecific::set_param(name,param);
168 Action::ValueDescSet::is_ready()const
170 if(!value_desc || !value.is_valid())
172 return Action::CanvasSpecific::is_ready();
176 Action::ValueDescSet::prepare()
180 // If our tangents are merged, and
181 // our first tangent is being manipulated,
182 // then we also need to adjust the other
184 if( value_desc.parent_is_value_node()
185 && value_desc.get_parent_value_node()->get_type()==ValueBase::TYPE_BLINEPOINT
186 && value_desc.get_index()==4
187 && (*value_desc.get_parent_value_node())(time).get(BLinePoint()).get_split_tangent_flag()==false
191 ValueNode_Composite::Handle parent_value_node;
192 parent_value_node=parent_value_node.cast_dynamic(value_desc.get_parent_value_node());
194 assert(parent_value_node);
196 Action::Handle action(Action::create("value_desc_set"));
199 throw Error(_("Unable to find action value_desc_set (bug)"));
201 action->set_param("canvas",get_canvas());
202 action->set_param("canvas_interface",get_canvas_interface());
203 action->set_param("time",time);
204 action->set_param("new_value",value);
205 action->set_param("value_desc",ValueDesc(parent_value_node,5));
207 if(!action->is_ready())
208 throw Error(Error::TYPE_NOTREADY);
213 // If we are a reference value node, then
214 // we need to distribute the changes to the
215 // referenced value node
216 if(value_desc.is_value_node() && ValueNode_Reference::Handle::cast_dynamic(value_desc.get_value_node()))
218 ValueDesc reference_value_desc(ValueNode_Reference::Handle::cast_dynamic(value_desc.get_value_node()),0);
220 Action::Handle action(Action::create("value_desc_set"));
223 throw Error(_("Unable to find action value_desc_set (bug)"));
225 action->set_param("canvas",get_canvas());
226 action->set_param("canvas_interface",get_canvas_interface());
227 action->set_param("time",time);
228 action->set_param("new_value",value);
229 action->set_param("value_desc",reference_value_desc);
231 if(!action->is_ready())
232 throw Error(Error::TYPE_NOTREADY);
239 // If we are a composite value node, then
240 // we need to distribute the changes to the
242 if(value_desc.is_value_node() && ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()))
244 ValueBase components[6];
246 switch(value.get_type())
248 case ValueBase::TYPE_VECTOR:
249 components[0]=value.get(Vector())[0];
250 components[1]=value.get(Vector())[1];
253 case ValueBase::TYPE_COLOR:
254 components[0]=value.get(Color()).get_r();
255 components[1]=value.get(Color()).get_g();
256 components[2]=value.get(Color()).get_b();
257 components[3]=value.get(Color()).get_a();
260 case ValueBase::TYPE_SEGMENT:
261 components[0]=value.get(Segment()).p1;
262 components[1]=value.get(Segment()).t1;
263 components[2]=value.get(Segment()).p2;
264 components[3]=value.get(Segment()).t2;
267 case ValueBase::TYPE_BLINEPOINT:
269 BLinePoint bline_point(value);
270 components[0]=bline_point.get_vertex();
271 components[1]=bline_point.get_width();
272 components[2]=bline_point.get_origin();
273 components[3]=bline_point.get_split_tangent_flag();
274 components[4]=bline_point.get_tangent1();
275 components[5]=bline_point.get_tangent2();
280 throw Error("Bad type for composite (%s)",ValueBase::type_name(value.get_type()).c_str());
284 for(int i=0;i<n_components;i++)
286 ValueDesc component_value_desc(ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()),i);
288 Action::Handle action(Action::create("value_desc_set"));
291 throw Error(_("Unable to find action value_desc_set (bug)"));
293 action->set_param("canvas",get_canvas());
294 action->set_param("canvas_interface",get_canvas_interface());
295 action->set_param("time",time);
296 action->set_param("new_value",components[i]);
297 action->set_param("value_desc",component_value_desc);
299 if(!action->is_ready())
300 throw Error(Error::TYPE_NOTREADY);
309 // If we are a RADIAL composite value node, then
310 // we need to distribute the changes to the
312 if(value_desc.is_value_node() && ValueNode_RadialComposite::Handle::cast_dynamic(value_desc.get_value_node()))
314 ValueBase components[6];
316 switch(value.get_type())
318 case ValueBase::TYPE_VECTOR:
320 Vector vect(value.get(Vector()));
321 components[0]=vect.mag();
322 components[1]=Angle(Angle::tan(vect[1],vect[0]));
326 case ValueBase::TYPE_COLOR:
327 components[0]=value.get(Color()).get_y();
328 components[1]=value.get(Color()).get_s();
329 components[2]=value.get(Color()).get_hue();
330 components[3]=value.get(Color()).get_a();
334 throw Error("Bad type for radial composite (%s)",ValueBase::type_name(value.get_type()).c_str());
337 for(int i=0;i<n_components;i++)
339 ValueDesc component_value_desc(ValueNode_RadialComposite::Handle::cast_dynamic(value_desc.get_value_node()),i);
341 Action::Handle action(Action::create("value_desc_set"));
344 throw Error(_("Unable to find action value_desc_set (bug)"));
346 action->set_param("canvas",get_canvas());
347 action->set_param("canvas_interface",get_canvas_interface());
348 action->set_param("time",time);
349 action->set_param("new_value",components[i]);
350 action->set_param("value_desc",component_value_desc);
352 if(!action->is_ready())
353 throw Error(Error::TYPE_NOTREADY);
361 // If we are merging the tangents of a BLinePoint,
362 // we must also set the second tangent for things
363 // to interpolate properly
364 if( value_desc.parent_is_value_node()
365 && value_desc.get_parent_value_node()->get_type()==ValueBase::TYPE_BLINEPOINT
366 && value_desc.get_index()==3
367 // && value.get(bool())==false // Actually, we want to do this any time the split flag is tweaked with
370 ValueNode_Composite::Handle parent_value_node;
371 parent_value_node=parent_value_node.cast_dynamic(value_desc.get_parent_value_node());
373 assert(parent_value_node);
375 Action::Handle action(Action::create("value_desc_set"));
378 throw Error(_("Unable to find action value_desc_set (bug)"));
380 action->set_param("canvas",get_canvas());
381 action->set_param("canvas_interface",get_canvas_interface());
382 action->set_param("time",time);
383 action->set_param("new_value",(*parent_value_node->get_link(4))(time));
384 action->set_param("value_desc",ValueDesc(parent_value_node,5));
386 if(!action->is_ready())
387 throw Error(Error::TYPE_NOTREADY);
393 if( value_desc.parent_is_value_node())
396 if(value_desc.get_parent_value_node()->get_type()==ValueBase::TYPE_BLINEPOINT)
399 if(value_desc.get_index()==4)
402 if((*value_desc.get_parent_value_node())(time).get(BLinePoint()).get_split_tangent_flag()==false)
412 // If we are in animate editing mode
413 if(get_edit_mode()&MODE_ANIMATE)
416 ValueNode_Animated::Handle& value_node(value_node_animated);
418 // If this value isn't a ValueNode_Animated, but
419 // it is somewhat constant, then go ahead and convert
420 // it to a ValueNode_Animated.
421 if(!value_desc.is_value_node() || ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
424 if(value_desc.is_value_node())
425 value=ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node())->get_value();
427 value=value_desc.get_value();
429 if(!value_node)value_node=ValueNode_Animated::create(value,time);
430 //if(!value_node)value_node=ValueNode_Animated::create(value.get_type());
432 Action::Handle action;
434 if(!value_desc.is_value_node())
436 action=(ValueDescConnect::create());
437 action->set_param("dest",value_desc);
438 action->set_param("src",ValueNode::Handle(value_node));
442 action=Action::create("value_node_replace");
443 action->set_param("dest",value_desc.get_value_node());
444 action->set_param("src",ValueNode::Handle(value_node));
447 action->set_param("canvas",get_canvas());
448 action->set_param("canvas_interface",get_canvas_interface());
450 if(!action->is_ready())
451 throw Error(Error::TYPE_NOTREADY);
454 add_action_front(action);
458 value_node=value_node.cast_dynamic(value_desc.get_value_node());
463 throw Error(_("Direct manipulation of this ValueNode type is not yet supported"));
465 Action::Handle action(WaypointSetSmart::create());
467 //Waypoint waypoint(value,time);
469 Waypoint waypoint(value_node->new_waypoint_at_time(time));
470 waypoint.set_value(value);
472 waypoint.set_before(synfigapp::Main::get_interpolation());
473 waypoint.set_after(synfigapp::Main::get_interpolation());
475 action->set_param("canvas",get_canvas());
476 action->set_param("canvas_interface",get_canvas_interface());
477 action->set_param("value_node",ValueNode::Handle(value_node));
478 action->set_param("waypoint",waypoint);
480 if(!action->is_ready())
481 throw Error(Error::TYPE_NOTREADY);
490 if(value_desc.is_value_node())
492 if(ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
494 Action::Handle action(ValueNodeConstSet::create());
496 action->set_param("canvas",get_canvas());
497 action->set_param("canvas_interface",get_canvas_interface());
498 action->set_param("value_node",value_desc.get_value_node());
499 action->set_param("new_value",value);
501 if(!action->is_ready())
502 throw Error(Error::TYPE_NOTREADY);
504 add_action_front(action);
508 if(ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node()))
509 throw Error(_("You must be in Animate-Editing-Mode to directly manipulate this value"));
511 throw Error(_("Direct manipulation of this ValueNode type is not yet supported"));
514 if(value_desc.parent_is_layer_param() && !value_desc.is_value_node())
516 Action::Handle layer_param_set(LayerParamSet::create());
518 layer_param_set->set_param("canvas",get_canvas());
519 layer_param_set->set_param("canvas_interface",get_canvas_interface());
520 layer_param_set->set_param("layer",value_desc.get_layer());
521 layer_param_set->set_param("param",value_desc.get_param_name());
522 layer_param_set->set_param("new_value",value);
524 if(!layer_param_set->is_ready())
525 throw Error(Error::TYPE_NOTREADY);
527 add_action_front(layer_param_set);
531 throw Error(_("Unsupported ValueDesc type"));