b7aefb6af9fa71eba7672d900a0d90395a42ab6e
[synfig.git] / synfig-core / trunk / 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 **
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 value)
84 {
85         assert(i>=0 && i<link_count());
86
87         switch(i)
88         {
89         case 0: CHECK_TYPE_AND_SET_VALUE(link_off_, get_type());
90         case 1: CHECK_TYPE_AND_SET_VALUE(link_on_,  get_type());
91         case 2: CHECK_TYPE_AND_SET_VALUE(switch_,   ValueBase::TYPE_BOOL);
92         }
93         return false;
94 }
95
96 ValueNode::LooseHandle
97 ValueNode_Switch::get_link_vfunc(int i)const
98 {
99         assert(i>=0 && i<link_count());
100
101         switch(i)
102         {
103         case 0: return link_off_;
104         case 1: return link_on_;
105         case 2: return switch_;
106         }
107         return 0;
108 }
109
110 int
111 ValueNode_Switch::link_count()const
112 {
113         return 3;
114 }
115
116 String
117 ValueNode_Switch::link_name(int i)const
118 {
119         assert(i>=0 && i<link_count());
120
121         switch(i)
122         {
123         case 0: return "link_off";
124         case 1: return "link_on";
125         case 2: return "switch";
126         }
127         return String();
128 }
129
130 String
131 ValueNode_Switch::link_local_name(int i)const
132 {
133         assert(i>=0 && i<link_count());
134
135         switch(i)
136         {
137         case 0: return "Link Off";
138         case 1: return "Link On";
139         case 2: return "Switch";
140         }
141         return String();
142 }
143
144 int
145 ValueNode_Switch::get_link_index_from_name(const String &name)const
146 {
147         if(name=="link_off") return 0;
148         if(name=="link_on" ) return 1;
149         if(name=="switch"  ) return 2;
150         throw Exception::BadLinkName(name);
151 }
152
153 ValueBase
154 ValueNode_Switch::operator()(Time t)const
155 {
156         return (*switch_)(t).get(bool()) ? (*link_on_)(t) : (*link_off_)(t);
157 }
158
159
160 String
161 ValueNode_Switch::get_name()const
162 {
163         return "switch";
164 }
165
166 String
167 ValueNode_Switch::get_local_name()const
168 {
169         return _("Switch");
170 }
171
172 bool
173 ValueNode_Switch::check_type(ValueBase::Type type)
174 {
175         if(type)
176                 return true;
177         return false;
178 }