Remove .gitignore do nothing is ignored.
[synfig.git] / synfig-core / trunk / src / synfig / layer_duplicate.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layer_duplicate.cpp
3 **      \brief Implementation of the "Duplicate" layer
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 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 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "string.h"
34 #include "layer_duplicate.h"
35 #include "time.h"
36 #include "context.h"
37 #include "paramdesc.h"
38 #include "renddesc.h"
39 #include "surface.h"
40 #include "value.h"
41 #include "valuenode.h"
42 #include "canvas.h"
43
44 #endif
45
46 /* === U S I N G =========================================================== */
47
48 using namespace synfig;
49 using namespace etl;
50 using namespace std;
51
52 /* === G L O B A L S ======================================================= */
53
54 SYNFIG_LAYER_INIT(Layer_Duplicate);
55 SYNFIG_LAYER_SET_NAME(Layer_Duplicate,"duplicate");
56 SYNFIG_LAYER_SET_LOCAL_NAME(Layer_Duplicate,N_("Duplicate"));
57 SYNFIG_LAYER_SET_CATEGORY(Layer_Duplicate,N_("Other"));
58 SYNFIG_LAYER_SET_VERSION(Layer_Duplicate,"0.1");
59 SYNFIG_LAYER_SET_CVS_ID(Layer_Duplicate,"$Id$");
60
61 /* === M E M B E R S ======================================================= */
62
63 Layer_Duplicate::Layer_Duplicate():
64         Layer_Composite(1.0,Color::BLEND_COMPOSITE)
65 {
66         LinkableValueNode* index_value_node = ValueNode_Duplicate::create(Real(3));
67         connect_dynamic_param("index", index_value_node);
68 }
69
70 Layer::Handle
71 Layer_Duplicate::clone(const GUID& deriv_guid)const
72 {
73         Layer::Handle ret = (Layer::Handle)Layer_Composite::clone(deriv_guid);
74
75         const DynamicParamList &dpl = dynamic_param_list();
76         DynamicParamList::const_iterator iter = dpl.find("index");
77
78         // if we have a dynamic "index" parameter, make a new one in the clone
79         // it's not good to have two references to the same index valuenode,
80         // or nested duplications cause an infinite loop
81         if (iter != dpl.end())
82                 ret->connect_dynamic_param(iter->first,iter->second->clone(deriv_guid));
83
84         return ret;
85 }
86
87 bool
88 Layer_Duplicate::set_param(const String &param, const ValueBase &value)
89 {
90         IMPORT(index);
91         return Layer_Composite::set_param(param,value);
92 }
93
94 ValueBase
95 Layer_Duplicate::get_param(const String &param)const
96 {
97         EXPORT(index);
98
99         EXPORT_NAME();
100         EXPORT_VERSION();
101
102         return Layer_Composite::get_param(param);
103 }
104
105 void
106 Layer_Duplicate::set_time(Context context, Time time)const
107 {
108         context.set_time(time);
109         time_cur=time;
110 }
111
112 void
113 Layer_Duplicate::set_time(Context context, Time time, const Point &pos)const
114 {
115         context.set_time(time,pos);
116         time_cur=time;
117 }
118
119 Color
120 Layer_Duplicate::get_color(Context context, const Point &pos)const
121 {
122         handle<ValueNode_Duplicate> duplicate_param = get_duplicate_param();
123         if (!duplicate_param) return context.get_color(pos);
124
125         Color::BlendMethod blend_method(get_blend_method());
126         float amount(get_amount());
127         Color color;
128
129         Mutex::Lock lock(mutex);
130         duplicate_param->reset_index(time_cur);
131         do
132         {
133                 context.set_time(time_cur+1);
134                 context.set_time(time_cur);
135                 color = Color::blend(context.get_color(pos),color,amount,blend_method);
136         } while (duplicate_param->step(time_cur));
137
138         return color;
139 }
140
141 Layer::Vocab
142 Layer_Duplicate::get_param_vocab()const
143 {
144         Layer::Vocab ret;
145         ret=Layer_Composite::get_param_vocab();
146
147         ret.push_back(ParamDesc("index")
148                 .set_local_name(_("Index"))
149                 .set_description(_("Copy Index"))
150         );
151
152         return ret;
153 }
154
155 ValueNode_Duplicate::Handle
156 Layer_Duplicate::get_duplicate_param()const
157 {
158         const DynamicParamList &dpl = dynamic_param_list();
159         DynamicParamList::const_iterator iter = dpl.find("index");
160         if (iter == dpl.end()) return NULL;
161         etl::rhandle<ValueNode> param(iter->second);
162         return ValueNode_Duplicate::Handle::cast_dynamic(param);
163 }
164
165 bool
166 Layer_Duplicate::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
167 {
168         if(quality == 10)
169                 return context.accelerated_render(surface,quality,renddesc,cb);
170
171         if(context->empty())
172         {
173                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
174                 surface->clear();
175                 return true;
176         }
177
178         SuperCallback subimagecb;
179         Surface tmp;
180         int i = 0;
181
182         handle<ValueNode_Duplicate> duplicate_param = get_duplicate_param();
183         if (!duplicate_param) return context.accelerated_render(surface,quality,renddesc,cb);
184
185         surface->set_wh(renddesc.get_w(),renddesc.get_h());
186         surface->clear();
187
188         Color::BlendMethod blend_method(get_blend_method());
189         int steps = duplicate_param->count_steps(time_cur);
190
191         Mutex::Lock lock(mutex);
192         duplicate_param->reset_index(time_cur);
193         do
194         {
195                 subimagecb=SuperCallback(cb,i*(5000/steps),(i+1)*(5000/steps),5000);
196                 // \todo can we force a re-evaluation of all the variables without changing the time twice?
197                 context.set_time(time_cur+1);
198                 context.set_time(time_cur);
199                 if(!context.accelerated_render(&tmp,quality,renddesc,&subimagecb)) return false;
200
201                 Surface::alpha_pen apen(surface->begin());
202                 apen.set_alpha(get_amount());
203                 // \todo have a checkbox allowing use of 'behind' to reverse the order?
204                 apen.set_blend_method(i ? blend_method : Color::BLEND_COMPOSITE);
205                 tmp.blit_to(apen);
206                 i++;
207         } while (duplicate_param->step(time_cur));
208
209         return true;
210 }