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