deafce4102ad14d6a557cfdbe34ff7b3fd20f195
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_twotone.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_twotone.cpp
3 **      \brief Implementation of the "Two-Tone" 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 "general.h"
33 #include "valuenode_twotone.h"
34 #include "valuenode_const.h"
35 #include <stdexcept>
36 #include "color.h"
37 #include "gradient.h"
38
39 #endif
40
41 /* === U S I N G =========================================================== */
42
43 using namespace std;
44 using namespace etl;
45 using namespace synfig;
46
47 /* === M A C R O S ========================================================= */
48
49 /* === G L O B A L S ======================================================= */
50
51 /* === P R O C E D U R E S ================================================= */
52
53 /* === M E T H O D S ======================================================= */
54
55 synfig::ValueNode_TwoTone::ValueNode_TwoTone():LinkableValueNode(synfig::ValueBase::TYPE_GRADIENT)
56 {
57         set_link("color1",ValueNode_Const::create(Color::black()));
58         set_link("color2",ValueNode_Const::create(Color::white()));
59         DCAST_HACK_ENABLE();
60 }
61
62 LinkableValueNode*
63 ValueNode_TwoTone::create_new()const
64 {
65         return new ValueNode_TwoTone();
66 }
67
68 ValueNode_TwoTone*
69 ValueNode_TwoTone::create(const ValueBase& x)
70 {
71         ValueBase::Type id(x.get_type());
72         if(id!=ValueBase::TYPE_GRADIENT)
73         {
74                 assert(0);
75                 throw runtime_error(String(_("Two-Tone"))+_(":Bad type ")+ValueBase::type_local_name(id));
76         }
77
78         ValueNode_TwoTone* value_node=new ValueNode_TwoTone();
79
80         assert(value_node->get_type()==id);
81
82         return value_node;
83 }
84
85 synfig::ValueNode_TwoTone::~ValueNode_TwoTone()
86 {
87         unlink_all();
88 }
89
90 synfig::ValueBase
91 synfig::ValueNode_TwoTone::operator()(Time t)const
92 {
93         return Gradient((*ref_a)(t).get(Color()),(*ref_b)(t).get(Color()));
94 }
95
96 bool
97 ValueNode_TwoTone::set_link_vfunc(int i,ValueNode::Handle value)
98 {
99         assert(i>=0 && i<link_count());
100
101         switch(i)
102         {
103         case 0: CHECK_TYPE_AND_SET_VALUE(ref_a, ValueBase::TYPE_COLOR);
104         case 1: CHECK_TYPE_AND_SET_VALUE(ref_b, ValueBase::TYPE_COLOR);
105         }
106         return false;
107 }
108
109 ValueNode::LooseHandle
110 ValueNode_TwoTone::get_link_vfunc(int i)const
111 {
112         assert(i>=0 && i<link_count());
113
114         switch(i)
115         {
116                 case 0:
117                         return ref_a;
118                 case 1:
119                         return ref_b;
120         }
121         return 0;
122 }
123
124 int
125 ValueNode_TwoTone::link_count()const
126 {
127         return 2;
128 }
129
130 String
131 ValueNode_TwoTone::link_local_name(int i)const
132 {
133         assert(i>=0 && i<link_count());
134
135         switch(i)
136         {
137                 case 0:
138                         return _("Color1");
139                 case 1:
140                         return _("Color2");
141                 default:
142                         return String();
143         }
144 }
145
146 String
147 ValueNode_TwoTone::link_name(int i)const
148 {
149         assert(i>=0 && i<link_count());
150
151         switch(i)
152         {
153                 case 0:
154                         return "color1";
155                 case 1:
156                         return "color2";
157                 default:
158                         return String();
159         }
160 }
161
162 int
163 ValueNode_TwoTone::get_link_index_from_name(const String &name)const
164 {
165         if(name=="color1")
166                 return 0;
167         if(name=="color2")
168                 return 1;
169         throw Exception::BadLinkName(name);
170 }
171
172 String
173 ValueNode_TwoTone::get_name()const
174 {
175         return "twotone";
176 }
177
178 String
179 ValueNode_TwoTone::get_local_name()const
180 {
181         return _("Two-Tone");
182 }
183
184 bool
185 ValueNode_TwoTone::check_type(ValueBase::Type type)
186 {
187         return type==ValueBase::TYPE_GRADIENT;
188 }