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