Set the description of some parameters
[synfig.git] / synfig-core / src / synfig / layer_composite.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layer_composite.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2008 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_composite.h"
34 #include "context.h"
35 #include "time.h"
36 #include "color.h"
37 #include "surface.h"
38 #include "renddesc.h"
39 #include "target.h"
40
41 #include "layer_bitmap.h"
42
43 #include "general.h"
44 #include "render.h"
45 #include "paramdesc.h"
46
47 #endif
48
49 /* === U S I N G =========================================================== */
50
51 using namespace std;
52 using namespace etl;
53 using namespace synfig;
54
55 /* === M A C R O S ========================================================= */
56
57 /* === G L O B A L S ======================================================= */
58
59 /* === P R O C E D U R E S ================================================= */
60
61 /* === M E T H O D S ======================================================= */
62 Layer_Composite::Layer_Composite(float  a, Color::BlendMethod   bm):
63                 amount                          (a),
64                 blend_method            (bm),
65                 converted_blend_        (false),
66                 transparent_color_      (false)
67         {
68                 Layer::Vocab voc(get_param_vocab());
69                 Layer::fill_static(voc);
70         }
71
72 bool
73 Layer_Composite::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc_, ProgressCallback *cb)  const
74 {
75         RendDesc renddesc(renddesc_);
76
77         if(!amount)
78                 return context.accelerated_render(surface,quality,renddesc,cb);
79
80         CanvasBase image;
81
82         SuperCallback stageone(cb,0,50000,100000);
83         SuperCallback stagetwo(cb,50000,100000,100000);
84
85         Layer_Bitmap::Handle surfacelayer(new class Layer_Bitmap());
86
87         Context iter;
88
89         for(iter=context;*iter;iter++)
90                 image.push_back(*iter);
91
92         image.push_front(surfacelayer.get());
93
94         // We want to go ahead and schedule any other
95         // layers...
96 //      while(dynamic_cast<Layer_Composite*>(context->get()))
97 //      while(context->get() &&
98 //              &context->get()->AcceleratedRender==
99 //              &Layer_Composite::AcceleratedRender)
100 //              image.push_back(*(context++));
101
102         image.push_back(0);     // Alpha black
103
104         // Render the backdrop on the surface layer's surface.
105         if(!context.accelerated_render(&surfacelayer->surface,quality,renddesc,&stageone))
106                 return false;
107         // Sets up the interpolation of the context (now the surface layer is the first one)
108         // depending on the quality
109         if(quality<=4)surfacelayer->c=3;else
110         if(quality<=5)surfacelayer->c=2;
111         else if(quality<=6)surfacelayer->c=1;
112         else surfacelayer->c=0;
113         surfacelayer->tl=renddesc.get_tl();
114         surfacelayer->br=renddesc.get_br();
115         // Sets the blend method to straight. See below
116         surfacelayer->set_blend_method(Color::BLEND_STRAIGHT);
117         // Push this layer on the image. The blending result is only this layer
118         // adn the surface layer. The rest of the context is ignored by the straight
119         // blend method of surface layer
120         image.push_front(const_cast<synfig::Layer_Composite*>(this));
121
122         // Set up a surface target
123         Target::Handle target(surface_target(surface));
124
125         if(!target)
126         {
127                 if(cb)cb->error(_("Unable to create surface target"));
128                 return false;
129         }
130
131         RendDesc desc(renddesc);
132
133         target->set_rend_desc(&desc);
134
135         // Render the scene
136         return render(Context(image.begin()),target,desc,&stagetwo);
137         //return render_threaded(Context(image.begin()),target,desc,&stagetwo,2);
138 }
139
140 Rect
141 Layer_Composite::get_full_bounding_rect(Context context)const
142 {
143         if(is_disabled() || Color::is_onto(get_blend_method()))
144                 return context.get_full_bounding_rect();
145
146         return context.get_full_bounding_rect()|get_bounding_rect();
147 }
148
149 Layer::Vocab
150 Layer_Composite::get_param_vocab()const
151 {
152         //! First fills the returning vocabulary with the ancestor class
153         Layer::Vocab ret(Layer::get_param_vocab());
154         //! Now inserts the two parameters that this layer knows.
155         ret.push_back(ParamDesc(amount,"amount")
156                 .set_local_name(_("Amount"))
157                 .set_description(_("Alpha channel of the layer"))
158         );
159         ret.push_back(ParamDesc(blend_method,"blend_method")
160                 .set_local_name(_("Blend Method"))
161                 .set_description(_("The blending method used to composite on the layers below"))
162         );
163
164         return ret;
165 }
166
167 bool
168 Layer_Composite::set_param(const String & param, const ValueBase &value)
169 {
170         if(param=="amount" && value.same_type_as(amount))
171         {
172                 amount=value.get(amount);
173                 set_param_static(param,value.get_static());
174         }
175         else
176         if(param=="blend_method" && value.same_type_as(int()))
177         {
178                 blend_method = static_cast<Color::BlendMethod>(value.get(int()));
179                 set_param_static(param,value.get_static());
180
181                 if (blend_method < 0 || blend_method >= Color::BLEND_END)
182                 {
183                         warning("illegal value (%d) for blend_method - using Composite instead", blend_method);
184                         blend_method = Color::BLEND_COMPOSITE;
185                         return false;
186                 }
187
188                 if (blend_method == Color::BLEND_STRAIGHT && !reads_context())
189                 {
190                         Canvas::Handle canvas(get_canvas());
191                         if (canvas)
192                         {
193                                 String version(canvas->get_version());
194
195                                 if (version == "0.1" || version == "0.2")
196                                 {
197                                         if (get_name() == "PasteCanvas")
198                                                 warning("loaded a version %s canvas with a 'Straight' blended PasteCanvas (%s) - check it renders OK",
199                                                                 version.c_str(), get_non_empty_description().c_str());
200                                         else
201                                         {
202                                                 blend_method = Color::BLEND_COMPOSITE;
203                                                 converted_blend_ = true;
204
205                                                 // if this layer has a transparent color, go back and set the color again
206                                                 // now that we know we are converting the blend method as well.  that will
207                                                 // make the color non-transparent, and change the blend method to alpha over
208                                                 if (transparent_color_)
209                                                         set_param("color", get_param("color"));
210                                         }
211                                 }
212                         }
213                 }
214         }
215         else
216                 return Layer::set_param(param,value);
217         return true;
218 }
219
220 ValueBase
221 Layer_Composite::get_param(const String & param)const
222 {
223
224         //! First check if the parameter's string is known.
225         if(param=="amount")
226         {
227                 synfig::ValueBase ret(get_amount());
228                 ret.set_static(get_param_static(param));
229                 return ret;
230         }
231         if(param=="blend_method")
232         {
233                 synfig::ValueBase ret(static_cast<int>(get_blend_method()));
234                 ret.set_static(get_param_static(param));
235                 return ret;
236         }
237         //! If it is unknown then call the ancestor's get param member
238         //! to see if it can handle that parameter's string.
239         return Layer::get_param(param);
240 }