| 1 | // Aseprite Document Library |
| 2 | // Copyright (c) 2020-2022 Igara Studio S.A. |
| 3 | // Copyright (c) 2017 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/slice.h" |
| 13 | |
| 14 | #include "base/debug.h" |
| 15 | #include "doc/slices.h" |
| 16 | |
| 17 | #include <limits> |
| 18 | |
| 19 | namespace doc { |
| 20 | |
| 21 | const gfx::Point SliceKey::NoPivot(std::numeric_limits<int32_t>::min(), |
| 22 | std::numeric_limits<int32_t>::min()); |
| 23 | |
| 24 | SliceKey::SliceKey() |
| 25 | : m_pivot(NoPivot) |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | SliceKey::SliceKey(const gfx::Rect& bounds, |
| 30 | const gfx::Rect& center, |
| 31 | const gfx::Point& pivot) |
| 32 | : m_bounds(bounds) |
| 33 | , m_center(center) |
| 34 | , m_pivot(pivot) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | Slice::Slice() |
| 39 | : WithUserData(ObjectType::Slice) |
| 40 | , m_owner(nullptr) |
| 41 | , m_name("Slice" ) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | Slice::Slice(const Slice& other) |
| 46 | : WithUserData(other) |
| 47 | , m_owner(nullptr) |
| 48 | , m_name(other.m_name) |
| 49 | , m_keys(other.m_keys) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | Slice::~Slice() |
| 54 | { |
| 55 | ASSERT(!m_owner); |
| 56 | } |
| 57 | |
| 58 | int Slice::getMemSize() const |
| 59 | { |
| 60 | return sizeof(*this) + sizeof(SliceKey)*m_keys.size(); |
| 61 | } |
| 62 | |
| 63 | void Slice::insert(const frame_t frame, const SliceKey& key) |
| 64 | { |
| 65 | m_keys.insert(frame, std::make_unique<SliceKey>(key)); |
| 66 | } |
| 67 | |
| 68 | void Slice::remove(const frame_t frame) |
| 69 | { |
| 70 | m_keys.remove(frame); |
| 71 | } |
| 72 | |
| 73 | const SliceKey* Slice::getByFrame(const frame_t frame) const |
| 74 | { |
| 75 | auto it = const_cast<Slice*>(this)->m_keys.getIterator(frame); |
| 76 | if (it != m_keys.end()) |
| 77 | return it->value(); |
| 78 | else |
| 79 | return nullptr; |
| 80 | } |
| 81 | |
| 82 | Slice::iterator Slice::getIteratorByFrame(const frame_t frame) const |
| 83 | { |
| 84 | return const_cast<Slice*>(this)->m_keys.getIterator(frame); |
| 85 | } |
| 86 | |
| 87 | Sprite* Slice::sprite() const |
| 88 | { |
| 89 | if (m_owner) |
| 90 | return m_owner->sprite(); |
| 91 | else |
| 92 | return nullptr; |
| 93 | } |
| 94 | |
| 95 | void Slice::setOwner(Slices* owner) |
| 96 | { |
| 97 | m_owner = owner; |
| 98 | } |
| 99 | |
| 100 | void Slice::setName(const std::string& name) |
| 101 | { |
| 102 | m_name = name; |
| 103 | } |
| 104 | |
| 105 | } // namespace doc |
| 106 | |