1 /* === S Y N F I G ========================================================= */
3 ** \brief Implementation of the "Twirl" layer
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
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/transform.h>
45 /* === U S I N G =========================================================== */
49 using namespace synfig;
51 /* === G L O B A L S ======================================================= */
53 SYNFIG_LAYER_INIT(Twirl);
54 SYNFIG_LAYER_SET_NAME(Twirl,"twirl");
55 SYNFIG_LAYER_SET_LOCAL_NAME(Twirl,N_("Twirl"));
56 SYNFIG_LAYER_SET_CATEGORY(Twirl,N_("Distortions"));
57 SYNFIG_LAYER_SET_VERSION(Twirl,"0.1");
58 SYNFIG_LAYER_SET_CVS_ID(Twirl,"$Id$");
60 /* === P R O C E D U R E S ================================================= */
62 /* === M E T H O D S ======================================================= */
64 /* === E N T R Y P O I N T ================================================= */
67 Layer_Composite(1.0,Color::BLEND_STRAIGHT),
70 rotations(Angle::zero()),
72 distort_outside(false)
74 Layer::Vocab voc(get_param_vocab());
75 Layer::fill_static(voc);
79 Twirl::set_param(const String & param, const ValueBase &value)
84 IMPORT(distort_inside);
85 IMPORT(distort_outside);
87 return Layer_Composite::set_param(param,value);
91 Twirl::get_param(const String ¶m)const
96 EXPORT(distort_inside);
97 EXPORT(distort_outside);
106 Twirl::get_param_vocab()const
110 ret.push_back(ParamDesc("center")
111 .set_local_name(_("Center"))
112 .set_description(_("Center of the circle"))
115 ret.push_back(ParamDesc("radius")
116 .set_local_name(_("Radius"))
117 .set_description(_("This is the radius of the circle"))
119 .set_origin("center")
122 ret.push_back(ParamDesc("rotations")
123 .set_local_name(_("Rotations"))
124 .set_description(_("The number of rotations of the twirl effect"))
125 .set_origin("center")
128 ret.push_back(ParamDesc("distort_inside")
129 .set_local_name(_("Distort Inside"))
130 .set_description(_("When checked, distorts inside the circle"))
133 ret.push_back(ParamDesc("distort_outside")
134 .set_local_name(_("Distort Outside"))
135 .set_description(_("When checked, distorts outside the circle"))
142 Twirl::distort(const synfig::Point &pos,bool reverse)const
144 Point centered(pos-center);
145 Real mag(centered.mag());
149 if((distort_inside || mag>radius) && (distort_outside || mag<radius))
150 a=rotations*((centered.mag()-radius)/radius);
156 const Real sin(Angle::sin(a).get());
157 const Real cos(Angle::cos(a).get());
160 twirled[0]=cos*centered[0]-sin*centered[1];
161 twirled[1]=sin*centered[0]+cos*centered[1];
163 return twirled+center;
166 synfig::Layer::Handle
167 Twirl::hit_check(synfig::Context context, const synfig::Point &pos)const
169 return context.hit_check(distort(pos));
173 Twirl::get_color(Context context, const Point &pos)const
175 return context.get_color(distort(pos));
178 class Twirl_Trans : public Transform
180 etl::handle<const Twirl> layer;
182 Twirl_Trans(const Twirl* x):Transform(x->get_guid()),layer(x) { }
184 synfig::Vector perform(const synfig::Vector& x)const
186 return layer->distort(x,true);
189 synfig::Vector unperform(const synfig::Vector& x)const
191 return layer->distort(x,false);
194 etl::handle<Transform>
195 Twirl::get_transform()const
197 return new Twirl_Trans(this);
202 Twirl::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
204 SuperCallback supercb(cb,0,9500,10000);
206 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
208 surface->set_wh(renddesc.get_w(),renddesc.get_h());
212 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
221 Surface::pen pen(surface->begin());
222 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
224 Point tl(renddesc.get_tl());
225 const int w(surface->get_w());
226 const int h(surface->get_h());
228 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
230 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
231 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
232 pen.put_value(color_func(pos));
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::blend(color_func(pos),pen.get_value(),get_amount(),get_blend_method()));
241 // Mark our progress as finished
242 if(cb && !cb->amount_complete(10000,10000))