Use LinkableValueNode members functions when possible in the derived valuenodes.
[synfig.git] / synfig-core / 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         Vocab ret(get_children_vocab());
59         set_children_vocab(ret);
60         set_link("color1",ValueNode_Const::create(Color::alpha()));
61         set_link("color2",ValueNode_Const::create(Color::black()));
62         set_link("stripes",stripes_=ValueNode_Const::create(int(5)));
63         set_link("width",ValueNode_Const::create(0.5));
64 }
65
66 LinkableValueNode*
67 ValueNode_Stripes::create_new()const
68 {
69         return new ValueNode_Stripes();
70 }
71
72 ValueNode_Stripes*
73 ValueNode_Stripes::create(const ValueBase& x)
74 {
75         ValueBase::Type id(x.get_type());
76
77         if(id!=ValueBase::TYPE_GRADIENT)
78         {
79                 assert(0);
80                 throw runtime_error(String(_("Stripes"))+_(":Bad type ")+ValueBase::type_local_name(id));
81         }
82
83         ValueNode_Stripes* value_node=new ValueNode_Stripes();
84
85         assert(value_node->get_type()==id);
86
87         return value_node;
88 }
89
90 synfig::ValueNode_Stripes::~ValueNode_Stripes()
91 {
92         unlink_all();
93 }
94
95 synfig::ValueBase
96 synfig::ValueNode_Stripes::operator()(Time t)const
97 {
98         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
99                 printf("%s:%d operator()\n", __FILE__, __LINE__);
100
101         const int total((*stripes_)(t).get(int()));
102         int i;
103         Gradient ret;
104
105         if(total<=0)
106                 return ret;
107
108         const Color color1((*color1_)(t).get(Color()));
109         const Color color2((*color2_)(t).get(Color()));
110         const float width(max(0.0,min(1.0,(*width_)(t).get(Real()))));
111
112         const float stripe_width_a(width/total);
113         const float stripe_width_b((1.0-width)/total);
114
115         for(i=0;i<total;i++)
116         {
117                 float pos(float(i)/total+stripe_width_b/2);
118                 ret.push_back(Gradient::CPoint(pos,color1));
119                 ret.push_back(Gradient::CPoint(pos,color2));
120                 pos+=stripe_width_a;
121                 ret.push_back(Gradient::CPoint(pos,color2));
122                 ret.push_back(Gradient::CPoint(pos,color1));
123         }
124         return ret;
125 }
126
127 bool
128 ValueNode_Stripes::set_link_vfunc(int i,ValueNode::Handle value)
129 {
130         assert(i>=0 && i<link_count());
131
132         switch(i)
133         {
134         case 0: CHECK_TYPE_AND_SET_VALUE(color1_,  ValueBase::TYPE_COLOR);
135         case 1: CHECK_TYPE_AND_SET_VALUE(color2_,  ValueBase::TYPE_COLOR);
136         case 2: CHECK_TYPE_AND_SET_VALUE(stripes_, ValueBase::TYPE_INTEGER);
137         case 3: CHECK_TYPE_AND_SET_VALUE(width_,   ValueBase::TYPE_REAL);
138         }
139         return false;
140 }
141
142 ValueNode::LooseHandle
143 ValueNode_Stripes::get_link_vfunc(int i)const
144 {
145         assert(i>=0 && i<link_count());
146
147         switch(i)
148         {
149                 case 0:
150                         return color1_;
151                 case 1:
152                         return color2_;
153                 case 2:
154                         return stripes_;
155                 case 3:
156                         return width_;
157         }
158         return 0;
159 }
160
161 String
162 ValueNode_Stripes::get_name()const
163 {
164         return "stripes";
165 }
166
167 String
168 ValueNode_Stripes::get_local_name()const
169 {
170         return _("Stripes");
171 }
172
173 bool
174 ValueNode_Stripes::check_type(ValueBase::Type type)
175 {
176         return type==ValueBase::TYPE_GRADIENT;
177 }
178
179 LinkableValueNode::Vocab
180 ValueNode_Stripes::get_children_vocab_vfunc()const
181 {
182         if(children_vocab.size())
183                 return children_vocab;
184
185         LinkableValueNode::Vocab ret;
186
187         ret.push_back(ParamDesc(ValueBase(),"color1")
188                 .set_local_name(_("Color 1"))
189                 .set_description(_("One color of the gradient stripes"))
190         );
191
192         ret.push_back(ParamDesc(ValueBase(),"color2")
193                 .set_local_name(_("Color 2"))
194                 .set_description(_("Other color of the gradient stripes"))
195         );
196
197                 ret.push_back(ParamDesc(ValueBase(),"stripes")
198                 .set_local_name(_("Stripe Count"))
199                 .set_description(_("Number of stripes in the gradient"))
200         );
201
202                 ret.push_back(ParamDesc(ValueBase(),"width")
203                 .set_local_name(_("Width"))
204                 .set_description(_("Width of stripes in the gradient between [0,1]"))
205         );
206
207         return ret;
208 }