89bebdc1389b9169e8e0b98ed71d1cd15817189a
[synfig.git] / synfig-studio / trunk / src / gtkmm / duck.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file duck.h
3 **      \brief Template Header
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 /* === S T A R T =========================================================== */
24
25 #ifndef __SYNFIG_DUCKMATIC_DUCK_H
26 #define __SYNFIG_DUCKMATIC_DUCK_H
27
28 /* === H E A D E R S ======================================================= */
29
30 #include <list>
31
32 #include <ETL/smart_ptr>
33 #include <ETL/handle>
34
35 #include <synfig/vector.h>
36 #include <synfig/string.h>
37 #include <synfig/real.h>
38 #include <sigc++/signal.h>
39 #include <sigc++/object.h>
40 #include <synfig/time.h>
41 #include <ETL/smart_ptr>
42 #include <synfigapp/value_desc.h>
43 #include <synfig/transform.h>
44
45 /* === M A C R O S ========================================================= */
46
47 #define HASH_MAP_H <ext/hash_map>
48 #define HASH_SET_H <ext/hash_set>
49
50 #ifdef HASH_MAP_H
51 #include HASH_MAP_H
52 #ifndef __STRING_HASH__
53 #define __STRING_HASH__
54 class StringHash
55 {
56         __gnu_cxx::hash<const char*> hasher_;
57 public:
58         size_t operator()(const synfig::String& x)const
59         {
60                 return hasher_(x.c_str());
61         }
62 };
63 #endif
64 #else
65 #include <map>
66 #endif
67
68 #include <set>
69
70 /* === T Y P E D E F S ===================================================== */
71
72 /* === C L A S S E S & S T R U C T S ======================================= */
73
74 namespace studio {
75 class Duckmatic;
76
77 /*! \class Duck
78 **      \writeme */
79 class Duck : public etl::shared_object
80 {
81         friend class Duckmatic;
82
83 public:
84         enum Type
85         {
86                 TYPE_NONE               =       (0),
87                 TYPE_POSITION   =       (1<<0),
88                 TYPE_TANGENT    =       (1<<1),
89                 TYPE_RADIUS             =       (1<<2),
90                 TYPE_WIDTH              =       (1<<3),
91                 TYPE_ANGLE              =       (1<<4),
92                 TYPE_VERTEX             =       (1<<5),
93
94                 TYPE_ALL                =       (~0),
95
96                 TYPE_DEFAULT    =       0xdefadefa
97         };
98
99         typedef etl::handle<Duck> Handle;
100         typedef etl::loose_handle<Duck> LooseHandle;
101
102 private:
103
104         sigc::signal<bool,const synfig::Point &> signal_edited_;
105         sigc::signal<void> signal_user_click_[5];
106
107         Type type_;
108
109         synfig::Point point;
110         synfig::Angle rotations;
111
112         etl::smart_ptr<synfig::Point> shared_point;
113
114         synfig::Point origin;
115         synfig::String name;
116         synfig::Real scalar;
117
118         etl::handle<Duck> origin_duck;
119
120         etl::handle<Duck> connect_duck;
121         etl::handle<Duck> box_duck;
122
123         synfig::GUID guid_;
124
125         // Flags
126         bool editable;
127         bool radius_;
128         bool tangent_;
129
130         synfig::TransformStack transform_stack_;
131
132         synfigapp::ValueDesc value_desc_;
133
134         static int duck_count;
135 public:
136         Duck();
137         Duck(const synfig::Point &point);
138         Duck(const synfig::Point &point,const synfig::Point &origin);
139         ~Duck();
140
141         sigc::signal<bool,const synfig::Point &> &signal_edited() { return signal_edited_; }
142         sigc::signal<void> &signal_user_click(int i=0) { assert(i>=0); assert(i<5); return signal_user_click_[i]; }
143
144         void set_guid(const synfig::GUID& x) { guid_=x; }
145         const synfig::GUID& get_guid()const { return guid_; }
146
147         synfig::GUID get_data_guid()const;
148
149         //! Changes the editable flag. If set, the duck will not be able to be moved.
150         void set_editable(bool x) { editable=x; }
151
152         //! Retrieves the status of the editable flag
153         bool get_editable()const { return editable; }
154
155         //! \writeme
156         void set_tangent(bool x) { tangent_=x; type_=TYPE_TANGENT; }
157
158         //! \writeme
159         bool get_tangent()const { return tangent_; }
160
161         void set_connect_duck(const etl::handle<Duck>& x) { connect_duck=x; }
162         void set_box_duck(const etl::handle<Duck>& x) { box_duck=x; }
163
164         const etl::handle<Duck>& get_connect_duck()const { return connect_duck; }
165         const etl::handle<Duck>& get_box_duck()const { return box_duck; }
166
167         void set_value_desc(synfigapp::ValueDesc x) { value_desc_=x; }
168
169         synfigapp::ValueDesc& get_value_desc() { return value_desc_; }
170
171         void set_transform_stack(const synfig::TransformStack& x) { transform_stack_=x; }
172
173         const synfig::TransformStack& get_transform_stack()const { return transform_stack_; }
174
175         //! \writeme
176         void set_type(Type x) { type_=x; }
177
178         //! \writeme
179         Type get_type()const { return type_; }
180
181         //! Sets the scalar multiplier for the duck with respect to the origin
182         void set_scalar(synfig::Vector::value_type n) { scalar=n; }
183
184         //! Retrieves the scalar value
185         synfig::Vector::value_type get_scalar()const { return scalar; }
186
187         void set_shared_point(const etl::smart_ptr<synfig::Point>&x) { shared_point=x; }
188
189         //! Sets the location of the duck with respect to the origin
190         void set_point(const synfig::Point &x) { (shared_point?*shared_point:point)=x; }
191
192         //! Returns the location of the duck
193         synfig::Point get_point()const { return shared_point?*shared_point:point; }
194
195         synfig::Point get_trans_point()const;
196
197         void set_trans_point(const synfig::Point &x);
198
199         synfig::Point get_sub_trans_point()const;
200         void set_sub_trans_point(const synfig::Point &x);
201         synfig::Point get_sub_trans_origin()const;
202
203         //! Sets the origin point.
204         void set_origin(const synfig::Point &x);
205
206         //! Sets the origin point as another duck
207         void set_origin(const etl::handle<Duck> &x);
208
209         //! Retrieves the origin location
210         synfig::Point get_origin()const;
211
212         //! Retrieves the origin duck
213         const etl::handle<Duck> & get_origin_duck() const;
214
215         //! Retrieves the origin location
216         synfig::Point get_trans_origin()const;
217
218         void set_radius(bool r) { radius_=r; }
219         bool is_radius()const { return radius_; }
220
221         //! Sets the name of the duck
222         void set_name(const synfig::String &x);
223
224         //! Retrieves the name of the duck
225         synfig::String get_name()const { return name; }
226
227         bool operator==(const Duck &rhs)const;
228 }; // END of class Duck
229
230 //! Combine Flags
231 inline Duck::Type
232 operator|(Duck::Type lhs, const Duck::Type rhs)
233 { return static_cast<Duck::Type>(int(lhs)|int(rhs)); }
234
235 //! Exclude Flags
236 inline Duck::Type
237 operator-(Duck::Type lhs, const Duck::Type rhs)
238 { return static_cast<Duck::Type>(int(lhs)&~int(rhs)); }
239
240 inline Duck::Type&
241 operator|=(Duck::Type& lhs, const Duck::Type rhs)
242 { *reinterpret_cast<int*>(&lhs)|=int(rhs); return lhs; }
243
244 inline Duck::Type
245 operator&(const Duck::Type lhs, const Duck::Type rhs)
246 { return static_cast<Duck::Type>(int(lhs)&int(rhs)); }
247
248 class DuckMap : public
249 #ifdef HASH_MAP_H
250 __gnu_cxx::hash_map<synfig::GUID,etl::handle<studio::Duck>,synfig::GUIDHash>
251 {
252         typedef __gnu_cxx::hash_map<synfig::GUID,etl::handle<studio::Duck>,synfig::GUIDHash> PARENT_TYPE;
253 #else
254 std::map<synfig::GUID,etl::handle<studio::Duck> >
255 {
256         typedef std::map<synfig::GUID,etl::handle<studio::Duck> > PARENT_TYPE;
257 #endif
258 public:
259         void insert(const Duck::Handle& x) { operator[](x->get_guid())=x;  }
260 }; // END of class DuckMap
261
262 typedef std::list<Duck::Handle> DuckList;
263
264 }; // END of namespace studio
265
266 /* === E N D =============================================================== */
267
268 #endif