d5d09fa675646fd02a4f636b8aa344986ffca8fc
[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         bool value(x.get(bool()));
58
59         set_link("lhs",          ValueNode_Const::create(Real(0)));
60         set_link("rhs",          ValueNode_Const::create(Real(0)));
61         set_link("greater",      ValueNode_Const::create(bool(false)));
62         if (value)
63                 set_link("equal",ValueNode_Const::create(bool(true)));
64         else
65                 set_link("equal",ValueNode_Const::create(bool(false)));
66         set_link("less",         ValueNode_Const::create(bool(false)));
67 }
68
69 ValueNode_Compare*
70 ValueNode_Compare::create(const ValueBase &x)
71 {
72         return new ValueNode_Compare(x);
73 }
74
75 LinkableValueNode*
76 ValueNode_Compare::create_new()const
77 {
78         return new ValueNode_Compare(get_type());
79 }
80
81 ValueNode_Compare::~ValueNode_Compare()
82 {
83         unlink_all();
84 }
85
86 bool
87 ValueNode_Compare::set_link_vfunc(int i,ValueNode::Handle value)
88 {
89         assert(i>=0 && i<link_count());
90
91         switch(i)
92         {
93         case 0: CHECK_TYPE_AND_SET_VALUE(lhs_,      ValueBase::TYPE_REAL);
94         case 1: CHECK_TYPE_AND_SET_VALUE(rhs_,      ValueBase::TYPE_REAL);
95         case 2: CHECK_TYPE_AND_SET_VALUE(greater_,  ValueBase::TYPE_BOOL);
96         case 3: CHECK_TYPE_AND_SET_VALUE(equal_,    ValueBase::TYPE_BOOL);
97         case 4: CHECK_TYPE_AND_SET_VALUE(less_,     ValueBase::TYPE_BOOL);
98         }
99         return false;
100 }
101
102 ValueNode::LooseHandle
103 ValueNode_Compare::get_link_vfunc(int i)const
104 {
105         assert(i>=0 && i<link_count());
106
107         if(i==0) return lhs_;
108         if(i==1) return rhs_;
109         if(i==2) return greater_;
110         if(i==3) return equal_;
111         if(i==4) return less_;
112         return 0;
113 }
114
115 int
116 ValueNode_Compare::link_count()const
117 {
118         return 5;
119 }
120
121 String
122 ValueNode_Compare::link_local_name(int i)const
123 {
124         assert(i>=0 && i<link_count());
125
126         if(i==0) return _("LHS");
127         if(i==1) return _("RHS");
128         if(i==2) return _("Greater Than");
129         if(i==3) return _("Equal to");
130         if(i==4) return _("Less Than");
131         return String();
132 }
133
134 String
135 ValueNode_Compare::link_name(int i)const
136 {
137         assert(i>=0 && i<link_count());
138
139         if(i==0) return "lhs";
140         if(i==1) return "rhs";
141         if(i==2) return "greater";
142         if(i==3) return "equal";
143         if(i==4) return "less";
144         return String();
145 }
146
147 int
148 ValueNode_Compare::get_link_index_from_name(const String &name)const
149 {
150         if(name=="lhs")     return 0;
151         if(name=="rhs")     return 1;
152         if(name=="greater") return 2;
153         if(name=="equal")   return 3;
154         if(name=="less")    return 4;
155
156         throw Exception::BadLinkName(name);
157 }
158
159 ValueBase
160 ValueNode_Compare::operator()(Time t)const
161 {
162         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
163                 printf("%s:%d operator()\n", __FILE__, __LINE__);
164
165         Real lhs      = (*lhs_)     (t).get(Real());
166         Real rhs      = (*rhs_)     (t).get(Real());
167         Real greater  = (*greater_) (t).get(bool());
168         Real equal    = (*equal_)   (t).get(bool());
169         Real less     = (*less_)    (t).get(bool());
170
171         if (greater && lhs > rhs)
172                 return true;
173         if (equal && lhs == rhs)
174                 return true;
175         if (less && lhs < rhs)
176                 return true;
177
178         return false;
179 }
180
181 String
182 ValueNode_Compare::get_name()const
183 {
184         return "compare";
185 }
186
187 String
188 ValueNode_Compare::get_local_name()const
189 {
190         return _("Compare");
191 }
192
193 bool
194 ValueNode_Compare::check_type(ValueBase::Type type)
195 {
196         return type==ValueBase::TYPE_BOOL;
197 }
198
199 LinkableValueNode::Vocab
200 ValueNode_Compare::get_children_vocab_vfunc()const
201 {
202         LinkableValueNode::Vocab ret;
203
204         ret.push_back(ParamDesc(ValueBase(),"lhs")
205                 .set_local_name(_("LHS"))
206                 .set_description(_("The left side of the comparison"))
207         );
208
209         ret.push_back(ParamDesc(ValueBase(),"rhs")
210                 .set_local_name(_("RHS"))
211                 .set_description(_("The right side of the comparison"))
212         );
213
214         ret.push_back(ParamDesc(ValueBase(),"greater")
215                 .set_local_name(_("Greater"))
216                 .set_description(_("When checked, returns true if LHS > RHS"))
217         );
218
219         ret.push_back(ParamDesc(ValueBase(),"equal")
220                 .set_local_name(_("Equal"))
221                 .set_description(_("When checked, returns true if LHS = RHS"))
222         );
223
224         ret.push_back(ParamDesc(ValueBase(),"less")
225                 .set_local_name(_("Less"))
226                 .set_description(_("When checked, returns true if LHS < RHS"))
227         );
228
229         return ret;
230 }