Rename get_param_vocab to get_children_vocab and use a wrapper for the pure virtual...
[synfig.git] / synfig-core / src / synfig / valuenode_switch.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_switch.cpp
3 **      \brief Implementation of the "Switch" 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_switch.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_Switch::ValueNode_Switch(const ValueBase::Type &x):
54         LinkableValueNode(x)
55 {
56 }
57
58 ValueNode_Switch::ValueNode_Switch(const ValueNode::Handle &x):
59         LinkableValueNode(x->get_type())
60 {
61         set_link("link_off",x);
62         set_link("link_on",x);
63         set_link("switch",ValueNode_Const::create(bool(false)));
64 }
65
66 ValueNode_Switch*
67 ValueNode_Switch::create(const ValueBase &x)
68 {
69         return new ValueNode_Switch(ValueNode_Const::create(x));
70 }
71
72 LinkableValueNode*
73 ValueNode_Switch::create_new()const
74 {
75         return new ValueNode_Switch(get_type());
76 }
77
78 ValueNode_Switch::~ValueNode_Switch()
79 {
80         unlink_all();
81 }
82
83 bool
84 ValueNode_Switch::set_link_vfunc(int i,ValueNode::Handle value)
85 {
86         assert(i>=0 && i<link_count());
87
88         switch(i)
89         {
90         case 0: CHECK_TYPE_AND_SET_VALUE(link_off_, get_type());
91         case 1: CHECK_TYPE_AND_SET_VALUE(link_on_,  get_type());
92         case 2: CHECK_TYPE_AND_SET_VALUE(switch_,   ValueBase::TYPE_BOOL);
93         }
94         return false;
95 }
96
97 ValueNode::LooseHandle
98 ValueNode_Switch::get_link_vfunc(int i)const
99 {
100         assert(i>=0 && i<link_count());
101
102         switch(i)
103         {
104         case 0: return link_off_;
105         case 1: return link_on_;
106         case 2: return switch_;
107         }
108         return 0;
109 }
110
111 int
112 ValueNode_Switch::link_count()const
113 {
114         return 3;
115 }
116
117 String
118 ValueNode_Switch::link_name(int i)const
119 {
120         assert(i>=0 && i<link_count());
121
122         switch(i)
123         {
124         case 0: return "link_off";
125         case 1: return "link_on";
126         case 2: return "switch";
127         }
128         return String();
129 }
130
131 String
132 ValueNode_Switch::link_local_name(int i)const
133 {
134         assert(i>=0 && i<link_count());
135
136         switch(i)
137         {
138         case 0: return "Link Off";
139         case 1: return "Link On";
140         case 2: return "Switch";
141         }
142         return String();
143 }
144
145 int
146 ValueNode_Switch::get_link_index_from_name(const String &name)const
147 {
148         if(name=="link_off") return 0;
149         if(name=="link_on" ) return 1;
150         if(name=="switch"  ) return 2;
151         throw Exception::BadLinkName(name);
152 }
153
154 ValueBase
155 ValueNode_Switch::operator()(Time t)const
156 {
157         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
158                 printf("%s:%d operator()\n", __FILE__, __LINE__);
159
160         return (*switch_)(t).get(bool()) ? (*link_on_)(t) : (*link_off_)(t);
161 }
162
163
164 String
165 ValueNode_Switch::get_name()const
166 {
167         return "switch";
168 }
169
170 String
171 ValueNode_Switch::get_local_name()const
172 {
173         return _("Switch");
174 }
175
176 bool
177 ValueNode_Switch::check_type(ValueBase::Type type)
178 {
179         if(type)
180                 return true;
181         return false;
182 }
183
184 LinkableValueNode::Vocab
185 ValueNode_Switch::get_children_vocab_vfunc()const
186 {
187         LinkableValueNode::Vocab ret;
188
189         ret.push_back(ParamDesc(ValueBase(),"link_off")
190                 .set_local_name(_("Link Off"))
191                 .set_description(_("The value node returned when the switch is off"))
192         );
193
194         ret.push_back(ParamDesc(ValueBase(),"link_on")
195                 .set_local_name(_("Link On"))
196                 .set_description(_("The value node returned when the switch is on"))
197         );
198
199         ret.push_back(ParamDesc(ValueBase(),"switch")
200                 .set_local_name(_("Switch"))
201                 .set_description(_("When checked, returns 'Link On', otherwise returns 'Link Off'"))
202         );
203
204         return ret;
205 }