Added copyright lines for files I've edited this year.
[synfig.git] / synfig-studio / trunk / src / synfigapp / value_desc.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file value_desc.h
3 **      \brief Template Header
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 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 /* === S T A R T =========================================================== */
25
26 #ifndef __SYNFIG_APP_VALUE_DESC_H
27 #define __SYNFIG_APP_VALUE_DESC_H
28
29 /* === H E A D E R S ======================================================= */
30
31 #include <synfig/valuenode.h>
32 #include <synfig/string.h>
33 #include <synfig/layer.h>
34 #include <synfig/value.h>
35 #include <synfig/valuenode_const.h>
36 #include <synfig/canvas.h>
37
38 /* === M A C R O S ========================================================= */
39
40 /* === T Y P E D E F S ===================================================== */
41
42 /* === C L A S S E S & S T R U C T S ======================================= */
43
44 namespace synfigapp {
45
46 class ValueDesc
47 {
48         // Info for Layer parent
49         synfig::Layer::Handle layer;
50         synfig::String name;
51
52         // Info for ValueNode parent
53         synfig::ValueNode::Handle parent_value_node;
54         int index;
55
56         // Info for exported ValueNode
57         synfig::Canvas::Handle canvas;
58
59 public:
60         bool operator==(const ValueDesc &rhs)const
61         {
62                 if((layer||rhs.layer) && layer!=rhs.layer)
63                         return false;
64                 if((!name.empty()||!rhs.name.empty()) && name!=rhs.name)
65                         return false;
66                 if(layer)
67                         return true;
68                 if((canvas||rhs.canvas) && canvas!=rhs.canvas)
69                         return false;
70                 if((parent_value_node||rhs.parent_value_node) && parent_value_node!=rhs.parent_value_node)
71                         return false;
72                 if((index>-1||rhs.index>-1) && index!=rhs.index)
73                         return false;
74                 return true;
75         }
76         bool operator!=(const ValueDesc &rhs)const
77         {
78                 return !operator==(rhs);
79         }
80
81
82         ValueDesc(synfig::Layer::Handle layer,const synfig::String& param_name):
83                 layer(layer),
84                 name(param_name) { }
85
86         ValueDesc(synfig::Layer::LooseHandle layer,const synfig::String& param_name):
87                 layer(layer),
88                 name(param_name) { }
89
90         ValueDesc(synfig::LinkableValueNode::Handle parent_value_node,int index):
91                 parent_value_node(parent_value_node),
92                 index(index) { }
93
94 //      ValueDesc(synfig::LinkableValueNode::Handle parent_value_node,const synfig::String& param_name):
95 //              parent_value_node(parent_value_node),
96 //              index(parent_value_node->get_link_index_from_name(param_name)) { }
97
98         ValueDesc(synfig::Canvas::Handle canvas,const synfig::String& name):
99                 name(name),
100                 canvas(canvas) { }
101
102         ValueDesc(synfig::ValueNode_Const::Handle parent_value_node):
103                 parent_value_node(parent_value_node),
104                 index(-1) { }
105
106         ValueDesc() { }
107
108         bool is_valid()const { return layer || parent_value_node || canvas; }
109         operator bool()const { return is_valid(); }
110
111         bool parent_is_layer_param()const { return (bool)layer; }
112         bool parent_is_value_node()const { return (bool)parent_value_node; }
113         bool parent_is_linkable_value_node()const { return parent_is_value_node() && index>=0; }
114         bool parent_is_value_node_const()const { return parent_is_value_node() && index==-1; }
115         bool parent_is_canvas()const { return (bool)canvas; }
116
117         bool is_value_node()const { return parent_is_value_node() || parent_is_canvas() || (parent_is_layer_param() && (bool)layer->dynamic_param_list().count(name)); }
118         bool is_const()const { return (parent_is_layer_param() && !layer->dynamic_param_list().count(name)) || parent_is_value_node_const(); }
119
120         synfig::Layer::Handle get_layer()const { assert(parent_is_layer_param()); return layer; }
121         const synfig::String& get_param_name()const { assert(parent_is_layer_param()); return name; }
122
123         synfig::ValueNode::Handle get_parent_value_node()const { assert(parent_is_value_node()); return parent_value_node; }
124         int get_index()const { assert(parent_is_linkable_value_node()); return index; }
125
126         const synfig::String& get_value_node_id()const { assert(parent_is_canvas()); return name; }
127
128         synfig::Canvas::Handle get_canvas()const
129         {
130                 if(canvas)
131                         return canvas;
132                 if(layer)
133                         return layer->get_canvas();
134                 if(parent_value_node)
135                         return parent_value_node->get_root_canvas();
136                 return 0;
137         }
138
139         synfig::ValueNode::Handle
140         get_value_node()const
141         {
142                 if(parent_is_canvas())
143                         return canvas->find_value_node(name);
144                 if(parent_is_layer_param() && layer->dynamic_param_list().count(name))
145                         return layer->dynamic_param_list().find(name)->second;
146                 if(parent_is_linkable_value_node())
147                         return (synfig::LinkableValueNode::Handle::cast_reinterpret(parent_value_node))->get_link(index);
148 //                      return reinterpret_cast<synfig::LinkableValueNode*>(parent_value_node.get())->get_link(index);
149                 return 0;
150         }
151
152         synfig::ValueBase
153         get_value(synfig::Time time=0)const
154         {
155                 // if the value is constant, return that constant value (at *any* time, it doesn't matter which)
156                 if(parent_is_value_node_const())
157                         return (*parent_value_node)(0);
158                 if(is_value_node() && get_value_node())
159                         return (*get_value_node())(time);
160                 if(parent_is_layer_param() && layer)
161                         return layer->get_param(name);
162                 return synfig::ValueBase();
163         }
164
165         synfig::ValueBase::Type
166         get_value_type()const
167         {
168                 synfig::ValueNode::Handle value_node=get_value_node();
169                 if(value_node)
170                         return value_node->get_type();
171                 return get_value().get_type();
172         }
173
174         bool
175         is_exported()const
176         {
177                 return is_value_node() && get_value_node()->is_exported();
178         }
179
180         synfig::String
181         get_description(bool show_exported_name = true)const;
182 }; // END of class ValueDesc
183
184 }; // END of namespace synfigapp_instance
185
186 /* === E N D =============================================================== */
187
188 #endif