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