Use link_count from children vocabulary and return the stored vocabulary if already...
[synfig.git] / synfig-core / src / synfig / valuenode_integer.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_integer.cpp
3 **      \brief Implementation of the "Integer" 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_integer.h"
34 #include "valuenode_const.h"
35 #include "general.h"
36 #include <ETL/misc>
37
38 #endif
39
40 /* === U S I N G =========================================================== */
41
42 using namespace std;
43 using namespace etl;
44 using namespace synfig;
45
46 /* === M A C R O S ========================================================= */
47
48 /* === G L O B A L S ======================================================= */
49
50 /* === P R O C E D U R E S ================================================= */
51
52 /* === M E T H O D S ======================================================= */
53
54 ValueNode_Integer::ValueNode_Integer(const ValueBase::Type &x):
55         LinkableValueNode(x)
56 {
57 }
58
59 ValueNode_Integer::ValueNode_Integer(const ValueBase &x):
60         LinkableValueNode(x.get_type())
61 {
62         Vocab ret(get_children_vocab());
63         set_children_vocab(ret);
64         switch(x.get_type())
65         {
66         case ValueBase::TYPE_ANGLE:
67                 set_link("integer", ValueNode_Const::create(round_to_int(Angle::deg(x.get(Angle())).get())));
68                 break;
69         case ValueBase::TYPE_BOOL:
70                 set_link("integer", ValueNode_Const::create(int(x.get(bool()))));
71                 break;
72         case ValueBase::TYPE_REAL:
73                 set_link("integer", ValueNode_Const::create(round_to_int(x.get(Real()))));
74                 break;
75         case ValueBase::TYPE_TIME:
76                 set_link("integer", ValueNode_Const::create(round_to_int(x.get(Time()))));
77                 break;
78         default:
79                 assert(0);
80                 throw runtime_error(get_local_name()+_(":Bad type ")+ValueBase::type_local_name(x.get_type()));
81         }
82 }
83
84 ValueNode_Integer*
85 ValueNode_Integer::create(const ValueBase &x)
86 {
87         return new ValueNode_Integer(x);
88 }
89
90 LinkableValueNode*
91 ValueNode_Integer::create_new()const
92 {
93         return new ValueNode_Integer(get_type());
94 }
95
96 ValueNode_Integer::~ValueNode_Integer()
97 {
98         unlink_all();
99 }
100
101 bool
102 ValueNode_Integer::set_link_vfunc(int i,ValueNode::Handle value)
103 {
104         assert(i>=0 && i<link_count());
105
106         switch(i)
107         {
108         case 0: CHECK_TYPE_AND_SET_VALUE(integer_, get_type());
109         }
110         return false;
111 }
112
113 ValueNode::LooseHandle
114 ValueNode_Integer::get_link_vfunc(int i)const
115 {
116         assert(i>=0 && i<link_count());
117
118         if(i==0) return integer_;
119
120         return 0;
121 }
122
123 int
124 ValueNode_Integer::link_count()const
125 {
126         return 1;
127 }
128
129 String
130 ValueNode_Integer::link_local_name(int i)const
131 {
132         assert(i>=0 && i<link_count());
133
134         if(i==0) return _("Integer");
135         return String();
136 }
137
138 String
139 ValueNode_Integer::link_name(int i)const
140 {
141         assert(i>=0 && i<link_count());
142
143         if(i==0) return "integer";
144         return String();
145 }
146
147 int
148 ValueNode_Integer::get_link_index_from_name(const String &name)const
149 {
150         if(name=="integer") return 0;
151
152         throw Exception::BadLinkName(name);
153 }
154
155 ValueBase
156 ValueNode_Integer::operator()(Time t)const
157 {
158         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
159                 printf("%s:%d operator()\n", __FILE__, __LINE__);
160
161         int integer = (*integer_)(t).get(int());
162
163         switch (get_type())
164         {
165         case ValueBase::TYPE_ANGLE:
166                 return Angle::deg(integer);
167         case ValueBase::TYPE_BOOL:
168                 return bool(integer);
169         case ValueBase::TYPE_REAL:
170                 return Real(integer);
171         case ValueBase::TYPE_TIME:
172                 return Time(integer);
173         default:
174                 assert(0);
175                 throw runtime_error(get_local_name()+_(":Bad type ")+ValueBase::type_local_name(get_type()));
176         }
177 }
178
179 String
180 ValueNode_Integer::get_name()const
181 {
182         return "fromint";
183 }
184
185 String
186 ValueNode_Integer::get_local_name()const
187 {
188         return _("From Integer");
189 }
190
191 // don't show this to the user at the moment - maybe it's not very useful
192 bool
193 ValueNode_Integer::check_type(ValueBase::Type type __attribute__ ((unused)))
194 {
195         return false;
196 //      return
197 //              type==ValueBase::TYPE_ANGLE ||
198 //              type==ValueBase::TYPE_BOOL  ||
199 //              type==ValueBase::TYPE_REAL  ||
200 //              type==ValueBase::TYPE_TIME;
201 }
202
203 LinkableValueNode::Vocab
204 ValueNode_Integer::get_children_vocab_vfunc()const
205 {
206         if(children_vocab.size())
207                 return children_vocab;
208
209         LinkableValueNode::Vocab ret;
210
211         ret.push_back(ParamDesc(ValueBase(),"integer")
212                 .set_local_name(_("Integer"))
213                 .set_description(_("The integer value to be converted"))
214         );
215
216         return ret;
217 }