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