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/add_tag.h"
13
14#include "app/doc.h"
15#include "app/doc_event.h"
16#include "doc/sprite.h"
17#include "doc/tag.h"
18#include "doc/tag_io.h"
19
20namespace app {
21namespace cmd {
22
23using namespace doc;
24
25AddTag::AddTag(Sprite* sprite, Tag* tag)
26 : WithSprite(sprite)
27 , WithTag(tag)
28 , m_size(0)
29{
30}
31
32void AddTag::onExecute()
33{
34 Sprite* sprite = this->sprite();
35 Tag* tag = this->tag();
36
37 sprite->tags().add(tag);
38 sprite->incrementVersion();
39
40 // Notify observers about the new frame.
41 Doc* doc = static_cast<Doc*>(sprite->document());
42 DocEvent ev(doc);
43 ev.sprite(sprite);
44 ev.tag(tag);
45 doc->notify_observers<DocEvent&>(&DocObserver::onAddTag, ev);
46}
47
48void AddTag::onUndo()
49{
50 Sprite* sprite = this->sprite();
51 Tag* tag = this->tag();
52 write_tag(m_stream, tag);
53 m_size = size_t(m_stream.tellp());
54
55 // Notify observers about the new frame.
56 {
57 Doc* doc = static_cast<Doc*>(sprite->document());
58 DocEvent ev(doc);
59 ev.sprite(sprite);
60 ev.tag(tag);
61 doc->notify_observers<DocEvent&>(&DocObserver::onRemoveTag, ev);
62 }
63
64 sprite->tags().remove(tag);
65 sprite->incrementVersion();
66 delete tag;
67}
68
69void AddTag::onRedo()
70{
71 Sprite* sprite = this->sprite();
72 Tag* tag = read_tag(m_stream);
73
74 sprite->tags().add(tag);
75 sprite->incrementVersion();
76
77 m_stream.str(std::string());
78 m_stream.clear();
79 m_size = 0;
80
81 // Notify observers about the new frame.
82 Doc* doc = static_cast<Doc*>(sprite->document());
83 DocEvent ev(doc);
84 ev.sprite(sprite);
85 ev.tag(tag);
86 doc->notify_observers<DocEvent&>(&DocObserver::onAddTag, ev);
87}
88
89} // namespace cmd
90} // namespace app
91