1// Aseprite Document Library
2// Copyright (c) 2019 Igara Studio S.A.
3// Copyright (c) 2001-2016 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/cels_range.h"
13
14#include "doc/cel.h"
15#include "doc/layer.h"
16#include "doc/sprite.h"
17
18namespace doc {
19
20CelsRange::CelsRange(const Sprite* sprite,
21 const SelectedFrames& selFrames,
22 const Flags flags)
23 : m_selFrames(selFrames)
24 , m_begin(sprite, m_selFrames, flags)
25 , m_end(m_selFrames)
26{
27}
28
29CelsRange::iterator::iterator(const SelectedFrames& selFrames)
30 : m_cel(nullptr)
31 , m_selFrames(selFrames)
32 , m_frameIterator(selFrames.begin())
33{
34}
35
36CelsRange::iterator::iterator(const Sprite* sprite,
37 const SelectedFrames& selFrames,
38 const CelsRange::Flags flags)
39 : m_cel(nullptr)
40 , m_selFrames(selFrames)
41 , m_frameIterator(selFrames.begin())
42 , m_flags(flags)
43{
44 // Get first cel
45 Layer* layer = sprite->root()->firstLayerInWholeHierarchy();
46 while (layer && !m_cel) {
47 if (layer->isImage()) {
48 m_frameIterator = m_selFrames.begin();
49 auto endFrame = m_selFrames.end();
50 for (; m_frameIterator!=endFrame; ++m_frameIterator) {
51 m_cel = layer->cel(*m_frameIterator);
52 if (m_cel)
53 break;
54 }
55 }
56
57 if (!m_cel)
58 layer = layer->getNextInWholeHierarchy();
59 }
60
61 if (m_cel && flags == CelsRange::UNIQUE)
62 m_visited.insert(m_cel->data()->id());
63}
64
65CelsRange::iterator& CelsRange::iterator::operator++()
66{
67 if (!m_cel)
68 return *this;
69
70 auto endFrame = m_selFrames.end();
71 if (m_frameIterator != endFrame)
72 ++m_frameIterator;
73
74 Layer* layer = m_cel->layer();
75 m_cel = nullptr;
76 while (layer && !m_cel) {
77 if (layer->isImage()) {
78 for (; m_frameIterator!=endFrame; ++m_frameIterator) {
79 m_cel = layer->cel(*m_frameIterator);
80 if (m_cel) {
81 if (m_flags == CelsRange::UNIQUE) {
82 if (m_visited.find(m_cel->data()->id()) == m_visited.end()) {
83 m_visited.insert(m_cel->data()->id());
84 break;
85 }
86 else
87 m_cel = nullptr;
88 }
89 else
90 break;
91 }
92 }
93 }
94
95 if (!m_cel) {
96 layer = layer->getNextInWholeHierarchy();
97 m_frameIterator = m_selFrames.begin();
98 }
99 }
100 return *this;
101}
102
103} // namespace doc
104