f3a5323a07473dfc84fcba20a6854547d50861a0
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / activepointset.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file activepointset.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 "activepointset.h"
33 #include <synfigapp/canvasinterface.h>
34
35 #include <synfigapp/general.h>
36
37 #endif
38
39 using namespace std;
40 using namespace etl;
41 using namespace synfig;
42 using namespace synfigapp;
43 using namespace Action;
44
45 /* === M A C R O S ========================================================= */
46
47 ACTION_INIT(Action::ActivepointSet);
48 ACTION_SET_NAME(Action::ActivepointSet,"activepoint_set");
49 ACTION_SET_LOCAL_NAME(Action::ActivepointSet,N_("Set Activepoint"));
50 ACTION_SET_TASK(Action::ActivepointSet,"set");
51 ACTION_SET_CATEGORY(Action::ActivepointSet,Action::CATEGORY_ACTIVEPOINT);
52 ACTION_SET_PRIORITY(Action::ActivepointSet,0);
53 ACTION_SET_VERSION(Action::ActivepointSet,"0.0");
54 ACTION_SET_CVS_ID(Action::ActivepointSet,"$Id$");
55
56 /* === G L O B A L S ======================================================= */
57
58 /* === P R O C E D U R E S ================================================= */
59
60 /* === M E T H O D S ======================================================= */
61
62 Action::ActivepointSet::ActivepointSet()
63 {
64         set_dirty(true);
65 }
66
67 Action::ParamVocab
68 Action::ActivepointSet::get_param_vocab()
69 {
70         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
71
72         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
73                 .set_local_name(_("ValueDesc"))
74         );
75
76         ret.push_back(ParamDesc("activepoint",Param::TYPE_ACTIVEPOINT)
77                 .set_local_name(_("Activepoint"))
78                 .set_desc(_("Activepoint to be changed"))
79                 .set_supports_multiple()
80         );
81
82         return ret;
83 }
84
85 bool
86 Action::ActivepointSet::is_candidate(const ParamList &x)
87 {
88         if (!candidate_check(get_param_vocab(),x))
89                 return false;
90
91         ValueDesc value_desc(x.find("value_desc")->second.get_value_desc());
92
93         return (value_desc.parent_is_value_node() &&
94                         // We need a dynamic list.
95                         ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node()));
96 }
97
98 bool
99 Action::ActivepointSet::set_param(const synfig::String& name, const Action::Param &param)
100 {
101         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
102         {
103                 ValueDesc value_desc(param.get_value_desc());
104
105                 if(!value_desc.parent_is_value_node())
106                         return false;
107
108                 value_node=ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node());
109
110                 if(!value_node)
111                         return false;
112
113                 index=value_desc.get_index();
114
115                 return true;
116         }
117         if(name=="activepoint" && param.get_type()==Param::TYPE_ACTIVEPOINT)
118         {
119                 //NOTE: there is no duplication checking at the moment
120                 activepoints.push_back(param.get_activepoint());
121
122                 return true;
123         }
124
125         return Action::CanvasSpecific::set_param(name,param);
126 }
127
128 bool
129 Action::ActivepointSet::is_ready()const
130 {
131         if(!value_node || activepoints.empty())
132                 return false;
133         return Action::CanvasSpecific::is_ready();
134 }
135
136 void
137 Action::ActivepointSet::perform()
138 {
139         typedef ValueNode_DynamicList::ListEntry::ActivepointList AList;
140         AList::iterator iter;
141
142 #if 1
143         vector<AList::iterator> iters;
144         vector<Activepoint>::iterator i = activepoints.begin(), end = activepoints.end();
145
146         for(; i != end; ++i)
147         {
148                 try { iters.push_back(value_node->list[index].find(*i)); }
149                 catch(synfig::Exception::NotFound)
150                 {
151                         throw Error(_("Unable to find activepoint"));
152                 }
153         }
154
155         //check to see which valuenodes are going to override because of the time...
156         ValueNode_DynamicList::ListEntry::findresult timeiter;
157
158         for(i = activepoints.begin(); i != end; ++i)
159         {
160                 timeiter = value_node->list[index].find_time(i->get_time());
161
162                 bool candelete = timeiter.second;
163
164                 //we only want to track overwrites (not activepoints that are also being modified)
165                 if(candelete)
166                 {
167                         for(vector<AList::iterator>::iterator ii = iters.begin(); ii != iters.end(); ++ii)
168                         {
169                                 if(timeiter.first == *ii)
170                                 {
171                                         candelete = false;
172                                         break;
173                                 }
174                         }
175                 }
176
177                 //if we can still delete it after checking, record it, and then remove them all later
178                 if(candelete)
179                 {
180                         Activepoint a = *timeiter.first;
181                         overwritten_activepoints.push_back(a);
182                 }
183         }
184
185         //overwrite all the valuenodes we're supposed to set
186         {
187                 i = activepoints.begin();
188                 for(vector<AList::iterator>::iterator ii = iters.begin(); ii != iters.end() && i != end; ++ii, ++i)
189                 {
190                         old_activepoints.push_back(**ii);
191                         **ii = *i; //set the point to the corresponding point in the normal waypoint list
192                 }
193         }
194
195         //remove all the points we're supposed to be overwriting
196         {
197                 vector<Activepoint>::iterator   oi = overwritten_activepoints.begin(),
198                                                                                 oend = overwritten_activepoints.end();
199                 for(; oi != oend; ++oi)
200                 {
201                         value_node->list[index].erase(*oi);
202                 }
203         }
204
205 #else
206         try { iter=value_node->list[index].find(activepoint); }
207         catch(synfig::Exception::NotFound)
208         {
209                 throw Error(_("Unable to find activepoint"));
210         }
211
212         //find the value at the old time before we replace it
213         ValueNode_DynamicList::ListEntry::findresult timeiter;
214         timeiter = value_node->list[index].find_time(activepoint.get_time());
215
216         //we only want to track overwrites (not inplace modifications)
217         if(timeiter.second && activepoint.get_uid() == timeiter.first->get_uid())
218         {
219                 timeiter.second = false;
220         }
221
222         old_activepoint=*iter;
223         *iter=activepoint;
224
225         if(timeiter.second)
226         {
227                 synfig::info("Erasing the found activepoint");
228                 time_overwrite = true;
229                 overwritten_ap = *timeiter.first;
230
231                 value_node->list[index].erase(overwritten_ap);
232         }
233
234 #endif
235
236         value_node->list[index].timing_info.sort();
237
238         // Signal that a valuenode has been changed
239         value_node->changed();
240 }
241
242 void
243 Action::ActivepointSet::undo()
244 {
245         ValueNode_DynamicList::ListEntry::ActivepointList::iterator iter;
246
247 #if 1
248         vector<Activepoint>::iterator i = old_activepoints.begin(), end = old_activepoints.end();
249
250         for(; i != end; ++i)
251         {
252                 try { iter = value_node->list[index].find(*i); }
253                 catch(synfig::Exception::NotFound)
254                 {
255                         throw Error(_("Unable to find activepoint"));
256                 }
257
258                 //overwrite with old one
259                 *iter = *i;
260         }
261
262         //add back in all the points that we removed before...
263         {
264                 vector<Activepoint>::iterator   oi = overwritten_activepoints.begin(),
265                                                                                 oend = overwritten_activepoints.end();
266                 for(; oi != oend; ++oi)
267                 {
268                         value_node->list[index].add(*oi);
269                 }
270         }
271
272 #else
273         try { iter=value_node->list[index].find(activepoint); }
274         catch(synfig::Exception::NotFound)
275         {
276                 throw Error(_("Unable to find activepoint"));
277         }
278
279         *iter=old_activepoint;
280
281         if(time_overwrite)
282         {
283                 value_node->list[index].add(overwritten_ap);
284         }
285 #endif
286
287         value_node->list[index].timing_info.sort();
288
289         // Signal that a valuenode has been changed
290         value_node->changed();
291 }