my log
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / activepointadd.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file acitvepointadd.cpp
3 **      \brief Template File
4 **
5 **      $Id: activepointadd.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 "activepointadd.h"
32 #include <synfigapp/canvasinterface.h>
33
34 #endif
35
36 using namespace std;
37 using namespace etl;
38 using namespace synfig;
39 using namespace synfigapp;
40 using namespace Action;
41
42 /* === M A C R O S ========================================================= */
43
44 ACTION_INIT(Action::ActivepointAdd);
45 ACTION_SET_NAME(Action::ActivepointAdd,"activepoint_add");
46 ACTION_SET_LOCAL_NAME(Action::ActivepointAdd,"Add Activepoint");
47 ACTION_SET_TASK(Action::ActivepointAdd,"add");
48 ACTION_SET_CATEGORY(Action::ActivepointAdd,Action::CATEGORY_ACTIVEPOINT);
49 ACTION_SET_PRIORITY(Action::ActivepointAdd,0);
50 ACTION_SET_VERSION(Action::ActivepointAdd,"0.0");
51 ACTION_SET_CVS_ID(Action::ActivepointAdd,"$Id: activepointadd.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $");
52
53 /* === G L O B A L S ======================================================= */
54
55 /* === P R O C E D U R E S ================================================= */
56
57 /* === M E T H O D S ======================================================= */
58
59 Action::ActivepointAdd::ActivepointAdd()
60 {
61         activepoint.set_time(Time::begin()-1);
62         time_set=false;
63         set_dirty(true);
64 }
65
66 Action::ParamVocab
67 Action::ActivepointAdd::get_param_vocab()
68 {
69         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
70         
71         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
72                 .set_local_name(_("ValueDesc"))
73         );
74
75         ret.push_back(ParamDesc("activepoint",Param::TYPE_ACTIVEPOINT)
76                 .set_local_name(_("New Activepoint"))
77                 .set_desc(_("Activepoint to be added"))
78                 .set_optional()
79         );
80
81         ret.push_back(ParamDesc("time",Param::TYPE_TIME)
82                 .set_local_name(_("Time"))
83                 .set_desc(_("Time where activepoint is to be added"))
84                 .set_optional()
85         );
86
87         return ret;
88 }
89
90 bool
91 Action::ActivepointAdd::is_canidate(const ParamList &x)
92 {
93         if(canidate_check(get_param_vocab(),x))
94         {
95                 ValueDesc value_desc(x.find("value_desc")->second.get_value_desc());
96                 if(!value_desc.parent_is_value_node() || !ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node()))
97                         return false;
98
99                 // We need either a activepoint or a time.
100                 if(x.count("activepoint") || x.count("time"))
101                         return true;
102         }
103         return false;
104 }
105
106 bool
107 Action::ActivepointAdd::set_param(const synfig::String& name, const Action::Param &param)
108 {
109         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
110         {
111                 ValueDesc value_desc(param.get_value_desc());
112                 
113                 if(!value_desc.parent_is_value_node())
114                         return false;
115                 
116                 value_node=ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node());
117                 
118                 if(!value_node)
119                         return false;
120                 
121                 index=value_desc.get_index();
122                 
123                 if(time_set)
124                         calc_activepoint();
125                 
126                 return true;
127         }
128         if(name=="activepoint" && param.get_type()==Param::TYPE_ACTIVEPOINT && !time_set)
129         {
130                 activepoint=param.get_activepoint();
131                 
132                 return true;
133         }
134         if(name=="time" && param.get_type()==Param::TYPE_TIME && activepoint.get_time()==Time::begin()-1)
135         {
136                 activepoint.set_time(param.get_time());
137                 time_set=true;
138
139                 if(value_node)
140                         calc_activepoint();
141                 
142                 return true;
143         }
144
145         return Action::CanvasSpecific::set_param(name,param);
146 }
147
148 bool
149 Action::ActivepointAdd::is_ready()const
150 {
151         if(!value_node || activepoint.get_time()==(Time::begin()-1))
152                 return false;
153         return Action::CanvasSpecific::is_ready();
154 }
155
156 // This function is called if a time is specified, but not 
157 // a activepoint. In this case, we need to calculate the value
158 // of the activepoint
159 void
160 Action::ActivepointAdd::calc_activepoint()
161 {       
162         const Time time(activepoint.get_time());
163         activepoint.set_state(value_node->list[index].status_at_time(time));
164         activepoint.set_priority(0);
165         
166         // In this case, nothing is really changing, so there will be
167         // no need to redraw the window
168         set_dirty(false);
169 }
170
171 void
172 Action::ActivepointAdd::perform()
173 {       
174         try { value_node->list[index].find(activepoint.get_time()); throw Error(_("A Activepoint already exists at this point in time"));}
175         catch(synfig::Exception::NotFound) { }  
176
177         try { if(value_node->list[index].find(activepoint)!=value_node->list[index].timing_info.end()) throw Error(_("This activepoint is already in the ValueNode"));}
178         catch(synfig::Exception::NotFound) { }  
179         
180         value_node->list[index].add(activepoint);
181         value_node->changed();
182         
183         /*if(get_canvas_interface())
184         {
185                 get_canvas_interface()->signal_value_node_changed()(value_node);
186         }
187         else synfig::warning("CanvasInterface not set on action");
188         */
189 }
190
191 void
192 Action::ActivepointAdd::undo()
193 {
194         value_node->list[index].erase(activepoint);
195         value_node->changed();
196         /*
197         // Signal that a layer has been inserted
198         if(get_canvas_interface())
199         {
200                 get_canvas_interface()->signal_value_node_changed()(value_node);
201         }
202         else synfig::warning("CanvasInterface not set on action");
203         */
204 }