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 ** Copyright (c) 2011-2013 Carlos López 11 ** 12 ** This package is free software; you can redistribute it and/or 13 ** modify it under the terms of the GNU General Public License as 14 ** published by the Free Software Foundation; either version 2 of 15 ** the License, or (at your option) any later version. 16 ** 17 ** This package is distributed in the hope that it will be useful, 18 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20 ** General Public License for more details. 21 ** \endlegal 22 */ 23 /* ========================================================================= */ 24 25 /* === S T A R T =========================================================== */ 26 27 #ifndef __SYNFIG_LAYER_H 28 #define __SYNFIG_LAYER_H 29 30 /* === H E A D E R S ======================================================= */ 31 32 #include <map> 33 34 #include <ETL/handle> 35 36 #include <sigc++/signal.h> 37 #include <sigc++/connection.h> 38 39 #include "cairo.h" 40 #include "guid.h" 41 #include "interpolation.h" 42 #include "node.h" 43 #include "paramdesc.h" 44 #include "progresscallback.h" 45 #include "real.h" 46 #include "rendermethod.h" 47 #include "rendering/task.h" 48 #include "string.h" 49 #include "time.h" 50 #include "vector.h" 51 #include "value.h" 52 #include <giomm.h> 53 54 /* === M A C R O S ========================================================= */ 55 56 // This macros should be removed when rendering optimization complete 57 #define RENDER_TRANSFORMED_IF_NEED(file, line) \ 58 if (!renddesc.get_transformation_matrix().is_identity()) \ 59 return render_transformed(this, context, surface, quality, renddesc, cb, file, line); 60 61 62 //! Defines various variables and the create method, common for all importers. 63 //! To be used in the private part of the importer class definition. 64 #define SYNFIG_LAYER_MODULE_EXT \ 65 public: \ 66 static const char name__[], version__[], cvs_id__[], local_name__[], category__[]; \ 67 static Layer *create(); 68 69 //! Sets the name of the layer 70 #define SYNFIG_LAYER_SET_NAME(class,x) \ 71 const char class::name__[]=x 72 73 //! Sets the local name of the layer 74 #define SYNFIG_LAYER_SET_LOCAL_NAME(class,x) \ 75 const char class::local_name__[]=x; 76 77 //! Sets the category of the layer 78 #define SYNFIG_LAYER_SET_CATEGORY(class,x) \ 79 const char class::category__[]=x 80 81 //! Sets the version string for the layer 82 #define SYNFIG_LAYER_SET_VERSION(class,x) \ 83 const char class::version__[]=x 84 85 //! Sets the CVS ID string for the layer 86 #define SYNFIG_LAYER_SET_CVS_ID(class,x) \ 87 const char class::cvs_id__[]=x 88 89 //! Defines de implementation of the create method for the importer 90 #define SYNFIG_LAYER_INIT(class) \ 91 synfig::Layer* class::create() \ 92 { \ 93 return new class(); \ 94 } 95 96 //! Imports a parameter if it is of the same type as param 97 #define IMPORT_VALUE(x) \ 98 if (#x=="param_"+param && x.get_type()==value.get_type()) \ 99 { \ 100 x=value; \ 101 static_param_changed(param); \ 102 return true; \ 103 } 104 105 //! Imports a parameter 'x' and perform an action usually based on 106 //! some condition 'y' 107 #define IMPORT_VALUE_PLUS_BEGIN(x) \ 108 if (#x=="param_"+param && x.get_type()==value.get_type()) \ 109 { \ 110 x=value; \ 111 { 112 #define IMPORT_VALUE_PLUS_END \ 113 } \ 114 static_param_changed(param); \ 115 return true; \ 116 } 117 #define IMPORT_VALUE_PLUS(x,y) \ 118 IMPORT_VALUE_PLUS_BEGIN(x) \ 119 y; \ 120 IMPORT_VALUE_PLUS_END 121 122 //! Exports a parameter if it is the same type as value 123 #define EXPORT_VALUE(x) \ 124 if (#x=="param_"+param) \ 125 { \ 126 synfig::ValueBase ret; \ 127 ret.copy(x); \ 128 return ret; \ 129 } 130 131 //! Exports the name or the local name of the layer 132 #define EXPORT_NAME() \ 133 if (param=="Name" || param=="name" || param=="name__") \ 134 return name__; \ 135 else if (param=="local_name__") \ 136 return dgettext("synfig",local_name__); 137 138 //! Exports the version of the layer 139 #define EXPORT_VERSION() \ 140 if (param=="Version" || param=="version" || param=="version__") \ 141 return version__; 142 143 //! This is used as the category for layer book entries which represent aliases of layers. 144 //! It prevents these layers showing up in the menu. 145 #define CATEGORY_DO_NOT_USE "Do Not Use" 146 147 //! Sets the interpolation defaults for the layer 148 #define SET_INTERPOLATION_DEFAULTS() \ 149 { \ 150 Vocab vocab(get_param_vocab()); \ 151 Vocab::const_iterator viter; \ 152 for(viter=vocab.begin();viter!=vocab.end();viter++) \ 153 { \ 154 ValueBase v=get_param(viter->get_name()); \ 155 v.set_interpolation(viter->get_interpolation()); \ 156 set_param(viter->get_name(), v); \ 157 } \ 158 } \ 159 160 //! Sets the static defaults for the layer 161 #define SET_STATIC_DEFAULTS() \ 162 { \ 163 Vocab vocab(get_param_vocab()); \ 164 Vocab::const_iterator viter; \ 165 for(viter=vocab.begin();viter!=vocab.end();viter++) \ 166 { \ 167 ValueBase v=get_param(viter->get_name()); \ 168 v.set_static(viter->get_static()); \ 169 set_param(viter->get_name(), v); \ 170 } \ 171 } \ 172 173 /* === T Y P E D E F S ===================================================== */ 174 175 /* === C L A S S E S & S T R U C T S ======================================= */ 176 177 namespace synfig { 178 179 class CairoColor; 180 class CairoSurface; 181 class Canvas; 182 class Color; 183 class Context; 184 class ContextParams; 185 class IndependentContext; 186 class Rect; 187 class RendDesc; 188 class SoundProcessor; 189 class Surface; 190 class Transform; 191 class ValueNode; 192 193 194 /*! \class Layer 195 ** \todo writeme 196 ** \see Canvas 197 */ 198 class Layer : public Node 199 { 200 friend class ValueNode; 201 friend class IndependentContext; 202 friend class Context; 203 204 /* 205 -- ** -- T Y P E S ----------------------------------------------------------- 206 */ 207 208 public: 209 210 //! Type that represents a pointer to a Layer's constructor. 211 /*! As a pointer to the constructor, it represents a "factory" of layers. 212 */ 213 typedef Layer* (*Factory)(); 214 215 struct BookEntry 216 { 217 Factory factory; 218 String name; 219 String local_name; 220 String category; 221 String cvs_id; 222 String version; BookEntryBookEntry223 BookEntry(): factory() { } BookEntryBookEntry224 BookEntry(Factory factory, 225 const String &name, 226 const String &local_name, 227 const String &category, 228 const String &cvs_id, 229 const String &version): 230 factory(factory), 231 name(name), 232 local_name(local_name), 233 category(category), 234 cvs_id(cvs_id), 235 version(version) { } 236 }; 237 238 //! Book of types of layers indexed by layer type name. 239 /*! While the sifz file is read, each time a new layer entry is found, 240 ** the factory constructor that the "factory" pointer member of the 241 ** "BookEntry" struct points to, is called, and a new layer of that type 242 ** is created. 243 ** \sa Layer::Factory 244 */ 245 typedef std::map<String,BookEntry> Book; 246 247 static void register_in_book(const BookEntry &); 248 249 static Book& book(); 250 251 //! Inits the book of layers and inserts in it the basic layers that 252 //! doesn't depend on modules 253 /*! \todo motionblur should be in the mod_filter module 254 */ 255 static bool subsys_init(); 256 257 //! Stops the layer system by deleting the book of registered layers 258 static bool subsys_stop(); 259 260 //! Map of Value Base parameters indexed by name 261 typedef std::map<String,ValueBase> ParamList; 262 263 typedef etl::handle<Layer> Handle; 264 265 typedef etl::loose_handle<Layer> LooseHandle; 266 267 typedef etl::handle<const Layer> ConstHandle; 268 269 //! Map of parameters that are animated Value Nodes indexed by the param name 270 typedef std::map<String,etl::rhandle<ValueNode> > DynamicParamList; 271 272 //! A list type which describes all the parameters that a layer has. 273 /*! \see get_param_vocab() */ 274 typedef ParamVocab Vocab; 275 276 /* 277 -- ** -- D A T A ------------------------------------------------------------- 278 */ 279 280 private: 281 282 /*! \c true if the layer is visible, \c false if it is to be skipped 283 ** \see set_active(), enable(), disable, active() 284 */ 285 bool active_; 286 287 //! flag to prevent re-apply optimization features 288 bool optimized_; 289 290 /*! When \c true, layer will skipped while final rendering 291 ** but will still present onto work view. 292 ** \see set_exclude_from_rendering(), get_exclude_from_rendering() 293 */ 294 bool exclude_from_rendering_; 295 296 //! Handle to the canvas to which this layer belongs 297 etl::loose_handle<Canvas> canvas_; 298 299 //! Map of parameter with animated value nodes 300 DynamicParamList dynamic_param_list_; 301 302 //! A description of what this layer does 303 String description_; 304 305 //! The depth parameter of the layer in the layer stack 306 ValueBase param_z_depth; 307 308 //! \writeme 309 mutable Time time_mark; 310 mutable Real outline_grow_mark; 311 312 //! Contains the name of the group that this layer belongs to 313 String group_; 314 315 //! Signal to connect to the signal_deleted canvas's member 316 //! Used to do let a layer with a canvas parent that doesn't exists 317 //! Instead of that it connects to a zero canvas 318 //! \see Layer::set_canvas() 319 sigc::connection parent_death_connect_; 320 321 // added ability to monitor file changes 322 Glib::RefPtr<Gio::FileMonitor> file_monitor; 323 void on_file_changed(const Glib::RefPtr<Gio::File>&, const Glib::RefPtr<Gio::File>&, Gio::FileMonitorEvent); 324 sigc::connection monitor_connection; 325 std::string monitored_path; 326 public: 327 bool monitor(const std::string& path); // append file monitor (returns true on success, false on fail) 328 329 330 /* 331 -- ** -- S I G N A L S ------------------------------------------------------- 332 */ 333 334 private: 335 336 //! Status Changed 337 sigc::signal<void> signal_status_changed_; 338 339 //! Parameter changed 340 sigc::signal<void,String> signal_param_changed_; 341 342 //! Description Changed 343 sigc::signal<void> signal_description_changed_; 344 345 //! Moved 346 sigc::signal<void, int, etl::handle<Canvas> > signal_moved_; 347 348 sigc::signal<void, String> signal_added_to_group_; 349 350 sigc::signal<void, String> signal_removed_from_group_; 351 352 sigc::signal<void, String> signal_static_param_changed_; 353 354 sigc::signal<void, String> signal_dynamic_param_changed_; 355 356 /* 357 -- ** -- S I G N A L I N T E R F A C E ------------------------------------- 358 */ 359 360 public: 361 362 //! Status Changed signal_status_changed()363 sigc::signal<void>& signal_status_changed() { return signal_status_changed_; } 364 365 //! Parameter changed signal_param_changed()366 sigc::signal<void,String>& signal_param_changed() { return signal_param_changed_; } 367 368 //! Description Changed signal_description_changed()369 sigc::signal<void>& signal_description_changed() { return signal_description_changed_;} 370 371 //! Moved signal_moved()372 sigc::signal<void, int, etl::handle<Canvas> >& signal_moved() { return signal_moved_; } 373 signal_added_to_group()374 sigc::signal<void, String>& signal_added_to_group() { return signal_added_to_group_; } 375 signal_removed_from_group()376 sigc::signal<void, String>& signal_removed_from_group() { return signal_removed_from_group_; } 377 signal_static_param_changed()378 sigc::signal<void, String>& signal_static_param_changed() { return signal_static_param_changed_; } 379 signal_dynamic_param_changed()380 sigc::signal<void, String>& signal_dynamic_param_changed() { return signal_dynamic_param_changed_; } 381 382 /* 383 -- ** -- C O N S T R U C T O R S --------------------------------------------- 384 */ 385 386 protected: 387 388 void static_param_changed(const String ¶m); 389 void dynamic_param_changed(const String ¶m); 390 391 Layer(); 392 393 public: 394 virtual ~Layer(); 395 396 /* 397 -- ** -- M E M B E R F U N C T I O N S ------------------------------------- 398 */ 399 400 public: 401 402 virtual void on_canvas_set(); 403 virtual void on_static_param_changed(const String ¶m); 404 virtual void on_dynamic_param_changed(const String ¶m); 405 406 //! Adds this layer to the given layer group 407 void add_to_group(const String&); 408 409 //! Removes this layer from the given layer group 410 void remove_from_group(const String&); 411 412 //! Removes this layer from all layer groups 413 void remove_from_all_groups(); 414 415 //! Gets the name of the group that this layer belongs to 416 String get_group()const; 417 418 //! Retrieves the dynamic param list member 419 //! \see DynamicParamList dynamic_param_list()420 const DynamicParamList &dynamic_param_list()const { return dynamic_param_list_; } 421 422 //! Enables the layer for rendering (Making it \em active) enable()423 void enable() { set_active(true); } 424 425 //! Disables the layer for rendering. (Making it \em inactive) 426 /*! When a layer is disabled, it will be skipped when the 427 ** canvas is rendered. */ disable()428 void disable() { set_active(false); } 429 430 //! Sets the 'active' flag for the Layer to the state described by \a x 431 /*! When a layer is disabled, it will be skipped when the 432 ** canvas is rendered. */ 433 void set_active(bool x); 434 435 //! Returns that status of the 'active' flag active()436 bool active()const { return active_; } 437 438 //! flag to prevent re-apply optimization features optimized()439 bool optimized()const { return optimized_; } 440 441 //! set flag to prevent re-apply optimization features set_optimized(bool x)442 void set_optimized(bool x) { optimized_ = x; } 443 444 //! Sets the 'exclude_from_rendering' flag for the Layer 445 /*! When set, layer will skipped while final rendering 446 ** but will still present onto work view. */ 447 void set_exclude_from_rendering(bool x); 448 449 //! Returns that status of the 'exclude_from_rendering' flag get_exclude_from_rendering()450 bool get_exclude_from_rendering()const { return exclude_from_rendering_; } 451 452 //! Returns the position of the layer in the canvas. 453 /*! Returns negative on error */ 454 int get_depth()const; 455 456 //! Gets the non animated z depth of the layer get_z_depth()457 float get_z_depth()const { return param_z_depth.get(Real()); } 458 459 //! Gets the z depth of the layer at a time t 460 float get_z_depth(const synfig::Time& t)const; 461 462 //! Gets the true z depth of the layer (index + parameter) 463 float get_true_z_depth(const synfig::Time& t)const; 464 465 //! Gets the true z depth of the layer (index + parameter) for current time 466 float get_true_z_depth()const; 467 468 //! Sets the z depth of the layer (non animated) set_z_depth(float x)469 void set_z_depth(float x) { param_z_depth=ValueBase(Real(x)); } 470 471 //! Sets the Canvas that this Layer is a part of 472 void set_canvas(etl::loose_handle<Canvas> canvas); 473 474 //! Returns a handle to the Canvas to which this Layer belongs 475 etl::loose_handle<Canvas> get_canvas()const; 476 477 //! Returns the description of the layer get_description()478 const String& get_description()const { return description_; } 479 480 481 String get_string()const; 482 483 //! Sets the description of the layer 484 void set_description(const String& x); 485 486 //! Returns the layer's description if it's not empty, else its local name get_non_empty_description()487 const String get_non_empty_description()const { return get_description().empty() ? get_local_name() : get_description(); } 488 489 //! Returns the localised version of the given layer parameter 490 const String get_param_local_name(const String ¶m_name)const; 491 492 //! Returns a handle to the Parent PasteCanvas layer or NULL if layer belongs to root canvas 493 /*! Notice that it could return the wrong handle to PasteCanvas if the layer */ 494 /*! belongs to a exported canvas (canvas can be referenced multiple times)*/ 495 Layer::LooseHandle get_parent_paste_canvas_layer()const; 496 497 /* 498 -- ** -- V I R T U A L F U N C T I O N S ----------------------------------- 499 */ 500 501 public: 502 //! Returns the rectangle that includes the layer 503 //! \see synfig::Rect 504 virtual Rect get_bounding_rect()const; 505 506 //!Returns the rectangle that includes the context of the layer 507 //!\see synfig::Rect synfig::Context 508 virtual Rect get_full_bounding_rect(Context context)const; 509 510 //! Returns a string containing the name of the Layer 511 virtual String get_name()const; 512 513 //! Returns a string containing the localized name of the Layer 514 virtual String get_local_name()const; 515 516 //! Gets the parameter vocabulary 517 virtual Vocab get_param_vocab()const; 518 519 //! Gets the version string for this layer 520 virtual String get_version()const; 521 522 //! Returns a handle to the Transform class of the layer 523 //! \see synfig::Transform 524 virtual etl::handle<Transform> get_transform()const; 525 526 //! Sets the virtual version to use for backwards-compatibility 527 /*! 528 ** \see reset_version() */ 529 virtual bool set_version(const String &ver); 530 531 //! Resets the virtual version 532 /*! 533 ** \see set_version() */ 534 virtual void reset_version(); 535 536 //! Sets the parameter described by \a param to \a value. 537 /*! \param param The name of the parameter to set 538 ** \param value What the parameter is to be set to. 539 ** \return \c true on success, \c false upon rejection or failure. 540 ** If it returns \c false, then the Layer is assumed to remain unchanged. 541 ** \sa get_param() 542 ** \todo \a param should be of the type <tt>const String \¶m</tt> 543 */ 544 virtual bool set_param(const String ¶m, const ValueBase &value); 545 546 //! Sets a list of parameters 547 virtual bool set_param_list(const ParamList &); 548 549 //! Get the value of the specified parameter. 550 /*! \return The requested parameter value, or (upon failure) a NIL ValueBase. 551 ** \sa set_param() 552 ** \todo \a param should be of the type <tt>const String \&</tt> 553 */ 554 virtual ValueBase get_param(const String ¶m)const; 555 556 //! Get a list of all of the parameters and their values 557 virtual ParamList get_param_list()const; 558 get_time_mark()559 Time get_time_mark() const { return time_mark; } set_time_mark(Time time)560 void set_time_mark(Time time) const { time_mark = time; } clear_time_mark()561 void clear_time_mark() const { time_mark = Time::end(); } 562 get_outline_grow_mark()563 Real get_outline_grow_mark() const { return outline_grow_mark; } set_outline_grow_mark(Real outline_grow)564 void set_outline_grow_mark(Real outline_grow) const { outline_grow_mark = outline_grow; } clear_outline_grow_mark()565 void clear_outline_grow_mark() const { outline_grow_mark = 0.0; } 566 567 //! Sets the \a time for the Layer and those under it 568 /*! \param context Context iterator referring to next Layer. 569 ** \param time writeme 570 ** \see Context::set_time() 571 */ 572 void set_time(IndependentContext context, Time time)const; 573 574 //! Sets the \a outline_grow for the Layer and those under it 575 /*! \param context Context iterator referring to next Layer. 576 ** \param outline_grow writeme 577 ** \see Context::set_outline_grow() 578 */ 579 void set_outline_grow(IndependentContext context, Real outline_grow)const; 580 581 //! Gets the blend color of the Layer in the context at \a pos 582 /*! \param context Context iterator referring to next Layer. 583 ** \param pos Point which indicates where the Color should come from 584 ** \see Context::get_color() 585 */ 586 virtual Color get_color(Context context, const Point &pos)const; 587 virtual CairoColor get_cairocolor(Context context, const Point &pos)const; 588 589 //! Sets the render method to the layer. Not all layers uses this virtual member 590 virtual void set_render_method(Context context, RenderMethod x); 591 592 // Temporary function to render transformed layer for leyers which yet not suppurt transformed rendering 593 static bool render_transformed(const Layer *layer, Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb, const char *file, int line); 594 595 //! Renders the Canvas to the given Surface in an accelerated manner 596 /*! \param context Context iterator referring to next Layer. 597 ** \param surface Pointer to Surface to render to. 598 ** \param quality The requested quality-level to render at. 599 ** \param renddesc The associated RendDesc. 600 ** \param cb Pointer to callback object. May be NULL if there is no callback. 601 ** \return \c true on success, \c false on failure 602 ** \see Context::accelerated_render() 603 */ 604 virtual bool accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const; 605 virtual bool accelerated_cairorender(Context context, cairo_t* cr, int quality, const RendDesc &renddesc, ProgressCallback *cb)const; 606 607 protected: 608 virtual void set_time_vfunc(IndependentContext context, Time time) const; 609 virtual void set_outline_grow_vfunc(IndependentContext context, Real outline_grow) const; 610 virtual rendering::Task::Handle build_rendering_task_vfunc(Context context) const; 611 612 virtual RendDesc get_sub_renddesc_vfunc(const RendDesc &renddesc) const; 613 virtual void get_sub_renddesc_vfunc(const RendDesc &renddesc, std::vector<RendDesc> &out_descs) const; 614 615 public: 616 void get_sub_renddesc(const RendDesc &renddesc, std::vector<RendDesc> &out_descs) const; 617 RendDesc get_sub_renddesc(const RendDesc &renddesc, int index = 0) const; 618 619 //! Returns rendering task for context 620 /*! \param context Context iterator referring to next Layer. 621 ** \return \c null on failure 622 ** \see Context::build_rendering_task() 623 */ 624 rendering::Task::Handle build_rendering_task(Context context)const; 625 626 //! Checks to see if a part of the layer is directly under \a point 627 /*! \param context Context iterator referring to next Layer. 628 ** \param point The point to check 629 ** \return The handle of the layer under \a point. If there is not 630 ** a layer under \a point, then returns an empty handle. 631 ** \see Context::hit_check 632 */ 633 virtual Handle hit_check(Context context, const Point &point)const; 634 635 //! Duplicates the Layer 636 virtual Handle clone(etl::loose_handle<Canvas> canvas, const GUID& deriv_guid=GUID())const; 637 638 //! Returns true if the layer needs to be able to examine its context. 639 /*! context to render itself, other than for simple blending. For 640 ** example, the blur layer will return true - it can't do its job 641 ** if it can't see its context, and the circle layer will return 642 ** false - rendering a circle doesn't depend on the underlying 643 ** context until the final blend operation. */ 644 virtual bool reads_context()const; 645 646 //! Duplicates the Layer without duplicating the value nodes 647 virtual Handle simple_clone()const; 648 649 //! Connects the parameter to another Value Node 650 virtual bool connect_dynamic_param(const String& param, etl::loose_handle<ValueNode>); 651 652 //! Disconnects the parameter from any Value Node 653 virtual bool disconnect_dynamic_param(const String& param); 654 655 virtual void fill_sound_processor(SoundProcessor &soundProcessor) const; 656 657 protected: 658 659 //! This is called whenever a parameter is changed 660 virtual void on_changed(); 661 662 virtual void on_child_changed(const Node *x); 663 664 //! Called to figure out the animation time information 665 virtual void get_times_vfunc(Node::time_set &set) const; 666 667 /* 668 -- ** -- S T A T I C F U N C T I O N S -------------------------------------- 669 */ 670 671 public: 672 673 //! Creates a Layer of type \a type 674 /*! If the Layer type is unknown, then a Mime layer is created in its place. 675 ** \param type A string describing the name of the layer to construct. 676 ** \return Always returns a handle to a new Layer. 677 ** \see Mime 678 */ 679 static Layer::LooseHandle create(const String &type); 680 681 }; // END of class Layer 682 683 }; // END of namespace synfig 684 685 686 /* === E N D =============================================================== */ 687 688 #endif 689