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 / region.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file region.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 "region.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 #include <synfig/valuenode_bline.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(Region);
61 SYNFIG_LAYER_SET_NAME(Region,"region");
62 SYNFIG_LAYER_SET_LOCAL_NAME(Region,_("Region"));
63 SYNFIG_LAYER_SET_CATEGORY(Region,_("Geometry"));
64 SYNFIG_LAYER_SET_VERSION(Region,"0.1");
65 SYNFIG_LAYER_SET_CVS_ID(Region,"$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 Region::Region()
74 {
75         clear();
76         vector<BLinePoint> bline_point_list;
77         bline_point_list.push_back(BLinePoint());
78         bline_point_list.push_back(BLinePoint());
79         bline_point_list.push_back(BLinePoint());
80         bline_point_list[0].set_vertex(Point(0,1));
81         bline_point_list[1].set_vertex(Point(0,-1));
82         bline_point_list[2].set_vertex(Point(1,0));
83         bline_point_list[0].set_tangent(bline_point_list[1].get_vertex()-bline_point_list[2].get_vertex()*0.5f);
84         bline_point_list[1].set_tangent(bline_point_list[2].get_vertex()-bline_point_list[0].get_vertex()*0.5f);
85         bline_point_list[2].set_tangent(bline_point_list[0].get_vertex()-bline_point_list[1].get_vertex()*0.5f);
86         bline_point_list[0].set_width(1.0f);
87         bline_point_list[1].set_width(1.0f);
88         bline_point_list[2].set_width(1.0f);
89         bline=bline_point_list;
90 }
91
92 void
93 Region::sync()
94 {
95         if(bline.get_contained_type()==ValueBase::TYPE_BLINEPOINT)
96         {
97                 segment_list=convert_bline_to_segment_list(bline);
98         }
99         else
100         {
101                 synfig::warning("Region: incorrect type on bline, layer disabled");
102                 clear();
103                 return;
104         }
105
106         if(segment_list.empty())
107         {
108                 synfig::warning("Region: segment_list is empty, layer disabled");
109                 clear();
110                 return;
111         }
112
113         bool looped = bline.get_loop();
114
115         Vector::value_type n;
116         etl::hermite<Vector> curve;
117         vector<Point> vector_list;
118
119         vector<Segment>::const_iterator iter=segment_list.begin();
120         //Vector                                                        last = iter->p1;
121
122         //make sure the shape has a clean slate for writing
123         //clear();
124
125         //and start off at the first point
126         //move_to(last[0],last[1]);
127
128         for(;iter!=segment_list.end();++iter)
129         {
130                 //connect them with a line if they aren't already joined
131                 /*if(iter->p1 != last)
132                 {
133                         line_to(iter->p1[0],iter->p1[1]);
134                 }
135
136                 //curve to the next end point
137                 curve_to(iter->p1[0] + iter->t1[0]/3.0,iter->p1[1] + iter->t1[1]/3.0,
138                                  iter->p2[0] - iter->t2[0]/3.0,iter->p2[1] - iter->t2[1]/3.0,
139                                  iter->p2[0],iter->p2[1]);
140
141                 last = iter->p2;*/
142
143                 if(iter->t1.is_equal_to(Vector(0,0)) && iter->t2.is_equal_to(Vector(0,0)))
144                 {
145                         vector_list.push_back(iter->p2);
146                 }
147                 else
148                 {
149                         curve.p1()=iter->p1;
150                         curve.t1()=iter->t1;
151                         curve.p2()=iter->p2;
152                         curve.t2()=iter->t2;
153                         curve.sync();
154
155                         for(n=0.0;n<1.0;n+=1.0/SAMPLES)
156                                 vector_list.push_back(curve(n));
157                 }
158         }
159
160         //add the starting point onto the end so it actually fits the shape, so we can be extra awesome...
161         if(!looped)
162                 vector_list.push_back(segment_list[0].p1);
163
164         clear();
165         add_polygon(vector_list);
166
167         /*close();
168         endpath();*/
169 }
170
171 bool
172 Region::set_param(const String & param, const ValueBase &value)
173 {
174         if(param=="segment_list")
175         {
176                 if(dynamic_param_list().count("segment_list"))
177                 {
178                         connect_dynamic_param("bline",dynamic_param_list().find("segment_list")->second);
179                         disconnect_dynamic_param("segment_list");
180                         synfig::warning("Region::set_param(): Updated valuenode connection to use the new \"bline\" parameter.");
181                 }
182                 else
183                         synfig::warning("Region::set_param(): The parameter \"segment_list\" is deprecated. Use \"bline\" instead.");
184         }
185
186         if(     (param=="segment_list" || param=="bline") && value.get_type()==ValueBase::TYPE_LIST)
187         {
188                 //if(value.get_contained_type()!=ValueBase::TYPE_BLINEPOINT)
189                 //      return false;
190
191                 bline=value;
192
193                 return true;
194         }
195
196 /*      if(     param=="segment_list" && value.get_type()==ValueBase::TYPE_LIST)
197         {
198                 if(value.get_contained_type()==ValueBase::TYPE_BLINEPOINT)
199                         segment_list=convert_bline_to_segment_list(value);
200                 else
201                 if(value.get_contained_type()==ValueBase::TYPE_SEGMENT)
202                         segment_list=value;
203                 else
204                 if(value.empty())
205                         segment_list.clear();
206                 else
207                         return false;
208                 sync();
209                 return true;
210         }
211         */
212         return Layer_Shape::set_param(param,value);
213 }
214
215 ValueBase
216 Region::get_param(const String& param)const
217 {
218         EXPORT(bline);
219         EXPORT_NAME();
220         EXPORT_VERSION();
221
222         return Layer_Shape::get_param(param);
223 }
224
225 Layer::Vocab
226 Region::get_param_vocab()const
227 {
228         Layer::Vocab ret(Layer_Shape::get_param_vocab());
229
230         ret.push_back(ParamDesc("bline")
231                 .set_local_name(_("Vertices"))
232                 .set_origin("offset")
233         );
234
235         return ret;
236 }
237
238 void
239 Region::set_time(Context context, Time time)const
240 {
241         const_cast<Region*>(this)->sync();
242         context.set_time(time);
243 }
244
245 void
246 Region::set_time(Context context, Time time, Vector pos)const
247 {
248         const_cast<Region*>(this)->sync();
249         context.set_time(time,pos);
250 }