1 /* === S Y N F I G ========================================================= */
2 /*! \file layerresetpose.cpp
3 ** \brief Template File
4 **
5 ** $Id$
6 **
7 ** \legal
8 ** ......... ... 2014 Ivan Mahonin
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 <synfig/general.h>
33
34 #include "layerresetpose.h"
35 #include <synfigapp/canvasinterface.h>
36 #include <synfigapp/localization.h>
37
38 #include <synfig/valuenodes/valuenode_bone.h>
39 #include <synfig/valuenodes/valuenode_composite.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::LayerResetPose);
52 ACTION_SET_NAME(Action::LayerResetPose,"LayerResetPose");
53 ACTION_SET_LOCAL_NAME(Action::LayerResetPose,N_("Reset Pose"));
54 ACTION_SET_TASK(Action::LayerResetPose,"layer_reset_pose");
55 ACTION_SET_CATEGORY(Action::LayerResetPose,Action::CATEGORY_LAYER);
56 ACTION_SET_PRIORITY(Action::LayerResetPose,0);
57 ACTION_SET_VERSION(Action::LayerResetPose,"0.0");
58 ACTION_SET_CVS_ID(Action::LayerResetPose,"$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
LayerResetPose()66 Action::LayerResetPose::LayerResetPose():
67 time(0)
68 {
69 }
70
71 Action::ParamVocab
get_param_vocab()72 Action::LayerResetPose::get_param_vocab()
73 {
74 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
75
76 ret.push_back(ParamDesc("layer",Param::TYPE_LAYER)
77 .set_local_name(_("Layer"))
78 .set_desc(_("Layer to reset"))
79 .set_supports_multiple()
80 );
81 ret.push_back(ParamDesc("time",Param::TYPE_TIME)
82 .set_local_name(_("Time"))
83 .set_optional()
84 );
85
86 return ret;
87 }
88
89 bool
is_candidate(const ParamList & x)90 Action::LayerResetPose::is_candidate(const ParamList &x)
91 {
92 if(!candidate_check(get_param_vocab(),x))
93 return false;
94
95 for(ParamList::const_iterator i = x.find("layer"); i != x.end() && i->first == "layer"; ++i)
96 if (i->second.get_type()==Param::TYPE_LAYER
97 && i->second.get_layer()->get_name() == "skeleton_deformation")
98 return true;
99
100 return false;
101 }
102
103 bool
set_param(const synfig::String & name,const Action::Param & param)104 Action::LayerResetPose::set_param(const synfig::String& name, const Action::Param ¶m)
105 {
106 if (name=="layer"
107 && param.get_type()==Param::TYPE_LAYER
108 && param.get_layer()->get_name() == "skeleton_deformation" )
109 {
110 layers.push_back(param.get_layer());
111 return true;
112 }
113
114 if(name=="time" && param.get_type()==Param::TYPE_TIME)
115 {
116 time=param.get_time();
117 return true;
118 }
119
120 return Action::CanvasSpecific::set_param(name,param);
121 }
122
123 bool
is_ready() const124 Action::LayerResetPose::is_ready()const
125 {
126 if(layers.empty())
127 return false;
128 return Action::CanvasSpecific::is_ready();
129 }
130
131 void
prepare()132 Action::LayerResetPose::prepare()
133 {
134 clear();
135
136 for(std::list<synfig::Layer::Handle>::const_iterator i=layers.begin(); i!=layers.end(); ++i)
137 {
138 Layer::Handle layer(*i);
139 Canvas::Handle subcanvas(layer->get_canvas());
140
141 Layer::DynamicParamList::const_iterator j = layer->dynamic_param_list().find("bones");
142 if (j == layer->dynamic_param_list().end()) continue;
143
144 LinkableValueNode::Handle bones_node = LinkableValueNode::Handle::cast_dynamic(j->second);
145 if (!bones_node) continue;
146
147 for(int k = 0; k < bones_node->link_count(); ++k)
148 {
149 ValueNode_Composite::Handle pair_node =
150 ValueNode_Composite::Handle::cast_dynamic(
151 bones_node->get_link(k) );
152 if (!pair_node) continue;
153
154 ValueNode_Bone::Handle bone_node =
155 ValueNode_Bone::Handle::cast_dynamic(
156 pair_node->get_link("first") );
157 if (!bone_node) continue;
158
159 Action::Handle action(Action::create("ValueDescResetPose"));
160 action->set_param("canvas", get_canvas());
161 action->set_param("canvas_interface", get_canvas_interface());
162 action->set_param(
163 "value_desc",
164 ValueDesc(
165 bone_node,
166 bone_node->get_link_index_from_name("origin"),
167 ValueDesc(pair_node, pair_node->get_link_index_from_name("first")) ));
168 action->set_param("time", time);
169 add_action_front(action);
170 }
171 }
172 }
173