Added copyright lines for files I've edited this year.
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_linear.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_linear.cpp
3 **      \brief Implementation of the "Linear" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 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 "valuenode_linear.h"
34 #include "valuenode_const.h"
35 #include "general.h"
36 #include "color.h"
37 #include <ETL/misc>
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 ValueNode_Linear::ValueNode_Linear(const ValueBase &value):
56         LinkableValueNode(value.get_type())
57 {
58         switch(get_type())
59         {
60         case ValueBase::TYPE_ANGLE:
61                 set_link("slope",ValueNode_Const::create(Angle::deg(0)));
62                 set_link("offset",ValueNode_Const::create(value.get(Angle())));
63                 break;
64         case ValueBase::TYPE_COLOR:
65                 set_link("slope",ValueNode_Const::create(Color(0,0,0,0)));
66                 set_link("offset",ValueNode_Const::create(value.get(Color())));
67                 break;
68         case ValueBase::TYPE_INTEGER:
69                 set_link("slope",ValueNode_Const::create(int(0)));
70                 set_link("offset",ValueNode_Const::create(value.get(int())));
71                 break;
72         case ValueBase::TYPE_REAL:
73                 set_link("slope",ValueNode_Const::create(Real(0)));
74                 set_link("offset",ValueNode_Const::create(value.get(Real())));
75                 break;
76         case ValueBase::TYPE_TIME:
77                 set_link("slope",ValueNode_Const::create(Time(0)));
78                 set_link("offset",ValueNode_Const::create(value.get(Time())));
79                 break;
80         case ValueBase::TYPE_VECTOR:
81                 set_link("slope",ValueNode_Const::create(Vector(0,0)));
82                 set_link("offset",ValueNode_Const::create(value.get(Vector())));
83                 break;
84         default:
85                 throw Exception::BadType(ValueBase::type_local_name(get_type()));
86         }
87
88         DCAST_HACK_ENABLE();
89 }
90
91 LinkableValueNode*
92 ValueNode_Linear::create_new()const
93 {
94         return new ValueNode_Linear(get_type());
95 }
96
97 ValueNode_Linear*
98 ValueNode_Linear::create(const ValueBase &x)
99 {
100         return new ValueNode_Linear(x);
101 }
102
103 ValueNode_Linear::~ValueNode_Linear()
104 {
105         unlink_all();
106 }
107
108 ValueBase
109 ValueNode_Linear::operator()(Time t)const
110 {
111         switch(get_type())
112         {
113         case ValueBase::TYPE_ANGLE:
114                 return (*m_)(t).get( Angle())*t+(*b_)(t).get( Angle());
115         case ValueBase::TYPE_COLOR:
116                 return (*m_)(t).get( Color())*t+(*b_)(t).get( Color());
117         case ValueBase::TYPE_INTEGER:
118                 return round_to_int((*m_)(t).get(int())*t+(*b_)(t).get(int()));
119         case ValueBase::TYPE_REAL:
120                 return (*m_)(t).get(  Real())*t+(*b_)(t).get(  Real());
121         case ValueBase::TYPE_TIME:
122                 return (*m_)(t).get(  Time())*t+(*b_)(t).get(  Time());
123         case ValueBase::TYPE_VECTOR:
124                 return (*m_)(t).get(Vector())*t+(*b_)(t).get(Vector());
125         default:
126                 assert(0);
127                 break;
128         }
129         return ValueBase();
130 }
131
132
133 String
134 ValueNode_Linear::get_name()const
135 {
136         return "linear";
137 }
138
139 String
140 ValueNode_Linear::get_local_name()const
141 {
142         return _("Linear");
143 }
144
145 bool
146 ValueNode_Linear::check_type(ValueBase::Type type)
147 {
148         return
149                 type==ValueBase::TYPE_ANGLE             ||
150                 type==ValueBase::TYPE_COLOR             ||
151                 type==ValueBase::TYPE_INTEGER   ||
152                 type==ValueBase::TYPE_REAL              ||
153                 type==ValueBase::TYPE_TIME              ||
154                 type==ValueBase::TYPE_VECTOR    ;
155 }
156
157 bool
158 ValueNode_Linear::set_link_vfunc(int i,ValueNode::Handle value)
159 {
160         assert(i>=0 && i<link_count());
161
162         switch(i)
163         {
164         case 0: CHECK_TYPE_AND_SET_VALUE(m_, get_type());
165         case 1: CHECK_TYPE_AND_SET_VALUE(b_, get_type());
166         }
167         return false;
168 }
169
170 ValueNode::LooseHandle
171 ValueNode_Linear::get_link_vfunc(int i)const
172 {
173         assert(i>=0 && i<link_count());
174
175         if(i==0) return m_;
176         if(i==1) return b_;
177         return 0;
178 }
179
180 int
181 ValueNode_Linear::link_count()const
182 {
183         return 2;
184 }
185
186 String
187 ValueNode_Linear::link_name(int i)const
188 {
189         assert(i>=0 && i<link_count());
190
191         if(i==0) return "slope";
192         if(i==1) return "offset";
193         return String();
194 }
195
196 String
197 ValueNode_Linear::link_local_name(int i)const
198 {
199         assert(i>=0 && i<link_count());
200
201         if(i==0)
202                 switch(get_type())
203                 {
204                 case ValueBase::TYPE_ANGLE:
205                 case ValueBase::TYPE_COLOR:
206                 case ValueBase::TYPE_INTEGER:
207                 case ValueBase::TYPE_REAL:
208                 case ValueBase::TYPE_TIME:
209                         return _("Rate");
210                 case ValueBase::TYPE_VECTOR:
211                 default:
212                         return _("Slope");
213                 }
214         if(i==1)
215                 return _("Offset");
216         return String();
217 }
218
219 int
220 ValueNode_Linear::get_link_index_from_name(const String &name)const
221 {
222         if(name=="slope")  return 0;
223         if(name=="offset") return 1;
224
225         throw Exception::BadLinkName(name);
226 }