1 /* === S Y N F I G ========================================================= */
2 /*! \file layer_pastecanvas.cpp
3 ** \brief Template Header
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include "layer_pastecanvas.h"
37 #include "paramdesc.h"
41 #include "valuenode.h"
46 /* === U S I N G =========================================================== */
50 using namespace synfig;
52 /* === M A C R O S ========================================================= */
57 //#define SYNFIG_NO_CLIP
60 /* === C L A S S E S ======================================================= */
62 class depth_counter // Makes our recursive depth counter exception-safe
66 depth_counter(int &x):depth(&x) { (*depth)++; }
67 ~depth_counter() { (*depth)--; }
70 /* === G L O B A L S ======================================================= */
72 SYNFIG_LAYER_INIT(Layer_PasteCanvas);
73 SYNFIG_LAYER_SET_NAME(Layer_PasteCanvas,"PasteCanvas"); // todo: use paste_canvas
74 SYNFIG_LAYER_SET_LOCAL_NAME(Layer_PasteCanvas,_("Paste Canvas"));
75 SYNFIG_LAYER_SET_CATEGORY(Layer_PasteCanvas,_("Other"));
76 SYNFIG_LAYER_SET_VERSION(Layer_PasteCanvas,"0.1");
77 SYNFIG_LAYER_SET_CVS_ID(Layer_PasteCanvas,"$Id$");
79 /* === M E T H O D S ======================================================= */
81 Layer_PasteCanvas::Layer_PasteCanvas():
89 curr_time=Time::begin();
92 Layer_PasteCanvas::~Layer_PasteCanvas()
95 canvas->parent_set.erase(this);
98 //if(canvas)DEBUGINFO(strprintf("%d",canvas->count()));
102 //if(canvas && (canvas->is_inline() || !get_canvas() || get_canvas()->get_root()!=canvas->get_root()))
107 Layer_PasteCanvas::get_local_name()const
109 if(!canvas) return _("Pasted Canvas");
110 if(canvas->is_inline()) return _("Inline Canvas");
111 if(canvas->get_root()==get_canvas()->get_root()) return '[' + canvas->get_id() + ']';
113 return '[' + canvas->get_file_name() + ']';
117 Layer_PasteCanvas::get_param_vocab()const
119 Layer::Vocab ret(Layer_Composite::get_param_vocab());
121 ret.push_back(ParamDesc("origin")
122 .set_local_name(_("Origin"))
123 .set_description(_("Point where you want the origin to be"))
125 ret.push_back(ParamDesc("canvas")
126 .set_local_name(_("Canvas"))
127 .set_description(_("Canvas to paste"))
129 ret.push_back(ParamDesc("zoom")
130 .set_local_name(_("Zoom"))
131 .set_description(_("Size of canvas"))
134 ret.push_back(ParamDesc("time_offset")
135 .set_local_name(_("Time Offset"))
138 ret.push_back(ParamDesc("children_lock")
139 .set_local_name(_("Children Lock"))
146 Layer_PasteCanvas::set_param(const String & param, const ValueBase &value)
150 if(param=="canvas" && value.same_type_as(Canvas::Handle()))
152 set_sub_canvas(value.get(Canvas::Handle()));
157 IMPORT(children_lock);
161 return Layer_Composite::set_param(param,value);
165 Layer_PasteCanvas::set_sub_canvas(etl::handle<synfig::Canvas> x)
167 if(canvas && muck_with_time_)
168 remove_child(canvas.get());
170 if(canvas && (canvas->is_inline() || !get_canvas() || get_canvas()->get_root()!=canvas->get_root()))
173 child_changed_connection.disconnect();
178 child_changed_connection=canvas->signal_changed().connect(
181 &Layer_PasteCanvas::changed
186 bounds=(canvas->get_context().get_full_bounding_rect()-canvas->rend_desc().get_focus())*exp(zoom)+origin+canvas->rend_desc().get_focus();
188 if(canvas && muck_with_time_)
189 add_child(canvas.get());
191 if(canvas && (canvas->is_inline() || !get_canvas() || get_canvas()->get_root()!=canvas->get_root()))
198 // This is called whenever the parent canvas gets set/changed
200 Layer_PasteCanvas::on_canvas_set()
202 //synfig::info("before count()=%d",count());
203 if(get_canvas() && canvas && canvas->is_inline() && canvas->parent()!=get_canvas())
205 //synfig::info("during count()=%d",count());
206 canvas->set_inline(get_canvas());
208 //synfig::info("after count()=%d",count());
212 Layer_PasteCanvas::get_param(const String& param)const
218 EXPORT(children_lock);
223 return Layer_Composite::get_param(param);
227 Layer_PasteCanvas::set_time(Context context, Time time)const
229 if(depth==MAX_DEPTH)return;depth_counter counter(depth);
232 context.set_time(time);
235 canvas->set_time(time+time_offset);
237 bounds=(canvas->get_context().get_full_bounding_rect()-canvas->rend_desc().get_focus())*exp(zoom)+origin+canvas->rend_desc().get_focus();
243 synfig::Layer::Handle
244 Layer_PasteCanvas::hit_check(synfig::Context context, const synfig::Point &pos)const
246 if(depth==MAX_DEPTH)return 0;depth_counter counter(depth);
249 Point target_pos=(pos-canvas->rend_desc().get_focus()-origin)/exp(zoom)+canvas->rend_desc().get_focus();
251 if(canvas && get_amount() && canvas->get_context().get_color(target_pos).get_a()>=0.25)
255 return canvas->get_context().hit_check(target_pos);
257 return const_cast<Layer_PasteCanvas*>(this);
260 return context.hit_check(pos);
264 Layer_PasteCanvas::get_color(Context context, const Point &pos)const
266 if(!canvas || !get_amount())
267 return context.get_color(pos);
269 if(depth==MAX_DEPTH)return Color::alpha();depth_counter counter(depth);
271 Point target_pos=(pos-canvas->rend_desc().get_focus()-origin)/exp(zoom)+canvas->rend_desc().get_focus();
273 return Color::blend(canvas->get_context().get_color(target_pos),context.get_color(pos),get_amount(),get_blend_method());
278 Layer_PasteCanvas::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
280 if(cb && !cb->amount_complete(0,10000)) return false;
285 // if we are at the extent of our depth,
286 // then we should just return whatever is under us.
287 return context.accelerated_render(surface,quality,renddesc,cb);
289 depth_counter counter(depth);
291 if(!canvas || !get_amount())
292 return context.accelerated_render(surface,quality,renddesc,cb);
294 if(muck_with_time_ && curr_time!=Time::begin() && canvas->get_time()!=curr_time+time_offset)
296 canvas->set_time(curr_time+time_offset);
299 SuperCallback stageone(cb,0,4500,10000);
300 SuperCallback stagetwo(cb,4500,9000,10000);
301 SuperCallback stagethree(cb,9000,9999,10000);
303 RendDesc desc(renddesc);
304 Vector::value_type zoomfactor=1.0/exp(zoom);
306 desc.set_tl((desc.get_tl()-canvas->rend_desc().get_focus()-origin)*zoomfactor+canvas->rend_desc().get_focus());
307 desc.set_br((desc.get_br()-canvas->rend_desc().get_focus()-origin)*zoomfactor+canvas->rend_desc().get_focus());
308 desc.set_flags(RendDesc::PX_ASPECT);
310 if(is_solid_color() || context->empty())
312 surface->set_wh(renddesc.get_w(),renddesc.get_h());
315 else if(!context.accelerated_render(surface,quality,renddesc,&stageone))
317 Color::BlendMethod blend_method(get_blend_method());
319 const Rect full_bounding_rect(canvas->get_context().get_full_bounding_rect());
323 if(Color::is_onto(blend_method))
326 if(blend_method==Color::BLEND_COMPOSITE)
327 blend_method=Color::BLEND_STRAIGHT;
330 if(!etl::intersect(context.get_full_bounding_rect(),full_bounding_rect+origin))
332 if(Color::is_onto(blend_method))
335 if(blend_method==Color::BLEND_COMPOSITE)
336 blend_method=Color::BLEND_STRAIGHT;
339 #ifndef SYNFIG_NO_CLIP
341 //synfig::info("PasteCanv Clip");
342 Rect area(desc.get_rect()&full_bounding_rect);
344 Point min(area.get_min());
345 Point max(area.get_max());
347 if(desc.get_tl()[0]>desc.get_br()[0])
349 if(desc.get_tl()[1]>desc.get_br()[1])
353 x(floor_to_int((min[0]-desc.get_tl()[0])/desc.get_pw())),
354 y(floor_to_int((min[1]-desc.get_tl()[1])/desc.get_ph())),
355 w(ceil_to_int((max[0]-desc.get_tl()[0])/desc.get_pw())-x),
356 h(ceil_to_int((max[1]-desc.get_tl()[1])/desc.get_ph())-y);
358 desc.set_subwindow(x,y,w,h);
360 Surface pastesurface;
362 if(area.area()<=0.000001 || desc.get_w()==0 || desc.get_h()==0)
364 if(cb && !cb->amount_complete(10000,10000)) return false;
369 if(!canvas->get_context().accelerated_render(&pastesurface,quality,desc,&stagetwo))
372 Surface::alpha_pen apen(surface->get_pen(x,y));
374 apen.set_alpha(get_amount());
375 apen.set_blend_method(blend_method);
377 pastesurface.blit_to(apen);
381 Surface pastesurface;
383 if(!canvas->get_context().accelerated_render(&pastesurface,quality,desc,&stagetwo))
386 Surface::alpha_pen apen(surface->begin());
388 apen.set_alpha(get_amount());
389 apen.set_blend_method(blend_method);
391 pastesurface.blit_to(apen);
395 if(cb && !cb->amount_complete(10000,10000)) return false;
401 Layer_PasteCanvas::get_bounding_rect()const
406 void Layer_PasteCanvas::get_times_vfunc(Node::time_set &set) const
409 if(canvas) tset = canvas->get_times();
411 Node::time_set::iterator i = tset.begin(),
414 //Make sure we offset the time...
415 //TODO: SOMETHING STILL HAS TO BE DONE WITH THE OTHER DIRECTION
416 // (recursing down the tree needs to take this into account too...)
419 set.insert(*i + time_offset);
422 Layer::get_times_vfunc(set);