55e6a6631623b49608936ec367be61dc416e1830
[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 **
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 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "general.h"
33 #include "valuenode_stripes.h"
34 #include "valuenode_const.h"
35 #include <stdexcept>
36 #include "color.h"
37 #include "gradient.h"
38
39 #endif
40
41 /* === U S I N G =========================================================== */
42
43 using namespace std;
44 using namespace etl;
45 using namespace synfig;
46
47 /* === M A C R O S ========================================================= */
48
49 /* === G L O B A L S ======================================================= */
50
51 /* === P R O C E D U R E S ================================================= */
52
53 /* === M E T H O D S ======================================================= */
54
55 synfig::ValueNode_Stripes::ValueNode_Stripes():LinkableValueNode(synfig::ValueBase::TYPE_GRADIENT)
56 {
57         set_link("color1",ValueNode_Const::create(Color::alpha()));
58         set_link("color2",ValueNode_Const::create(Color::black()));
59         set_link("stripes",stripes_=ValueNode_Const::create(int(5)));
60         set_link("width",ValueNode_Const::create(0.5));
61 }
62
63 LinkableValueNode*
64 ValueNode_Stripes::create_new()const
65 {
66         return new ValueNode_Stripes();
67 }
68
69 ValueNode_Stripes*
70 ValueNode_Stripes::create(const ValueBase& x)
71 {
72         ValueBase::Type id(x.get_type());
73
74         if(id!=ValueBase::TYPE_GRADIENT)
75         {
76                 assert(0);
77                 throw runtime_error(String(_("Stripes"))+_(":Bad type ")+ValueBase::type_local_name(id));
78         }
79
80         ValueNode_Stripes* value_node=new ValueNode_Stripes();
81
82         assert(value_node->get_type()==id);
83
84         return value_node;
85 }
86
87 synfig::ValueNode_Stripes::~ValueNode_Stripes()
88 {
89         unlink_all();
90 }
91
92 synfig::ValueBase
93 synfig::ValueNode_Stripes::operator()(Time t)const
94 {
95         const int total((*stripes_)(t).get(int()));
96         int i;
97         Gradient ret;
98
99         if(total<=0)
100                 return ret;
101
102         const Color color1((*color1_)(t).get(Color()));
103         const Color color2((*color2_)(t).get(Color()));
104         const float width(max(0.0,min(1.0,(*width_)(t).get(Real()))));
105
106         const float stripe_width_a(width/total);
107         const float stripe_width_b((1.0-width)/total);
108
109         for(i=0;i<total;i++)
110         {
111                 float pos(float(i)/total+stripe_width_b/2);
112                 ret.push_back(Gradient::CPoint(pos,color1));
113                 ret.push_back(Gradient::CPoint(pos,color2));
114                 pos+=stripe_width_a;
115                 ret.push_back(Gradient::CPoint(pos,color2));
116                 ret.push_back(Gradient::CPoint(pos,color1));
117         }
118         return ret;
119 }
120
121 bool
122 ValueNode_Stripes::set_link_vfunc(int i,ValueNode::Handle value)
123 {
124         assert(i>=0 && i<link_count());
125
126         switch(i)
127         {
128         case 0: CHECK_TYPE_AND_SET_VALUE(color1_,  ValueBase::TYPE_COLOR);
129         case 1: CHECK_TYPE_AND_SET_VALUE(color2_,  ValueBase::TYPE_COLOR);
130         case 2: CHECK_TYPE_AND_SET_VALUE(stripes_, ValueBase::TYPE_INTEGER);
131         case 3: CHECK_TYPE_AND_SET_VALUE(width_,   ValueBase::TYPE_REAL);
132         }
133         return false;
134 }
135
136 ValueNode::LooseHandle
137 ValueNode_Stripes::get_link_vfunc(int i)const
138 {
139         assert(i>=0 && i<link_count());
140
141         switch(i)
142         {
143                 case 0:
144                         return color1_;
145                 case 1:
146                         return color2_;
147                 case 2:
148                         return stripes_;
149                 case 3:
150                         return width_;
151         }
152         return 0;
153 }
154
155 int
156 ValueNode_Stripes::link_count()const
157 {
158         return 4;
159 }
160
161 String
162 ValueNode_Stripes::link_local_name(int i)const
163 {
164         assert(i>=0 && i<link_count());
165
166         switch(i)
167         {
168                 case 0:
169                         return _("Color 1");
170                 case 1:
171                         return _("Color 2");
172                 case 2:
173                         return _("Stripe Count");
174                 case 3:
175                         return _("Width");
176                 default:
177                         return String();
178         }
179 }
180
181 String
182 ValueNode_Stripes::link_name(int i)const
183 {
184         assert(i>=0 && i<link_count());
185
186         switch(i)
187         {
188                 case 0:
189                         return "color1";
190                 case 1:
191                         return "color2";
192                 case 2:
193                         return "stripes";
194                 case 3:
195                         return "width";
196                 default:
197                         return String();
198         }
199 }
200
201 int
202 ValueNode_Stripes::get_link_index_from_name(const String &name)const
203 {
204         if(name=="color1")
205                 return 0;
206         if(name=="color2")
207                 return 1;
208         if(name=="stripes")
209                 return 2;
210         if(name=="width")
211                 return 3;
212         throw Exception::BadLinkName(name);
213 }
214
215 String
216 ValueNode_Stripes::get_name()const
217 {
218         return "stripes";
219 }
220
221 String
222 ValueNode_Stripes::get_local_name()const
223 {
224         return _("Stripes");
225 }
226
227 bool
228 ValueNode_Stripes::check_type(ValueBase::Type type)
229 {
230         return type==ValueBase::TYPE_GRADIENT;
231 }