Use LinkableValueNode members functions when possible in the derived valuenodes.
[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 ValueBase
118 ValueNode_Compare::operator()(Time t)const
119 {
120         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
121                 printf("%s:%d operator()\n", __FILE__, __LINE__);
122
123         Real lhs      = (*lhs_)     (t).get(Real());
124         Real rhs      = (*rhs_)     (t).get(Real());
125         Real greater  = (*greater_) (t).get(bool());
126         Real equal    = (*equal_)   (t).get(bool());
127         Real less     = (*less_)    (t).get(bool());
128
129         if (greater && lhs > rhs)
130                 return true;
131         if (equal && lhs == rhs)
132                 return true;
133         if (less && lhs < rhs)
134                 return true;
135
136         return false;
137 }
138
139 String
140 ValueNode_Compare::get_name()const
141 {
142         return "compare";
143 }
144
145 String
146 ValueNode_Compare::get_local_name()const
147 {
148         return _("Compare");
149 }
150
151 bool
152 ValueNode_Compare::check_type(ValueBase::Type type)
153 {
154         return type==ValueBase::TYPE_BOOL;
155 }
156
157 LinkableValueNode::Vocab
158 ValueNode_Compare::get_children_vocab_vfunc()const
159 {
160         if(children_vocab.size())
161                 return children_vocab;
162
163         LinkableValueNode::Vocab ret;
164
165         ret.push_back(ParamDesc(ValueBase(),"lhs")
166                 .set_local_name(_("LHS"))
167                 .set_description(_("The left side of the comparison"))
168         );
169
170         ret.push_back(ParamDesc(ValueBase(),"rhs")
171                 .set_local_name(_("RHS"))
172                 .set_description(_("The right side of the comparison"))
173         );
174
175         ret.push_back(ParamDesc(ValueBase(),"greater")
176                 .set_local_name(_("Greater"))
177                 .set_description(_("When checked, returns true if LHS > RHS"))
178         );
179
180         ret.push_back(ParamDesc(ValueBase(),"equal")
181                 .set_local_name(_("Equal"))
182                 .set_description(_("When checked, returns true if LHS = RHS"))
183         );
184
185         ret.push_back(ParamDesc(ValueBase(),"less")
186                 .set_local_name(_("Less"))
187                 .set_description(_("When checked, returns true if LHS < RHS"))
188         );
189
190         return ret;
191 }