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