1 | /* === S Y N F I G ========================================================= */ |
---|
2 | /*! \file layerzdepthrangeset.cpp |
---|
3 | ** \brief Template File |
---|
4 | ** |
---|
5 | ** $Id$ |
---|
6 | ** |
---|
7 | ** \legal |
---|
8 | ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley |
---|
9 | ** Copyright (c) 2007, 2008 Chris Moore |
---|
10 | ** |
---|
11 | ** This package is free software; you can redistribute it and/or |
---|
12 | ** modify it under the terms of the GNU General Public License as |
---|
13 | ** published by the Free Software Foundation; either version 2 of |
---|
14 | ** the License, or (at your option) any later version. |
---|
15 | ** |
---|
16 | ** This package is distributed in the hope that it will be useful, |
---|
17 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | ** General Public License for more details. |
---|
20 | ** \endlegal |
---|
21 | */ |
---|
22 | /* ========================================================================= */ |
---|
23 | |
---|
24 | /* === H E A D E R S ======================================================= */ |
---|
25 | |
---|
26 | #ifdef USING_PCH |
---|
27 | # include "pch.h" |
---|
28 | #else |
---|
29 | #ifdef HAVE_CONFIG_H |
---|
30 | # include <config.h> |
---|
31 | #endif |
---|
32 | |
---|
33 | #include "layerzdepthrangeset.h" |
---|
34 | #include "layeradd.h" |
---|
35 | #include "layerremove.h" |
---|
36 | #include <synfigapp/canvasinterface.h> |
---|
37 | |
---|
38 | #include <synfigapp/general.h> |
---|
39 | |
---|
40 | #include <synfig/layers/layer_group.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_NO_GET_LOCAL_NAME(Action::LayerZDepthRangeSet); |
---|
53 | ACTION_SET_NAME(Action::LayerZDepthRangeSet,"LayerZDepthRangeSet"); |
---|
54 | ACTION_SET_LOCAL_NAME(Action::LayerZDepthRangeSet,N_("Make Z Range visible")); |
---|
55 | ACTION_SET_TASK(Action::LayerZDepthRangeSet,"zdetph_range_set"); |
---|
56 | ACTION_SET_CATEGORY(Action::LayerZDepthRangeSet,Action::CATEGORY_LAYER); |
---|
57 | ACTION_SET_PRIORITY(Action::LayerZDepthRangeSet,0); |
---|
58 | ACTION_SET_VERSION(Action::LayerZDepthRangeSet,"0.0"); |
---|
59 | ACTION_SET_CVS_ID(Action::LayerZDepthRangeSet,"$Id$"); |
---|
60 | |
---|
61 | #define ZDEPTH_MAX 1e8 |
---|
62 | #define ZDEPTH_EPS 0.0 |
---|
63 | /* === G L O B A L S ======================================================= */ |
---|
64 | |
---|
65 | /* === P R O C E D U R E S ================================================= */ |
---|
66 | |
---|
67 | /* === M E T H O D S ======================================================= */ |
---|
68 | |
---|
69 | Action::LayerZDepthRangeSet::LayerZDepthRangeSet() |
---|
70 | { |
---|
71 | z_depth=ZDEPTH_EPS; |
---|
72 | z_position=ZDEPTH_MAX; |
---|
73 | } |
---|
74 | |
---|
75 | synfig::String |
---|
76 | Action::LayerZDepthRangeSet::get_local_name()const |
---|
77 | { |
---|
78 | return get_layer_descriptions(layers, _("Make Z Range visible"), _("Make Z Range visible")); |
---|
79 | } |
---|
80 | |
---|
81 | Action::ParamVocab |
---|
82 | Action::LayerZDepthRangeSet::get_param_vocab() |
---|
83 | { |
---|
84 | ParamVocab ret(Action::CanvasSpecific::get_param_vocab()); |
---|
85 | |
---|
86 | ret.push_back(ParamDesc("layer",Param::TYPE_LAYER) |
---|
87 | .set_local_name(_("Layer")) |
---|
88 | .set_desc(_("Layer to make Z Range visible")) |
---|
89 | .set_supports_multiple() |
---|
90 | ); |
---|
91 | |
---|
92 | return ret; |
---|
93 | } |
---|
94 | |
---|
95 | bool |
---|
96 | Action::LayerZDepthRangeSet::is_candidate(const ParamList &x) |
---|
97 | { |
---|
98 | if(!candidate_check(get_param_vocab(),x)) |
---|
99 | return false; |
---|
100 | // Check if all layers belong to the same canvas |
---|
101 | Canvas::Handle canvas=0; |
---|
102 | for(ParamList::const_iterator i = x.begin(); i != x.end(); i++) |
---|
103 | { |
---|
104 | if (i->first == "layer" && i->second.get_type() == Param::TYPE_LAYER) |
---|
105 | { |
---|
106 | const Layer::Handle layer = i->second.get_layer(); |
---|
107 | if(layer.empty()) |
---|
108 | return false; |
---|
109 | if(!canvas) |
---|
110 | canvas=layer->get_canvas(); |
---|
111 | if(canvas && canvas->is_root()) |
---|
112 | return false; |
---|
113 | if(canvas && !canvas->is_inline()) |
---|
114 | return false; |
---|
115 | if(layer->get_canvas() && canvas && layer->get_canvas()!=canvas) |
---|
116 | return false; |
---|
117 | } |
---|
118 | } |
---|
119 | return true; |
---|
120 | } |
---|
121 | |
---|
122 | bool |
---|
123 | Action::LayerZDepthRangeSet::set_param(const synfig::String& name, const Action::Param ¶m) |
---|
124 | { |
---|
125 | if(name=="layer" && param.get_type()==Param::TYPE_LAYER) |
---|
126 | { |
---|
127 | layers.push_back(param.get_layer()); |
---|
128 | Layer::Handle layer=param.get_layer(); |
---|
129 | if(layer) |
---|
130 | { |
---|
131 | // Expand position and depth to include the given layer |
---|
132 | float layer_z_depth=layer->get_true_z_depth(); |
---|
133 | if(z_position > layer_z_depth) |
---|
134 | z_position=layer_z_depth; |
---|
135 | if(z_position + z_depth <= layer_z_depth) |
---|
136 | z_depth=layer_z_depth - z_position; |
---|
137 | } |
---|
138 | return true; |
---|
139 | } |
---|
140 | return Action::CanvasSpecific::set_param(name,param); |
---|
141 | } |
---|
142 | |
---|
143 | bool |
---|
144 | Action::LayerZDepthRangeSet::is_ready()const |
---|
145 | { |
---|
146 | if(layers.empty()) |
---|
147 | return false; |
---|
148 | if(z_position == ZDEPTH_MAX) |
---|
149 | return false; |
---|
150 | return Action::CanvasSpecific::is_ready(); |
---|
151 | } |
---|
152 | |
---|
153 | void |
---|
154 | Action::LayerZDepthRangeSet::prepare() |
---|
155 | { |
---|
156 | if(!first_time()) |
---|
157 | return; |
---|
158 | |
---|
159 | if(layers.empty()) |
---|
160 | throw Error(_("No layers selected")); |
---|
161 | |
---|
162 | Layer::Handle layer=layers.front(); |
---|
163 | |
---|
164 | etl::handle<Layer_Group> paste= |
---|
165 | etl::handle<Layer_Group>::cast_dynamic( |
---|
166 | layer->get_parent_paste_canvas_layer() ); |
---|
167 | if(!paste) |
---|
168 | throw Error(_("No Parent Group found!")); |
---|
169 | // Z ENABLE |
---|
170 | { |
---|
171 | ValueBase new_value(true); |
---|
172 | Action::Handle action(Action::create("ValueDescSet")); |
---|
173 | if(!action) |
---|
174 | throw Error(_("Unable to find action ValueDescSet (bug)")); |
---|
175 | action->set_param("canvas",get_canvas()); |
---|
176 | action->set_param("canvas_interface",get_canvas_interface()); |
---|
177 | action->set_param("new_value",new_value); |
---|
178 | action->set_param("value_desc",ValueDesc(Layer::Handle(paste), "z_range")); |
---|
179 | action->set_param("recursive", true); |
---|
180 | if(!action->is_ready()) |
---|
181 | throw Error(Error::TYPE_NOTREADY); |
---|
182 | add_action(action); |
---|
183 | } |
---|
184 | // Z POSITION |
---|
185 | { |
---|
186 | ValueBase new_value(z_position); |
---|
187 | Action::Handle action(Action::create("ValueDescSet")); |
---|
188 | if(!action) |
---|
189 | throw Error(_("Unable to find action ValueDescSet (bug)")); |
---|
190 | action->set_param("canvas",get_canvas()); |
---|
191 | action->set_param("canvas_interface",get_canvas_interface()); |
---|
192 | action->set_param("new_value",new_value); |
---|
193 | action->set_param("value_desc",ValueDesc(Layer::Handle(paste), "z_range_position")); |
---|
194 | action->set_param("recursive", false); |
---|
195 | if(!action->is_ready()) |
---|
196 | throw Error(Error::TYPE_NOTREADY); |
---|
197 | add_action(action); |
---|
198 | } |
---|
199 | // Z DEPTH |
---|
200 | { |
---|
201 | ValueBase new_value(z_depth); |
---|
202 | Action::Handle action(Action::create("ValueDescSet")); |
---|
203 | if(!action) |
---|
204 | throw Error(_("Unable to find action ValueDescSet (bug)")); |
---|
205 | action->set_param("canvas",get_canvas()); |
---|
206 | action->set_param("canvas_interface",get_canvas_interface()); |
---|
207 | action->set_param("new_value",new_value); |
---|
208 | action->set_param("value_desc",ValueDesc(Layer::Handle(paste), "z_range_depth")); |
---|
209 | action->set_param("recursive", false); |
---|
210 | if(!action->is_ready()) |
---|
211 | throw Error(Error::TYPE_NOTREADY); |
---|
212 | add_action(action); |
---|
213 | } |
---|
214 | return; |
---|
215 | } |
---|