1// Aseprite
2// Copyright (C) 2019 Igara Studio S.A.
3// Copyright (C) 2001-2018 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "app/cmd/shift_masked_cel.h"
13
14#include "app/doc.h"
15#include "doc/algorithm/shift_image.h"
16#include "doc/cel.h"
17#include "doc/image.h"
18#include "doc/layer.h"
19#include "doc/mask.h"
20
21namespace app {
22namespace cmd {
23
24ShiftMaskedCel::ShiftMaskedCel(Cel* cel, int dx, int dy)
25 : WithCel(cel)
26 , m_dx(dx)
27 , m_dy(dy)
28{
29}
30
31void ShiftMaskedCel::onExecute()
32{
33 shift(m_dx, m_dy);
34}
35
36void ShiftMaskedCel::onUndo()
37{
38 shift(-m_dx, -m_dy);
39}
40
41void ShiftMaskedCel::shift(int dx, int dy)
42{
43 Cel* cel = this->cel();
44 Mask* mask = static_cast<Doc*>(cel->document())->mask();
45 ASSERT(mask->bitmap());
46 if (!mask->bitmap())
47 return;
48
49 gfx::Rect newBounds;
50 ImageRef newImage =
51 doc::algorithm::shift_image_with_mask(cel, mask, dx, dy, newBounds);
52
53 ImageRef oldImage = cel->imageRef();
54 if (!is_same_image(oldImage.get(), newImage.get())) {
55 ObjectId id = oldImage->id();
56 ObjectVersion ver = oldImage->version();
57 oldImage->setId(NullId);
58
59 newImage->setId(id);
60 newImage->setVersion(ver);
61 newImage->incrementVersion();
62 cel->data()->setImage(newImage, cel->layer());
63 }
64 cel->data()->setBounds(newBounds);
65 cel->data()->incrementVersion();
66}
67
68} // namespace cmd
69} // namespace app
70