| 1 | // Aseprite |
| 2 | // Copyright (C) 2018-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/docs.h" |
| 13 | |
| 14 | #include "app/doc.h" |
| 15 | #include "base/fs.h" |
| 16 | |
| 17 | #include <algorithm> |
| 18 | |
| 19 | namespace app { |
| 20 | |
| 21 | Docs::Docs(Context* ctx) |
| 22 | : m_ctx(ctx) |
| 23 | { |
| 24 | ASSERT(ctx != NULL); |
| 25 | } |
| 26 | |
| 27 | Docs::~Docs() |
| 28 | { |
| 29 | deleteAll(); |
| 30 | } |
| 31 | |
| 32 | Doc* Docs::add(Doc* doc) |
| 33 | { |
| 34 | ASSERT(doc != NULL); |
| 35 | ASSERT(doc->id() != doc::NullId); |
| 36 | |
| 37 | if (doc->context() != m_ctx) { |
| 38 | doc->setContext(m_ctx); |
| 39 | ASSERT(std::find(begin(), end(), doc) != end()); |
| 40 | return doc; |
| 41 | } |
| 42 | |
| 43 | m_docs.insert(begin(), doc); |
| 44 | |
| 45 | notify_observers(&DocsObserver::onAddDocument, doc); |
| 46 | return doc; |
| 47 | } |
| 48 | |
| 49 | Doc* Docs::add(int width, int height, doc::ColorMode colorMode, int ncolors) |
| 50 | { |
| 51 | std::unique_ptr<Doc> doc( |
| 52 | new Doc(Sprite::MakeStdSprite(ImageSpec(colorMode, width, height), ncolors))); |
| 53 | doc->setFilename("Sprite" ); |
| 54 | doc->setContext(m_ctx); // Change the document context to add the doc in this collection |
| 55 | |
| 56 | return doc.release(); |
| 57 | } |
| 58 | |
| 59 | void Docs::remove(Doc* doc) |
| 60 | { |
| 61 | iterator it = std::find(begin(), end(), doc); |
| 62 | if (it == end()) // Already removed. |
| 63 | return; |
| 64 | |
| 65 | m_docs.erase(it); |
| 66 | |
| 67 | notify_observers(&DocsObserver::onRemoveDocument, doc); |
| 68 | |
| 69 | doc->setContext(NULL); |
| 70 | } |
| 71 | |
| 72 | void Docs::move(Doc* doc, int index) |
| 73 | { |
| 74 | iterator it = std::find(begin(), end(), doc); |
| 75 | ASSERT(it != end()); |
| 76 | if (it != end()) |
| 77 | m_docs.erase(it); |
| 78 | |
| 79 | m_docs.insert(begin()+index, doc); |
| 80 | } |
| 81 | |
| 82 | Doc* Docs::getById(ObjectId id) const |
| 83 | { |
| 84 | for (const auto& doc : *this) { |
| 85 | if (doc->id() == id) |
| 86 | return doc; |
| 87 | } |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | Doc* Docs::getByName(const std::string& name) const |
| 92 | { |
| 93 | for (const auto& doc : *this) { |
| 94 | if (doc->name() == name) |
| 95 | return doc; |
| 96 | } |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | Doc* Docs::getByFileName(const std::string& filename) const |
| 101 | { |
| 102 | std::string fn = base::normalize_path(filename); |
| 103 | for (const auto& doc : *this) { |
| 104 | if (doc->filename() == fn) |
| 105 | return doc; |
| 106 | } |
| 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | void Docs::deleteAll() |
| 111 | { |
| 112 | while (!empty()) { |
| 113 | ASSERT(m_ctx == back()->context()); |
| 114 | delete back(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | } // namespace app |
| 119 | |