Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07 / src / modules / lyr_std / twirl.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file twirl.cpp
3 **      \brief Template Header
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/transform.h>
41 #include "twirl.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(Twirl);
54 SYNFIG_LAYER_SET_NAME(Twirl,"twirl");
55 SYNFIG_LAYER_SET_LOCAL_NAME(Twirl,_("Twirl"));
56 SYNFIG_LAYER_SET_CATEGORY(Twirl,_("Distortions"));
57 SYNFIG_LAYER_SET_VERSION(Twirl,"0.1");
58 SYNFIG_LAYER_SET_CVS_ID(Twirl,"$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 Twirl::Twirl():
67         Layer_Composite(1.0,Color::BLEND_STRAIGHT),
68         center(0,0),
69         radius(1.0),
70         rotations(Angle::zero()),
71         distort_inside(true),
72         distort_outside(false)
73 {
74 }
75
76 bool
77 Twirl::set_param(const String & param, const ValueBase &value)
78 {
79         IMPORT(center);
80         IMPORT(radius);
81         IMPORT(rotations);
82         IMPORT(distort_inside);
83         IMPORT(distort_outside);
84
85         return Layer_Composite::set_param(param,value);
86 }
87
88 ValueBase
89 Twirl::get_param(const String &param)const
90 {
91         EXPORT(center);
92         EXPORT(radius);
93         EXPORT(rotations);
94         EXPORT(distort_inside);
95         EXPORT(distort_outside);
96
97         EXPORT_NAME();
98         EXPORT_VERSION();
99
100         return false;
101 }
102
103 Layer::Vocab
104 Twirl::get_param_vocab()const
105 {
106         Layer::Vocab ret;
107
108         ret.push_back(ParamDesc("center")
109                 .set_local_name(_("Center"))
110         );
111
112         ret.push_back(ParamDesc("radius")
113                 .set_local_name(_("Radius"))
114                 .set_description(_("This is the radius of the circle"))
115                 .set_is_distance()
116                 .set_origin("center")
117         );
118
119         ret.push_back(ParamDesc("rotations")
120                 .set_local_name(_("Rotations"))
121                 .set_origin("center")
122         );
123
124         ret.push_back(ParamDesc("distort_inside")
125                 .set_local_name(_("Distort Inside"))
126         );
127
128         ret.push_back(ParamDesc("distort_outside")
129                 .set_local_name(_("Distort Outside"))
130         );
131
132         return ret;
133 }
134
135 synfig::Point
136 Twirl::distort(const synfig::Point &pos,bool reverse)const
137 {
138         Point centered(pos-center);
139         Real mag(centered.mag());
140
141         Angle a;
142
143         if((distort_inside || mag>radius) && (distort_outside || mag<radius))
144                 a=rotations*((centered.mag()-radius)/radius);
145         else
146                 return pos;
147
148         if(reverse)     a=-a;
149
150         const Real sin(Angle::sin(a).get());
151         const Real cos(Angle::cos(a).get());
152
153         Point twirled;
154         twirled[0]=cos*centered[0]-sin*centered[1];
155         twirled[1]=sin*centered[0]+cos*centered[1];
156
157         return twirled+center;
158 }
159
160 synfig::Layer::Handle
161 Twirl::hit_check(synfig::Context context, const synfig::Point &pos)const
162 {
163         return context.hit_check(distort(pos));
164 }
165
166 Color
167 Twirl::get_color(Context context, const Point &pos)const
168 {
169         return context.get_color(distort(pos));
170 }
171
172 class Twirl_Trans : public Transform
173 {
174         etl::handle<const Twirl> layer;
175 public:
176         Twirl_Trans(const Twirl* x):Transform(x->get_guid()),layer(x) { }
177
178         synfig::Vector perform(const synfig::Vector& x)const
179         {
180                 return layer->distort(x,true);
181         }
182
183         synfig::Vector unperform(const synfig::Vector& x)const
184         {
185                 return layer->distort(x,false);
186         }
187 };
188 etl::handle<Transform>
189 Twirl::get_transform()const
190 {
191         return new Twirl_Trans(this);
192 }
193
194 /*
195 bool
196 Twirl::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
197 {
198         SuperCallback supercb(cb,0,9500,10000);
199
200         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
201         {
202                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
203         }
204         else
205         {
206                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
207                         return false;
208                 if(get_amount()==0)
209                         return true;
210         }
211
212
213         int x,y;
214
215         Surface::pen pen(surface->begin());
216         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
217         Point pos;
218         Point tl(renddesc.get_tl());
219         const int w(surface->get_w());
220         const int h(surface->get_h());
221
222         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
223         {
224                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
225                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
226                                 pen.put_value(color_func(pos));
227         }
228         else
229         {
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::blend(color_func(pos),pen.get_value(),get_amount(),get_blend_method()));
233         }
234
235         // Mark our progress as finished
236         if(cb && !cb->amount_complete(10000,10000))
237                 return false;
238
239         return true;
240 }
241 */