1// Aseprite
2// Copyright (C) 2020 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#ifndef APP_DOC_RANGE_H_INCLUDED
9#define APP_DOC_RANGE_H_INCLUDED
10#pragma once
11
12#include "doc/frame.h"
13#include "doc/selected_frames.h"
14#include "doc/selected_layers.h"
15
16#include <iosfwd>
17
18namespace doc {
19 class Cel;
20 class Sprite;
21}
22
23namespace app {
24 using namespace doc;
25
26 class DocRange {
27 public:
28 enum Type { kNone = 0,
29 kCels = 1,
30 kFrames = 2,
31 kLayers = 4 };
32
33 DocRange();
34 DocRange(Cel* cel);
35 DocRange(const DocRange&) = default;
36 DocRange& operator=(const DocRange&) = default;
37
38 Type type() const { return m_type; }
39 bool enabled() const { return m_type != kNone; }
40 layer_t layers() const { return int(m_selectedLayers.size()); }
41 frame_t frames() const { return int(m_selectedFrames.size()); }
42 const SelectedLayers& selectedLayers() const { return m_selectedLayers; }
43 const SelectedFrames& selectedFrames() const { return m_selectedFrames; }
44
45 void setType(const Type type);
46 void setSelectedLayers(const SelectedLayers& layers);
47 void setSelectedFrames(const SelectedFrames& frames);
48
49 void displace(layer_t layerDelta, frame_t frameDelta);
50
51 bool contains(const Layer* layer) const;
52 bool contains(const frame_t frame) const {
53 return m_selectedFrames.contains(frame);
54 }
55 bool contains(const Layer* layer,
56 const frame_t frame) const;
57
58 // If the range includes the given layer, it will be erased from
59 // the selection and other candidates might be selected (e.g. a
60 // sibling, parent, etc.) using the
61 // candidate_if_layer_is_deleted() function.
62 void eraseAndAdjust(const Layer* layer);
63
64 void clearRange();
65 void startRange(Layer* fromLayer, frame_t fromFrame, Type type);
66 void endRange(Layer* toLayer, frame_t toFrame);
67
68 void selectLayer(Layer* layer);
69 void selectLayers(const SelectedLayers& selLayers);
70
71 frame_t firstFrame() const { return m_selectedFrames.firstFrame(); }
72 frame_t lastFrame() const { return m_selectedFrames.lastFrame(); }
73
74 bool operator==(const DocRange& o) const {
75 return (m_type == o.m_type &&
76 m_selectedLayers == o.m_selectedLayers &&
77 m_selectedFrames == o.m_selectedFrames);
78 }
79
80 bool convertToCels(const Sprite* sprite);
81
82 bool write(std::ostream& os) const;
83 bool read(std::istream& is);
84
85 private:
86 void selectLayerRange(Layer* fromLayer, Layer* toLayer);
87 void selectFrameRange(frame_t fromFrame, frame_t toFrame);
88
89 Type m_type; // Last used type of the range
90 int m_flags; // All used types in startRange()
91 SelectedLayers m_selectedLayers;
92 SelectedFrames m_selectedFrames;
93 Layer* m_selectingFromLayer;
94 frame_t m_selectingFromFrame;
95 };
96
97} // namespace app
98
99#endif
100