Typo: 'totaly' -> 'totally'.
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / keyframewaypointset.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file keyframewaypointset.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 "keyframewaypointset.h"
33 #include <synfigapp/canvasinterface.h>
34 #include <synfig/valuenode_dynamiclist.h>
35 #include <synfig/valuenode_animated.h>
36 #include "activepointsetsmart.h"
37 #include "waypointsetsmart.h"
38
39 #endif
40
41 using namespace std;
42 using namespace etl;
43 using namespace synfig;
44 using namespace synfigapp;
45 using namespace Action;
46
47 /* === M A C R O S ========================================================= */
48
49 ACTION_INIT(Action::KeyframeWaypointSet);
50 ACTION_SET_NAME(Action::KeyframeWaypointSet,"keyframe_waypoint_set");
51 ACTION_SET_LOCAL_NAME(Action::KeyframeWaypointSet,"Set Waypoints at Keyframe");
52 ACTION_SET_TASK(Action::KeyframeWaypointSet,"set");
53 ACTION_SET_CATEGORY(Action::KeyframeWaypointSet,Action::CATEGORY_KEYFRAME);
54 ACTION_SET_PRIORITY(Action::KeyframeWaypointSet,0);
55 ACTION_SET_VERSION(Action::KeyframeWaypointSet,"0.0");
56 ACTION_SET_CVS_ID(Action::KeyframeWaypointSet,"$Id$");
57
58 /* === G L O B A L S ======================================================= */
59
60 /* === P R O C E D U R E S ================================================= */
61
62 /* === M E T H O D S ======================================================= */
63
64 Action::KeyframeWaypointSet::KeyframeWaypointSet()
65 {
66         keyframe.set_time(Time::begin()-1);
67         set_dirty(false);
68 }
69
70 Action::ParamVocab
71 Action::KeyframeWaypointSet::get_param_vocab()
72 {
73         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
74
75         ret.push_back(ParamDesc("keyframe",Param::TYPE_KEYFRAME)
76                 .set_local_name(_("Keyframe"))
77         );
78
79         ret.push_back(ParamDesc("model",Param::TYPE_WAYPOINTMODEL)
80                 .set_local_name(_("Waypoint Model"))
81         );
82
83         return ret;
84 }
85
86 bool
87 Action::KeyframeWaypointSet::is_candidate(const ParamList &x)
88 {
89         return candidate_check(get_param_vocab(),x);
90 }
91
92 bool
93 Action::KeyframeWaypointSet::set_param(const synfig::String& name, const Action::Param &param)
94 {
95         if(name=="keyframe" && param.get_type()==Param::TYPE_KEYFRAME)
96         {
97                 keyframe=param.get_keyframe();
98
99                 return true;
100         }
101         if(name=="model" && param.get_type()==Param::TYPE_WAYPOINTMODEL)
102         {
103                 waypoint_model=param.get_waypoint_model();
104
105                 return true;
106         }
107
108         return Action::CanvasSpecific::set_param(name,param);
109 }
110
111 bool
112 Action::KeyframeWaypointSet::is_ready()const
113 {
114         if(keyframe.get_time()==(Time::begin()-1) || waypoint_model.is_trivial())
115                 return false;
116         return Action::CanvasSpecific::is_ready();
117 }
118
119 void
120 Action::KeyframeWaypointSet::prepare()
121 {
122         clear();
123
124         try { get_canvas()->keyframe_list().find(keyframe);}
125         catch(synfig::Exception::NotFound)
126         {
127                 throw Error(_("Unable to find the given keyframe"));
128         }
129
130         {
131                 std::vector<synfigapp::ValueDesc> value_desc_list;
132                 get_canvas_interface()->find_important_value_descs(value_desc_list);
133                 while(!value_desc_list.empty())
134                 {
135                         process_value_desc(value_desc_list.back());
136                         value_desc_list.pop_back();
137                 }
138         }
139 }
140
141 void
142 Action::KeyframeWaypointSet::process_value_desc(const synfigapp::ValueDesc& value_desc)
143 {
144         if(value_desc.is_value_node())
145         {
146                 ValueNode_Animated::Handle value_node(ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node()));
147
148                 if(value_node)
149                 {
150                         Action::Handle action(WaypointSetSmart::create());
151
152                         action->set_param("canvas",get_canvas());
153                         action->set_param("canvas_interface",get_canvas_interface());
154                         action->set_param("value_node",ValueNode::Handle(value_node));
155
156                         Waypoint waypoint;
157                         try
158                         {
159                                 waypoint=*value_node->find(keyframe.get_time());
160                         }
161                         catch(...)
162                         {
163                                 waypoint.set_time(keyframe.get_time());
164                                 waypoint.set_value((*value_node)(keyframe.get_time()));
165                         }
166                         waypoint.apply_model(waypoint_model);
167
168                         action->set_param("waypoint",waypoint);
169
170                         assert(action->is_ready());
171                         if(!action->is_ready())
172                                 throw Error(Error::TYPE_NOTREADY);
173
174                         add_action_front(action);
175                 }
176         }
177 }