Set the description of some parameters
[synfig.git] / synfig-core / src / modules / mod_gradient / lineargradient.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file lineargradient.cpp
3 **      \brief Implementation of the "Linear Gradient" layer
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 ** === N O T E S ===========================================================
22 **
23 ** ========================================================================= */
24
25 /* === H E A D E R S ======================================================= */
26
27 #ifdef USING_PCH
28 #       include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #       include <config.h>
32 #endif
33
34 #include "lineargradient.h"
35
36 #include <synfig/string.h>
37 #include <synfig/time.h>
38 #include <synfig/context.h>
39 #include <synfig/paramdesc.h>
40 #include <synfig/renddesc.h>
41 #include <synfig/surface.h>
42 #include <synfig/value.h>
43 #include <synfig/valuenode.h>
44
45 #endif
46
47 /* === M A C R O S ========================================================= */
48
49 /* === G L O B A L S ======================================================= */
50
51 SYNFIG_LAYER_INIT(LinearGradient);
52 SYNFIG_LAYER_SET_NAME(LinearGradient,"linear_gradient");
53 SYNFIG_LAYER_SET_LOCAL_NAME(LinearGradient,N_("Linear Gradient"));
54 SYNFIG_LAYER_SET_CATEGORY(LinearGradient,N_("Gradients"));
55 SYNFIG_LAYER_SET_VERSION(LinearGradient,"0.0");
56 SYNFIG_LAYER_SET_CVS_ID(LinearGradient,"$Id$");
57
58 /* === P R O C E D U R E S ================================================= */
59
60 /* === M E T H O D S ======================================================= */
61
62 inline void
63 LinearGradient::sync()
64 {
65         diff=(p2-p1);
66         const Real mag(diff.inv_mag());
67         diff*=mag*mag;
68 }
69
70
71 LinearGradient::LinearGradient():
72         Layer_Composite(1.0,Color::BLEND_COMPOSITE),
73         p1(1,1),
74         p2(-1,-1),
75         gradient(Color::black(), Color::white()),
76         loop(false),
77         zigzag(false)
78 {
79         sync();
80         Layer::Vocab voc(get_param_vocab());
81         Layer::fill_static(voc);
82 }
83
84 inline Color
85 LinearGradient::color_func(const Point &point, float supersample)const
86 {
87         Real dist(point*diff-p1*diff);
88
89         if(loop)
90                 dist-=floor(dist);
91
92         if(zigzag)
93         {
94                 dist*=2.0;
95                 supersample*=2.0;
96                 if(dist>1)dist=2.0-dist;
97         }
98
99         if(loop)
100         {
101                 if(dist+supersample*0.5>1.0)
102                 {
103                         float  left(supersample*0.5-(dist-1.0));
104                         float right(supersample*0.5+(dist-1.0));
105                         Color pool(gradient(1.0-(left*0.5),left).premult_alpha()*left/supersample);
106                         if (zigzag) pool+=gradient(1.0-right*0.5,right).premult_alpha()*right/supersample;
107                         else            pool+=gradient(right*0.5,right).premult_alpha()*right/supersample;
108                         return pool.demult_alpha();
109                 }
110                 if(dist-supersample*0.5<0.0)
111                 {
112                         float  left(supersample*0.5-dist);
113                         float right(supersample*0.5+dist);
114                         Color pool(gradient(right*0.5,right).premult_alpha()*right/supersample);
115                         if (zigzag) pool+=gradient(left*0.5,left).premult_alpha()*left/supersample;
116                         else            pool+=gradient(1.0-left*0.5,left).premult_alpha()*left/supersample;
117                         return pool.demult_alpha();
118                 }
119         }
120         return gradient(dist,supersample);
121 }
122
123 float
124 LinearGradient::calc_supersample(const synfig::Point &/*x*/, float pw,float /*ph*/)const
125 {
126         return pw/(p2-p1).mag();
127 }
128
129 synfig::Layer::Handle
130 LinearGradient::hit_check(synfig::Context context, const synfig::Point &point)const
131 {
132         if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
133                 return const_cast<LinearGradient*>(this);
134         if(get_amount()==0.0)
135                 return context.hit_check(point);
136         if((get_blend_method()==Color::BLEND_STRAIGHT || get_blend_method()==Color::BLEND_COMPOSITE) && color_func(point).get_a()>0.5)
137                 return const_cast<LinearGradient*>(this);
138         return context.hit_check(point);
139 }
140
141 bool
142 LinearGradient::set_param(const String & param, const ValueBase &value)
143 {
144         if(param=="p1" && value.same_type_as(p1))
145         {
146                 p1=value.get(p1);
147                 set_param_static(param, value.get_static());
148                 sync();
149                 return true;
150         }
151         if(param=="p2" && value.same_type_as(p2))
152         {
153                 p2=value.get(p2);
154                 set_param_static(param, value.get_static());
155                 sync();
156                 return true;
157         }
158         //IMPORT(p1);
159         //IMPORT(p2);
160
161
162         IMPORT(gradient);
163         IMPORT(loop);
164         IMPORT(zigzag);
165         return Layer_Composite::set_param(param,value);
166 }
167
168 ValueBase
169 LinearGradient::get_param(const String & param)const
170 {
171         EXPORT(p1);
172         EXPORT(p2);
173         EXPORT(gradient);
174         EXPORT(loop);
175         EXPORT(zigzag);
176
177         EXPORT_NAME();
178         EXPORT_VERSION();
179
180         return Layer_Composite::get_param(param);
181 }
182
183 Layer::Vocab
184 LinearGradient::get_param_vocab()const
185 {
186         Layer::Vocab ret(Layer_Composite::get_param_vocab());
187
188         ret.push_back(ParamDesc("p1")
189                 .set_local_name(_("Point 1"))
190                 .set_connect("p2")
191                 .set_description(_("Start point of the gradient"))
192         );
193         ret.push_back(ParamDesc("p2")
194                 .set_local_name(_("Point 2"))
195                 .set_description(_("End point of the gradient"))
196         );
197         ret.push_back(ParamDesc("gradient")
198                 .set_local_name(_("Gradient"))
199                 .set_description(_("Gradient to apply"))
200         );
201         ret.push_back(ParamDesc("loop")
202                 .set_local_name(_("Loop"))
203                 .set_description(_("When checked the gradient is looped"))
204         );
205         ret.push_back(ParamDesc("zigzag")
206                 .set_local_name(_("ZigZag"))
207                 .set_description(_("When checked the gradient is summetrical at the center"))
208         );
209
210         return ret;
211 }
212
213 Color
214 LinearGradient::get_color(Context context, const Point &point)const
215 {
216         const Color color(color_func(point));
217
218         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
219                 return color;
220         else
221                 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
222 }
223
224 bool
225 LinearGradient::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
226 {
227         SuperCallback supercb(cb,0,9500,10000);
228
229         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
230         {
231                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
232         }
233         else
234         {
235                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
236                         return false;
237                 if(get_amount()==0)
238                         return true;
239         }
240
241
242         int x,y;
243
244         Surface::pen pen(surface->begin());
245         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
246         Point pos;
247         Point tl(renddesc.get_tl());
248         const int w(surface->get_w());
249         const int h(surface->get_h());
250
251         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
252         {
253                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
254                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
255                                 pen.put_value(color_func(pos,calc_supersample(pos,pw,ph)));
256         }
257         else
258         {
259                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
260                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
261                                 pen.put_value(Color::blend(color_func(pos,calc_supersample(pos,pw,ph)),pen.get_value(),get_amount(),get_blend_method()));
262         }
263
264         // Mark our progress as finished
265         if(cb && !cb->amount_complete(10000,10000))
266                 return false;
267
268         return true;
269 }