Fix 1501383: "del key deletes object instead of character". Changed the LayerRemove...
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / valuedescblinelink.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuedescblinelink.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 Chris Moore
10 **
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.
15 **
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.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "layerparamconnect.h"
34 #include "valuenodelinkconnect.h"
35 #include "valuenodereplace.h"
36 #include "valuedescblinelink.h"
37
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>
44
45 #include <synfigapp/general.h>
46
47 #endif
48
49 using namespace std;
50 using namespace etl;
51 using namespace synfig;
52 using namespace synfigapp;
53 using namespace Action;
54
55 /* === M A C R O S ========================================================= */
56
57 ACTION_INIT(Action::ValueDescBLineLink);
58 ACTION_SET_NAME(Action::ValueDescBLineLink,"ValueDescBLineLink");
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$");
65
66 /* === G L O B A L S ======================================================= */
67
68 /* === P R O C E D U R E S ================================================= */
69
70 /* === M E T H O D S ======================================================= */
71
72 Action::ValueDescBLineLink::ValueDescBLineLink()
73 {
74 }
75
76 Action::ParamVocab
77 Action::ValueDescBLineLink::get_param_vocab()
78 {
79         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
80
81         ret.push_back(ParamDesc("selected_value_desc",Param::TYPE_VALUEDESC)
82                 .set_local_name(_("ValueDesc to link"))
83                 .set_supports_multiple()
84         );
85         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
86                 .set_local_name(_("ValueDesc on BLine to link to"))
87         );
88         ret.push_back(ParamDesc("time",Param::TYPE_TIME)
89                 .set_local_name(_("Time"))
90                 .set_optional()
91         );
92         ret.push_back(ParamDesc("origin",Param::TYPE_REAL)
93                 .set_local_name(_("Origin"))
94                 .set_optional()
95         );
96
97         return ret;
98 }
99
100 bool
101 Action::ValueDescBLineLink::is_candidate(const ParamList &x)
102 {
103         ParamList::const_iterator i;
104
105         ValueDesc value_desc(x.find("value_desc")->second.get_value_desc());
106
107         if (!candidate_check(get_param_vocab(),x))
108                 return false;
109
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()));
113 }
114
115 bool
116 Action::ValueDescBLineLink::set_param(const synfig::String& name, const Action::Param &param)
117 {
118         if (name == "time" && param.get_type() == Param::TYPE_TIME)
119         {
120                 time = param.get_time();
121                 return true;
122         }
123
124         if (name == "value_desc" && param.get_type() == Param::TYPE_VALUEDESC)
125         {
126                 value_desc = param.get_value_desc();
127                 index = value_desc.get_index();
128                 return true;
129         }
130
131         if (name == "selected_value_desc" && param.get_type() == Param::TYPE_VALUEDESC)
132         {
133                 ValueDesc value_desc(param.get_value_desc());
134                 value_desc_list.push_back(value_desc);
135                 return true;
136         }
137
138         if (name == "origin" && param.get_type() == Param::TYPE_REAL)
139         {
140                 origin = param.get_real();
141                 return true;
142         }
143
144         return Action::CanvasSpecific::set_param(name,param);
145 }
146
147 bool
148 Action::ValueDescBLineLink::is_ready()const
149 {
150         if (value_desc_list.size()<1)
151                 return false;
152         if (!value_desc)
153                 return false;
154         return Action::CanvasSpecific::is_ready();
155 }
156
157 void
158 Action::ValueDescBLineLink::prepare()
159 {
160         if (value_desc_list.empty())
161                 throw Error(Error::TYPE_NOTREADY);
162
163         clear();
164
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).get_list());
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;
173
174         ValueNode::Handle loop_value_node(ValueNode_Const::create(loop));
175         ValueNode::Handle amount_value_node(ValueNode_Const::create(amount));
176
177         for (std::list<ValueDesc>::iterator iter = value_desc_list.begin(); iter != value_desc_list.end(); ++iter)
178         {
179                 ValueDesc& value_desc(*iter);
180
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()))
185                 {
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);
194                         else
195                         {
196                                 synfig::warning("can't link '%s'", link_name.c_str());
197                                 continue;
198                         }
199
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());
203                 }
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()))
208                 {
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'
213                 }
214                 // exported ValueNode
215                 else if (value_desc.parent_is_canvas())
216                 {
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);
221                         else
222                                 continue;
223
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));
227
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());
233
234                         assert(action->is_ready());
235                         if (!action->is_ready()) throw Error(Error::TYPE_NOTREADY);
236                         add_action_front(action);
237
238                         continue;
239                 }
240                 else if (value_desc.parent_is_layer_param())
241                 {
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);
251                         else
252                                 continue;
253
254                         action = LayerParamConnect::create();
255                         action->set_param("layer", value_desc.get_layer());
256                         action->set_param("param", value_desc.get_param_name());
257                 }
258                 else
259                         continue;
260
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);
264
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));
268
269                 assert(action->is_ready());
270                 if (!action->is_ready()) throw Error(Error::TYPE_NOTREADY);
271                 add_action_front(action);
272         }
273 }