a9952990f7f6497dccd320578cd1753a1852d339
[synfig.git] / synfig-core / trunk / src / synfig / layer.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layer.h
3 **      \brief Layer Class 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_LAYER_H
26 #define __SYNFIG_LAYER_H
27
28 /* === H E A D E R S ======================================================= */
29
30 #include "string_decl.h"
31 #include <map>
32 #include <ETL/handle>
33 #include "real.h"
34 #include "string.h"
35 #include <sigc++/signal.h>
36 #include <sigc++/connection.h>
37 #include "node.h"
38 #include "time.h"
39 #include "guid.h"
40
41 /* === M A C R O S ========================================================= */
42
43 //! \writeme
44 #define SYNFIG_LAYER_MODULE_EXT public: static const char name__[], version__[], cvs_id__[], local_name__[], category__[]; static Layer *create();
45
46 //! Sets the name of the layer
47 #define SYNFIG_LAYER_SET_NAME(class,x) const char class::name__[]=x
48
49 //! Sets the local name of the layer
50 #define SYNFIG_LAYER_SET_LOCAL_NAME(class,x) const char class::local_name__[]=x;
51
52 //! Sets the category of the layer
53 #define SYNFIG_LAYER_SET_CATEGORY(class,x) const char class::category__[]=x
54
55 //! Sets the version string for the layer
56 #define SYNFIG_LAYER_SET_VERSION(class,x) const char class::version__[]=x
57
58 //! Sets the CVS ID string for the layer
59 #define SYNFIG_LAYER_SET_CVS_ID(class,x) const char class::cvs_id__[]=x
60
61 //! \writeme
62 #define SYNFIG_LAYER_INIT(class) synfig::Layer* class::create() { return new class(); }
63
64 //! \writeme
65 #define IMPORT_PLUS(x,y) if(param==#x && value.same_type_as(x)) { value.put(&x);{y;}return true;}
66
67 //! \writeme
68 #define IMPORT_AS(x,y) if(param==y && value.same_type_as(x)) { value.put(&x); return true;}
69
70 //! \writeme
71 #define IMPORT(x) IMPORT_AS(x,#x)
72
73 //! \writeme
74 #define EXPORT_AS(x,y) if(param==y) { return ValueBase(x); }
75
76 //! \writeme
77 #define EXPORT(x)                                       EXPORT_AS(x,#x)
78
79 //! \writeme
80 #define EXPORT_NAME()   if(param=="Name" || param=="name" || param=="name__") { return name__; } if(param=="local_name__") { return dgettext("synfig",local_name__); }
81
82 //! \writeme
83 #define EXPORT_VERSION()        if(param=="Version" || param=="version" || param=="version__") { return version__; }
84
85 //! This is used as the category for layer book entries which represent aliases of layers.
86 //! It prevents these layers showing up in the menu.
87 #define CATEGORY_DO_NOT_USE "Do Not Use"
88
89 /* === T Y P E D E F S ===================================================== */
90
91 /* === C L A S S E S & S T R U C T S ======================================= */
92
93 namespace synfig {
94
95 class Canvas;
96 class Vector;
97 typedef Vector Point;
98 class Canvas;
99 class ParamDesc;
100 class ParamVocab;
101 class ValueNode;
102 class ValueBase;
103 class Time;
104 class Surface;
105 class RendDesc;
106 class ProgressCallback;
107 class Context;
108 class Color;
109 class Transform;
110 class Rect;
111 class GUID;
112
113
114 /*!     \class Layer
115 **      \todo writeme
116 **      \see Canvas
117 */
118 class Layer : public Node
119 {
120         friend class ValueNode;
121         friend class Context;
122
123         /*
124  --     ** -- T Y P E S -----------------------------------------------------------
125         */
126
127 public:
128
129         //! Type that represents a pointer to a layer's constructor
130         typedef Layer* (*Factory)();
131
132         struct BookEntry
133         {
134                 Factory factory;
135                 String name;
136                 String local_name;
137                 String category;
138                 String cvs_id;
139                 String version;
140                 BookEntry() { }
141                 BookEntry(Factory factory, const String& name,const String& local_name,const String& category,const String& cvs_id,const String& version):
142                         factory(factory),name(name),local_name(local_name),category(category),cvs_id(cvs_id),version(version) { }
143         };
144
145         typedef std::map<String,BookEntry> Book;
146
147         static void register_in_book(const BookEntry &);
148
149         static Book& book();
150
151         static bool subsys_init();
152
153         static bool subsys_stop();
154
155         typedef std::map<String,ValueBase> ParamList;
156
157         typedef etl::handle<Layer> Handle;
158
159         typedef etl::loose_handle<Layer> LooseHandle;
160
161         typedef etl::handle<const Layer> ConstHandle;
162
163         typedef std::map<String,etl::rhandle<ValueNode> > DynamicParamList;
164
165         //! A list type which describes all the parameters that a layer has.
166         /*! \see get_param_vocab() */
167         typedef ParamVocab Vocab;
168
169         /*
170  --     ** -- D A T A -------------------------------------------------------------
171         */
172
173 private:
174
175         /*! \c true if the layer is visible, \c false if it is to be skipped
176         **      \see set_active(), enable(), disable, active()
177         */
178         bool active_;
179
180         //! Handle to the canvas to which this layer belongs
181         etl::loose_handle<Canvas> canvas_;
182
183         DynamicParamList dynamic_param_list_;
184
185         //! A description of what this layer does
186         String description_;
187
188         //! \writeme
189         float z_depth_;
190
191         //! \writeme
192         mutable Time dirty_time_;
193
194         //! Contains the name of the group that this layer belongs to
195         String group_;
196
197         //! \writeme
198         sigc::connection parent_death_connect_;
199
200         /*
201  -- ** -- S I G N A L S -------------------------------------------------------
202         */
203
204 private:
205
206         //!     Status Changed
207         sigc::signal<void> signal_status_changed_;
208
209         //!     Parameter changed
210         sigc::signal<void,String> signal_param_changed_;
211
212         //!     Description Changed
213         sigc::signal<void> signal_description_changed_;
214
215         //!     Moved
216         sigc::signal<void, int, etl::handle<Canvas> > signal_moved_;
217
218         sigc::signal<void, String> signal_added_to_group_;
219
220         sigc::signal<void, String> signal_removed_from_group_;
221
222         /*
223  -- ** -- S I G N A L   I N T E R F A C E -------------------------------------
224         */
225
226 public:
227
228         //!     Status Changed
229         sigc::signal<void>& signal_status_changed() { return signal_status_changed_; }
230
231         //!     Parameter changed
232         sigc::signal<void,String>& signal_param_changed() { return signal_param_changed_; }
233
234         //!     Description Changed
235         sigc::signal<void>& signal_description_changed() { return signal_description_changed_;}
236
237         //!     Moved
238         sigc::signal<void, int, etl::handle<Canvas> >& signal_moved() { return signal_moved_; }
239
240         sigc::signal<void, String>& signal_added_to_group() { return signal_added_to_group_; }
241
242         sigc::signal<void, String>& signal_removed_from_group() { return signal_removed_from_group_; }
243
244         /*
245  --     ** -- C O N S T R U C T O R S ---------------------------------------------
246         */
247
248 protected:
249
250         Layer();
251
252 public:
253         virtual ~Layer();
254
255         /*
256  --     ** -- M E M B E R   F U N C T I O N S -------------------------------------
257         */
258
259 public:
260
261         virtual void on_canvas_set();
262
263         //! Adds this layer to the given layer group
264         void add_to_group(const String&);
265
266         //! Removes this layer from the given layer group
267         void remove_from_group(const String&);
268
269         //! Removes this layer from all layer groups
270         void remove_from_all_groups();
271
272         //! Gets the name of the group that this layer belongs to
273         String get_group()const;
274
275         //! writeme
276         //DynamicParamList &dynamic_param_list() { return dynamic_param_list_; }
277
278         //! \todo writeme
279         const DynamicParamList &dynamic_param_list()const { return dynamic_param_list_; }
280
281         bool connect_dynamic_param(const String& param, etl::loose_handle<ValueNode>);
282         bool disconnect_dynamic_param(const String& param);
283
284         //! Enables the layer for rendering (Making it \em active)
285         void enable() { set_active(true); }
286
287         //! Disables the layer for rendering. (Making it \em inactive)
288         /*! When a layer is disabled, it will be skipped when the
289         **      canvas is rendered. */
290         void disable() { set_active(false); }
291
292         //! Sets the 'active' flag for the Layer to the state described by \a x
293         /*! When a layer is disabled, it will be skipped when the
294         **      canvas is rendered. */
295         void set_active(bool x);
296
297         //! Returns that status of the 'active' flag
298         bool active()const { return active_; }
299
300         //! Returns the position of the layer in the canvas.
301         /*! Returns negative on error */
302         int get_depth()const;
303
304         //! \writeme
305         float get_z_depth()const { return z_depth_; }
306
307         //! \writeme
308         float get_z_depth(const synfig::Time& t)const;
309
310         //! \writeme
311         void set_z_depth(float x) { z_depth_=x; }
312
313         //! Sets the Canvas that this Layer is a part of
314         void set_canvas(etl::loose_handle<Canvas> canvas);
315
316         //! Returns a handle to the Canvas to which this Layer belongs
317         etl::loose_handle<Canvas> get_canvas()const;
318
319         //! \writeme
320         const String& get_description()const { return description_; }
321
322         //! \writeme
323         void set_description(const String& x);
324
325         //! Returns the layer's description if it's not empty, else its local name
326         const String get_non_empty_description()const { return get_description().empty() ? get_local_name() : get_description(); }
327
328         //! Returns the localised version of the given layer parameter
329         const String get_param_local_name(const String &param_name)const;
330
331         /*
332  --     ** -- V I R T U A L   F U N C T I O N S -----------------------------------
333         */
334
335 public:
336         virtual Rect get_bounding_rect()const;
337
338         virtual Rect get_full_bounding_rect(Context context)const;
339
340         //! Returns a string containing the name of the Layer
341         virtual String get_name()const;
342
343         //! Returns a string containing the localized name of the Layer
344         virtual String get_local_name()const;
345
346         //! Gets the parameter vocabulary
347         virtual Vocab get_param_vocab()const;
348
349         //! Gets the version string for this layer
350         virtual String get_version()const;
351
352         //! \writeme
353         virtual etl::handle<Transform> get_transform()const;
354
355         //! Sets the virtual version to use for backwards-compatibility
356         /*!
357         **      \see reset_version() */
358         virtual bool set_version(const String &ver);
359
360         //! Resets the virtual version
361         /*!
362         **      \see set_version() */
363         virtual void reset_version();
364
365         //!     Sets the parameter described by \a param to \a value.
366         /*!     \param param The name of the parameter to set
367         **      \param value What the parameter is to be set to.
368         **      \return \c true on success, \c false upon rejection or failure.
369         **              If it returns \c false, then the Layer is assumed to remain unchanged.
370         **      \sa get_param()
371         **      \todo \a param should be of the type <tt>const String \&param</tt>
372         */
373         virtual bool set_param(const String &param, const ValueBase &value);
374
375         //!     Sets a list of parameters
376         virtual bool set_param_list(const ParamList &);
377
378         //! Get the value of the specified parameter.
379         /*!     \return The requested parameter value, or (upon failure) a NIL ValueBase.
380         **      \sa set_param()
381         **      \todo \a param should be of the type <tt>const String \&</tt>
382         */
383         virtual ValueBase get_param(const String &param)const;
384
385         //! Get a list of all of the parameters and their values
386         virtual ParamList get_param_list()const;
387
388         //! Sets the \a time for the selected Layer and those under it
389         /*!     \param context          Context iterator referring to next Layer.
390         **      \param time                     writeme
391         **      \see Handle::set_time()
392         */
393         virtual void set_time(Context context, Time time)const;
394
395         //! Sets the \a time for the selected Layer and those under it for a specific \a point
396         /*!     \param context          Context iterator referring to next Layer.
397         **      \param time                     writeme
398         **      \param point            writeme
399         **      \see Handle::set_time()
400         **      \todo \a point should be of the type <tt>const Point \&</tt> */
401         virtual void set_time(Context context, Time time, const Point &point)const;
402
403         //! Gets the color of the Canvas at \a pos
404         /*!     \param context          Context iterator referring to next Layer.
405         **      \param pos              Point which indicates where the Color should come from
406         **      \see Handle::get_color()
407         */
408         virtual Color get_color(Context context, const Point &pos)const;
409
410         //! Renders the Canvas to the given Surface in an accelerated manner
411         /*!     \param context          Context iterator referring to next Layer.
412         **      \param surface          Pointer to Surface to render to.
413         **      \param quality          The requested quality-level to render at.
414         **      \param renddesc         The associated RendDesc.
415         **      \param cb                       Pointer to callback object. May be NULL if there is no callback.
416         **      \return \c true on success, \c false on failure
417         **      \see Handle::accelerated_render()
418         */
419         virtual bool accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const;
420
421         //! Checks to see if a part of the layer is directly under \a point
422         /*!     \param context          Context iterator referring to next Layer.
423         **      \param point            The point to check
424         **      \return         The handle of the layer under \a point. If there is not
425         **                              a layer under \a point, then returns an empty handle. */
426         virtual Handle hit_check(Context context, const Point &point)const;
427
428         //! Duplicates the Layer
429         virtual Handle clone(const GUID& deriv_guid=GUID())const;
430
431         //! Returns true if the layer needs to be able to examine its context.
432         /*! context to render itself, other than for simple blending.  For
433         **  example, the blur layer will return true - it can't do its job
434         **  if it can't see its context, and the circle layer will return
435         **  false - rendering a circle doesn't depend on the underlying
436         **  context until the final blend operation. */
437         virtual bool reads_context()const;
438
439 #ifdef THIS_CODE_IS_NOT_USED
440         //! Duplicates the Layer without duplicating the value nodes
441         virtual Layer *simple_clone()const;
442 #endif /* THIS_CODE_IS_NOT_USED */
443
444 protected:
445
446         //! This is called whenever a parameter is changed
447         virtual void on_changed();
448
449         //! Called to figure out the animation time information
450         virtual void get_times_vfunc(Node::time_set &set) const;
451
452         /*
453  --     ** -- S T A T I C  F U N C T I O N S --------------------------------------
454         */
455
456 public:
457
458         //! Creates a Layer of type \a type
459         /*!     If the Layer type is unknown, then a Mime layer is created in its place.
460         **      \param type     A string describing the name of the layer to construct.
461         **      \return Always returns a handle to a new Layer.
462         **      \see Mime
463         */
464         static Layer::LooseHandle create(const String &type);
465
466 }; // END of class Layer
467
468 }; // END of namespace synfig
469
470
471 /* === E N D =============================================================== */
472
473 #endif