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