Initial Stable Commit
[synfig.git] / synfig-core / trunk / src / modules / mod_geometry / star.cpp
1 /*! ========================================================================
2 ** Sinfg
3 ** Template File
4 ** $Id: star.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $
5 **
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
7 **
8 ** This software and associated documentation
9 ** are CONFIDENTIAL and PROPRIETARY property of
10 ** the above-mentioned copyright holder.
11 **
12 ** You may not copy, print, publish, or in any
13 ** other way distribute this software without
14 ** a prior written agreement with
15 ** the copyright holder.
16 **
17 ** === N O T E S ===========================================================
18 **
19 ** ========================================================================= */
20
21 /* === H E A D E R S ======================================================= */
22
23 #ifdef USING_PCH
24 #       include "pch.h"
25 #else
26 #ifdef HAVE_CONFIG_H
27 #       include <config.h>
28 #endif
29
30 #include "star.h"
31 #include <ETL/stringf>
32 #include <ETL/bezier>
33 #include <ETL/hermite>
34
35 #include <sinfg/string.h>
36 #include <sinfg/time.h>
37 #include <sinfg/context.h>
38 #include <sinfg/paramdesc.h>
39 #include <sinfg/renddesc.h>
40 #include <sinfg/surface.h>
41 #include <sinfg/value.h>
42 #include <sinfg/valuenode.h>
43 #include <sinfg/segment.h>
44
45 #endif
46
47 using namespace etl;
48
49 /* === M A C R O S ========================================================= */
50
51 #define SAMPLES         75
52
53 /* === G L O B A L S ======================================================= */
54
55 SINFG_LAYER_INIT(Star);
56 SINFG_LAYER_SET_NAME(Star,"star");
57 SINFG_LAYER_SET_LOCAL_NAME(Star,_("Star"));
58 SINFG_LAYER_SET_CATEGORY(Star,_("Geometry"));
59 SINFG_LAYER_SET_VERSION(Star,"0.1");
60 SINFG_LAYER_SET_CVS_ID(Star,"$Id: star.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $");
61
62 /* === P R O C E D U R E S ================================================= */
63
64 /* === M E T H O D S ======================================================= */
65
66 /* === E N T R Y P O I N T ================================================= */
67
68 Star::Star():
69         radius1(1.0),
70         radius2(0.38),
71         points(5),
72         angle(Angle::deg(90))
73 {
74         sync();
75 }
76
77 void
78 Star::sync()
79 {
80         Angle dist_between_points(Angle::rot(1)/float(points));
81         std::vector<Point> vector_list;
82         
83         int i;
84         for(i=0;i<points;i++)
85         {
86                 Angle dist1(dist_between_points*i+angle);
87                 Angle dist2(dist_between_points*i+dist_between_points/2+angle);
88                 vector_list.push_back(Point(Angle::cos(dist1).get()*radius1,Angle::sin(dist1).get()*radius1));
89                 vector_list.push_back(Point(Angle::cos(dist2).get()*radius2,Angle::sin(dist2).get()*radius2));
90         }
91         clear();
92         add_polygon(vector_list);
93 }
94
95 bool
96 Star::set_param(const String & param, const ValueBase &value)
97 {
98         if(     param=="radius1" && value.same_as(radius1))
99         {
100                 value.put(&radius1);
101                 sync();
102                 return true;
103         }
104
105         if(     param=="radius2" && value.same_as(radius2))
106         {
107                 value.put(&radius2);
108                 sync();
109                 return true;
110         }
111
112         if(     param=="points" && value.same_as(points))
113         {
114                 value.put(&points);
115                 if(points<2)points=2;
116                 sync();
117                 return true;
118         }
119
120         if(     param=="angle" && value.same_as(angle))
121         {
122                 value.put(&angle);
123                 sync();
124                 return true;
125         }
126
127         if(param=="vector_list")
128                 return false;
129
130         return Layer_Polygon::set_param(param,value);
131 }
132
133 ValueBase
134 Star::get_param(const String& param)const
135 {
136         EXPORT(radius1);
137         EXPORT(radius2);
138         EXPORT(points);
139         EXPORT(angle);
140
141         EXPORT_NAME();
142         EXPORT_VERSION();
143
144         if(param=="vector_list")
145                 return ValueBase();     
146
147         return Layer_Polygon::get_param(param);
148 }
149
150 Layer::Vocab
151 Star::get_param_vocab()const
152 {
153         Layer::Vocab ret(Layer_Polygon::get_param_vocab());
154
155         // Pop off the polygon parameter from the polygon vocab
156         ret.pop_back();
157
158         ret.push_back(ParamDesc("radius1")
159                 .set_local_name(_("Outer Radius"))
160                 .set_description(_("The radius of the outer points in the star"))
161                 .set_is_distance()
162                 .set_origin("offset")
163         );
164
165         ret.push_back(ParamDesc("radius2")
166                 .set_local_name(_("Inner Radius"))
167                 .set_description(_("The radius of the inner points in the star"))
168                 .set_is_distance()
169                 .set_origin("offset")
170         );
171
172         ret.push_back(ParamDesc("angle")
173                 .set_local_name(_("Angle"))
174                 .set_description(_("The orientation of the star"))
175                 .set_origin("offset")
176         );
177
178         ret.push_back(ParamDesc("points")
179                 .set_local_name(_("Points"))
180                 .set_description(_("The number of points in the star"))
181         );
182
183         return ret;
184 }