Added copyright lines for files I've edited this year.
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_stripes.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_stripes.cpp
3 **      \brief Implementation of the "Stripes" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2008 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "general.h"
34 #include "valuenode_stripes.h"
35 #include "valuenode_const.h"
36 #include <stdexcept>
37 #include "color.h"
38 #include "gradient.h"
39
40 #endif
41
42 /* === U S I N G =========================================================== */
43
44 using namespace std;
45 using namespace etl;
46 using namespace synfig;
47
48 /* === M A C R O S ========================================================= */
49
50 /* === G L O B A L S ======================================================= */
51
52 /* === P R O C E D U R E S ================================================= */
53
54 /* === M E T H O D S ======================================================= */
55
56 synfig::ValueNode_Stripes::ValueNode_Stripes():LinkableValueNode(synfig::ValueBase::TYPE_GRADIENT)
57 {
58         set_link("color1",ValueNode_Const::create(Color::alpha()));
59         set_link("color2",ValueNode_Const::create(Color::black()));
60         set_link("stripes",stripes_=ValueNode_Const::create(int(5)));
61         set_link("width",ValueNode_Const::create(0.5));
62 }
63
64 LinkableValueNode*
65 ValueNode_Stripes::create_new()const
66 {
67         return new ValueNode_Stripes();
68 }
69
70 ValueNode_Stripes*
71 ValueNode_Stripes::create(const ValueBase& x)
72 {
73         ValueBase::Type id(x.get_type());
74
75         if(id!=ValueBase::TYPE_GRADIENT)
76         {
77                 assert(0);
78                 throw runtime_error(String(_("Stripes"))+_(":Bad type ")+ValueBase::type_local_name(id));
79         }
80
81         ValueNode_Stripes* value_node=new ValueNode_Stripes();
82
83         assert(value_node->get_type()==id);
84
85         return value_node;
86 }
87
88 synfig::ValueNode_Stripes::~ValueNode_Stripes()
89 {
90         unlink_all();
91 }
92
93 synfig::ValueBase
94 synfig::ValueNode_Stripes::operator()(Time t)const
95 {
96         const int total((*stripes_)(t).get(int()));
97         int i;
98         Gradient ret;
99
100         if(total<=0)
101                 return ret;
102
103         const Color color1((*color1_)(t).get(Color()));
104         const Color color2((*color2_)(t).get(Color()));
105         const float width(max(0.0,min(1.0,(*width_)(t).get(Real()))));
106
107         const float stripe_width_a(width/total);
108         const float stripe_width_b((1.0-width)/total);
109
110         for(i=0;i<total;i++)
111         {
112                 float pos(float(i)/total+stripe_width_b/2);
113                 ret.push_back(Gradient::CPoint(pos,color1));
114                 ret.push_back(Gradient::CPoint(pos,color2));
115                 pos+=stripe_width_a;
116                 ret.push_back(Gradient::CPoint(pos,color2));
117                 ret.push_back(Gradient::CPoint(pos,color1));
118         }
119         return ret;
120 }
121
122 bool
123 ValueNode_Stripes::set_link_vfunc(int i,ValueNode::Handle value)
124 {
125         assert(i>=0 && i<link_count());
126
127         switch(i)
128         {
129         case 0: CHECK_TYPE_AND_SET_VALUE(color1_,  ValueBase::TYPE_COLOR);
130         case 1: CHECK_TYPE_AND_SET_VALUE(color2_,  ValueBase::TYPE_COLOR);
131         case 2: CHECK_TYPE_AND_SET_VALUE(stripes_, ValueBase::TYPE_INTEGER);
132         case 3: CHECK_TYPE_AND_SET_VALUE(width_,   ValueBase::TYPE_REAL);
133         }
134         return false;
135 }
136
137 ValueNode::LooseHandle
138 ValueNode_Stripes::get_link_vfunc(int i)const
139 {
140         assert(i>=0 && i<link_count());
141
142         switch(i)
143         {
144                 case 0:
145                         return color1_;
146                 case 1:
147                         return color2_;
148                 case 2:
149                         return stripes_;
150                 case 3:
151                         return width_;
152         }
153         return 0;
154 }
155
156 int
157 ValueNode_Stripes::link_count()const
158 {
159         return 4;
160 }
161
162 String
163 ValueNode_Stripes::link_local_name(int i)const
164 {
165         assert(i>=0 && i<link_count());
166
167         switch(i)
168         {
169                 case 0:
170                         return _("Color 1");
171                 case 1:
172                         return _("Color 2");
173                 case 2:
174                         return _("Stripe Count");
175                 case 3:
176                         return _("Width");
177                 default:
178                         return String();
179         }
180 }
181
182 String
183 ValueNode_Stripes::link_name(int i)const
184 {
185         assert(i>=0 && i<link_count());
186
187         switch(i)
188         {
189                 case 0:
190                         return "color1";
191                 case 1:
192                         return "color2";
193                 case 2:
194                         return "stripes";
195                 case 3:
196                         return "width";
197                 default:
198                         return String();
199         }
200 }
201
202 int
203 ValueNode_Stripes::get_link_index_from_name(const String &name)const
204 {
205         if(name=="color1")
206                 return 0;
207         if(name=="color2")
208                 return 1;
209         if(name=="stripes")
210                 return 2;
211         if(name=="width")
212                 return 3;
213         throw Exception::BadLinkName(name);
214 }
215
216 String
217 ValueNode_Stripes::get_name()const
218 {
219         return "stripes";
220 }
221
222 String
223 ValueNode_Stripes::get_local_name()const
224 {
225         return _("Stripes");
226 }
227
228 bool
229 ValueNode_Stripes::check_type(ValueBase::Type type)
230 {
231         return type==ValueBase::TYPE_GRADIENT;
232 }