Add the 'Switch' valuenode.
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_switch.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_switch.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "valuenode_switch.h"
33 #include "valuenode_const.h"
34 #include "general.h"
35
36 #endif
37
38 /* === U S I N G =========================================================== */
39
40 using namespace std;
41 using namespace etl;
42 using namespace synfig;
43
44 /* === M A C R O S ========================================================= */
45
46 /* === G L O B A L S ======================================================= */
47
48 /* === P R O C E D U R E S ================================================= */
49
50 /* === M E T H O D S ======================================================= */
51
52 ValueNode_Switch::ValueNode_Switch(const ValueBase::Type &x):
53         LinkableValueNode(x)
54 {
55 }
56
57 ValueNode_Switch::ValueNode_Switch(const ValueNode::Handle &x):
58         LinkableValueNode(x->get_type())
59 {
60         set_link("link_off",x);
61         set_link("link_on",x);
62         set_link("switch",ValueNode_Const::create(bool(false)));
63 }
64
65 ValueNode_Switch*
66 ValueNode_Switch::create(const ValueBase &x)
67 {
68         return new ValueNode_Switch(ValueNode_Const::create(x));
69 }
70
71 LinkableValueNode*
72 ValueNode_Switch::create_new()const
73 {
74         return new ValueNode_Switch(get_type());
75 }
76
77 ValueNode_Switch::~ValueNode_Switch()
78 {
79         unlink_all();
80 }
81
82 bool
83 ValueNode_Switch::set_link_vfunc(int i,ValueNode::Handle x)
84 {
85         assert(i>=0 && i<link_count());
86         switch(i)
87         {
88         case 0:
89                 if(x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x))
90                         return false;
91                 link_off_=x;
92                 signal_child_changed()(i);signal_value_changed()();
93                 return true;
94         case 1:
95                 if(x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x))
96                         return false;
97                 link_on_=x;
98                 signal_child_changed()(i);signal_value_changed()();
99                 return true;
100         case 2:
101                 switch_=x;
102                 signal_child_changed()(i);signal_value_changed()();
103                 return true;
104         }
105         return true;
106 }
107
108 ValueNode::LooseHandle
109 ValueNode_Switch::get_link_vfunc(int i)const
110 {
111         assert(i>=0 && i<link_count());
112         switch(i)
113         {
114         case 0: return link_off_;
115         case 1: return link_on_;
116         case 2: return switch_;
117         }
118         return 0;
119 }
120
121 int
122 ValueNode_Switch::link_count()const
123 {
124         return 3;
125 }
126
127 String
128 ValueNode_Switch::link_name(int i)const
129 {
130         assert(i>=0 && i<link_count());
131         switch(i)
132         {
133         case 0: return "link_off";
134         case 1: return "link_on";
135         case 2: return "switch";
136         }
137         return String();
138 }
139
140 String
141 ValueNode_Switch::link_local_name(int i)const
142 {
143         assert(i>=0 && i<link_count());
144         switch(i)
145         {
146         case 0: return "Link Off";
147         case 1: return "Link On";
148         case 2: return "Switch";
149         }
150         return String();
151 }
152
153 int
154 ValueNode_Switch::get_link_index_from_name(const String &name)const
155 {
156         if(name=="link_off") return 0;
157         if(name=="link_on" ) return 1;
158         if(name=="switch"  ) return 2;
159         throw Exception::BadLinkName(name);
160 }
161
162 ValueBase
163 ValueNode_Switch::operator()(Time t)const
164 {
165         return (*switch_)(t).get(bool()) ? (*link_on_)(t) : (*link_off_)(t);
166 }
167
168
169 String
170 ValueNode_Switch::get_name()const
171 {
172         return "switch";
173 }
174
175 String
176 ValueNode_Switch::get_local_name()const
177 {
178         return _("Switch");
179 }
180
181 bool
182 ValueNode_Switch::check_type(ValueBase::Type type)
183 {
184         if(type)
185                 return true;
186         return false;
187 }