forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_access.h
93 lines (76 loc) · 2.64 KB
/
context_access.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Aseprite
// Copyright (C) 2020 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifndef APP_CONTEXT_ACCESS_H_INCLUDED
#define APP_CONTEXT_ACCESS_H_INCLUDED
#pragma once
#include "app/doc_access.h"
#include "app/site.h"
namespace app {
class Context;
template<typename DocumentAccessT>
class ContextAccess {
public:
const Context* context() const { return m_context; }
const Site* site() const { return &m_site; }
const DocumentAccessT& document() const { return m_document; }
const Sprite* sprite() const { return m_site.sprite(); }
const Layer* layer() const { return m_site.layer(); }
frame_t frame() const { return m_site.frame(); }
const Cel* cel() const { return m_site.cel(); }
// You cannot change the site directly from a writable ContextAccess anyway.
const Site* site() { return &m_site; }
Context* context() { return const_cast<Context*>(m_context); }
DocumentAccessT& document() { return m_document; }
Sprite* sprite() { return m_site.sprite(); }
Layer* layer() { return m_site.layer(); }
Cel* cel() { return m_site.cel(); }
Image* image(int* x = NULL, int* y = NULL, int* opacity = NULL) const {
return m_site.image(x, y, opacity);
}
Palette* palette() const {
return m_site.palette();
}
protected:
ContextAccess(const Context* context, int timeout)
: m_context(context)
, m_document(context->activeDocument(), timeout)
, m_site(context->activeSite())
{
}
template<typename DocReaderT>
ContextAccess(const Context* context, const DocReaderT& docReader, int timeout)
: m_context(context)
, m_document(docReader, timeout)
, m_site(context->activeSite())
{
}
private:
const Context* m_context;
DocumentAccessT m_document;
Site m_site;
};
// You can use this class to access to the given context to read the
// active document.
class ContextReader : public ContextAccess<DocReader> {
public:
ContextReader(const Context* context, int timeout = 0)
: ContextAccess<DocReader>(context, timeout) {
}
};
// You can use this class to access to the given context to write the
// active document.
class ContextWriter : public ContextAccess<DocWriter> {
public:
ContextWriter(const Context* context, int timeout = 500)
: ContextAccess<DocWriter>(context, timeout) {
}
ContextWriter(const ContextReader& reader, int timeout = 500)
: ContextAccess<DocWriter>(reader.context(), reader.document(), timeout) {
}
};
} // namespace app
#endif