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_CONTEXT_ACCESS_H_INCLUDED
9#define APP_CONTEXT_ACCESS_H_INCLUDED
10#pragma once
11
12#include "app/doc_access.h"
13#include "app/site.h"
14
15namespace app {
16
17 class Context;
18
19 template<typename DocumentAccessT>
20 class ContextAccess {
21 public:
22 const Context* context() const { return m_context; }
23 const Site* site() const { return &m_site; }
24 const DocumentAccessT& document() const { return m_document; }
25 const Sprite* sprite() const { return m_site.sprite(); }
26 const Layer* layer() const { return m_site.layer(); }
27 frame_t frame() const { return m_site.frame(); }
28 const Cel* cel() const { return m_site.cel(); }
29
30 // You cannot change the site directly from a writable ContextAccess anyway.
31 const Site* site() { return &m_site; }
32
33 Context* context() { return const_cast<Context*>(m_context); }
34 DocumentAccessT& document() { return m_document; }
35 Sprite* sprite() { return m_site.sprite(); }
36 Layer* layer() { return m_site.layer(); }
37 Cel* cel() { return m_site.cel(); }
38
39 Image* image(int* x = NULL, int* y = NULL, int* opacity = NULL) const {
40 return m_site.image(x, y, opacity);
41 }
42
43 Palette* palette() const {
44 return m_site.palette();
45 }
46
47 protected:
48 ContextAccess(const Context* context, int timeout)
49 : m_context(context)
50 , m_document(context->activeDocument(), timeout)
51 , m_site(context->activeSite())
52 {
53 }
54
55 template<typename DocReaderT>
56 ContextAccess(const Context* context, const DocReaderT& docReader, int timeout)
57 : m_context(context)
58 , m_document(docReader, timeout)
59 , m_site(context->activeSite())
60 {
61 }
62
63 private:
64 const Context* m_context;
65 DocumentAccessT m_document;
66 Site m_site;
67 };
68
69 // You can use this class to access to the given context to read the
70 // active document.
71 class ContextReader : public ContextAccess<DocReader> {
72 public:
73 ContextReader(const Context* context, int timeout = 0)
74 : ContextAccess<DocReader>(context, timeout) {
75 }
76 };
77
78 // You can use this class to access to the given context to write the
79 // active document.
80 class ContextWriter : public ContextAccess<DocWriter> {
81 public:
82 ContextWriter(const Context* context, int timeout = 500)
83 : ContextAccess<DocWriter>(context, timeout) {
84 }
85
86 ContextWriter(const ContextReader& reader, int timeout = 500)
87 : ContextAccess<DocWriter>(reader.context(), reader.document(), timeout) {
88 }
89 };
90
91} // namespace app
92
93#endif
94