1 /* === S Y N F I G ========================================================= */
2 /*! \file layer_pastecanvas.cpp
3 ** \brief Implementation of the "Paste Canvas" layer
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 ========================================================= */
56 // if this isn't defined, the 'dead heads' in examples/pirates.sifz don't render properly
57 #define SYNFIG_CLIP_PASTECANVAS
60 //#undef SYNFIG_CLIP_PASTECANVAS
63 /* === C L A S S E S ======================================================= */
65 class depth_counter // Makes our recursive depth counter exception-safe
69 depth_counter(int &x):depth(&x) { (*depth)++; }
70 ~depth_counter() { (*depth)--; }
73 /* === G L O B A L S ======================================================= */
75 SYNFIG_LAYER_INIT(Layer_PasteCanvas);
76 SYNFIG_LAYER_SET_NAME(Layer_PasteCanvas,"PasteCanvas"); // todo: use paste_canvas
77 SYNFIG_LAYER_SET_LOCAL_NAME(Layer_PasteCanvas,N_("Paste Canvas"));
78 SYNFIG_LAYER_SET_CATEGORY(Layer_PasteCanvas,N_("Other"));
79 SYNFIG_LAYER_SET_VERSION(Layer_PasteCanvas,"0.1");
80 SYNFIG_LAYER_SET_CVS_ID(Layer_PasteCanvas,"$Id$");
82 /* === M E T H O D S ======================================================= */
84 Layer_PasteCanvas::Layer_PasteCanvas():
92 curr_time=Time::begin();
95 Layer_PasteCanvas::~Layer_PasteCanvas()
98 canvas->parent_set.erase(this);
101 //if(canvas)DEBUGINFO(strprintf("%d",canvas->count()));
105 //if(canvas && (canvas->is_inline() || !get_canvas() || get_canvas()->get_root()!=canvas->get_root()))
110 Layer_PasteCanvas::get_local_name()const
112 if(!canvas) return _("Pasted Canvas");
113 if(canvas->is_inline()) return _("Inline Canvas");
114 if(canvas->get_root()==get_canvas()->get_root()) return '[' + canvas->get_id() + ']';
116 return '[' + canvas->get_file_name() + ']';
120 Layer_PasteCanvas::get_param_vocab()const
122 Layer::Vocab ret(Layer_Composite::get_param_vocab());
124 ret.push_back(ParamDesc("origin")
125 .set_local_name(_("Origin"))
126 .set_description(_("Point where you want the origin to be"))
128 ret.push_back(ParamDesc("canvas")
129 .set_local_name(_("Canvas"))
130 .set_description(_("Canvas to paste"))
132 ret.push_back(ParamDesc("zoom")
133 .set_local_name(_("Zoom"))
134 .set_description(_("Size of canvas"))
137 ret.push_back(ParamDesc("time_offset")
138 .set_local_name(_("Time Offset"))
141 ret.push_back(ParamDesc("children_lock")
142 .set_local_name(_("Children Lock"))
149 Layer_PasteCanvas::set_param(const String & param, const ValueBase &value)
154 if(param=="canvas" && value.same_type_as(Canvas::Handle()))
156 set_sub_canvas(value.get(Canvas::Handle()));
160 //! \todo this introduces bug 1844764 if enabled; it was introduced in r954, but I can't see if it's useful
162 if (param=="time_offset" && value.same_type_as(time_offset))
164 if (time_offset != value.get(Time()))
166 value.put(&time_offset);
167 // notify that the time_offset has changed so we can update the
168 // waypoint positions in parent layers
177 IMPORT(children_lock);
180 return Layer_Composite::set_param(param,value);
184 Layer_PasteCanvas::set_sub_canvas(etl::handle<synfig::Canvas> x)
186 if(canvas && muck_with_time_)
187 remove_child(canvas.get());
189 if(canvas && (canvas->is_inline() || !get_canvas() || get_canvas()->get_root()!=canvas->get_root()))
192 child_changed_connection.disconnect();
197 child_changed_connection=canvas->signal_changed().connect(
200 &Layer_PasteCanvas::changed
205 bounds = ((canvas->get_context().get_full_bounding_rect() - canvas->rend_desc().get_focus()) * exp(zoom) +
206 origin + canvas->rend_desc().get_focus());
208 if(canvas && muck_with_time_)
209 add_child(canvas.get());
211 if(canvas && (canvas->is_inline() || !get_canvas() || get_canvas()->get_root()!=canvas->get_root()))
218 // This is called whenever the parent canvas gets set/changed
220 Layer_PasteCanvas::on_canvas_set()
222 //synfig::info("before count()=%d",count());
223 if(get_canvas() && canvas && canvas->is_inline() && canvas->parent()!=get_canvas())
225 //synfig::info("during count()=%d",count());
226 canvas->set_inline(get_canvas());
228 //synfig::info("after count()=%d",count());
232 Layer_PasteCanvas::get_param(const String& param)const
238 EXPORT(children_lock);
243 return Layer_Composite::get_param(param);
247 Layer_PasteCanvas::set_time(Context context, Time time)const
249 if(depth==MAX_DEPTH)return;depth_counter counter(depth);
252 context.set_time(time);
255 canvas->set_time(time+time_offset);
257 bounds=(canvas->get_context().get_full_bounding_rect()-canvas->rend_desc().get_focus())*exp(zoom)+origin+canvas->rend_desc().get_focus();
263 synfig::Layer::Handle
264 Layer_PasteCanvas::hit_check(synfig::Context context, const synfig::Point &pos)const
266 if(depth==MAX_DEPTH)return 0;depth_counter counter(depth);
269 Point target_pos=(pos-canvas->rend_desc().get_focus()-origin)/exp(zoom)+canvas->rend_desc().get_focus();
271 if(canvas && get_amount() && canvas->get_context().get_color(target_pos).get_a()>=0.25)
275 return canvas->get_context().hit_check(target_pos);
277 return const_cast<Layer_PasteCanvas*>(this);
280 return context.hit_check(pos);
284 Layer_PasteCanvas::get_color(Context context, const Point &pos)const
286 if(!canvas || !get_amount())
287 return context.get_color(pos);
289 if(depth==MAX_DEPTH)return Color::alpha();depth_counter counter(depth);
291 Point target_pos=(pos-canvas->rend_desc().get_focus()-origin)/exp(zoom)+canvas->rend_desc().get_focus();
293 return Color::blend(canvas->get_context().get_color(target_pos),context.get_color(pos),get_amount(),get_blend_method());
298 Layer_PasteCanvas::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
300 if(cb && !cb->amount_complete(0,10000)) return false;
303 // if we are at the extent of our depth,
304 // then we should just return whatever is under us.
305 return context.accelerated_render(surface,quality,renddesc,cb);
307 depth_counter counter(depth);
309 if(!canvas || !get_amount())
310 return context.accelerated_render(surface,quality,renddesc,cb);
312 if(muck_with_time_ && curr_time!=Time::begin() && canvas->get_time()!=curr_time+time_offset)
313 canvas->set_time(curr_time+time_offset);
315 SuperCallback stageone(cb,0,4500,10000);
316 SuperCallback stagetwo(cb,4500,9000,10000);
317 SuperCallback stagethree(cb,9000,9999,10000);
319 RendDesc desc(renddesc);
320 Vector::value_type zoomfactor=1.0/exp(zoom);
322 desc.set_tl((desc.get_tl()-canvas->rend_desc().get_focus()-origin)*zoomfactor+canvas->rend_desc().get_focus());
323 desc.set_br((desc.get_br()-canvas->rend_desc().get_focus()-origin)*zoomfactor+canvas->rend_desc().get_focus());
324 desc.set_flags(RendDesc::PX_ASPECT);
326 if (is_solid_color() || context->empty())
328 surface->set_wh(renddesc.get_w(),renddesc.get_h());
331 else if (!context.accelerated_render(surface,quality,renddesc,&stageone))
334 Color::BlendMethod blend_method(get_blend_method());
335 const Rect full_bounding_rect(canvas->get_context().get_full_bounding_rect());
336 bool blend_using_straight = false; // use 'straight' just for the central blit
340 if (Color::is_onto(blend_method)) return true;
341 if (blend_method==Color::BLEND_COMPOSITE) blend_method=Color::BLEND_STRAIGHT;
344 if (!etl::intersect(context.get_full_bounding_rect(),full_bounding_rect+origin))
346 if (Color::is_onto(blend_method)) return true;
348 /* 'straight' is faster than 'composite' and has the same
349 * effect if the affected area of the lower layer is
350 * transparent; however, if we're not clipping the blit to
351 * just the bounding rectangle, the affected area is the whole
352 * tile, so we can't use this optimisation. if we are
353 * clipping, then we can use 'straight' to blit the clipped
354 * rectangle, but we shouldn't set blend_method to 'straight',
355 * or the surrounding areas will be blanked, which we don't
358 #ifdef SYNFIG_CLIP_PASTECANVAS
359 if (blend_method==Color::BLEND_COMPOSITE) blend_using_straight = true;
360 #endif // SYNFIG_CLIP_PASTECANVAS
363 #ifdef SYNFIG_CLIP_PASTECANVAS
364 Rect area(desc.get_rect() & full_bounding_rect);
366 Point min(area.get_min());
367 Point max(area.get_max());
369 if (desc.get_tl()[0] > desc.get_br()[0]) swap(min[0], max[0]);
370 if (desc.get_tl()[1] > desc.get_br()[1]) swap(min[1], max[1]);
372 const int x(floor_to_int((min[0] - desc.get_tl()[0]) / desc.get_pw()));
373 const int y(floor_to_int((min[1] - desc.get_tl()[1]) / desc.get_ph()));
374 const int w( ceil_to_int((max[0] - desc.get_tl()[0]) / desc.get_pw()) - x);
375 const int h( ceil_to_int((max[1] - desc.get_tl()[1]) / desc.get_ph()) - y);
377 const int tw = desc.get_w();
378 const int th = desc.get_h();
380 desc.set_subwindow(x,y,w,h);
382 // \todo this used to also have "area.area()<=0.000001 || " - is it useful?
383 // it was causing bug #1809480 (Zoom in beyond 8.75 in nested canvases fails)
384 if(desc.get_w()==0 || desc.get_h()==0)
386 if(cb && !cb->amount_complete(10000,10000)) return false;
390 // SYNFIG_CLIP_PASTECANVAS is defined, so we are only touching the
391 // pixels within the affected rectangle. If the blend method is
392 // 'straight', then we need to blend transparent pixels with the
393 // clipped areas of this tile, because with the 'straight' blend
394 // method, even transparent pixels have an effect on the layers below
395 if (blend_method == Color::BLEND_STRAIGHT || blend_method == Color::BLEND_STRAIGHT_ONTO)
397 Surface clearsurface;
399 Surface::alpha_pen apen(surface->begin());
400 apen.set_alpha(get_amount());
402 // the area we're about to blit is transparent, so it doesn't
403 // matter whether we use 'straight' or 'straight onto' here
404 apen.set_blend_method(Color::BLEND_STRAIGHT);
406 /* This represents the area we're pasting into the tile,
407 * within the tile as a whole. Areas (A), (B), (C) and (D)
408 * need blending with the underlying context if they're not
412 * 0 +------------------------+
416 * y | - - +----------+ - - - |
418 * | (C) | w by h | (D) |
420 * y+h | - - +----------+ - - - |
424 * tw +------------------------+
427 if (y > 0) // draw the full-width strip above the rectangle (A)
428 { apen.move_to(0,0); clearsurface.set_wh(tw,y); clearsurface.clear(); clearsurface.blit_to(apen); }
429 if (y+h < th) // draw the full-width strip below the rectangle (B)
430 { apen.move_to(0,y+h); clearsurface.set_wh(tw,th-(y+h)); clearsurface.clear(); clearsurface.blit_to(apen); }
431 if (x > 0) // draw the box directly left of the rectangle (C)
432 { apen.move_to(0,y); clearsurface.set_wh(x,h); clearsurface.clear(); clearsurface.blit_to(apen); }
433 if (x+w < tw) // draw the box directly right of the rectangle (D)
434 { apen.move_to(x+w,y); clearsurface.set_wh(tw-(x+w),h); clearsurface.clear(); clearsurface.blit_to(apen); }
436 #endif // SYNFIG_CLIP_PASTECANVAS
438 // render the canvas to be pasted onto pastesurface
439 Surface pastesurface;
440 if(!canvas->get_context().accelerated_render(&pastesurface,quality,desc,&stagetwo))
443 #ifdef SYNFIG_CLIP_PASTECANVAS
444 Surface::alpha_pen apen(surface->get_pen(x,y));
445 #else // SYNFIG_CLIP_PASTECANVAS
446 Surface::alpha_pen apen(surface->begin());
447 #endif // SYNFIG_CLIP_PASTECANVAS
449 apen.set_alpha(get_amount());
450 apen.set_blend_method(blend_using_straight ? Color::BLEND_STRAIGHT : blend_method);
451 pastesurface.blit_to(apen);
453 if(cb && !cb->amount_complete(10000,10000)) return false;
459 Layer_PasteCanvas::get_bounding_rect()const
464 void Layer_PasteCanvas::get_times_vfunc(Node::time_set &set) const
467 if(canvas) tset = canvas->get_times();
469 Node::time_set::iterator i = tset.begin(), end = tset.end();
471 //Make sure we offset the time...
472 //! \todo: SOMETHING STILL HAS TO BE DONE WITH THE OTHER DIRECTION
473 // (recursing down the tree needs to take this into account too...)
476 #ifdef ADJUST_WAYPOINTS_FOR_TIME_OFFSET // see node.h
481 Layer::get_times_vfunc(set);