/
cmd_goto_frame.cpp
276 lines (225 loc) · 7.08 KB
/
cmd_goto_frame.cpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Aseprite
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/commands/command.h"
#include "app/commands/params.h"
#include "app/loop_tag.h"
#include "app/match_words.h"
#include "app/modules/editors.h"
#include "app/modules/gui.h"
#include "app/ui/editor/editor.h"
#include "app/ui/editor/editor_customization_delegate.h"
#include "app/ui/search_entry.h"
#include "doc/frame_tag.h"
#include "doc/sprite.h"
#include "ui/combobox.h"
#include "ui/window.h"
#include "goto_frame.xml.h"
namespace app {
using namespace ui;
using namespace doc;
class GotoCommand : public Command {
protected:
GotoCommand(const char* id)
: Command(id, CmdRecordableFlag) { }
bool onEnabled(Context* context) override {
return (current_editor != NULL);
}
void onExecute(Context* context) override {
ASSERT(current_editor != NULL);
current_editor->setFrame(onGetFrame(current_editor));
}
virtual frame_t onGetFrame(Editor* editor) = 0;
};
class GotoFirstFrameCommand : public GotoCommand {
public:
GotoFirstFrameCommand()
: GotoCommand(CommandId::GotoFirstFrame()) { }
Command* clone() const override { return new GotoFirstFrameCommand(*this); }
protected:
frame_t onGetFrame(Editor* editor) override {
return 0;
}
};
class GotoPreviousFrameCommand : public GotoCommand {
public:
GotoPreviousFrameCommand()
: GotoCommand(CommandId::GotoPreviousFrame()) { }
Command* clone() const override { return new GotoPreviousFrameCommand(*this); }
protected:
frame_t onGetFrame(Editor* editor) override {
frame_t frame = editor->frame();
frame_t last = editor->sprite()->lastFrame();
return (frame > 0 ? frame-1: last);
}
};
class GotoNextFrameCommand : public GotoCommand {
public:
GotoNextFrameCommand() : GotoCommand(CommandId::GotoNextFrame()) { }
Command* clone() const override { return new GotoNextFrameCommand(*this); }
protected:
frame_t onGetFrame(Editor* editor) override {
frame_t frame = editor->frame();
frame_t last = editor->sprite()->lastFrame();
return (frame < last ? frame+1: 0);
}
};
class GotoNextFrameWithSameTagCommand : public GotoCommand {
public:
GotoNextFrameWithSameTagCommand() : GotoCommand(CommandId::GotoNextFrameWithSameTag()) { }
Command* clone() const override { return new GotoNextFrameWithSameTagCommand(*this); }
protected:
frame_t onGetFrame(Editor* editor) override {
frame_t frame = editor->frame();
FrameTag* tag = editor
->getCustomizationDelegate()
->getFrameTagProvider()
->getFrameTagByFrame(frame, false);
frame_t first = (tag ? tag->fromFrame(): 0);
frame_t last = (tag ? tag->toFrame(): editor->sprite()->lastFrame());
return (frame < last ? frame+1: first);
}
};
class GotoPreviousFrameWithSameTagCommand : public GotoCommand {
public:
GotoPreviousFrameWithSameTagCommand() : GotoCommand(CommandId::GotoPreviousFrameWithSameTag()) { }
Command* clone() const override { return new GotoPreviousFrameWithSameTagCommand(*this); }
protected:
frame_t onGetFrame(Editor* editor) override {
frame_t frame = editor->frame();
FrameTag* tag = editor
->getCustomizationDelegate()
->getFrameTagProvider()
->getFrameTagByFrame(frame, false);
frame_t first = (tag ? tag->fromFrame(): 0);
frame_t last = (tag ? tag->toFrame(): editor->sprite()->lastFrame());
return (frame > first ? frame-1: last);
}
};
class GotoLastFrameCommand : public GotoCommand {
public:
GotoLastFrameCommand() : GotoCommand(CommandId::GotoLastFrame()) { }
Command* clone() const override { return new GotoLastFrameCommand(*this); }
protected:
frame_t onGetFrame(Editor* editor) override {
return editor->sprite()->lastFrame();
}
};
class GotoFrameCommand : public GotoCommand {
public:
GotoFrameCommand() : GotoCommand(CommandId::GotoFrame())
, m_showUI(true) { }
Command* clone() const override { return new GotoFrameCommand(*this); }
private:
// TODO this combobox is similar to FileSelector::CustomFileNameEntry
class TagsEntry : public ComboBox {
public:
TagsEntry(FrameTags& frameTags)
: m_frameTags(frameTags) {
setEditable(true);
getEntryWidget()->Change.connect(&TagsEntry::onEntryChange, this);
fill(true);
}
private:
void fill(bool all) {
removeAllItems();
MatchWords match(getEntryWidget()->text());
bool matchAny = false;
for (const auto& frameTag : m_frameTags) {
if (match(frameTag->name())) {
matchAny = true;
break;
}
}
for (const auto& frameTag : m_frameTags) {
if (all || !matchAny || match(frameTag->name()))
addItem(frameTag->name());
}
}
void onEntryChange() {
closeListBox();
fill(false);
if (getItemCount() > 0)
openListBox();
}
FrameTags& m_frameTags;
};
void onLoadParams(const Params& params) override {
std::string frame = params.get("frame");
if (!frame.empty()) {
m_frame = strtol(frame.c_str(), nullptr, 10);
m_showUI = false;
}
else
m_showUI = true;
}
frame_t onGetFrame(Editor* editor) override {
auto& docPref = editor->docPref();
if (m_showUI) {
app::gen::GotoFrame window;
TagsEntry combobox(editor->sprite()->frameTags());
window.framePlaceholder()->addChild(&combobox);
combobox.setFocusMagnet(true);
combobox.getEntryWidget()->setTextf(
"%d", editor->frame()+docPref.timeline.firstFrame());
window.openWindowInForeground();
if (window.closer() != window.ok())
return editor->frame();
std::string text = combobox.getEntryWidget()->text();
frame_t frameNum = base::convert_to<int>(text);
std::string textFromInt = base::convert_to<std::string>(frameNum);
if (text == textFromInt) {
m_frame = frameNum;
}
// Search a tag name
else {
MatchWords match(text);
for (const auto& frameTag : editor->sprite()->frameTags()) {
if (match(frameTag->name())) {
m_frame =
frameTag->fromFrame()+docPref.timeline.firstFrame();
break;
}
}
}
}
return MID(0, m_frame-docPref.timeline.firstFrame(), editor->sprite()->lastFrame());
}
private:
bool m_showUI;
int m_frame;
};
Command* CommandFactory::createGotoFirstFrameCommand()
{
return new GotoFirstFrameCommand;
}
Command* CommandFactory::createGotoPreviousFrameCommand()
{
return new GotoPreviousFrameCommand;
}
Command* CommandFactory::createGotoNextFrameCommand()
{
return new GotoNextFrameCommand;
}
Command* CommandFactory::createGotoLastFrameCommand()
{
return new GotoLastFrameCommand;
}
Command* CommandFactory::createGotoNextFrameWithSameTagCommand()
{
return new GotoNextFrameWithSameTagCommand;
}
Command* CommandFactory::createGotoPreviousFrameWithSameTagCommand()
{
return new GotoPreviousFrameWithSameTagCommand;
}
Command* CommandFactory::createGotoFrameCommand()
{
return new GotoFrameCommand;
}
} // namespace app