Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_06 / src / modules / mod_geometry / star.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file star.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 ** === N O T E S ===========================================================
22 **
23 ** ========================================================================= */
24
25 /* === H E A D E R S ======================================================= */
26
27 #ifdef USING_PCH
28 #       include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #       include <config.h>
32 #endif
33
34 #include "star.h"
35 #include <ETL/stringf>
36 #include <ETL/bezier>
37 #include <ETL/hermite>
38
39 #include <synfig/string.h>
40 #include <synfig/time.h>
41 #include <synfig/context.h>
42 #include <synfig/paramdesc.h>
43 #include <synfig/renddesc.h>
44 #include <synfig/surface.h>
45 #include <synfig/value.h>
46 #include <synfig/valuenode.h>
47 #include <synfig/segment.h>
48
49 #endif
50
51 using namespace etl;
52
53 /* === M A C R O S ========================================================= */
54
55 #define SAMPLES         75
56
57 /* === G L O B A L S ======================================================= */
58
59 SYNFIG_LAYER_INIT(Star);
60 SYNFIG_LAYER_SET_NAME(Star,"star");
61 SYNFIG_LAYER_SET_LOCAL_NAME(Star,_("Star"));
62 SYNFIG_LAYER_SET_CATEGORY(Star,_("Geometry"));
63 SYNFIG_LAYER_SET_VERSION(Star,"0.1");
64 SYNFIG_LAYER_SET_CVS_ID(Star,"$Id$");
65
66 /* === P R O C E D U R E S ================================================= */
67
68 /* === M E T H O D S ======================================================= */
69
70 /* === E N T R Y P O I N T ================================================= */
71
72 Star::Star():
73         radius1(1.0),
74         radius2(0.38),
75         points(5),
76         angle(Angle::deg(90))
77 {
78         sync();
79 }
80
81 void
82 Star::sync()
83 {
84         Angle dist_between_points(Angle::rot(1)/float(points));
85         std::vector<Point> vector_list;
86
87         int i;
88         for(i=0;i<points;i++)
89         {
90                 Angle dist1(dist_between_points*i+angle);
91                 Angle dist2(dist_between_points*i+dist_between_points/2+angle);
92                 vector_list.push_back(Point(Angle::cos(dist1).get()*radius1,Angle::sin(dist1).get()*radius1));
93                 vector_list.push_back(Point(Angle::cos(dist2).get()*radius2,Angle::sin(dist2).get()*radius2));
94         }
95         clear();
96         add_polygon(vector_list);
97 }
98
99 bool
100 Star::set_param(const String & param, const ValueBase &value)
101 {
102         if(     param=="radius1" && value.same_as(radius1))
103         {
104                 value.put(&radius1);
105                 sync();
106                 return true;
107         }
108
109         if(     param=="radius2" && value.same_as(radius2))
110         {
111                 value.put(&radius2);
112                 sync();
113                 return true;
114         }
115
116         if(     param=="points" && value.same_as(points))
117         {
118                 value.put(&points);
119                 if(points<2)points=2;
120                 sync();
121                 return true;
122         }
123
124         if(     param=="angle" && value.same_as(angle))
125         {
126                 value.put(&angle);
127                 sync();
128                 return true;
129         }
130
131         if(param=="vector_list")
132                 return false;
133
134         return Layer_Polygon::set_param(param,value);
135 }
136
137 ValueBase
138 Star::get_param(const String& param)const
139 {
140         EXPORT(radius1);
141         EXPORT(radius2);
142         EXPORT(points);
143         EXPORT(angle);
144
145         EXPORT_NAME();
146         EXPORT_VERSION();
147
148         if(param=="vector_list")
149                 return ValueBase();
150
151         return Layer_Polygon::get_param(param);
152 }
153
154 Layer::Vocab
155 Star::get_param_vocab()const
156 {
157         Layer::Vocab ret(Layer_Polygon::get_param_vocab());
158
159         // Pop off the polygon parameter from the polygon vocab
160         ret.pop_back();
161
162         ret.push_back(ParamDesc("radius1")
163                 .set_local_name(_("Outer Radius"))
164                 .set_description(_("The radius of the outer points in the star"))
165                 .set_is_distance()
166                 .set_origin("offset")
167         );
168
169         ret.push_back(ParamDesc("radius2")
170                 .set_local_name(_("Inner Radius"))
171                 .set_description(_("The radius of the inner points in the star"))
172                 .set_is_distance()
173                 .set_origin("offset")
174         );
175
176         ret.push_back(ParamDesc("angle")
177                 .set_local_name(_("Angle"))
178                 .set_description(_("The orientation of the star"))
179                 .set_origin("offset")
180         );
181
182         ret.push_back(ParamDesc("points")
183                 .set_local_name(_("Points"))
184                 .set_description(_("The number of points in the star"))
185         );
186
187         return ret;
188 }