Use LinkableValueNode members functions when possible in the derived valuenodes.
[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         Vocab ret(get_children_vocab());
62         set_children_vocab(ret);
63         set_link("link_off",x);
64         set_link("link_on",x);
65         set_link("switch",ValueNode_Const::create(bool(false)));
66 }
67
68 ValueNode_Switch*
69 ValueNode_Switch::create(const ValueBase &x)
70 {
71         return new ValueNode_Switch(ValueNode_Const::create(x));
72 }
73
74 LinkableValueNode*
75 ValueNode_Switch::create_new()const
76 {
77         return new ValueNode_Switch(get_type());
78 }
79
80 ValueNode_Switch::~ValueNode_Switch()
81 {
82         unlink_all();
83 }
84
85 bool
86 ValueNode_Switch::set_link_vfunc(int i,ValueNode::Handle value)
87 {
88         assert(i>=0 && i<link_count());
89
90         switch(i)
91         {
92         case 0: CHECK_TYPE_AND_SET_VALUE(link_off_, get_type());
93         case 1: CHECK_TYPE_AND_SET_VALUE(link_on_,  get_type());
94         case 2: CHECK_TYPE_AND_SET_VALUE(switch_,   ValueBase::TYPE_BOOL);
95         }
96         return false;
97 }
98
99 ValueNode::LooseHandle
100 ValueNode_Switch::get_link_vfunc(int i)const
101 {
102         assert(i>=0 && i<link_count());
103
104         switch(i)
105         {
106         case 0: return link_off_;
107         case 1: return link_on_;
108         case 2: return switch_;
109         }
110         return 0;
111 }
112
113 ValueBase
114 ValueNode_Switch::operator()(Time t)const
115 {
116         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
117                 printf("%s:%d operator()\n", __FILE__, __LINE__);
118
119         return (*switch_)(t).get(bool()) ? (*link_on_)(t) : (*link_off_)(t);
120 }
121
122
123 String
124 ValueNode_Switch::get_name()const
125 {
126         return "switch";
127 }
128
129 String
130 ValueNode_Switch::get_local_name()const
131 {
132         return _("Switch");
133 }
134
135 bool
136 ValueNode_Switch::check_type(ValueBase::Type type)
137 {
138         if(type)
139                 return true;
140         return false;
141 }
142
143 LinkableValueNode::Vocab
144 ValueNode_Switch::get_children_vocab_vfunc()const
145 {
146         if(children_vocab.size())
147                 return children_vocab;
148
149         LinkableValueNode::Vocab ret;
150
151         ret.push_back(ParamDesc(ValueBase(),"link_off")
152                 .set_local_name(_("Link Off"))
153                 .set_description(_("The value node returned when the switch is off"))
154         );
155
156         ret.push_back(ParamDesc(ValueBase(),"link_on")
157                 .set_local_name(_("Link On"))
158                 .set_description(_("The value node returned when the switch is on"))
159         );
160
161         ret.push_back(ParamDesc(ValueBase(),"switch")
162                 .set_local_name(_("Switch"))
163                 .set_description(_("When checked, returns 'Link On', otherwise returns 'Link Off'"))
164         );
165
166         return ret;
167 }