aec6a11de088395051f9ac9d949c43f28f931853
[synfig.git] / synfig-core / trunk / src / synfig / layer_solidcolor.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layer_solidcolor.cpp
3 **      \brief Implementation of the "Solid Color" 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_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,N_("Solid Color"));
56 SYNFIG_LAYER_SET_CATEGORY(Layer_SolidColor,N_("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_PLUS(color, { if (color.get_a() == 0) { if (converted_blend_) {
76                                         set_blend_method(Color::BLEND_ALPHA_OVER);
77                                         color.set_a(1); } else transparent_color_ = true; } });
78
79         return Layer_Composite::set_param(param,value);
80 }
81
82 ValueBase
83 Layer_SolidColor::get_param(const String &param)const
84 {
85         EXPORT(color);
86
87         EXPORT_NAME();
88         EXPORT_VERSION();
89
90         return Layer_Composite::get_param(param);
91 }
92
93 Layer::Vocab
94 Layer_SolidColor::get_param_vocab()const
95 {
96         Layer::Vocab ret(Layer_Composite::get_param_vocab());
97
98         ret.push_back(ParamDesc("color")
99                 .set_local_name(_("Color"))
100         );
101
102         return ret;
103 }
104
105 synfig::Layer::Handle
106 Layer_SolidColor::hit_check(synfig::Context context, const synfig::Point &point)const
107 {
108         if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
109                 return const_cast<Layer_SolidColor*>(this);
110         else
111         if(get_blend_method()==Color::BLEND_COMPOSITE && get_amount()*color.get_a()>=0.5)
112                 return const_cast<Layer_SolidColor*>(this);
113
114         Layer::Handle layer(context.hit_check(point));
115
116         return layer?layer:const_cast<Layer_SolidColor*>(this);
117 }
118
119 Color
120 Layer_SolidColor::get_color(Context context, const Point &pos)const
121 {
122         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
123                 return color;
124         else
125                 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
126 }
127
128 bool
129 Layer_SolidColor::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
130 {
131         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
132         {
133                 // Mark our progress as starting
134                 if(cb && !cb->amount_complete(0,1000))
135                         return false;
136
137                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
138                 surface->fill(color);
139
140                 // Mark our progress as finished
141                 if(cb && !cb->amount_complete(1000,1000))
142                         return false;
143
144                 return true;
145         }
146
147         SuperCallback supercb(cb,0,9500,10000);
148
149         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
150                 return false;
151
152         int x,y;
153
154         Surface::alpha_pen apen(surface->begin());
155
156         apen.set_value(color);
157         apen.set_alpha(get_amount());
158         apen.set_blend_method(get_blend_method());
159
160         for(y=0;y<renddesc.get_h();y++,apen.inc_y(),apen.dec_x(x))
161                 for(x=0;x<renddesc.get_w();x++,apen.inc_x())
162                         apen.put_value();
163
164         // Mark our progress as finished
165         if(cb && !cb->amount_complete(10000,10000))
166                 return false;
167
168         return true;
169 }