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