1 /* === S Y N F I G ========================================================= */
2 /*! \file valuedescblinelink.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 "layerparamconnect.h"
34 #include "valuenodelinkconnect.h"
35 #include "valuenodereplace.h"
36 #include "valuedescblinelink.h"
38 #include <synfigapp/canvasinterface.h>
39 #include <synfig/valuenode_const.h>
40 #include <synfig/valuenode_composite.h>
41 #include <synfig/valuenode_blinecalctangent.h>
42 #include <synfig/valuenode_blinecalcvertex.h>
43 #include <synfig/valuenode_blinecalcwidth.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(Action::ValueDescBLineLink);
58 ACTION_SET_NAME(Action::ValueDescBLineLink,"value_desc_bline_link");
59 ACTION_SET_LOCAL_NAME(Action::ValueDescBLineLink,N_("Link to BLine"));
60 ACTION_SET_TASK(Action::ValueDescBLineLink,"connect");
61 ACTION_SET_CATEGORY(Action::ValueDescBLineLink,Action::CATEGORY_BEZIER);
62 ACTION_SET_PRIORITY(Action::ValueDescBLineLink,0);
63 ACTION_SET_VERSION(Action::ValueDescBLineLink,"0.0");
64 ACTION_SET_CVS_ID(Action::ValueDescBLineLink,"$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::ValueDescBLineLink::ValueDescBLineLink()
77 Action::ValueDescBLineLink::get_param_vocab()
79 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
81 ret.push_back(ParamDesc("selected_value_desc",Param::TYPE_VALUEDESC)
82 .set_local_name(_("ValueDesc to link"))
83 .set_supports_multiple()
85 ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
86 .set_local_name(_("ValueDesc on BLine to link to"))
88 ret.push_back(ParamDesc("time",Param::TYPE_TIME)
89 .set_local_name(_("Time"))
92 ret.push_back(ParamDesc("origin",Param::TYPE_REAL)
93 .set_local_name(_("Origin"))
101 Action::ValueDescBLineLink::is_candidate(const ParamList &x)
103 ParamList::const_iterator i;
105 ValueDesc value_desc(x.find("value_desc")->second.get_value_desc());
107 if (!candidate_check(get_param_vocab(),x))
110 return (value_desc.parent_is_value_node() &&
111 // We need a dynamic list.
112 ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node()));
116 Action::ValueDescBLineLink::set_param(const synfig::String& name, const Action::Param ¶m)
118 if (name == "time" && param.get_type() == Param::TYPE_TIME)
120 time = param.get_time();
124 if (name == "value_desc" && param.get_type() == Param::TYPE_VALUEDESC)
126 value_desc = param.get_value_desc();
127 index = value_desc.get_index();
131 if (name == "selected_value_desc" && param.get_type() == Param::TYPE_VALUEDESC)
133 ValueDesc value_desc(param.get_value_desc());
134 value_desc_list.push_back(value_desc);
138 if (name == "origin" && param.get_type() == Param::TYPE_REAL)
140 origin = param.get_real();
144 return Action::CanvasSpecific::set_param(name,param);
148 Action::ValueDescBLineLink::is_ready()const
150 if (value_desc_list.size()<1)
154 return Action::CanvasSpecific::is_ready();
158 Action::ValueDescBLineLink::prepare()
160 if (value_desc_list.empty())
161 throw Error(Error::TYPE_NOTREADY);
165 ValueNode_DynamicList::Handle bline_value_node(ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node()));
166 bool loop(bline_value_node->get_loop());
167 int loop_adjust(loop ? 0 : -1);
168 const std::vector<ValueBase> bline((*bline_value_node)(time));
169 int size = bline.size();
170 Real amount = (index + origin + loop_adjust) / (size + loop_adjust);
171 LinkableValueNode::Handle calculated_value_node;
172 Action::Handle action;
174 ValueNode::Handle loop_value_node(ValueNode_Const::create(loop));
175 ValueNode::Handle amount_value_node(ValueNode_Const::create(amount));
177 for (std::list<ValueDesc>::iterator iter = value_desc_list.begin(); iter != value_desc_list.end(); ++iter)
179 ValueDesc& value_desc(*iter);
181 // parent is BLINEPOINT ValueNode
182 if (value_desc.parent_is_linkable_value_node() &&
183 value_desc.get_parent_value_node()->get_type() == ValueBase::TYPE_BLINEPOINT &&
184 ValueNode_Composite::Handle::cast_dynamic(value_desc.get_parent_value_node()))
186 String link_name(synfig::LinkableValueNode::Handle::cast_reinterpret(value_desc.get_parent_value_node())->
187 link_name(value_desc.get_index()));
188 if (link_name == "t1" || link_name == "t2")
189 calculated_value_node = ValueNode_BLineCalcTangent::create(ValueBase::TYPE_VECTOR);
190 else if (link_name == "width")
191 calculated_value_node = ValueNode_BLineCalcWidth::create(ValueBase::TYPE_REAL);
192 else if (link_name == "point")
193 calculated_value_node = ValueNode_BLineCalcVertex::create(ValueBase::TYPE_VECTOR);
196 synfig::warning("can't link '%s'", link_name.c_str());
200 action = ValueNodeLinkConnect::create();
201 action->set_param("parent_value_node", value_desc.get_parent_value_node());
202 action->set_param("index", value_desc.get_index());
204 // BLINEPOINT ValueNode - link its vertex
205 else if (value_desc.is_value_node() &&
206 value_desc.get_value_type() == ValueBase::TYPE_BLINEPOINT &&
207 ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()))
209 calculated_value_node = ValueNode_BLineCalcVertex::create(ValueBase::TYPE_VECTOR);
210 action = ValueNodeLinkConnect::create();
211 action->set_param("parent_value_node", value_desc.get_value_node());
212 action->set_param("index", 0); // index for 'vertex' in 'composite'
214 // exported ValueNode
215 else if (value_desc.parent_is_canvas())
217 if (value_desc.get_value_type() == ValueBase::TYPE_VECTOR)
218 calculated_value_node = ValueNode_BLineCalcVertex::create(ValueBase::TYPE_VECTOR);
219 else if (value_desc.get_value_type() == ValueBase::TYPE_REAL)
220 calculated_value_node = ValueNode_BLineCalcWidth::create(ValueBase::TYPE_REAL);
224 calculated_value_node->set_link("bline", bline_value_node);
225 calculated_value_node->set_link("loop", ValueNode_Const::create(loop));
226 calculated_value_node->set_link("amount", ValueNode_Const::create(amount));
228 action = ValueNodeReplace::create();
229 action->set_param("canvas", get_canvas());
230 action->set_param("canvas_interface", get_canvas_interface());
231 action->set_param("src", ValueNode::Handle(calculated_value_node));
232 action->set_param("dest", value_desc.get_value_node());
234 assert(action->is_ready());
235 if (!action->is_ready()) throw Error(Error::TYPE_NOTREADY);
236 add_action_front(action);
240 else if (value_desc.parent_is_layer_param())
242 // VECTOR layer parameter
243 if (value_desc.get_value_type() == ValueBase::TYPE_VECTOR)
244 calculated_value_node = ValueNode_BLineCalcVertex::create(ValueBase::TYPE_VECTOR);
245 // REAL layer parameter
246 else if (value_desc.get_value_type() == ValueBase::TYPE_REAL)
247 calculated_value_node = ValueNode_BLineCalcWidth::create(ValueBase::TYPE_REAL);
248 // ANGLE layer parameter
249 else if (value_desc.get_value_type() == ValueBase::TYPE_ANGLE)
250 calculated_value_node = ValueNode_BLineCalcTangent::create(ValueBase::TYPE_ANGLE);
254 action = LayerParamConnect::create();
255 action->set_param("layer", value_desc.get_layer());
256 action->set_param("param", value_desc.get_param_name());
261 calculated_value_node->set_link("bline", bline_value_node );
262 calculated_value_node->set_link("loop", loop_value_node );
263 calculated_value_node->set_link("amount", amount_value_node);
265 action->set_param("canvas", get_canvas());
266 action->set_param("canvas_interface", get_canvas_interface());
267 action->set_param("value_node", ValueNode::Handle(calculated_value_node));
269 assert(action->is_ready());
270 if (!action->is_ready()) throw Error(Error::TYPE_NOTREADY);
271 add_action_front(action);