1// Aseprite
2// Copyright (C) 2020 Igara Studio S.A.
3// Copyright (C) 2001-2017 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/app.h"
13#include "app/commands/command.h"
14#include "app/context_access.h"
15#include "app/modules/editors.h"
16#include "app/modules/gui.h"
17#include "app/ui/editor/editor.h"
18#include "app/ui/status_bar.h"
19#include "app/ui/timeline/timeline.h"
20#include "doc/layer.h"
21#include "doc/sprite.h"
22#include "fmt/format.h"
23
24namespace app {
25
26class GotoLayerCommand : public Command {
27public:
28 GotoLayerCommand(int offset,
29 const char* id,
30 CommandFlags flags)
31 : Command(id, flags),
32 m_offset(offset) {
33 }
34
35protected:
36
37 bool onEnabled(Context* context) override {
38 return (current_editor &&
39 current_editor->document());
40 }
41
42 void onExecute(Context* context) override {
43 Site site = current_editor->getSite();
44
45 Layer* layer = site.layer();
46 if (!layer)
47 return;
48
49 if (m_offset > 0) {
50 int i = m_offset;
51 while (i-- > 0) {
52 layer = layer->getNextBrowsable();
53 if (!layer)
54 layer = site.sprite()->firstBrowsableLayer();
55 }
56 }
57 else if (m_offset < 0) {
58 int i = m_offset;
59 while (i++ < 0) {
60 layer = layer->getPreviousBrowsable();
61 if (!layer)
62 layer = site.sprite()->root()->lastLayer();
63 }
64 }
65
66 site.layer(layer);
67
68 // Flash the current layer
69 current_editor->setLayer(site.layer());
70 current_editor->flashCurrentLayer();
71
72 updateStatusBar(site);
73 }
74
75 void updateStatusBar(Site& site) {
76 if (site.layer() != NULL)
77 StatusBar::instance()->setStatusText(
78 1000, fmt::format("{} '{}' selected",
79 (site.layer()->isGroup() ? "Group": "Layer"),
80 site.layer()->name()));
81 }
82
83private:
84 int m_offset;
85};
86
87class GotoPreviousLayerCommand : public GotoLayerCommand {
88public:
89 GotoPreviousLayerCommand()
90 : GotoLayerCommand(-1, "GotoPreviousLayer",
91 CmdUIOnlyFlag) {
92 }
93};
94
95class GotoNextLayerCommand : public GotoLayerCommand {
96public:
97 GotoNextLayerCommand()
98 : GotoLayerCommand(+1, "GotoNextLayer",
99 CmdUIOnlyFlag) {
100 }
101};
102
103Command* CommandFactory::createGotoPreviousLayerCommand()
104{
105 return new GotoPreviousLayerCommand;
106}
107
108Command* CommandFactory::createGotoNextLayerCommand()
109{
110 return new GotoNextLayerCommand;
111}
112
113} // namespace app
114