cf2ae81501d883e3e5c673f52641283f3c1036da
[synfig.git] / synfig-core / trunk / src / synfig / layer_pastecanvas.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layer_pastecanvas.cpp
3 **      \brief Implementation of the "Paste Canvas" layer
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 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 "layer_pastecanvas.h"
34 #include "string.h"
35 #include "time.h"
36 #include "context.h"
37 #include "paramdesc.h"
38 #include "renddesc.h"
39 #include "surface.h"
40 #include "value.h"
41 #include "valuenode.h"
42 #include "canvas.h"
43
44 #endif
45
46 /* === U S I N G =========================================================== */
47
48 using namespace etl;
49 using namespace std;
50 using namespace synfig;
51
52 /* === M A C R O S ========================================================= */
53
54 #define MAX_DEPTH 10
55
56 //#ifdef __APPLE__
57 //#define SYNFIG_NO_CLIP
58 //#endif
59
60 /* === C L A S S E S ======================================================= */
61
62 class depth_counter     // Makes our recursive depth counter exception-safe
63 {
64         int *depth;
65 public:
66         depth_counter(int &x):depth(&x) { (*depth)++; }
67         ~depth_counter() { (*depth)--; }
68 };
69
70 /* === G L O B A L S ======================================================= */
71
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,N_("Paste Canvas"));
75 SYNFIG_LAYER_SET_CATEGORY(Layer_PasteCanvas,N_("Other"));
76 SYNFIG_LAYER_SET_VERSION(Layer_PasteCanvas,"0.1");
77 SYNFIG_LAYER_SET_CVS_ID(Layer_PasteCanvas,"$Id$");
78
79 /* === M E T H O D S ======================================================= */
80
81 Layer_PasteCanvas::Layer_PasteCanvas():
82         origin(0,0),
83         depth(0),
84         zoom(0),
85         time_offset(0)
86 {
87         children_lock=false;
88         muck_with_time_=true;
89         curr_time=Time::begin();
90 }
91
92 Layer_PasteCanvas::~Layer_PasteCanvas()
93 {
94 /*      if(canvas)
95                 canvas->parent_set.erase(this);
96 */
97
98         //if(canvas)DEBUGINFO(strprintf("%d",canvas->count()));
99
100         set_sub_canvas(0);
101
102         //if(canvas && (canvas->is_inline() || !get_canvas() || get_canvas()->get_root()!=canvas->get_root()))
103         //      canvas->unref();
104 }
105
106 String
107 Layer_PasteCanvas::get_local_name()const
108 {
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() + ']';
112
113         return '[' + canvas->get_file_name() + ']';
114 }
115
116 Layer::Vocab
117 Layer_PasteCanvas::get_param_vocab()const
118 {
119         Layer::Vocab ret(Layer_Composite::get_param_vocab());
120
121         ret.push_back(ParamDesc("origin")
122                 .set_local_name(_("Origin"))
123                 .set_description(_("Point where you want the origin to be"))
124         );
125         ret.push_back(ParamDesc("canvas")
126                 .set_local_name(_("Canvas"))
127                 .set_description(_("Canvas to paste"))
128         );
129         ret.push_back(ParamDesc("zoom")
130                 .set_local_name(_("Zoom"))
131                 .set_description(_("Size of canvas"))
132         );
133
134         ret.push_back(ParamDesc("time_offset")
135                 .set_local_name(_("Time Offset"))
136         );
137
138         ret.push_back(ParamDesc("children_lock")
139                 .set_local_name(_("Children Lock"))
140         );
141
142         return ret;
143 }
144
145 bool
146 Layer_PasteCanvas::set_param(const String & param, const ValueBase &value)
147 {
148         IMPORT(origin);
149
150         // IMPORT(canvas);
151         if(param=="canvas" && value.same_type_as(Canvas::Handle()))
152         {
153                 set_sub_canvas(value.get(Canvas::Handle()));
154                 return true;
155         }
156
157         //! \todo this introduces bug 1844764 if enabled; it was introduced in r954, but I can't see if it's useful
158 #if 0
159         if (param=="time_offset" && value.same_type_as(time_offset))
160         {
161                 if (time_offset != value.get(Time()))
162                 {
163                         value.put(&time_offset);
164                         // notify that the time_offset has changed so we can update the
165                         // waypoint positions in parent layers
166                         changed();
167                 }
168                 return true;
169         }
170 #else
171         IMPORT(time_offset);
172 #endif
173
174         IMPORT(children_lock);
175         IMPORT(zoom);
176
177         return Layer_Composite::set_param(param,value);
178 }
179
180 void
181 Layer_PasteCanvas::set_sub_canvas(etl::handle<synfig::Canvas> x)
182 {
183         if(canvas && muck_with_time_)
184                 remove_child(canvas.get());
185
186         if(canvas && (canvas->is_inline() || !get_canvas() || get_canvas()->get_root()!=canvas->get_root()))
187                 canvas->unref();
188
189         child_changed_connection.disconnect();
190
191         canvas=x;
192
193         /*if(canvas)
194                 child_changed_connection=canvas->signal_changed().connect(
195                         sigc::mem_fun(
196                                 *this,
197                                 &Layer_PasteCanvas::changed
198                         )
199                 );
200         */
201         if(canvas)
202                 bounds=(canvas->get_context().get_full_bounding_rect()-canvas->rend_desc().get_focus())*exp(zoom)+origin+canvas->rend_desc().get_focus();
203
204         if(canvas && muck_with_time_)
205                 add_child(canvas.get());
206
207         if(canvas && (canvas->is_inline() || !get_canvas() || get_canvas()->get_root()!=canvas->get_root()))
208                 canvas->ref();
209
210         if(canvas)
211                 on_canvas_set();
212 }
213
214 // This is called whenever the parent canvas gets set/changed
215 void
216 Layer_PasteCanvas::on_canvas_set()
217 {
218         //synfig::info("before count()=%d",count());
219         if(get_canvas() && canvas && canvas->is_inline() && canvas->parent()!=get_canvas())
220         {
221                 //synfig::info("during count()=%d",count());
222                 canvas->set_inline(get_canvas());
223         }
224         //synfig::info("after count()=%d",count());
225 }
226
227 ValueBase
228 Layer_PasteCanvas::get_param(const String& param)const
229 {
230         EXPORT(origin);
231         EXPORT(canvas);
232         EXPORT(zoom);
233         EXPORT(time_offset);
234         EXPORT(children_lock);
235
236         EXPORT_NAME();
237         EXPORT_VERSION();
238
239         return Layer_Composite::get_param(param);
240 }
241
242 void
243 Layer_PasteCanvas::set_time(Context context, Time time)const
244 {
245         if(depth==MAX_DEPTH)return;depth_counter counter(depth);
246         curr_time=time;
247
248         context.set_time(time);
249         if(canvas)
250         {
251                 canvas->set_time(time+time_offset);
252
253                 bounds=(canvas->get_context().get_full_bounding_rect()-canvas->rend_desc().get_focus())*exp(zoom)+origin+canvas->rend_desc().get_focus();
254         }
255         else
256                 bounds=Rect::zero();
257 }
258
259 synfig::Layer::Handle
260 Layer_PasteCanvas::hit_check(synfig::Context context, const synfig::Point &pos)const
261 {
262         if(depth==MAX_DEPTH)return 0;depth_counter counter(depth);
263
264         if (canvas) {
265                 Point target_pos=(pos-canvas->rend_desc().get_focus()-origin)/exp(zoom)+canvas->rend_desc().get_focus();
266
267                 if(canvas && get_amount() && canvas->get_context().get_color(target_pos).get_a()>=0.25)
268                 {
269                         if(!children_lock)
270                         {
271                                 return canvas->get_context().hit_check(target_pos);
272                         }
273                         return const_cast<Layer_PasteCanvas*>(this);
274                 }
275         }
276         return context.hit_check(pos);
277 }
278
279 Color
280 Layer_PasteCanvas::get_color(Context context, const Point &pos)const
281 {
282         if(!canvas || !get_amount())
283                 return context.get_color(pos);
284
285         if(depth==MAX_DEPTH)return Color::alpha();depth_counter counter(depth);
286
287         Point target_pos=(pos-canvas->rend_desc().get_focus()-origin)/exp(zoom)+canvas->rend_desc().get_focus();
288
289         return Color::blend(canvas->get_context().get_color(target_pos),context.get_color(pos),get_amount(),get_blend_method());
290 }
291
292
293 bool
294 Layer_PasteCanvas::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
295 {
296         if(cb && !cb->amount_complete(0,10000)) return false;
297
298         if(depth==MAX_DEPTH)
299         {
300                 DEBUGPOINT();
301                 // if we are at the extent of our depth,
302                 // then we should just return whatever is under us.
303                 return context.accelerated_render(surface,quality,renddesc,cb);
304         }
305         depth_counter counter(depth);
306
307         if(!canvas || !get_amount())
308                 return context.accelerated_render(surface,quality,renddesc,cb);
309
310         if(muck_with_time_ && curr_time!=Time::begin() && canvas->get_time()!=curr_time+time_offset)
311         {
312                 canvas->set_time(curr_time+time_offset);
313         }
314
315         SuperCallback stageone(cb,0,4500,10000);
316         SuperCallback stagetwo(cb,4500,9000,10000);
317         SuperCallback stagethree(cb,9000,9999,10000);
318
319         RendDesc desc(renddesc);
320         Vector::value_type zoomfactor=1.0/exp(zoom);
321         desc.clear_flags();
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);
325
326         if(is_solid_color() || context->empty())
327         {
328                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
329                 surface->clear();
330         }
331         else if(!context.accelerated_render(surface,quality,renddesc,&stageone))
332                 return false;
333         Color::BlendMethod blend_method(get_blend_method());
334
335         const Rect full_bounding_rect(canvas->get_context().get_full_bounding_rect());
336
337         if(context->empty())
338         {
339                 if(Color::is_onto(blend_method))
340                         return true;
341
342                 if(blend_method==Color::BLEND_COMPOSITE)
343                         blend_method=Color::BLEND_STRAIGHT;
344         }
345         else
346         if(!etl::intersect(context.get_full_bounding_rect(),full_bounding_rect+origin))
347         {
348                 if(Color::is_onto(blend_method))
349                         return true;
350
351                 if(blend_method==Color::BLEND_COMPOSITE)
352                         blend_method=Color::BLEND_STRAIGHT;
353         }
354
355 #ifndef SYNFIG_NO_CLIP
356         {
357                 //synfig::info("PasteCanv Clip");
358                 Rect area(desc.get_rect()&full_bounding_rect);
359
360                 Point min(area.get_min());
361                 Point max(area.get_max());
362
363                 if(desc.get_tl()[0]>desc.get_br()[0])
364                         swap(min[0],max[0]);
365                 if(desc.get_tl()[1]>desc.get_br()[1])
366                         swap(min[1],max[1]);
367
368                 const int
369                         x(floor_to_int((min[0]-desc.get_tl()[0])/desc.get_pw())),
370                         y(floor_to_int((min[1]-desc.get_tl()[1])/desc.get_ph())),
371                         w(ceil_to_int((max[0]-desc.get_tl()[0])/desc.get_pw())-x),
372                         h(ceil_to_int((max[1]-desc.get_tl()[1])/desc.get_ph())-y);
373
374                 desc.set_subwindow(x,y,w,h);
375
376                 Surface pastesurface;
377
378                 // \todo this used to also have "area.area()<=0.000001 || " - is it useful?
379                 //               it was causing bug #1809480 (Zoom in beyond 8.75 in nested canvases fails)
380                 if(desc.get_w()==0 || desc.get_h()==0)
381                 {
382                         if(cb && !cb->amount_complete(10000,10000)) return false;
383
384                         return true;
385                 }
386
387                 if(!canvas->get_context().accelerated_render(&pastesurface,quality,desc,&stagetwo))
388                         return false;
389
390                 Surface::alpha_pen apen(surface->get_pen(x,y));
391
392                 apen.set_alpha(get_amount());
393                 apen.set_blend_method(blend_method);
394
395                 pastesurface.blit_to(apen);
396         }
397 #else
398         {
399                 Surface pastesurface;
400
401                 if(!canvas->get_context().accelerated_render(&pastesurface,quality,desc,&stagetwo))
402                         return false;
403
404                 Surface::alpha_pen apen(surface->begin());
405
406                 apen.set_alpha(get_amount());
407                 apen.set_blend_method(blend_method);
408
409                 pastesurface.blit_to(apen);
410         }
411 #endif
412
413         if(cb && !cb->amount_complete(10000,10000)) return false;
414
415         return true;
416 }
417
418 Rect
419 Layer_PasteCanvas::get_bounding_rect()const
420 {
421         return bounds;
422 }
423
424 void Layer_PasteCanvas::get_times_vfunc(Node::time_set &set) const
425 {
426         Node::time_set tset;
427         if(canvas) tset = canvas->get_times();
428
429         Node::time_set::iterator i = tset.begin(), end = tset.end();
430
431         //Make sure we offset the time...
432         //! \todo: SOMETHING STILL HAS TO BE DONE WITH THE OTHER DIRECTION
433         //                 (recursing down the tree needs to take this into account too...)
434         for(; i != end; ++i)
435                 set.insert(*i
436 #ifdef ADJUST_WAYPOINTS_FOR_TIME_OFFSET // see node.h
437                                    - time_offset
438 #endif
439                         );
440
441         Layer::get_times_vfunc(set);
442 }