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