my log
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / activepointseton.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file activepointseton.cpp
3 **      \brief Template File
4 **
5 **      $Id: activepointseton.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include "activepointseton.h"
32 #include "activepointsetsmart.h"
33 #include "valuenodelinkconnect.h"
34 #include "valuenodereplace.h"
35
36 #include "activepointset.h"
37 #include "activepointadd.h"
38
39 #include "valuedescconnect.h"
40 #include <synfigapp/canvasinterface.h>
41
42 #endif
43
44 using namespace std;
45 using namespace etl;
46 using namespace synfig;
47 using namespace synfigapp;
48 using namespace Action;
49
50 /* === M A C R O S ========================================================= */
51
52 ACTION_INIT(Action::ActivepointSetOn);
53 ACTION_SET_NAME(Action::ActivepointSetOn,"activepoint_set_on");
54 ACTION_SET_LOCAL_NAME(Action::ActivepointSetOn,_("Mark Activepoint as \"On\""));
55 ACTION_SET_TASK(Action::ActivepointSetOn,"set_on");
56 ACTION_SET_CATEGORY(Action::ActivepointSetOn,Action::CATEGORY_ACTIVEPOINT|Action::CATEGORY_VALUEDESC);
57 ACTION_SET_PRIORITY(Action::ActivepointSetOn,-10);
58 ACTION_SET_VERSION(Action::ActivepointSetOn,"0.0");
59 ACTION_SET_CVS_ID(Action::ActivepointSetOn,"$Id: activepointseton.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $");
60
61 /* === G L O B A L S ======================================================= */
62
63 /* === P R O C E D U R E S ================================================= */
64
65 /* === M E T H O D S ======================================================= */
66
67 Action::ActivepointSetOn::ActivepointSetOn()
68 {
69         activepoint.set_time(Time::begin()-1);
70         time_set=false;
71         set_dirty(true);
72 }
73
74 Action::ParamVocab
75 Action::ActivepointSetOn::get_param_vocab()
76 {
77         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
78         
79         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
80                 .set_local_name(_("ValueDesc"))
81         );
82
83         ret.push_back(ParamDesc("activepoint",Param::TYPE_ACTIVEPOINT)
84                 .set_local_name(_("Activepoint"))
85                 .set_optional()
86         );
87
88         ret.push_back(ParamDesc("time",Param::TYPE_TIME)
89                 .set_local_name(_("Time"))
90                 .set_optional()
91         );
92         
93         return ret;
94 }
95
96 bool
97 Action::ActivepointSetOn::is_canidate(const ParamList &x)
98 {
99         if(canidate_check(get_param_vocab(),x))
100         {
101                 ValueDesc value_desc(x.find("value_desc")->second.get_value_desc());
102                 if(!value_desc.parent_is_value_node() || !ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node()))
103                         return false;
104
105                 // We are only a canidate if this canvas is animated.
106                 Canvas::Handle canvas(x.find("canvas")->second.get_canvas());
107                 if(canvas->rend_desc().get_time_start()==canvas->rend_desc().get_time_end())
108                         return false;
109
110                 // We need either a activepoint or a time.
111                 if(x.count("activepoint") || x.count("time"))
112                         return true;
113         }
114         return false;
115 }
116
117 bool
118 Action::ActivepointSetOn::set_param(const synfig::String& name, const Action::Param &param)
119 {
120         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
121         {
122                 value_desc=param.get_value_desc();
123                 
124                 if(!value_desc.parent_is_value_node())
125                         return false;
126                 
127                 value_node=ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node());
128                 
129                 if(!value_node)
130                         return false;
131                 
132                 index=value_desc.get_index();
133                 
134                 if(time_set)
135                         calc_activepoint();
136                 
137                 return true;
138         }
139         if(name=="activepoint" && param.get_type()==Param::TYPE_ACTIVEPOINT && !time_set)
140         {
141                 activepoint=param.get_activepoint();
142                 
143                 return true;
144         }
145         if(name=="time" && param.get_type()==Param::TYPE_TIME && activepoint.get_time()==Time::begin()-1)
146         {
147                 activepoint.set_time(param.get_time());
148                 time_set=true;
149
150                 if(value_node)
151                         calc_activepoint();
152                 
153                 return true;
154         }
155
156         return Action::CanvasSpecific::set_param(name,param);
157 }
158
159 bool
160 Action::ActivepointSetOn::is_ready()const
161 {
162         if(!value_node)
163                 synfig::error("Missing value_node");
164
165         if(activepoint.get_time()==(Time::begin()-1))
166                 synfig::error("Missing activepoint");
167         
168         if(!value_node || activepoint.get_time()==(Time::begin()-1))
169                 return false;
170         return Action::CanvasSpecific::is_ready();
171 }
172
173 // This function is called if a time is specified, but not 
174 // a activepoint. In this case, we need to calculate the value
175 // of the activepoint
176 void
177 Action::ActivepointSetOn::calc_activepoint()
178 {       
179         const Time time(activepoint.get_time());
180         
181         try { activepoint=*value_node->list[index].find(time); }
182         catch(...)
183         {
184                 activepoint.set_time(time);
185                 activepoint.set_state(value_node->list[index].status_at_time(time));
186                 activepoint.set_priority(0);
187         }
188 }
189
190 void
191 Action::ActivepointSetOn::prepare()
192 {
193         clear();
194
195         // Turn the activepoint off
196         activepoint.set_state(true);
197
198         Action::Handle action(ActivepointSetSmart::create());
199         
200         action->set_param("edit_mode",get_edit_mode());
201         action->set_param("canvas",get_canvas());
202         action->set_param("canvas_interface",get_canvas_interface());
203         action->set_param("value_desc",value_desc);
204         action->set_param("activepoint",activepoint);
205
206         assert(action->is_ready());
207         if(!action->is_ready())
208                 throw Error(Error::TYPE_NOTREADY);
209
210         add_action_front(action);
211         
212 }