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