1// Aseprite Document Library
2// Copyright (C) 2018 Igara Studio S.A.
3// Copyright (c) 2001-2018 David Capello
4//
5// This file is released under the terms of the MIT license.
6// Read LICENSE.txt for more information.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "doc/sprites.h"
13
14#include "doc/sprite.h"
15#include "doc/cel.h"
16#include "doc/image.h"
17#include "doc/layer.h"
18#include "doc/primitives.h"
19
20#include <algorithm>
21#include <memory>
22
23namespace doc {
24
25Sprites::Sprites(Document* doc)
26 : m_doc(doc)
27{
28 ASSERT(doc != NULL);
29}
30
31Sprites::~Sprites()
32{
33 deleteAll();
34}
35
36Sprite* Sprites::add(Sprite* spr)
37{
38 ASSERT(spr != NULL);
39
40 m_sprites.insert(begin(), spr);
41 spr->setDocument(m_doc);
42
43 return spr;
44}
45
46void Sprites::remove(Sprite* spr)
47{
48 iterator it = std::find(begin(), end(), spr);
49 ASSERT(it != end());
50
51 if (it != end()) {
52 (*it)->setDocument(NULL);
53 m_sprites.erase(it);
54 }
55}
56
57void Sprites::move(Sprite* spr, int index)
58{
59 remove(spr);
60
61 m_sprites.insert(begin()+index, spr);
62}
63
64void Sprites::deleteAll()
65{
66 std::vector<Sprite*> copy = m_sprites;
67
68 for (iterator it = copy.begin(), end = copy.end(); it != end; ++it) {
69 Sprite* spr = *it;
70 delete spr;
71 }
72
73 m_sprites.clear();
74}
75
76} // namespace doc
77