Add parameter static option for rest of layers.
[synfig.git] / synfig-core / src / modules / mod_geometry / star.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file star.cpp
3 **      \brief Implementation of the "Star" layer
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007-2008 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 **
22 ** === N O T E S ===========================================================
23 **
24 ** ========================================================================= */
25
26 /* === H E A D E R S ======================================================= */
27
28 #ifdef USING_PCH
29 #       include "pch.h"
30 #else
31 #ifdef HAVE_CONFIG_H
32 #       include <config.h>
33 #endif
34
35 #include "star.h"
36 #include <ETL/stringf>
37 #include <ETL/bezier>
38 #include <ETL/hermite>
39
40 #include <synfig/string.h>
41 #include <synfig/time.h>
42 #include <synfig/context.h>
43 #include <synfig/paramdesc.h>
44 #include <synfig/renddesc.h>
45 #include <synfig/surface.h>
46 #include <synfig/value.h>
47 #include <synfig/valuenode.h>
48 #include <synfig/segment.h>
49
50 #endif
51
52 using namespace etl;
53
54 /* === M A C R O S ========================================================= */
55
56 #define SAMPLES         75
57
58 /* === G L O B A L S ======================================================= */
59
60 SYNFIG_LAYER_INIT(Star);
61 SYNFIG_LAYER_SET_NAME(Star,"star");
62 SYNFIG_LAYER_SET_LOCAL_NAME(Star,N_("Star"));
63 SYNFIG_LAYER_SET_CATEGORY(Star,N_("Geometry"));
64 SYNFIG_LAYER_SET_VERSION(Star,"0.1");
65 SYNFIG_LAYER_SET_CVS_ID(Star,"$Id$");
66
67 /* === P R O C E D U R E S ================================================= */
68
69 /* === M E T H O D S ======================================================= */
70
71 /* === E N T R Y P O I N T ================================================= */
72
73 Star::Star():
74         radius1(1.0),
75         radius2(0.38),
76         points(5),
77         angle(Angle::deg(90)),
78         regular_polygon(false)
79 {
80         sync();
81         Layer::Vocab voc(get_param_vocab());
82         Layer::fill_static(voc);
83 }
84
85 void
86 Star::sync()
87 {
88         Angle dist_between_points(Angle::rot(1)/float(points));
89         std::vector<Point> vector_list;
90
91         int i;
92         for(i=0;i<points;i++)
93         {
94                 Angle dist1(dist_between_points*i+angle);
95                 Angle dist2(dist_between_points*i+dist_between_points/2+angle);
96                 vector_list.push_back(Point(Angle::cos(dist1).get()*radius1,Angle::sin(dist1).get()*radius1));
97                 if (!regular_polygon)
98                         vector_list.push_back(Point(Angle::cos(dist2).get()*radius2,Angle::sin(dist2).get()*radius2));
99         }
100         clear();
101         add_polygon(vector_list);
102 }
103
104 bool
105 Star::set_param(const String & param, const ValueBase &value)
106 {
107         if(     param=="radius1" && value.same_type_as(radius1))
108         {
109                 value.put(&radius1);
110                 sync();
111                 set_param_static(param, value.get_static());
112                 return true;
113         }
114
115         if(     param=="radius2" && value.same_type_as(radius2))
116         {
117                 value.put(&radius2);
118                 sync();
119                 set_param_static(param, value.get_static());
120                 return true;
121         }
122
123         if(     param=="points" && value.same_type_as(points))
124         {
125                 value.put(&points);
126                 if(points<2)points=2;
127                 sync();
128                 set_param_static(param, value.get_static());
129                 return true;
130         }
131
132         if(     param=="angle" && value.same_type_as(angle))
133         {
134                 value.put(&angle);
135                 sync();
136                 set_param_static(param, value.get_static());
137                 return true;
138         }
139
140         if(param=="regular_polygon" && value.same_type_as(regular_polygon))
141         {
142                 value.put(&regular_polygon);
143                 sync();
144                 set_param_static(param, value.get_static());
145                 return true;
146         }
147
148         if(param=="vector_list")
149                 return false;
150
151         return Layer_Polygon::set_param(param,value);
152 }
153
154 ValueBase
155 Star::get_param(const String& param)const
156 {
157         EXPORT(radius1);
158         EXPORT(radius2);
159         EXPORT(points);
160         EXPORT(angle);
161         EXPORT(regular_polygon);
162
163         EXPORT_NAME();
164         EXPORT_VERSION();
165
166         if(param=="vector_list")
167                 return ValueBase();
168
169         return Layer_Polygon::get_param(param);
170 }
171
172 Layer::Vocab
173 Star::get_param_vocab()const
174 {
175         Layer::Vocab ret(Layer_Polygon::get_param_vocab());
176
177         // Pop off the polygon parameter from the polygon vocab
178         ret.pop_back();
179
180         ret.push_back(ParamDesc("radius1")
181                 .set_local_name(_("Outer Radius"))
182                 .set_description(_("The radius of the outer points in the star"))
183                 .set_is_distance()
184                 .set_origin("origin")
185         );
186
187         ret.push_back(ParamDesc("radius2")
188                 .set_local_name(_("Inner Radius"))
189                 .set_description(_("The radius of the inner points in the star"))
190                 .set_is_distance()
191                 .set_origin("origin")
192         );
193
194         ret.push_back(ParamDesc("angle")
195                 .set_local_name(_("Angle"))
196                 .set_description(_("The orientation of the star"))
197                 .set_origin("origin")
198         );
199
200         ret.push_back(ParamDesc("points")
201                 .set_local_name(_("Points"))
202                 .set_description(_("The number of points in the star"))
203         );
204
205         ret.push_back(ParamDesc("regular_polygon")
206                 .set_local_name(_("Regular Polygon"))
207                 .set_description(_("Whether to draw a star or a regular polygon"))
208         );
209
210         return ret;
211 }