Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / stable / 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 }
82
83 void
84 Star::sync()
85 {
86         Angle dist_between_points(Angle::rot(1)/float(points));
87         std::vector<Point> vector_list;
88
89         int i;
90         for(i=0;i<points;i++)
91         {
92                 Angle dist1(dist_between_points*i+angle);
93                 Angle dist2(dist_between_points*i+dist_between_points/2+angle);
94                 vector_list.push_back(Point(Angle::cos(dist1).get()*radius1,Angle::sin(dist1).get()*radius1));
95                 if (!regular_polygon)
96                         vector_list.push_back(Point(Angle::cos(dist2).get()*radius2,Angle::sin(dist2).get()*radius2));
97         }
98         clear();
99         add_polygon(vector_list);
100 }
101
102 bool
103 Star::set_param(const String & param, const ValueBase &value)
104 {
105         if(     param=="radius1" && value.same_type_as(radius1))
106         {
107                 value.put(&radius1);
108                 sync();
109                 return true;
110         }
111
112         if(     param=="radius2" && value.same_type_as(radius2))
113         {
114                 value.put(&radius2);
115                 sync();
116                 return true;
117         }
118
119         if(     param=="points" && value.same_type_as(points))
120         {
121                 value.put(&points);
122                 if(points<2)points=2;
123                 sync();
124                 return true;
125         }
126
127         if(     param=="angle" && value.same_type_as(angle))
128         {
129                 value.put(&angle);
130                 sync();
131                 return true;
132         }
133
134         if(param=="regular_polygon" && value.same_type_as(regular_polygon))
135         {
136                 value.put(&regular_polygon);
137                 sync();
138                 return true;
139         }
140
141         if(param=="vector_list")
142                 return false;
143
144         return Layer_Polygon::set_param(param,value);
145 }
146
147 ValueBase
148 Star::get_param(const String& param)const
149 {
150         EXPORT(radius1);
151         EXPORT(radius2);
152         EXPORT(points);
153         EXPORT(angle);
154         EXPORT(regular_polygon);
155
156         EXPORT_NAME();
157         EXPORT_VERSION();
158
159         if(param=="vector_list")
160                 return ValueBase();
161
162         return Layer_Polygon::get_param(param);
163 }
164
165 Layer::Vocab
166 Star::get_param_vocab()const
167 {
168         Layer::Vocab ret(Layer_Polygon::get_param_vocab());
169
170         // Pop off the polygon parameter from the polygon vocab
171         ret.pop_back();
172
173         ret.push_back(ParamDesc("radius1")
174                 .set_local_name(_("Outer Radius"))
175                 .set_description(_("The radius of the outer points in the star"))
176                 .set_is_distance()
177                 .set_origin("origin")
178         );
179
180         ret.push_back(ParamDesc("radius2")
181                 .set_local_name(_("Inner Radius"))
182                 .set_description(_("The radius of the inner points in the star"))
183                 .set_is_distance()
184                 .set_origin("origin")
185         );
186
187         ret.push_back(ParamDesc("angle")
188                 .set_local_name(_("Angle"))
189                 .set_description(_("The orientation of the star"))
190                 .set_origin("origin")
191         );
192
193         ret.push_back(ParamDesc("points")
194                 .set_local_name(_("Points"))
195                 .set_description(_("The number of points in the star"))
196         );
197
198         ret.push_back(ParamDesc("regular_polygon")
199                 .set_local_name(_("Regular Polygon"))
200                 .set_description(_("Whether to draw a star or a regular polygon"))
201         );
202
203         return ret;
204 }