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