1// Aseprite
2// Copyright (C) 2017 David Capello
3//
4// This program is distributed under the terms of
5// the End-User License Agreement for Aseprite.
6
7#ifdef HAVE_CONFIG_H
8#include "config.h"
9#endif
10
11#include "app/cmd/set_slice_key.h"
12
13#include "doc/slice.h"
14#include "doc/slices.h"
15#include "doc/sprite.h"
16
17namespace app {
18namespace cmd {
19
20SetSliceKey::SetSliceKey(Slice* slice,
21 const doc::frame_t frame,
22 const doc::SliceKey& sliceKey)
23 : WithSlice(slice)
24 , m_frame(frame)
25 , m_newSliceKey(sliceKey)
26{
27 auto it = slice->getIteratorByFrame(frame);
28 if (it != slice->end() && it->frame() == frame)
29 m_oldSliceKey = *it->value();
30}
31
32void SetSliceKey::onExecute()
33{
34 if (!m_newSliceKey.isEmpty())
35 slice()->insert(m_frame, m_newSliceKey);
36 else
37 slice()->remove(m_frame);
38
39 slice()->incrementVersion();
40 slice()->owner()->sprite()->incrementVersion();
41}
42
43void SetSliceKey::onUndo()
44{
45 if (!m_oldSliceKey.isEmpty())
46 slice()->insert(m_frame, m_oldSliceKey);
47 else
48 slice()->remove(m_frame);
49
50 slice()->incrementVersion();
51 slice()->owner()->sprite()->incrementVersion();
52}
53
54} // namespace cmd
55} // namespace app
56