Use link_count from children vocabulary and return the stored vocabulary if already...
[synfig.git] / synfig-core / src / synfig / valuenode_exp.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_exp.cpp
3 **      \brief Implementation of the "Exponential" 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 "valuenode_exp.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_Exp::ValueNode_Exp(const ValueBase &value):
54         LinkableValueNode(value.get_type())
55 {
56         Vocab ret(get_children_vocab());
57         set_children_vocab(ret);
58         switch(value.get_type())
59         {
60         case ValueBase::TYPE_REAL:
61                 set_link("exp",ValueNode_Const::create(Real(0)));
62                 set_link("scale",ValueNode_Const::create(value.get(Real())));
63                 break;
64         default:
65                 throw Exception::BadType(ValueBase::type_local_name(value.get_type()));
66         }
67 }
68
69 LinkableValueNode*
70 ValueNode_Exp::create_new()const
71 {
72         return new ValueNode_Exp(get_type());
73 }
74
75 ValueNode_Exp*
76 ValueNode_Exp::create(const ValueBase &x)
77 {
78         return new ValueNode_Exp(x);
79 }
80
81 ValueNode_Exp::~ValueNode_Exp()
82 {
83         unlink_all();
84 }
85
86 ValueBase
87 ValueNode_Exp::operator()(Time t)const
88 {
89         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
90                 printf("%s:%d operator()\n", __FILE__, __LINE__);
91
92         return (exp((*exp_)(t).get(Real())) *
93                         (*scale_)(t).get(Real()));
94 }
95
96 String
97 ValueNode_Exp::get_name()const
98 {
99         return "exp";
100 }
101
102 String
103 ValueNode_Exp::get_local_name()const
104 {
105         return _("Exponential");
106 }
107
108 bool
109 ValueNode_Exp::set_link_vfunc(int i,ValueNode::Handle value)
110 {
111         assert(i>=0 && i<link_count());
112
113         switch(i)
114         {
115         case 0: CHECK_TYPE_AND_SET_VALUE(exp_,   ValueBase::TYPE_REAL);
116         case 1: CHECK_TYPE_AND_SET_VALUE(scale_, ValueBase::TYPE_REAL);
117         }
118         return false;
119 }
120
121 ValueNode::LooseHandle
122 ValueNode_Exp::get_link_vfunc(int i)const
123 {
124         assert(i>=0 && i<link_count());
125
126         if(i==0)
127                 return exp_;
128         if(i==1)
129                 return scale_;
130
131         return 0;
132 }
133
134 int
135 ValueNode_Exp::link_count()const
136 {
137         return 2;
138 }
139
140 String
141 ValueNode_Exp::link_name(int i)const
142 {
143         assert(i>=0 && i<link_count());
144
145         if(i==0)
146                 return "exp";
147         if(i==1)
148                 return "scale";
149         return String();
150 }
151
152 String
153 ValueNode_Exp::link_local_name(int i)const
154 {
155         assert(i>=0 && i<link_count());
156
157         if(i==0)
158                 return _("Exponent");
159         if(i==1)
160                 return _("Scale");
161         return String();
162 }
163
164 int
165 ValueNode_Exp::get_link_index_from_name(const String &name)const
166 {
167         if(name=="exp")
168                 return 0;
169         if(name=="scale")
170                 return 1;
171
172         throw Exception::BadLinkName(name);
173 }
174
175 bool
176 ValueNode_Exp::check_type(ValueBase::Type type)
177 {
178         return type==ValueBase::TYPE_REAL;
179 }
180
181 LinkableValueNode::Vocab
182 ValueNode_Exp::get_children_vocab_vfunc()const
183 {
184         if(children_vocab.size())
185                 return children_vocab;
186
187         LinkableValueNode::Vocab ret;
188
189         ret.push_back(ParamDesc(ValueBase(),"exp")
190                 .set_local_name(_("Exponent"))
191                 .set_description(_("The value to raise the constant 'e'"))
192         );
193
194         ret.push_back(ParamDesc(ValueBase(),"scale")
195                 .set_local_name(_("Scale"))
196                 .set_description(_("Multiplier of the resulting exponent"))
197         );
198
199         return ret;
200 }