Made all the assert() lines which check the valuenode sub-parameter index range the...
[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 x)
84 {
85         assert(i>=0 && i<link_count());
86
87         switch(i)
88         {
89         case 0:
90                 if(x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x))
91                         return false;
92                 link_off_=x;
93                 signal_child_changed()(i);signal_value_changed()();
94                 return true;
95         case 1:
96                 if(x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x))
97                         return false;
98                 link_on_=x;
99                 signal_child_changed()(i);signal_value_changed()();
100                 return true;
101         case 2:
102                 switch_=x;
103                 signal_child_changed()(i);signal_value_changed()();
104                 return true;
105         }
106         return true;
107 }
108
109 ValueNode::LooseHandle
110 ValueNode_Switch::get_link_vfunc(int i)const
111 {
112         assert(i>=0 && i<link_count());
113
114         switch(i)
115         {
116         case 0: return link_off_;
117         case 1: return link_on_;
118         case 2: return switch_;
119         }
120         return 0;
121 }
122
123 int
124 ValueNode_Switch::link_count()const
125 {
126         return 3;
127 }
128
129 String
130 ValueNode_Switch::link_name(int i)const
131 {
132         assert(i>=0 && i<link_count());
133
134         switch(i)
135         {
136         case 0: return "link_off";
137         case 1: return "link_on";
138         case 2: return "switch";
139         }
140         return String();
141 }
142
143 String
144 ValueNode_Switch::link_local_name(int i)const
145 {
146         assert(i>=0 && i<link_count());
147
148         switch(i)
149         {
150         case 0: return "Link Off";
151         case 1: return "Link On";
152         case 2: return "Switch";
153         }
154         return String();
155 }
156
157 int
158 ValueNode_Switch::get_link_index_from_name(const String &name)const
159 {
160         if(name=="link_off") return 0;
161         if(name=="link_on" ) return 1;
162         if(name=="switch"  ) return 2;
163         throw Exception::BadLinkName(name);
164 }
165
166 ValueBase
167 ValueNode_Switch::operator()(Time t)const
168 {
169         return (*switch_)(t).get(bool()) ? (*link_on_)(t) : (*link_off_)(t);
170 }
171
172
173 String
174 ValueNode_Switch::get_name()const
175 {
176         return "switch";
177 }
178
179 String
180 ValueNode_Switch::get_local_name()const
181 {
182         return _("Switch");
183 }
184
185 bool
186 ValueNode_Switch::check_type(ValueBase::Type type)
187 {
188         if(type)
189                 return true;
190         return false;
191 }