Initial Stable Commit
[synfig.git] / synfig-core / trunk / src / modules / lyr_std / twirl.cpp
1 /* === S I N F G =========================================================== */
2 /*!     \file spiralgradient.cpp
3 **      \brief Template Header
4 **
5 **      $Id: twirl.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include <sinfg/string.h>
32 #include <sinfg/time.h>
33 #include <sinfg/context.h>
34 #include <sinfg/paramdesc.h>
35 #include <sinfg/renddesc.h>
36 #include <sinfg/surface.h>
37 #include <sinfg/value.h>
38 #include <sinfg/valuenode.h>
39 #include <sinfg/transform.h>
40 #include "twirl.h"
41
42 #endif
43
44 /* === U S I N G =========================================================== */
45
46 using namespace etl;
47 using namespace std;
48 using namespace sinfg;
49
50 /* === G L O B A L S ======================================================= */
51
52 SINFG_LAYER_INIT(Twirl);
53 SINFG_LAYER_SET_NAME(Twirl,"twirl");
54 SINFG_LAYER_SET_LOCAL_NAME(Twirl,_("Twirl"));
55 SINFG_LAYER_SET_CATEGORY(Twirl,_("Distortions"));
56 SINFG_LAYER_SET_VERSION(Twirl,"0.1");
57 SINFG_LAYER_SET_CVS_ID(Twirl,"$Id: twirl.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $");
58
59 /* === P R O C E D U R E S ================================================= */
60
61 /* === M E T H O D S ======================================================= */
62
63 /* === E N T R Y P O I N T ================================================= */
64
65 Twirl::Twirl():
66         Layer_Composite(1.0,Color::BLEND_STRAIGHT),
67         center(0,0),
68         radius(1.0),
69         rotations(Angle::zero()),
70         distort_inside(true),
71         distort_outside(false)
72 {
73 }
74         
75 bool
76 Twirl::set_param(const String & param, const ValueBase &value)
77 {
78         IMPORT(center);
79         IMPORT(radius);
80         IMPORT(rotations);
81         IMPORT(distort_inside);
82         IMPORT(distort_outside);
83         
84         return Layer_Composite::set_param(param,value);
85 }
86
87 ValueBase
88 Twirl::get_param(const String &param)const
89 {
90         EXPORT(center);
91         EXPORT(radius);
92         EXPORT(rotations);
93         EXPORT(distort_inside);
94         EXPORT(distort_outside);
95         
96         EXPORT_NAME();
97         EXPORT_VERSION();
98                 
99         return false;
100 }
101
102 Layer::Vocab
103 Twirl::get_param_vocab()const
104 {
105         Layer::Vocab ret;
106         
107         ret.push_back(ParamDesc("center")
108                 .set_local_name(_("Center"))
109         );
110         
111         ret.push_back(ParamDesc("radius")
112                 .set_local_name(_("Radius"))
113                 .set_description(_("This is the radius of the circle"))
114                 .set_is_distance()
115                 .set_origin("center")
116         );
117
118         ret.push_back(ParamDesc("rotations")
119                 .set_local_name(_("Rotations"))
120                 .set_origin("center")
121         );
122
123         ret.push_back(ParamDesc("distort_inside")
124                 .set_local_name(_("Distort Inside"))
125         );
126
127         ret.push_back(ParamDesc("distort_outside")
128                 .set_local_name(_("Distort Outside"))
129         );
130
131         return ret;
132 }
133
134 sinfg::Point
135 Twirl::distort(const sinfg::Point &pos,bool reverse)const
136 {
137         Point centered(pos-center);
138         Real mag(centered.mag());
139         
140         Angle a;
141
142         if((distort_inside || mag>radius) && (distort_outside || mag<radius))
143                 a=rotations*((centered.mag()-radius)/radius);
144         else
145                 return pos;
146         
147         if(reverse)     a=-a;
148                 
149         const Real sin(Angle::sin(a).get());    
150         const Real cos(Angle::cos(a).get());    
151
152         Point twirled;
153         twirled[0]=cos*centered[0]-sin*centered[1];
154         twirled[1]=sin*centered[0]+cos*centered[1];
155
156         return twirled+center;
157 }
158
159 sinfg::Layer::Handle
160 Twirl::hit_check(sinfg::Context context, const sinfg::Point &pos)const
161 {
162         return context.hit_check(distort(pos));
163 }
164
165 Color
166 Twirl::get_color(Context context, const Point &pos)const
167 {
168         return context.get_color(distort(pos));
169 }
170
171 class Twirl_Trans : public Transform
172 {
173         etl::handle<const Twirl> layer;
174 public:
175         Twirl_Trans(const Twirl* x):Transform(x->get_guid()),layer(x) { }
176         
177         sinfg::Vector perform(const sinfg::Vector& x)const
178         {
179                 return layer->distort(x,true);
180         }
181         
182         sinfg::Vector unperform(const sinfg::Vector& x)const
183         {
184                 return layer->distort(x,false);
185         }
186 };
187 etl::handle<Transform>
188 Twirl::get_transform()const
189 {
190         return new Twirl_Trans(this);
191 }
192
193 /*
194 bool
195 Twirl::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
196 {
197         SuperCallback supercb(cb,0,9500,10000);
198
199         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
200         {
201                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
202         }
203         else
204         {
205                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
206                         return false;
207                 if(get_amount()==0)
208                         return true;
209         }
210
211                 
212         int x,y;
213
214         Surface::pen pen(surface->begin());
215         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
216         Point pos;
217         Point tl(renddesc.get_tl());
218         const int w(surface->get_w());
219         const int h(surface->get_h());
220         
221         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
222         {
223                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
224                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
225                                 pen.put_value(color_func(pos));
226         }
227         else
228         {
229                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
230                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
231                                 pen.put_value(Color::blend(color_func(pos),pen.get_value(),get_amount(),get_blend_method()));
232         }
233
234         // Mark our progress as finished
235         if(cb && !cb->amount_complete(10000,10000))
236                 return false;
237
238         return true;
239 }
240 */