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/set_mask.h"
13
14#include "app/doc.h"
15#include "doc/mask.h"
16
17namespace app {
18namespace cmd {
19
20SetMask::SetMask(Doc* doc, const Mask* newMask)
21 : WithDocument(doc)
22 , m_oldMask(doc->isMaskVisible() ? new Mask(*doc->mask()): nullptr)
23 , m_newMask(newMask && !newMask->isEmpty() ? new Mask(*newMask): nullptr)
24{
25}
26
27void SetMask::setNewMask(const Mask* newMask)
28{
29 m_newMask.reset(newMask ? new Mask(*newMask): nullptr);
30 setMask(m_newMask.get());
31}
32
33void SetMask::onExecute()
34{
35 setMask(m_newMask.get());
36}
37
38void SetMask::onUndo()
39{
40 setMask(m_oldMask.get());
41}
42
43size_t SetMask::onMemSize() const
44{
45 return sizeof(*this) +
46 (m_oldMask ? m_oldMask->getMemSize(): 0) +
47 (m_newMask ? m_newMask->getMemSize(): 0);
48}
49
50void SetMask::setMask(const Mask* mask)
51{
52 Doc* doc = document();
53
54 if (mask) {
55 doc->setMask(mask);
56 doc->setMaskVisible(!mask->isEmpty());
57 }
58 else {
59 Mask empty;
60 doc->setMask(&empty);
61 doc->setMaskVisible(false);
62 }
63
64 doc->notifySelectionChanged();
65}
66
67} // namespace cmd
68} // namespace app
69