More descriptions for layer parameters
[synfig.git] / synfig-core / src / modules / mod_gradient / spiralgradient.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file spiralgradient.cpp
3 **      \brief Implementation of the "Spiral 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
41 #include "spiralgradient.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(SpiralGradient);
54 SYNFIG_LAYER_SET_NAME(SpiralGradient,"spiral_gradient");
55 SYNFIG_LAYER_SET_LOCAL_NAME(SpiralGradient,N_("Spiral Gradient"));
56 SYNFIG_LAYER_SET_CATEGORY(SpiralGradient,N_("Gradients"));
57 SYNFIG_LAYER_SET_VERSION(SpiralGradient,"0.1");
58 SYNFIG_LAYER_SET_CVS_ID(SpiralGradient,"$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 SpiralGradient::SpiralGradient():
67         Layer_Composite(1.0,Color::BLEND_COMPOSITE),
68         gradient(Color::black(),Color::white()),
69         center(0,0),
70         radius(0.5),
71         angle(Angle::zero()),
72         clockwise(false)
73 {
74         Layer::Vocab voc(get_param_vocab());
75         Layer::fill_static(voc);
76 }
77
78 bool
79 SpiralGradient::set_param(const String & param, const ValueBase &value)
80 {
81         IMPORT(gradient);
82         IMPORT(center);
83         IMPORT(radius);
84         IMPORT(angle);
85         IMPORT(clockwise);
86
87         return Layer_Composite::set_param(param,value);
88 }
89
90 ValueBase
91 SpiralGradient::get_param(const String &param)const
92 {
93         EXPORT(gradient);
94         EXPORT(center);
95         EXPORT(radius);
96         EXPORT(angle);
97         EXPORT(clockwise);
98
99         EXPORT_NAME();
100         EXPORT_VERSION();
101
102         return Layer_Composite::get_param(param);
103 }
104
105 Layer::Vocab
106 SpiralGradient::get_param_vocab()const
107 {
108         Layer::Vocab ret(Layer_Composite::get_param_vocab());
109
110         ret.push_back(ParamDesc("gradient")
111                 .set_local_name(_("Gradient"))
112                 .set_description(_("Gradient to apply"))
113         );
114
115         ret.push_back(ParamDesc("center")
116                 .set_local_name(_("Center"))
117                 .set_description(_("Center of the gradient"))
118         );
119
120         ret.push_back(ParamDesc("radius")
121                 .set_local_name(_("Radius"))
122                 .set_description(_("This is the radius of the circle"))
123                 .set_is_distance()
124                 .set_origin("center")
125         );
126
127         ret.push_back(ParamDesc("angle")
128                 .set_local_name(_("Angle"))
129                 .set_description(_("Rotation of the spiral"))
130                 .set_origin("center")
131         );
132
133         ret.push_back(ParamDesc("clockwise")
134                 .set_local_name(_("Clockwise"))
135                 .set_description(_("When checked the spiral turns clockwise"))
136         );
137
138         return ret;
139 }
140
141 inline Color
142 SpiralGradient::color_func(const Point &pos, float supersample)const
143 {
144         const Point centered(pos-center);
145         Angle a;
146         a=Angle::tan(-centered[1],centered[0]).mod();
147         a=a+angle;
148
149         if(supersample<0.00001)supersample=0.00001;
150
151         Real dist((pos-center).mag()/radius);
152         if(clockwise)
153                 dist+=Angle::rot(a.mod()).get();
154         else
155                 dist-=Angle::rot(a.mod()).get();
156
157         dist-=floor(dist);
158         if(dist+supersample*0.5>1.0)
159         {
160                 float  left(supersample*0.5-(dist-1.0));
161                 float right(supersample*0.5+(dist-1.0));
162                 Color pool(gradient(1.0-(left*0.5),left).premult_alpha()*left/supersample);
163                 pool+=gradient(right*0.5,right).premult_alpha()*right/supersample;
164                 return pool.demult_alpha();
165         }
166         if(dist-supersample*0.5<0.0)
167         {
168                 float  left(supersample*0.5-dist);
169                 float right(supersample*0.5+dist);
170                 Color pool(gradient(right*0.5,right).premult_alpha()*right/supersample);
171                 pool+=gradient(1.0-left*0.5,left).premult_alpha()*left/supersample;
172                 return pool.demult_alpha();
173         }
174
175         return gradient(dist,supersample);
176 }
177
178 float
179 SpiralGradient::calc_supersample(const synfig::Point &x, float pw,float /*ph*/)const
180 {
181         return (1.41421*pw/radius+(1.41421*pw/Point(x-center).mag())/(PI*2))*0.5;
182 }
183
184 synfig::Layer::Handle
185 SpiralGradient::hit_check(synfig::Context context, const synfig::Point &point)const
186 {
187         if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
188                 return const_cast<SpiralGradient*>(this);
189         if(get_amount()==0.0)
190                 return context.hit_check(point);
191         if((get_blend_method()==Color::BLEND_STRAIGHT || get_blend_method()==Color::BLEND_COMPOSITE) && color_func(point).get_a()>0.5)
192                 return const_cast<SpiralGradient*>(this);
193         return context.hit_check(point);
194 }
195
196 Color
197 SpiralGradient::get_color(Context context, const Point &pos)const
198 {
199         const Color color(color_func(pos));
200
201         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
202                 return color;
203         else
204                 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
205 }
206
207 bool
208 SpiralGradient::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
209 {
210         SuperCallback supercb(cb,0,9500,10000);
211
212         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
213         {
214                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
215         }
216         else
217         {
218                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
219                         return false;
220                 if(get_amount()==0)
221                         return true;
222         }
223
224
225         int x,y;
226
227         Surface::pen pen(surface->begin());
228         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
229         Point pos;
230         Point tl(renddesc.get_tl());
231         const int w(surface->get_w());
232         const int h(surface->get_h());
233
234         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
235         {
236                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
237                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
238                                 pen.put_value(color_func(pos,calc_supersample(pos,pw,ph)));
239         }
240         else
241         {
242                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
243                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
244                                 pen.put_value(Color::blend(color_func(pos,calc_supersample(pos,pw,ph)),pen.get_value(),get_amount(),get_blend_method()));
245         }
246
247         // Mark our progress as finished
248         if(cb && !cb->amount_complete(10000,10000))
249                 return false;
250
251         return true;
252 }