d0e3b5830efb88841f372a654b989eed0245986b
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_reciprocal.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_reciprocal.cpp
3 **      \brief Implementation of the "Reciprocal" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 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_reciprocal.h"
34 #include "valuenode_const.h"
35 #include "general.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_Reciprocal::ValueNode_Reciprocal(const ValueBase::Type &x):
54         LinkableValueNode(x)
55 {
56 }
57
58 ValueNode_Reciprocal::ValueNode_Reciprocal(const ValueNode::Handle &x):
59         LinkableValueNode(x->get_type())
60 {
61         set_link("link", x);
62         set_link("epsilon",  ValueNode_Const::create(Real(0.000001)));
63         set_link("infinite", ValueNode_Const::create(Real(999999.0)));
64 }
65
66 ValueNode_Reciprocal*
67 ValueNode_Reciprocal::create(const ValueBase &x)
68 {
69         return new ValueNode_Reciprocal(ValueNode_Const::create(x));
70 }
71
72 LinkableValueNode*
73 ValueNode_Reciprocal::create_new()const
74 {
75         return new ValueNode_Reciprocal(get_type());
76 }
77
78 ValueNode_Reciprocal::~ValueNode_Reciprocal()
79 {
80         unlink_all();
81 }
82
83 bool
84 ValueNode_Reciprocal::set_link_vfunc(int i,ValueNode::Handle value)
85 {
86         assert(i>=0 && i<link_count());
87
88         switch(i)
89         {
90         case 0: CHECK_TYPE_AND_SET_VALUE(link_,     ValueBase::TYPE_REAL);
91         case 1: CHECK_TYPE_AND_SET_VALUE(epsilon_,  ValueBase::TYPE_REAL);
92         case 2: CHECK_TYPE_AND_SET_VALUE(infinite_, ValueBase::TYPE_REAL);
93         }
94         return false;
95 }
96
97 ValueNode::LooseHandle
98 ValueNode_Reciprocal::get_link_vfunc(int i)const
99 {
100         assert(i>=0 && i<link_count());
101
102         if(i==0) return link_;
103         if(i==1) return epsilon_;
104         if(i==2) return infinite_;
105
106         return 0;
107 }
108
109 int
110 ValueNode_Reciprocal::link_count()const
111 {
112         return 3;
113 }
114
115 String
116 ValueNode_Reciprocal::link_local_name(int i)const
117 {
118         assert(i>=0 && i<link_count());
119
120         if(i==0) return _("Link");
121         if(i==1) return _("Epsilon");
122         if(i==2) return _("Infinite");
123         return String();
124 }
125
126 String
127 ValueNode_Reciprocal::link_name(int i)const
128 {
129         assert(i>=0 && i<link_count());
130
131         if(i==0) return "link";
132         if(i==1) return "epsilon";
133         if(i==2) return "infinite";
134         return String();
135 }
136
137 int
138 ValueNode_Reciprocal::get_link_index_from_name(const String &name)const
139 {
140         if(name=="link")     return 0;
141         if(name=="epsilon")  return 1;
142         if(name=="infinite") return 2;
143
144         throw Exception::BadLinkName(name);
145 }
146
147 ValueBase
148 ValueNode_Reciprocal::operator()(Time t)const
149 {
150         Real link     = (*link_)    (t).get(Real());
151         Real epsilon  = (*epsilon_) (t).get(Real());
152         Real infinite = (*infinite_)(t).get(Real());
153
154         if (epsilon < 0.00000001)
155                 epsilon = 0.00000001;
156
157         if (abs(link) < epsilon)
158                 if (link < 0)
159                         return -infinite;
160                 else
161                         return infinite;
162         else
163                 return 1.0f / link;
164 }
165
166 String
167 ValueNode_Reciprocal::get_name()const
168 {
169         return "reciprocal";
170 }
171
172 String
173 ValueNode_Reciprocal::get_local_name()const
174 {
175         return _("Reciprocal");
176 }
177
178 bool
179 ValueNode_Reciprocal::check_type(ValueBase::Type type)
180 {
181         return type==ValueBase::TYPE_REAL;
182 }