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