Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc3 / src / synfig / layer_solidcolor.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layer_solidcolor.cpp
3 **      \brief Template Header
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_solidcolor.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
43 #endif
44
45 /* === U S I N G =========================================================== */
46
47 using namespace etl;
48 using namespace std;
49 using namespace synfig;
50
51 /* === G L O B A L S ======================================================= */
52
53 SYNFIG_LAYER_INIT(Layer_SolidColor);
54 SYNFIG_LAYER_SET_NAME(Layer_SolidColor,"SolidColor"); // todo: use solid_color
55 SYNFIG_LAYER_SET_LOCAL_NAME(Layer_SolidColor,_("Solid Color"));
56 SYNFIG_LAYER_SET_CATEGORY(Layer_SolidColor,_("Geometry"));
57 SYNFIG_LAYER_SET_VERSION(Layer_SolidColor,"0.1");
58 SYNFIG_LAYER_SET_CVS_ID(Layer_SolidColor,"$Id$");
59
60 /* === P R O C E D U R E S ================================================= */
61
62 /* === M E T H O D S ======================================================= */
63
64 /* === E N T R Y P O I N T ================================================= */
65
66 Layer_SolidColor::Layer_SolidColor():
67         Layer_Composite(1.0,Color::BLEND_STRAIGHT),
68         color(Color::black())
69 {
70 }
71
72 bool
73 Layer_SolidColor::set_param(const String & param, const ValueBase &value)
74 {
75         IMPORT(color);
76
77         return Layer_Composite::set_param(param,value);
78 }
79
80 ValueBase
81 Layer_SolidColor::get_param(const String &param)const
82 {
83         EXPORT(color);
84
85         EXPORT_NAME();
86         EXPORT_VERSION();
87
88         return Layer_Composite::get_param(param);
89 }
90
91 Layer::Vocab
92 Layer_SolidColor::get_param_vocab()const
93 {
94         Layer::Vocab ret(Layer_Composite::get_param_vocab());
95
96         ret.push_back(ParamDesc("color")
97                 .set_local_name(_("Color"))
98         );
99
100         return ret;
101 }
102
103 synfig::Layer::Handle
104 Layer_SolidColor::hit_check(synfig::Context context, const synfig::Point &point)const
105 {
106         if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
107                 return const_cast<Layer_SolidColor*>(this);
108         else
109         if(get_blend_method()==Color::BLEND_COMPOSITE && get_amount()*color.get_a()>=0.5)
110                 return const_cast<Layer_SolidColor*>(this);
111
112         Layer::Handle layer(context.hit_check(point));
113
114         return layer?layer:const_cast<Layer_SolidColor*>(this);
115 }
116
117 Color
118 Layer_SolidColor::get_color(Context context, const Point &pos)const
119 {
120         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
121                 return color;
122         else
123                 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
124 }
125
126 bool
127 Layer_SolidColor::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
128 {
129         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
130         {
131                 // Mark our progress as starting
132                 if(cb && !cb->amount_complete(0,1000))
133                         return false;
134
135                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
136                 surface->fill(color);
137
138                 // Mark our progress as finished
139                 if(cb && !cb->amount_complete(1000,1000))
140                         return false;
141
142                 return true;
143         }
144
145         SuperCallback supercb(cb,0,9500,10000);
146
147         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
148                 return false;
149
150         int x,y;
151
152         Surface::alpha_pen apen(surface->begin());
153
154         apen.set_value(color);
155         apen.set_alpha(get_amount());
156         apen.set_blend_method(get_blend_method());
157
158         for(y=0;y<renddesc.get_h();y++,apen.inc_y(),apen.dec_x(x))
159                 for(x=0;x<renddesc.get_w();x++,apen.inc_x())
160                         apen.put_value();
161
162         // Mark our progress as finished
163         if(cb && !cb->amount_complete(10000,10000))
164                 return false;
165
166         return true;
167 }