Allow the 'linear' convert type to work with colors and integers, like the 'timed...
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_linear.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_linear.cpp
3 **      \brief Template File
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 "valuenode_linear.h"
33 #include "valuenode_const.h"
34 #include "general.h"
35 #include "color.h"
36
37 #endif
38
39 /* === U S I N G =========================================================== */
40
41 using namespace std;
42 using namespace etl;
43 using namespace synfig;
44
45 /* === M A C R O S ========================================================= */
46
47 /* === G L O B A L S ======================================================= */
48
49 /* === P R O C E D U R E S ================================================= */
50
51 /* === M E T H O D S ======================================================= */
52
53 ValueNode_Linear::ValueNode_Linear(const ValueBase &value):
54         LinkableValueNode(value.get_type())
55 {
56         switch(get_type())
57         {
58         case ValueBase::TYPE_ANGLE:
59                 set_link("slope",ValueNode_Const::create(Angle::deg(0)));
60                 set_link("offset",ValueNode_Const::create(value.get(Angle())));
61                 break;
62         case ValueBase::TYPE_COLOR:
63                 set_link("slope",ValueNode_Const::create(Color(0,0,0,0)));
64                 set_link("offset",ValueNode_Const::create(value.get(Color())));
65                 break;
66         case ValueBase::TYPE_INTEGER:
67                 set_link("slope",ValueNode_Const::create(int(0)));
68                 set_link("offset",ValueNode_Const::create(value.get(int())));
69                 break;
70         case ValueBase::TYPE_REAL:
71                 set_link("slope",ValueNode_Const::create(Real(0)));
72                 set_link("offset",ValueNode_Const::create(value.get(Real())));
73                 break;
74         case ValueBase::TYPE_TIME:
75                 set_link("slope",ValueNode_Const::create(Time(0)));
76                 set_link("offset",ValueNode_Const::create(value.get(Time())));
77                 break;
78         case ValueBase::TYPE_VECTOR:
79                 set_link("slope",ValueNode_Const::create(Vector(0,0)));
80                 set_link("offset",ValueNode_Const::create(value.get(Vector())));
81                 break;
82         default:
83                 throw Exception::BadType(ValueBase::type_name(get_type()));
84         }
85
86         DCAST_HACK_ENABLE();
87 }
88
89 ValueNode_Linear*
90 ValueNode_Linear::create(const ValueBase &x)
91 {
92         return new ValueNode_Linear(x);
93 }
94
95 ValueNode_Linear::~ValueNode_Linear()
96 {
97         unlink_all();
98 }
99
100 ValueBase
101 ValueNode_Linear::operator()(Time t)const
102 {
103         switch(get_type())
104         {
105         case ValueBase::TYPE_ANGLE:
106                 return (*m_)(t).get( Angle())*t+(*b_)(t).get( Angle());
107         case ValueBase::TYPE_COLOR:
108                 return (*m_)(t).get( Color())*t+(*b_)(t).get( Color());
109         case ValueBase::TYPE_INTEGER:
110                 return static_cast<int>((*m_)(t).get(int())*t+(*b_)(t).get(int()) + 0.5f);
111         case ValueBase::TYPE_REAL:
112                 return (*m_)(t).get(  Real())*t+(*b_)(t).get(  Real());
113         case ValueBase::TYPE_TIME:
114                 return (*m_)(t).get(  Time())*t+(*b_)(t).get(  Time());
115         case ValueBase::TYPE_VECTOR:
116                 return (*m_)(t).get(Vector())*t+(*b_)(t).get(Vector());
117         default:
118                 assert(0);
119                 break;
120         }
121         return ValueBase();
122 }
123
124
125 String
126 ValueNode_Linear::get_name()const
127 {
128         return "linear";
129 }
130
131 String
132 ValueNode_Linear::get_local_name()const
133 {
134         return _("Linear");
135 }
136
137 bool
138 ValueNode_Linear::check_type(ValueBase::Type type)
139 {
140         return
141                 type==ValueBase::TYPE_ANGLE             ||
142                 type==ValueBase::TYPE_COLOR             ||
143                 type==ValueBase::TYPE_INTEGER   ||
144                 type==ValueBase::TYPE_REAL              ||
145                 type==ValueBase::TYPE_TIME              ||
146                 type==ValueBase::TYPE_VECTOR    ;
147 }
148
149 bool
150 ValueNode_Linear::set_link_vfunc(int i,ValueNode::Handle x)
151 {
152         assert(i==0 || i==1);
153         if(i==0)
154         {
155                 m_=x;
156                 signal_child_changed()(i);signal_value_changed()();
157                 return true;
158         }
159         if(i==1)
160         {
161                 b_=x;
162                 signal_child_changed()(i);signal_value_changed()();
163                 return true;
164         }
165         return false;
166 }
167
168 ValueNode::LooseHandle
169 ValueNode_Linear::get_link_vfunc(int i)const
170 {
171         assert(i==0 || i==1);
172         if(i==0) return m_;
173         if(i==1) return b_;
174         return 0;
175 }
176
177 int
178 ValueNode_Linear::link_count()const
179 {
180         return 2;
181 }
182
183 String
184 ValueNode_Linear::link_name(int i)const
185 {
186         assert(i==0 || i==1);
187         if(i==0) return "slope";
188         if(i==1) return "offset";
189         return String();
190 }
191
192 String
193 ValueNode_Linear::link_local_name(int i)const
194 {
195         assert(i==0 || i==1);
196         if(i==0)
197                 switch(get_type())
198                 {
199                 case ValueBase::TYPE_ANGLE:
200                 case ValueBase::TYPE_COLOR:
201                 case ValueBase::TYPE_INTEGER:
202                 case ValueBase::TYPE_REAL:
203                 case ValueBase::TYPE_TIME:
204                         return _("Rate");
205                 case ValueBase::TYPE_VECTOR:
206                 default:
207                         return _("Slope");
208                 }
209         if(i==1)
210                 return _("Offset");
211         return String();
212 }
213
214 int
215 ValueNode_Linear::get_link_index_from_name(const String &name)const
216 {
217         if(name=="slope")  return 0;
218         if(name=="offset") return 1;
219
220         throw Exception::BadLinkName(name);
221 }
222
223 LinkableValueNode*
224 ValueNode_Linear::create_new()const
225 {
226         return new ValueNode_Linear(get_type());
227 }