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 **	Copyright (c) 2012-2013 Carlos López
11 **
12 **	This package is free software; you can redistribute it and/or
13 **	modify it under the terms of the GNU General Public License as
14 **	published by the Free Software Foundation; either version 2 of
15 **	the License, or (at your option) any later version.
16 **
17 **	This package is distributed in the hope that it will be useful,
18 **	but WITHOUT ANY WARRANTY; without even the implied warranty of
19 **	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 **	General Public License for more details.
21 **	\endlegal
22 **
23 ** === N O T E S ===========================================================
24 **
25 ** ========================================================================= */
26 
27 /* === H E A D E R S ======================================================= */
28 
29 #ifdef USING_PCH
30 #	include "pch.h"
31 #else
32 #ifdef HAVE_CONFIG_H
33 #	include <config.h>
34 #endif
35 
36 #include "star.h"
37 #include <ETL/stringf>
38 #include <ETL/bezier>
39 #include <ETL/hermite>
40 
41 #include <synfig/localization.h>
42 #include <synfig/general.h>
43 
44 #include <synfig/string.h>
45 #include <synfig/time.h>
46 #include <synfig/context.h>
47 #include <synfig/paramdesc.h>
48 #include <synfig/renddesc.h>
49 #include <synfig/surface.h>
50 #include <synfig/value.h>
51 #include <synfig/valuenode.h>
52 #include <synfig/segment.h>
53 
54 #endif
55 
56 using namespace etl;
57 
58 /* === M A C R O S ========================================================= */
59 
60 #define SAMPLES		75
61 
62 /* === G L O B A L S ======================================================= */
63 
64 SYNFIG_LAYER_INIT(Star);
65 SYNFIG_LAYER_SET_NAME(Star,"star");
66 SYNFIG_LAYER_SET_LOCAL_NAME(Star,N_("Star"));
67 SYNFIG_LAYER_SET_CATEGORY(Star,N_("Geometry"));
68 SYNFIG_LAYER_SET_VERSION(Star,"0.1");
69 SYNFIG_LAYER_SET_CVS_ID(Star,"$Id$");
70 
71 /* === P R O C E D U R E S ================================================= */
72 
73 /* === M E T H O D S ======================================================= */
74 
75 /* === E N T R Y P O I N T ================================================= */
76 
Star()77 Star::Star():
78 	param_radius1(ValueBase(Real(1.0))),
79 	param_radius2(ValueBase(Real(0.38))),
80 	param_points(ValueBase(int(5))),
81 	param_angle(ValueBase(Angle::deg(90))),
82 	param_regular_polygon(ValueBase(bool(false)))
83 {
84 	sync();
85 	SET_INTERPOLATION_DEFAULTS();
86 	SET_STATIC_DEFAULTS();
87 }
88 
89 void
sync_vfunc()90 Star::sync_vfunc()
91 {
92 	Angle angle = param_angle.get(Angle());
93 	int points = param_points.get(int(0));
94 	Real radius1 = param_radius1.get(Real());
95 	Real radius2 = param_radius2.get(Real());
96 	bool regular_polygon = param_regular_polygon.get(bool(true));
97 
98 	Angle dist_between_points(Angle::rot(1)/float(points));
99 	std::vector<Point> vector_list;
100 
101 	int i;
102 	for(i=0;i<points;i++)
103 	{
104 		Angle dist1(dist_between_points*i+angle);
105 		Angle dist2(dist_between_points*i+dist_between_points/2+angle);
106 		vector_list.push_back(Point(Angle::cos(dist1).get()*radius1,Angle::sin(dist1).get()*radius1));
107 		if (!regular_polygon)
108 			vector_list.push_back(Point(Angle::cos(dist2).get()*radius2,Angle::sin(dist2).get()*radius2));
109 	}
110 	set_stored_polygon(vector_list);
111 }
112 
113 bool
set_shape_param(const String & param,const ValueBase & value)114 Star::set_shape_param(const String &param, const ValueBase &value)
115 {
116 	IMPORT_VALUE(param_radius1);
117 	IMPORT_VALUE(param_radius2);
118 	IMPORT_VALUE_PLUS(param_points,
119 		  {
120 			  int points(param_points.get(int(0)));
121 			  if(points<2)points=2;
122 			  param_points.set(points);
123 		  }
124 	);
125 	IMPORT_VALUE(param_angle);
126 	IMPORT_VALUE(param_regular_polygon);
127 
128 	// Skip polygon parameters
129 	return Layer_Shape::set_shape_param(param,value);
130 }
131 
132 bool
set_param(const String & param,const ValueBase & value)133 Star::set_param(const String & param, const ValueBase &value)
134 {
135 	// Skip polygon parameters
136 	return Layer_Shape::set_param(param,value);
137 }
138 
139 ValueBase
get_param(const String & param) const140 Star::get_param(const String& param)const
141 {
142 	EXPORT_VALUE(param_radius1);
143 	EXPORT_VALUE(param_radius2);
144 	EXPORT_VALUE(param_points);
145 	EXPORT_VALUE(param_angle);
146 	EXPORT_VALUE(param_regular_polygon);
147 
148 	EXPORT_NAME();
149 	EXPORT_VERSION();
150 
151 	// Skip polygon parameters
152 	return Layer_Shape::get_param(param);
153 }
154 
155 Layer::Vocab
get_param_vocab() const156 Star::get_param_vocab()const
157 {
158 	// Skip polygon parameters
159 	Layer::Vocab ret(Layer_Shape::get_param_vocab());
160 
161 	ret.push_back(ParamDesc("radius1")
162 		.set_local_name(_("Outer Radius"))
163 		.set_description(_("The radius of the outer points in the star"))
164 		.set_is_distance()
165 		.set_origin("origin")
166 	);
167 
168 	ret.push_back(ParamDesc("radius2")
169 		.set_local_name(_("Inner Radius"))
170 		.set_description(_("The radius of the inner points in the star"))
171 		.set_is_distance()
172 		.set_origin("origin")
173 	);
174 
175 	ret.push_back(ParamDesc("angle")
176 		.set_local_name(_("Angle"))
177 		.set_description(_("The orientation of the star"))
178 		.set_origin("origin")
179 	);
180 
181 	ret.push_back(ParamDesc("points")
182 		.set_local_name(_("Points"))
183 		.set_description(_("The number of points in the star"))
184 	);
185 
186 	ret.push_back(ParamDesc("regular_polygon")
187 		.set_local_name(_("Regular Polygon"))
188 		.set_description(_("Whether to draw a star or a regular polygon"))
189 	);
190 
191 	return ret;
192 }
193 
194