1 /* === S I N F G =========================================================== */
2 /*! \file layer_pastecanvas.h
3 ** \brief Template Header
5 ** $Id: layer_pastecanvas.cpp,v 1.3 2005/01/24 03:08:18 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
31 #include "layer_pastecanvas.h"
35 #include "paramdesc.h"
39 #include "valuenode.h"
44 /* === U S I N G =========================================================== */
48 using namespace sinfg;
50 /* === M A C R O S ========================================================= */
55 //#define SINFG_NO_CLIP
58 /* === C L A S S E S ======================================================= */
60 class depth_counter // Makes our recursive depth counter exception-safe
64 depth_counter(int &x):depth(&x) { (*depth)++; }
65 ~depth_counter() { (*depth)--; }
68 /* === G L O B A L S ======================================================= */
70 SINFG_LAYER_INIT(Layer_PasteCanvas);
71 SINFG_LAYER_SET_NAME(Layer_PasteCanvas,"PasteCanvas");
72 SINFG_LAYER_SET_LOCAL_NAME(Layer_PasteCanvas,_("Paste Canvas"));
73 SINFG_LAYER_SET_CATEGORY(Layer_PasteCanvas,_("Default"));
74 SINFG_LAYER_SET_VERSION(Layer_PasteCanvas,"0.1");
75 SINFG_LAYER_SET_CVS_ID(Layer_PasteCanvas,"$Id: layer_pastecanvas.cpp,v 1.3 2005/01/24 03:08:18 darco Exp $");
77 /* === M E T H O D S ======================================================= */
79 Layer_PasteCanvas::Layer_PasteCanvas():
85 do_not_muck_with_time_=false;
86 curr_time=Time::begin();
89 Layer_PasteCanvas::~Layer_PasteCanvas()
92 canvas->parent_set.erase(this);
95 //if(canvas)DEBUGINFO(strprintf("%d",canvas->count()));
99 //if(canvas && (canvas->is_inline() || !get_canvas() || get_canvas()->get_root()!=canvas->get_root()))
104 Layer_PasteCanvas::get_local_name()const
106 if(!canvas) return _("Pasted Canvas");
107 if(canvas->is_inline()) return _("Inline Canvas");
108 if(canvas->get_root()==get_canvas()->get_root()) return '[' + canvas->get_id() + ']';
110 return '[' + canvas->get_file_name() + ']';
114 Layer_PasteCanvas::get_param_vocab()const
116 Layer::Vocab ret(Layer_Composite::get_param_vocab());
118 ret.push_back(ParamDesc("origin")
119 .set_local_name(_("Origin"))
120 .set_description(_("Point where you want the origin to be"))
122 ret.push_back(ParamDesc("canvas")
123 .set_local_name(_("Canvas"))
124 .set_description(_("Canvas to paste"))
126 ret.push_back(ParamDesc("zoom")
127 .set_local_name(_("Zoom"))
128 .set_description(_("Size of canvas"))
131 ret.push_back(ParamDesc("time_offset")
132 .set_local_name(_("Time Offset"))
135 ret.push_back(ParamDesc("children_lock")
136 .set_local_name(_("Children Lock"))
143 Layer_PasteCanvas::set_param(const String & param, const ValueBase &value)
147 if(param=="canvas" && value.same_as(Canvas::Handle()))
149 set_sub_canvas(value.get(Canvas::Handle()));
154 IMPORT(children_lock);
158 return Layer_Composite::set_param(param,value);
162 Layer_PasteCanvas::set_sub_canvas(etl::handle<sinfg::Canvas> x)
164 if(canvas && !do_not_muck_with_time_)
165 remove_child(canvas.get());
167 if(canvas && (canvas->is_inline() || !get_canvas() || get_canvas()->get_root()!=canvas->get_root()))
170 child_changed_connection.disconnect();
175 child_changed_connection=canvas->signal_changed().connect(
178 &Layer_PasteCanvas::changed
183 bounds=(canvas->get_context().get_full_bounding_rect()-canvas->rend_desc().get_focus())*exp(zoom)+origin+canvas->rend_desc().get_focus();
185 if(canvas && !do_not_muck_with_time_)
186 add_child(canvas.get());
188 if(canvas && (canvas->is_inline() || !get_canvas() || get_canvas()->get_root()!=canvas->get_root()))
195 // This is called whenever the parent canvas gets set/changed
197 Layer_PasteCanvas::on_canvas_set()
199 //sinfg::info("before count()=%d",count());
200 if(get_canvas() && canvas && canvas->is_inline() && canvas->parent()!=get_canvas())
202 //sinfg::info("during count()=%d",count());
203 canvas->set_inline(get_canvas());
205 //sinfg::info("after count()=%d",count());
209 Layer_PasteCanvas::get_param(const String& param)const
215 EXPORT(children_lock);
220 return Layer_Composite::get_param(param);
224 Layer_PasteCanvas::set_time(Context context, Time time)const
226 if(depth==MAX_DEPTH)return;depth_counter counter(depth);
229 context.set_time(time);
232 canvas->set_time(time);
234 bounds=(canvas->get_context().get_full_bounding_rect()-canvas->rend_desc().get_focus())*exp(zoom)+origin+canvas->rend_desc().get_focus();
241 Layer_PasteCanvas::hit_check(sinfg::Context context, const sinfg::Point &pos)const
243 if(depth==MAX_DEPTH)return 0;depth_counter counter(depth);
245 Point target_pos=(pos-canvas->rend_desc().get_focus()-origin)/exp(zoom)+canvas->rend_desc().get_focus();
247 if(canvas && get_amount() && canvas->get_context().get_color(target_pos).get_a()>=0.25)
251 return canvas->get_context().hit_check(target_pos);
253 return const_cast<Layer_PasteCanvas*>(this);
256 return context.hit_check(pos);
260 Layer_PasteCanvas::get_color(Context context, const Point &pos)const
262 if(!canvas || !get_amount())
263 return context.get_color(pos);
265 if(depth==MAX_DEPTH)return Color::alpha();depth_counter counter(depth);
267 Point target_pos=(pos-canvas->rend_desc().get_focus()-origin)/exp(zoom)+canvas->rend_desc().get_focus();
269 return Color::blend(canvas->get_context().get_color(target_pos),context.get_color(pos),get_amount(),get_blend_method());
274 Layer_PasteCanvas::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
276 if(cb && !cb->amount_complete(0,10000)) return false;
281 // if we are at the extent of our depth,
282 // then we should just return whatever is under us.
283 return context.accelerated_render(surface,quality,renddesc,cb);
285 depth_counter counter(depth);
287 if(!canvas || !get_amount())
288 return context.accelerated_render(surface,quality,renddesc,cb);
290 if(!do_not_muck_with_time_ && curr_time!=Time::begin() && canvas->get_time()!=curr_time+time_offset)
292 canvas->set_time(curr_time+time_offset);
295 SuperCallback stageone(cb,0,4500,10000);
296 SuperCallback stagetwo(cb,4500,9000,10000);
297 SuperCallback stagethree(cb,9000,9999,10000);
299 RendDesc desc(renddesc);
300 Vector::value_type zoomfactor=1.0/exp(zoom);
302 desc.set_tl((desc.get_tl()-canvas->rend_desc().get_focus()-origin)*zoomfactor+canvas->rend_desc().get_focus());
303 desc.set_br((desc.get_br()-canvas->rend_desc().get_focus()-origin)*zoomfactor+canvas->rend_desc().get_focus());
304 desc.set_flags(RendDesc::PX_ASPECT);
306 if(is_solid_color() || context->empty())
308 surface->set_wh(renddesc.get_w(),renddesc.get_h());
311 else if(!context.accelerated_render(surface,quality,renddesc,&stageone))
313 Color::BlendMethod blend_method(get_blend_method());
315 const Rect full_bounding_rect(canvas->get_context().get_full_bounding_rect());
319 if(Color::is_onto(blend_method))
322 if(blend_method==Color::BLEND_COMPOSITE)
323 blend_method=Color::BLEND_STRAIGHT;
326 if(!etl::intersect(context.get_full_bounding_rect(),full_bounding_rect))
328 if(Color::is_onto(blend_method))
331 if(blend_method==Color::BLEND_COMPOSITE)
332 blend_method=Color::BLEND_STRAIGHT;
335 #ifndef SINFG_NO_CLIP
337 //sinfg::info("PasteCanv Clip");
338 Rect area(desc.get_rect()&full_bounding_rect);
340 Point min(area.get_min());
341 Point max(area.get_max());
343 if(desc.get_tl()[0]>desc.get_br()[0])
345 if(desc.get_tl()[1]>desc.get_br()[1])
349 x(floor_to_int((min[0]-desc.get_tl()[0])/desc.get_pw())),
350 y(floor_to_int((min[1]-desc.get_tl()[1])/desc.get_ph())),
351 w(ceil_to_int((max[0]-desc.get_tl()[0])/desc.get_pw())-x),
352 h(ceil_to_int((max[1]-desc.get_tl()[1])/desc.get_ph())-y);
354 desc.set_subwindow(x,y,w,h);
356 Surface pastesurface;
358 if(area.area()<=0.000001 || desc.get_w()==0 || desc.get_h()==0)
360 if(cb && !cb->amount_complete(10000,10000)) return false;
365 if(!canvas->get_context().accelerated_render(&pastesurface,quality,desc,&stagetwo))
368 Surface::alpha_pen apen(surface->get_pen(x,y));
370 apen.set_alpha(get_amount());
371 apen.set_blend_method(blend_method);
373 pastesurface.blit_to(apen);
377 Surface pastesurface;
379 if(!canvas->get_context().accelerated_render(&pastesurface,quality,desc,&stagetwo))
382 Surface::alpha_pen apen(surface->begin());
384 apen.set_alpha(get_amount());
385 apen.set_blend_method(blend_method);
387 pastesurface.blit_to(apen);
391 if(cb && !cb->amount_complete(10000,10000)) return false;
397 Layer_PasteCanvas::get_bounding_rect()const
402 void Layer_PasteCanvas::get_times_vfunc(Node::time_set &set) const
405 if(canvas) tset = canvas->get_times();
407 Node::time_set::iterator i = tset.begin(),
410 //Make sure we offset the time...
411 //TODO: SOMETHING STILL HAS TO BE DONE WITH THE OTHER DIRECTION
412 // (recursing down the tree needs to take this into account too...)
415 set.insert(*i + time_offset);
418 Layer::get_times_vfunc(set);