Use link_count from children vocabulary and return the stored vocabulary if already...
[synfig.git] / synfig-core / src / synfig / valuenode_compare.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_compare.cpp
3 **      \brief Implementation of the "Compare" 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 **      Copyright (c) 2009 Nikita Kitaev
11 **
12 **      This package is free software; you can redistribute it and/or
13 **      modify it under the terms of the GNU General Public License as
14 **      published by the Free Software Foundation; either version 2 of
15 **      the License, or (at your option) any later version.
16 **
17 **      This package is distributed in the hope that it will be useful,
18 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
19 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 **      General Public License for more details.
21 **      \endlegal
22 */
23 /* ========================================================================= */
24
25 /* === H E A D E R S ======================================================= */
26
27 #ifdef USING_PCH
28 #       include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #       include <config.h>
32 #endif
33
34 #include "valuenode_compare.h"
35 #include "valuenode_const.h"
36 #include "general.h"
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_Compare::ValueNode_Compare(const ValueBase &x):
55         LinkableValueNode(x.get_type())
56 {
57         Vocab ret(get_children_vocab());
58         set_children_vocab(ret);
59         bool value(x.get(bool()));
60
61         set_link("lhs",          ValueNode_Const::create(Real(0)));
62         set_link("rhs",          ValueNode_Const::create(Real(0)));
63         set_link("greater",      ValueNode_Const::create(bool(false)));
64         if (value)
65                 set_link("equal",ValueNode_Const::create(bool(true)));
66         else
67                 set_link("equal",ValueNode_Const::create(bool(false)));
68         set_link("less",         ValueNode_Const::create(bool(false)));
69 }
70
71 ValueNode_Compare*
72 ValueNode_Compare::create(const ValueBase &x)
73 {
74         return new ValueNode_Compare(x);
75 }
76
77 LinkableValueNode*
78 ValueNode_Compare::create_new()const
79 {
80         return new ValueNode_Compare(get_type());
81 }
82
83 ValueNode_Compare::~ValueNode_Compare()
84 {
85         unlink_all();
86 }
87
88 bool
89 ValueNode_Compare::set_link_vfunc(int i,ValueNode::Handle value)
90 {
91         assert(i>=0 && i<link_count());
92
93         switch(i)
94         {
95         case 0: CHECK_TYPE_AND_SET_VALUE(lhs_,      ValueBase::TYPE_REAL);
96         case 1: CHECK_TYPE_AND_SET_VALUE(rhs_,      ValueBase::TYPE_REAL);
97         case 2: CHECK_TYPE_AND_SET_VALUE(greater_,  ValueBase::TYPE_BOOL);
98         case 3: CHECK_TYPE_AND_SET_VALUE(equal_,    ValueBase::TYPE_BOOL);
99         case 4: CHECK_TYPE_AND_SET_VALUE(less_,     ValueBase::TYPE_BOOL);
100         }
101         return false;
102 }
103
104 ValueNode::LooseHandle
105 ValueNode_Compare::get_link_vfunc(int i)const
106 {
107         assert(i>=0 && i<link_count());
108
109         if(i==0) return lhs_;
110         if(i==1) return rhs_;
111         if(i==2) return greater_;
112         if(i==3) return equal_;
113         if(i==4) return less_;
114         return 0;
115 }
116
117 int
118 ValueNode_Compare::link_count()const
119 {
120         return 5;
121 }
122
123 String
124 ValueNode_Compare::link_local_name(int i)const
125 {
126         assert(i>=0 && i<link_count());
127
128         if(i==0) return _("LHS");
129         if(i==1) return _("RHS");
130         if(i==2) return _("Greater Than");
131         if(i==3) return _("Equal to");
132         if(i==4) return _("Less Than");
133         return String();
134 }
135
136 String
137 ValueNode_Compare::link_name(int i)const
138 {
139         assert(i>=0 && i<link_count());
140
141         if(i==0) return "lhs";
142         if(i==1) return "rhs";
143         if(i==2) return "greater";
144         if(i==3) return "equal";
145         if(i==4) return "less";
146         return String();
147 }
148
149 int
150 ValueNode_Compare::get_link_index_from_name(const String &name)const
151 {
152         if(name=="lhs")     return 0;
153         if(name=="rhs")     return 1;
154         if(name=="greater") return 2;
155         if(name=="equal")   return 3;
156         if(name=="less")    return 4;
157
158         throw Exception::BadLinkName(name);
159 }
160
161 ValueBase
162 ValueNode_Compare::operator()(Time t)const
163 {
164         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
165                 printf("%s:%d operator()\n", __FILE__, __LINE__);
166
167         Real lhs      = (*lhs_)     (t).get(Real());
168         Real rhs      = (*rhs_)     (t).get(Real());
169         Real greater  = (*greater_) (t).get(bool());
170         Real equal    = (*equal_)   (t).get(bool());
171         Real less     = (*less_)    (t).get(bool());
172
173         if (greater && lhs > rhs)
174                 return true;
175         if (equal && lhs == rhs)
176                 return true;
177         if (less && lhs < rhs)
178                 return true;
179
180         return false;
181 }
182
183 String
184 ValueNode_Compare::get_name()const
185 {
186         return "compare";
187 }
188
189 String
190 ValueNode_Compare::get_local_name()const
191 {
192         return _("Compare");
193 }
194
195 bool
196 ValueNode_Compare::check_type(ValueBase::Type type)
197 {
198         return type==ValueBase::TYPE_BOOL;
199 }
200
201 LinkableValueNode::Vocab
202 ValueNode_Compare::get_children_vocab_vfunc()const
203 {
204         if(children_vocab.size())
205                 return children_vocab;
206
207         LinkableValueNode::Vocab ret;
208
209         ret.push_back(ParamDesc(ValueBase(),"lhs")
210                 .set_local_name(_("LHS"))
211                 .set_description(_("The left side of the comparison"))
212         );
213
214         ret.push_back(ParamDesc(ValueBase(),"rhs")
215                 .set_local_name(_("RHS"))
216                 .set_description(_("The right side of the comparison"))
217         );
218
219         ret.push_back(ParamDesc(ValueBase(),"greater")
220                 .set_local_name(_("Greater"))
221                 .set_description(_("When checked, returns true if LHS > RHS"))
222         );
223
224         ret.push_back(ParamDesc(ValueBase(),"equal")
225                 .set_local_name(_("Equal"))
226                 .set_description(_("When checked, returns true if LHS = RHS"))
227         );
228
229         ret.push_back(ParamDesc(ValueBase(),"less")
230                 .set_local_name(_("Less"))
231                 .set_description(_("When checked, returns true if LHS < RHS"))
232         );
233
234         return ret;
235 }