1 /* === S Y N F I G ========================================================= */
2 /*! \file layerpaint.cpp
3 ** \brief Template File
4 **
5 ** $Id$
6 **
7 ** \legal
8 ** ......... ... 2014 Ivan Mahonin
9 **
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
14 **
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
19 ** \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 # include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif
31
32 #include <synfig/general.h>
33
34 #include "layerpaint.h"
35 #include <synfigapp/canvasinterface.h>
36 #include <synfigapp/localization.h>
37 #include <synfigapp/instance.h>
38
39 #include <synfig/layers/layer_pastecanvas.h>
40 #include <synfig/valuenodes/valuenode_composite.h>
41 #include <synfig/rendering/software/surfacesw.h>
42
43 #endif
44
45 using namespace std;
46 using namespace etl;
47 using namespace synfig;
48 using namespace synfigapp;
49 using namespace Action;
50
51 /* === M A C R O S ========================================================= */
52
53 ACTION_INIT(Action::LayerPaint);
54 ACTION_SET_NAME(Action::LayerPaint,"LayerPaint");
55 ACTION_SET_LOCAL_NAME(Action::LayerPaint,N_("Paint"));
56 ACTION_SET_TASK(Action::LayerPaint,"paint");
57 ACTION_SET_CATEGORY(Action::LayerPaint,Action::CATEGORY_NONE);
58 ACTION_SET_PRIORITY(Action::LayerPaint,0);
59 ACTION_SET_VERSION(Action::LayerPaint,"0.0");
60 ACTION_SET_CVS_ID(Action::LayerPaint,"$Id$");
61
62 /* === G L O B A L S ======================================================= */
63
64 Action::LayerPaint::PaintStroke* Action::LayerPaint::PaintStroke::first = NULL;
65 Action::LayerPaint::PaintStroke* Action::LayerPaint::PaintStroke::last = NULL;
66
67 /* === P R O C E D U R E S ================================================= */
68
69 /* === M E T H O D S ======================================================= */
70
PaintStroke()71 Action::LayerPaint::PaintStroke::PaintStroke():
72 prev(NULL),
73 next(NULL),
74 prevSameLayer(NULL),
75 nextSameLayer(NULL),
76 prepared(false),
77 applied(false)
78 {
79 }
80
~PaintStroke()81 Action::LayerPaint::PaintStroke::~PaintStroke()
82 {
83 if (prepared)
84 {
85 if (nextSameLayer != NULL)
86 {
87 if (prevSameLayer == NULL)
88 paint_self(nextSameLayer->surface);
89 else
90 nextSameLayer->points.insert(nextSameLayer->points.begin(), points.begin(), points.end());
91 nextSameLayer->prevSameLayer = prevSameLayer;
92 }
93 if (prevSameLayer != NULL) prevSameLayer->nextSameLayer = nextSameLayer;
94 if (prev == NULL) first = next; else prev->next = next;
95 if (next == NULL) last = prev; else next->prev = prev;
96 }
97 }
98
99 void
paint_prev(synfig::Surface & surface)100 Action::LayerPaint::PaintStroke::paint_prev(synfig::Surface &surface)
101 {
102 if (prevSameLayer == NULL) {
103 surface = this->surface;
104 return;
105 }
106 prevSameLayer->paint_self(surface);
107 }
108
109 void
paint_self(synfig::Surface & surface)110 Action::LayerPaint::PaintStroke::paint_self(synfig::Surface &surface)
111 {
112 paint_prev(surface);
113 brushlib::SurfaceWrapper wrapper(&surface);
114 if (!points.empty()) reset(points.front());
115 for(std::vector<PaintPoint>::const_iterator i = points.begin(); i != points.end(); i++)
116 {
117 brush_.stroke_to(&wrapper, i->x, i->y, i->pressure, 0.f, 0.f, i->dtime);
118 wrapper.offset_x = 0;
119 wrapper.offset_y = 0;
120 }
121 }
122
reset(const PaintPoint & point)123 void Action::LayerPaint::PaintStroke::reset(const PaintPoint &point)
124 {
125 for (int i=0; i<STATE_COUNT; i++)
126 brush_.set_state(i, 0);
127 brush_.set_state(STATE_X, point.x);
128 brush_.set_state(STATE_Y, point.y);
129 brush_.set_state(STATE_PRESSURE, point.pressure);
130 brush_.set_state(STATE_ACTUAL_X, brush_.get_state(STATE_X));
131 brush_.set_state(STATE_ACTUAL_Y, brush_.get_state(STATE_Y));
132 brush_.set_state(STATE_STROKE, 1.0); // start in a state as if the stroke was long finished
133 }
134
135 void
add_point_and_apply(const PaintPoint & point)136 Action::LayerPaint::PaintStroke::add_point_and_apply(const PaintPoint &point)
137 {
138 assert(prepared);
139 assert(applied || points.empty());
140 assert(prevSameLayer == NULL || prevSameLayer->applied);
141 assert(nextSameLayer == NULL);
142
143 if (points.empty()) reset(point);
144 points.push_back(point);
145 applied = true;
146
147 Surface surface = layer->get_surface();
148 brushlib::SurfaceWrapper wrapper(&surface);
149 int w = wrapper.surface->get_w();
150 int h = wrapper.surface->get_h();
151 {
152 Mutex::Lock lock(layer->mutex);
153 brush_.stroke_to(&wrapper, point.x, point.y, point.pressure, 0.f, 0.f, point.dtime);
154
155 // fix state in case of surface resized
156 float x = brush_.get_state(STATE_X) + wrapper.offset_x;
157 float y = brush_.get_state(STATE_Y) + wrapper.offset_y;
158 brush_.set_state(STATE_X, x);
159 brush_.set_state(STATE_Y, y);
160 brush_.set_state(STATE_ACTUAL_X, x);
161 brush_.set_state(STATE_ACTUAL_Y, y);
162
163 // TODO: optimize for hardware
164 layer->rendering_surface = new rendering::SurfaceSW();
165 layer->rendering_surface->assign(surface[0], surface.get_w(), surface.get_h());
166 }
167
168 if (wrapper.extra_left > 0 || wrapper.extra_top > 0) {
169 new_tl -= Point(
170 (Real)wrapper.extra_left/(Real)w*(new_br[0] - new_tl[0]),
171 (Real)wrapper.extra_top/(Real)h*(new_br[1] - new_tl[1]) );
172 layer->set_param("tl", ValueBase(new_tl));
173 }
174 if (wrapper.extra_right > 0 || wrapper.extra_bottom > 0) {
175 new_br += Point(
176 (Real)wrapper.extra_right/(Real)w*(new_br[0] - new_tl[0]),
177 (Real)wrapper.extra_bottom/(Real)h*(new_br[1] - new_tl[1]) );
178 layer->set_param("br", ValueBase(new_br));
179 }
180 layer->changed();
181 }
182
183 void
prepare()184 Action::LayerPaint::PaintStroke::prepare()
185 {
186 assert(layer);
187 assert(!prepared);
188
189 prev = last; last = this;
190 if (prev == NULL) first = this; else prev->next = this;
191
192 for(PaintStroke *p = prev; p != NULL; p = p->prev)
193 if (p->layer == layer)
194 {
195 assert(p->nextSameLayer == NULL);
196 prevSameLayer = p;
197 p->nextSameLayer = this;
198 break;
199 }
200
201 if (prevSameLayer == NULL) surface = layer->get_surface();
202 new_tl = tl = layer->get_param("tl").get(Point());
203 new_br = br = layer->get_param("br").get(Point());
204
205 prepared = true;
206 }
207
208
209 void
undo()210 Action::LayerPaint::PaintStroke::undo()
211 {
212 assert(prepared);
213 if (!applied) return;
214 {
215 Mutex::Lock lock(layer->mutex);
216 synfig::Surface surface;
217 paint_prev(surface);
218 layer->rendering_surface = new rendering::SurfaceSW();
219 layer->rendering_surface->assign(surface[0], surface.get_w(), surface.get_h());
220 }
221 applied = false;
222 layer->set_param("tl", ValueBase(tl));
223 layer->set_param("br", ValueBase(br));
224 layer->changed();
225 }
226
227 void
apply()228 Action::LayerPaint::PaintStroke::apply()
229 {
230 assert(prepared);
231 if (applied) return;
232 {
233 Mutex::Lock lock(layer->mutex);
234 synfig::Surface surface;
235 paint_self(surface);
236 layer->rendering_surface = new rendering::SurfaceSW();
237 layer->rendering_surface->assign(surface[0], surface.get_w(), surface.get_h());
238 }
239 applied = true;
240 layer->set_param("tl", ValueBase(new_tl));
241 layer->set_param("br", ValueBase(new_br));
242 layer->changed();
243 }
244
245
246
LayerPaint()247 Action::LayerPaint::LayerPaint():
248 applied(false)
249 { }
250
251 Action::ParamVocab
get_param_vocab()252 Action::LayerPaint::get_param_vocab()
253 {
254 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
255 return ret;
256 }
257
258 bool
is_candidate(const ParamList &)259 Action::LayerPaint::is_candidate(const ParamList & /* x */)
260 {
261 return false;
262 }
263
264 bool
set_param(const synfig::String & name,const Action::Param & param)265 Action::LayerPaint::set_param(const synfig::String& name, const Action::Param ¶m)
266 {
267 return Action::CanvasSpecific::set_param(name,param);
268 }
269
270 bool
is_ready() const271 Action::LayerPaint::is_ready()const
272 {
273 if(!stroke.is_prepared())
274 return false;
275 return Action::CanvasSpecific::is_ready();
276 }
277
278 void
perform()279 Action::LayerPaint::perform()
280 {
281 assert(!applied);
282 stroke.apply();
283 if (!applied) stroke.get_layer()->add_surface_modification_id(id);
284 applied = !applied;
285 }
286
287 void
undo()288 Action::LayerPaint::undo()
289 {
290 assert(applied);
291 stroke.undo();
292 if (applied) stroke.get_layer()->add_surface_modification_id(id);
293 applied = !applied;
294 }
295