Added a \todo note questioning a part of the code.
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / activepointsetoff.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file activepointsetoff.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 "activepointsetoff.h"
33 #include "activepointsetsmart.h"
34 #include "valuenodelinkconnect.h"
35 #include "valuenodereplace.h"
36
37 #include "activepointset.h"
38 #include "activepointadd.h"
39
40 #include "valuedescconnect.h"
41 #include <synfigapp/canvasinterface.h>
42
43 #endif
44
45 using namespace std;
46 using namespace etl;
47 using namespace synfig;
48 using namespace synfigapp;
49 using namespace Action;
50
51 /* === M A C R O S ========================================================= */
52
53 ACTION_INIT(Action::ActivepointSetOff);
54 ACTION_SET_NAME(Action::ActivepointSetOff,"activepoint_set_off");
55 ACTION_SET_LOCAL_NAME(Action::ActivepointSetOff,_("Mark Activepoint as \"Off\""));
56 ACTION_SET_TASK(Action::ActivepointSetOff,"set_off");
57 ACTION_SET_CATEGORY(Action::ActivepointSetOff,Action::CATEGORY_ACTIVEPOINT|Action::CATEGORY_VALUEDESC);
58 ACTION_SET_PRIORITY(Action::ActivepointSetOff,-9);
59 ACTION_SET_VERSION(Action::ActivepointSetOff,"0.0");
60 ACTION_SET_CVS_ID(Action::ActivepointSetOff,"$Id$");
61
62 /* === G L O B A L S ======================================================= */
63
64 /* === P R O C E D U R E S ================================================= */
65
66 /* === M E T H O D S ======================================================= */
67
68 Action::ActivepointSetOff::ActivepointSetOff()
69 {
70         activepoint.set_time(Time::begin()-1);
71         time_set=false;
72         set_dirty(true);
73 }
74
75 Action::ParamVocab
76 Action::ActivepointSetOff::get_param_vocab()
77 {
78         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
79
80         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
81                 .set_local_name(_("ValueDesc"))
82         );
83
84         ret.push_back(ParamDesc("activepoint",Param::TYPE_ACTIVEPOINT)
85                 .set_local_name(_("Activepoint"))
86                 .set_optional()
87         );
88
89         ret.push_back(ParamDesc("time",Param::TYPE_TIME)
90                 .set_local_name(_("Time"))
91                 .set_optional()
92         );
93
94         return ret;
95 }
96
97 bool
98 Action::ActivepointSetOff::is_candidate(const ParamList &x)
99 {
100         if(candidate_check(get_param_vocab(),x))
101         {
102                 ValueDesc value_desc(x.find("value_desc")->second.get_value_desc());
103                 if(!value_desc.parent_is_value_node() || !ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node()))
104                         return false;
105
106                 // We are only a candidate if this canvas is animated.
107                 Canvas::Handle canvas(x.find("canvas")->second.get_canvas());
108                 if(canvas->rend_desc().get_time_start()==canvas->rend_desc().get_time_end())
109                         return false;
110
111                 // We need either a activepoint or a time.
112                 if(x.count("activepoint") || x.count("time"))
113                         return true;
114         }
115         return false;
116 }
117
118 bool
119 Action::ActivepointSetOff::set_param(const synfig::String& name, const Action::Param &param)
120 {
121         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
122         {
123                 value_desc=param.get_value_desc();
124
125                 if(!value_desc.parent_is_value_node())
126                         return false;
127
128                 value_node=ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node());
129
130                 if(!value_node)
131                         return false;
132
133                 index=value_desc.get_index();
134
135                 if(time_set)
136                         calc_activepoint();
137
138                 return true;
139         }
140         if(name=="activepoint" && param.get_type()==Param::TYPE_ACTIVEPOINT && !time_set)
141         {
142                 activepoint=param.get_activepoint();
143
144                 return true;
145         }
146         if(name=="time" && param.get_type()==Param::TYPE_TIME && activepoint.get_time()==Time::begin()-1)
147         {
148                 activepoint.set_time(param.get_time());
149                 time_set=true;
150
151                 if(value_node)
152                         calc_activepoint();
153
154                 return true;
155         }
156
157         return Action::CanvasSpecific::set_param(name,param);
158 }
159
160 bool
161 Action::ActivepointSetOff::is_ready()const
162 {
163         if(!value_node)
164                 synfig::error("Missing value_node");
165
166         if(activepoint.get_time()==(Time::begin()-1))
167                 synfig::error("Missing activepoint");
168
169         if(!value_node || activepoint.get_time()==(Time::begin()-1))
170                 return false;
171         return Action::CanvasSpecific::is_ready();
172 }
173
174 // This function is called if a time is specified, but not
175 // a activepoint. In this case, we need to calculate the value
176 // of the activepoint
177 void
178 Action::ActivepointSetOff::calc_activepoint()
179 {
180         const Time time(activepoint.get_time());
181
182         try { activepoint=*value_node->list[index].find(time); }
183         catch(...)
184         {
185                 activepoint.set_time(time);
186                 activepoint.set_state(value_node->list[index].status_at_time(time));
187                 activepoint.set_priority(0);
188         }
189 }
190
191 void
192 Action::ActivepointSetOff::prepare()
193 {
194         clear();
195
196         // Turn the activepoint off
197         activepoint.set_state(false);
198
199         Action::Handle action(ActivepointSetSmart::create());
200
201         action->set_param("edit_mode",get_edit_mode());
202         action->set_param("canvas",get_canvas());
203         action->set_param("canvas_interface",get_canvas_interface());
204         action->set_param("value_desc",value_desc);
205         action->set_param("activepoint",activepoint);
206
207         assert(action->is_ready());
208         if(!action->is_ready())
209                 throw Error(Error::TYPE_NOTREADY);
210
211         add_action_front(action);
212
213 }