1 // Aseprite Document Library
2 // Copyright (c) 2001-2018 David Capello
3 //
4 // This file is released under the terms of the MIT license.
5 // Read LICENSE.txt for more information.
6 
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 
11 #include "doc/sprites.h"
12 
13 #include "base/unique_ptr.h"
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 
22 namespace doc {
23 
Sprites(Document * doc)24 Sprites::Sprites(Document* doc)
25   : m_doc(doc)
26 {
27   ASSERT(doc != NULL);
28 }
29 
~Sprites()30 Sprites::~Sprites()
31 {
32   deleteAll();
33 }
34 
add(int width,int height,ColorMode mode,int ncolors)35 Sprite* Sprites::add(int width, int height, ColorMode mode, int ncolors)
36 {
37   base::UniquePtr<Sprite> spr(
38     doc::Sprite::createBasicSprite(
39       (doc::PixelFormat)mode, width, height, ncolors));
40 
41   add(spr);
42 
43   return spr.release();
44 }
45 
add(Sprite * spr)46 Sprite* Sprites::add(Sprite* spr)
47 {
48   ASSERT(spr != NULL);
49 
50   m_sprites.insert(begin(), spr);
51   spr->setDocument(m_doc);
52 
53   return spr;
54 }
55 
remove(Sprite * spr)56 void Sprites::remove(Sprite* spr)
57 {
58   iterator it = std::find(begin(), end(), spr);
59   ASSERT(it != end());
60 
61   if (it != end()) {
62     (*it)->setDocument(NULL);
63     m_sprites.erase(it);
64   }
65 }
66 
move(Sprite * spr,int index)67 void Sprites::move(Sprite* spr, int index)
68 {
69   remove(spr);
70 
71   m_sprites.insert(begin()+index, spr);
72 }
73 
deleteAll()74 void Sprites::deleteAll()
75 {
76   std::vector<Sprite*> copy = m_sprites;
77 
78   for (iterator it = copy.begin(), end = copy.end(); it != end; ++it) {
79     Sprite* spr = *it;
80     delete spr;
81   }
82 
83   m_sprites.clear();
84 }
85 
86 } // namespace doc
87