When merging tangents, use the average of the two tangents we're merging, rather...
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / blinepointtangentmerge.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file blinepointtangentmerge.cpp
3 **      \brief Template File
4 **
5 **      $Id$
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 "blinepointtangentmerge.h"
33 #include "valuedescset.h"
34
35 #include "activepointset.h"
36 #include "activepointadd.h"
37
38 #include "valuedescconnect.h"
39 #include <synfigapp/canvasinterface.h>
40
41 #endif
42
43 using namespace std;
44 using namespace etl;
45 using namespace synfig;
46 using namespace synfigapp;
47 using namespace Action;
48
49 /* === M A C R O S ========================================================= */
50
51 ACTION_INIT(Action::BLinePointTangentMerge);
52 ACTION_SET_NAME(Action::BLinePointTangentMerge,"bline_point_tangent_merge");
53 ACTION_SET_LOCAL_NAME(Action::BLinePointTangentMerge,_("Merge Tangents"));
54 ACTION_SET_TASK(Action::BLinePointTangentMerge,"merge");
55 ACTION_SET_CATEGORY(Action::BLinePointTangentMerge,Action::CATEGORY_VALUENODE);
56 ACTION_SET_PRIORITY(Action::BLinePointTangentMerge,0);
57 ACTION_SET_VERSION(Action::BLinePointTangentMerge,"0.0");
58 ACTION_SET_CVS_ID(Action::BLinePointTangentMerge,"$Id$");
59
60 /* === G L O B A L S ======================================================= */
61
62 /* === P R O C E D U R E S ================================================= */
63
64 /* === M E T H O D S ======================================================= */
65
66 Action::BLinePointTangentMerge::BLinePointTangentMerge()
67 {
68         time=(Time::begin()-1);
69         set_dirty(true);
70 }
71
72 Action::ParamVocab
73 Action::BLinePointTangentMerge::get_param_vocab()
74 {
75         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
76
77         ret.push_back(ParamDesc("value_node",Param::TYPE_VALUENODE)
78                 .set_local_name(_("ValueNode of BLinePoint"))
79         );
80
81         ret.push_back(ParamDesc("time",Param::TYPE_TIME)
82                 .set_local_name(_("Time"))
83         );
84
85         return ret;
86 }
87
88 bool
89 Action::BLinePointTangentMerge::is_candidate(const ParamList &x)
90 {
91         if(candidate_check(get_param_vocab(),x))
92         {
93                 ValueNode_Composite::Handle value_node;
94                 value_node=ValueNode_Composite::Handle::cast_dynamic(x.find("value_node")->second.get_value_node());
95                 if(!value_node || value_node->get_type()!=ValueBase::TYPE_BLINEPOINT)
96                         return false;
97                 synfig::Time time(x.find("time")->second.get_time());
98                 if((*value_node->get_link("split"))(time).get(bool())==false)
99                         return false;
100                 return true;
101         }
102         return false;
103 }
104
105 bool
106 Action::BLinePointTangentMerge::set_param(const synfig::String& name, const Action::Param &param)
107 {
108         if(name=="value_node" && param.get_type()==Param::TYPE_VALUENODE)
109         {
110                 value_node=value_node.cast_dynamic(param.get_value_node());
111
112                 return (bool)(value_node);
113         }
114         if(name=="time" && param.get_type()==Param::TYPE_TIME)
115         {
116                 time=param.get_time();
117
118                 return true;
119         }
120
121         return Action::CanvasSpecific::set_param(name,param);
122 }
123
124 bool
125 Action::BLinePointTangentMerge::is_ready()const
126 {
127         if(!value_node)
128                 synfig::error("Missing or bad value_node");
129
130         if(time==(Time::begin()-1))
131                 synfig::error("Missing time");
132
133         if(!value_node || time==(Time::begin()-1))
134                 return false;
135         return Action::CanvasSpecific::is_ready();
136 }
137
138 void
139 Action::BLinePointTangentMerge::prepare()
140 {
141         clear();
142
143         Action::Handle action;
144
145         {
146                 action=Action::create("value_desc_set");
147                 if(!action)
148                         throw Error(_("Couldn't find action \"value_desc_set\""));
149
150                 action->set_param("canvas",get_canvas());
151                 action->set_param("canvas_interface",get_canvas_interface());
152                 action->set_param("value_desc",ValueDesc(value_node,3));
153                 action->set_param("time",time);
154                 action->set_param("new_value",synfig::ValueBase(false));
155
156                 assert(action->is_ready());
157                 if(!action->is_ready())
158                         throw Error(Error::TYPE_NOTREADY);
159
160                 add_action(action);
161         }
162
163         // the merged tangent should be the average of the 2 tangents we're merging
164         ValueBase average(((Vector)((*value_node->get_link("t1"))(time)) +
165                                            (Vector)((*value_node->get_link("t2"))(time))) / 2);
166
167         {
168                 // set tangent1
169                 action=Action::create("value_desc_set");
170                 if(!action)
171                         throw Error(_("Couldn't find action \"value_desc_set\""));
172
173                 action->set_param("canvas",get_canvas());
174                 action->set_param("canvas_interface",get_canvas_interface());
175                 action->set_param("value_desc",ValueDesc(value_node,4));
176                 action->set_param("time",time);
177                 action->set_param("new_value",average);
178
179                 assert(action->is_ready());
180                 if(!action->is_ready())
181                         throw Error(Error::TYPE_NOTREADY);
182
183                 add_action(action);
184         }
185
186         {
187                 // set tangent2
188                 action=Action::create("value_desc_set");
189                 if(!action)
190                         throw Error(_("Couldn't find action \"value_desc_set\""));
191
192                 action->set_param("canvas",get_canvas());
193                 action->set_param("canvas_interface",get_canvas_interface());
194                 action->set_param("value_desc",ValueDesc(value_node,5));
195                 action->set_param("time",time);
196                 action->set_param("new_value",average);
197
198                 assert(action->is_ready());
199                 if(!action->is_ready())
200                         throw Error(Error::TYPE_NOTREADY);
201
202                 add_action(action);
203         }
204 }