1 | // Aseprite |
---|---|
2 | // Copyright (C) 2001-2018 David Capello |
3 | // |
4 | // This program is distributed under the terms of |
5 | // the End-User License Agreement for Aseprite. |
6 | |
7 | #ifdef HAVE_CONFIG_H |
8 | #include "config.h" |
9 | #endif |
10 | |
11 | #include "app/app.h" |
12 | #include "app/commands/command.h" |
13 | #include "app/context.h" |
14 | #include "app/context_access.h" |
15 | #include "app/modules/editors.h" |
16 | #include "app/pref/preferences.h" |
17 | #include "app/ui/editor/editor.h" |
18 | #include "app/ui/main_window.h" |
19 | #include "app/ui/preview_editor.h" |
20 | |
21 | namespace app { |
22 | |
23 | using namespace ui; |
24 | |
25 | ////////////////////////////////////////////////////////////////////// |
26 | |
27 | class PlayAnimationCommand : public Command { |
28 | public: |
29 | PlayAnimationCommand(); |
30 | |
31 | protected: |
32 | bool onEnabled(Context* context) override; |
33 | void onExecute(Context* context) override; |
34 | }; |
35 | |
36 | PlayAnimationCommand::PlayAnimationCommand() |
37 | : Command(CommandId::PlayAnimation(), CmdUIOnlyFlag) |
38 | { |
39 | } |
40 | |
41 | bool PlayAnimationCommand::onEnabled(Context* context) |
42 | { |
43 | return context->checkFlags(ContextFlags::ActiveDocumentIsWritable | |
44 | ContextFlags::HasActiveSprite); |
45 | } |
46 | |
47 | void PlayAnimationCommand::onExecute(Context* context) |
48 | { |
49 | // Do not play one-frame images |
50 | { |
51 | ContextReader writer(context); |
52 | Sprite* sprite(writer.sprite()); |
53 | if (!sprite || sprite->totalFrames() < 2) |
54 | return; |
55 | } |
56 | |
57 | ASSERT(current_editor); |
58 | if (!current_editor) |
59 | return; |
60 | |
61 | if (current_editor->isPlaying()) |
62 | current_editor->stop(); |
63 | else |
64 | current_editor->play(Preferences::instance().editor.playOnce(), |
65 | Preferences::instance().editor.playAll()); |
66 | } |
67 | |
68 | ////////////////////////////////////////////////////////////////////// |
69 | |
70 | class PlayPreviewAnimationCommand : public Command { |
71 | public: |
72 | PlayPreviewAnimationCommand(); |
73 | |
74 | protected: |
75 | bool onEnabled(Context* context) override; |
76 | void onExecute(Context* context) override; |
77 | }; |
78 | |
79 | PlayPreviewAnimationCommand::PlayPreviewAnimationCommand() |
80 | : Command(CommandId::PlayPreviewAnimation(), CmdUIOnlyFlag) |
81 | { |
82 | } |
83 | |
84 | bool PlayPreviewAnimationCommand::onEnabled(Context* context) |
85 | { |
86 | return context->checkFlags(ContextFlags::ActiveDocumentIsWritable | |
87 | ContextFlags::HasActiveSprite); |
88 | } |
89 | |
90 | void PlayPreviewAnimationCommand::onExecute(Context* context) |
91 | { |
92 | PreviewEditorWindow* preview = App::instance()->mainWindow()->getPreviewEditor(); |
93 | if (!preview->isPreviewEnabled()) |
94 | preview->setPreviewEnabled(true); |
95 | preview->pressPlayButton(); |
96 | } |
97 | |
98 | ////////////////////////////////////////////////////////////////////// |
99 | |
100 | Command* CommandFactory::createPlayAnimationCommand() |
101 | { |
102 | return new PlayAnimationCommand; |
103 | } |
104 | |
105 | Command* CommandFactory::createPlayPreviewAnimationCommand() |
106 | { |
107 | return new PlayPreviewAnimationCommand; |
108 | } |
109 | |
110 | } // namespace app |
111 |