1// Aseprite
2// Copyright (C) 2019-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#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "app/context_flags.h"
13
14#include "app/context.h"
15#include "app/doc.h"
16#include "app/modules/editors.h"
17#include "app/site.h"
18#include "app/ui/editor/editor.h"
19#include "doc/cel.h"
20#include "doc/layer.h"
21#include "doc/sprite.h"
22
23namespace app {
24
25ContextFlags::ContextFlags()
26{
27 m_flags = 0;
28}
29
30void ContextFlags::update(Context* context)
31{
32 Site site = context->activeSite();
33 Doc* document = site.document();
34
35 m_flags = 0;
36
37 if (document) {
38 m_flags |= HasActiveDocument;
39
40 if (document->readLock(0)) {
41 m_flags |= ActiveDocumentIsReadable;
42
43 if (document->isMaskVisible())
44 m_flags |= HasVisibleMask;
45
46 updateFlagsFromSite(site);
47
48 if (document->canWriteLockFromRead())
49 m_flags |= ActiveDocumentIsWritable;
50
51 document->unlock();
52 }
53
54#ifdef ENABLE_UI
55 // TODO this is a hack, try to find a better design to handle this
56 // "moving pixels" state.
57 if (current_editor &&
58 current_editor->document() == document &&
59 current_editor->isMovingPixels()) {
60 // Flags enabled when we are in MovingPixelsState
61 m_flags |=
62 HasVisibleMask |
63 ActiveDocumentIsReadable |
64 ActiveDocumentIsWritable;
65
66 updateFlagsFromSite(current_editor->getSite());
67 }
68#endif // ENABLE_UI
69 }
70}
71
72void ContextFlags::updateFlagsFromSite(const Site& site)
73{
74 const Sprite* sprite = site.sprite();
75 if (!sprite)
76 return;
77
78 m_flags |= HasActiveSprite;
79
80 if (sprite->backgroundLayer())
81 m_flags |= HasBackgroundLayer;
82
83 const Layer* layer = site.layer();
84 frame_t frame = site.frame();
85 if (!layer)
86 return;
87
88 m_flags |= HasActiveLayer;
89
90 if (layer->isBackground())
91 m_flags |= ActiveLayerIsBackground;
92
93 if (layer->isVisibleHierarchy())
94 m_flags |= ActiveLayerIsVisible;
95
96 if (layer->isEditableHierarchy())
97 m_flags |= ActiveLayerIsEditable;
98
99 if (layer->isReference())
100 m_flags |= ActiveLayerIsReference;
101
102 if (layer->isTilemap())
103 m_flags |= ActiveLayerIsTilemap;
104
105 if (layer->isImage()) {
106 m_flags |= ActiveLayerIsImage;
107
108 Cel* cel = layer->cel(frame);
109 if (cel) {
110 m_flags |= HasActiveCel;
111
112 if (cel->image())
113 m_flags |= HasActiveImage;
114 }
115 }
116
117 if (site.selectedColors().picks() > 0)
118 m_flags |= HasSelectedColors;
119
120 if (site.selectedTiles().picks() > 0)
121 m_flags |= HasSelectedTiles;
122}
123
124} // namespace app
125