-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommands.ts
293 lines (269 loc) · 11.2 KB
/
commands.ts
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
enum CommandType
{
function, int, float, vec2
}
const control = 1;
const alt = 2;
const shift = 4;
class Command
{
public static commands = [
new Command("commandbar", 0, " ", CommandType.function, null, "Focus on command bar", (_) => {
CloseColorSelector();
CloseExportPanel();
CloseRecentPanel()
if (mainInput != document.activeElement)
mainInput.focus();
else
mainInput.value += " "; // Add a space manually, because the shortcut prevents it
}),
new Command("esc", 0, "Escape", CommandType.function, null, "Loose focus on command bar, hide panels", (_) => {
mainInput.blur();
CloseColorSelector();
CloseExportPanel();
CloseRecentPanel();
SetClickAction(ClickAction.none);
SetSelectAction(SelectAction.none);
SaveHistory();
}),
new Command("clear", shift | alt, "n", CommandType.function, null, "Clear image", (_) => {
CreateImage(imageSizeX, imageSizeY);
CreateNewHistoryEntry();
}),
new Command("imgsize", shift, "n", CommandType.vec2, new vec2(50, 50), "Set size of image", (vec) => {
CreateImage(vec.x, vec.y, true);
}),
new Command("color", 0, "c", CommandType.function, null, "Set the current color", (_) => {
OpenColorSelector((newColor) => {
currentColor = newColor
UpdateToolbarIcons();
}, currentColor);
}),
new Command("altcolor", alt, "c", CommandType.function, null, "Set the alternative color (right click color)", (_) => {
OpenColorSelector((newColor) => {
currentAltColor = newColor
UpdateToolbarIcons();
}, currentAltColor);
}),
new Command("free", 0, "f", CommandType.function, null, "Select free draw tool", (_) => {
SetTool(Tool.free);
}),
new Command("line", 0, "l", CommandType.function, null, "Select line tool", (_) => {
SetTool(Tool.line);
}),
new Command("paintpot", 0, "p", CommandType.function, null, "Select paintpot tool", (_) => {
SetTool(Tool.paintpot);
}),
new Command("rect", 0, "r", CommandType.function, null, "Select rect tool", (_) => {
SetTool(Tool.rect);
}),
new Command("filledrect", alt, "r", CommandType.function, null, "Select filled rect tool", (_) => {
SetTool(Tool.filledRect);
}),
new Command("pxsize", 0, "", CommandType.int, 16, "Set the screen size of one pixel", (val) => {
settings.pixelSize = val;
OnResize();
}),
new Command("seth", alt, "h", CommandType.float, 1, "Set the hue of the current color (0 - 1)", (val) => {
currentColor.SetH(val);
UpdateToolbarIcons();
}),
new Command("sets", alt, "s", CommandType.float, 1, "Set the saturation of the current color (0 - 1)", (val) => {
currentColor.SetS(val);
UpdateToolbarIcons();
}),
new Command("setv", alt, "v", CommandType.float, 1, "Set the value of the current color (0 - 1)", (val) => {
currentColor.SetV(val);
UpdateToolbarIcons();
}),
new Command("setr", alt, "r", CommandType.float, 1, "Set the red value of the current color (0 - 1)", (val) => {
currentColor.r = val;
UpdateToolbarIcons();
}),
new Command("setg", alt, "g", CommandType.float, 1, "Set the green value of the current color (0 - 1)", (val) => {
currentColor.g = val;
UpdateToolbarIcons();
}),
new Command("setb", alt, "b", CommandType.float, 1, "Set the blue value of the current color (0 - 1)", (val) => {
currentColor.b = val;
UpdateToolbarIcons();
}),
new Command("seta", alt, "a", CommandType.float, 1, "Set the alpha value of the current color (0 - 1)", (val) => {
currentColor.a = val;
UpdateToolbarIcons();
}),
new Command("undo", control, "z", CommandType.function, null, "Undo last action", (_) => {
Undo();
}),
new Command("redo", control, "y", CommandType.function, null, "Redo last action", (_) => {
Redo();
}),
new Command("redoshiftz", control | shift, "z", CommandType.function, null, "Redo last action, but with ctrl+shift+Z", (_) => {
Redo();
}),
new Command("theme", alt, "t", CommandType.function, null, "Switch between light and dark themes", (_) => {
SwitchTheme();
}),
new Command("export", control, "s", CommandType.function, null, "Export image as a file", (_) => {
OpenExportPanel();
}),
new Command("colorpicker", 0, "x", CommandType.function, null, "Sets the current color to a color form the image", (_) => {
SetClickAction(ClickAction.picker);
}),
new Command("altpicker", alt, "x", CommandType.function, null, "Sets the alternative color to a color form the image", (_) => {
SetClickAction(ClickAction.altpicker);
}),
new Command("grid", control, "g", CommandType.vec2, new vec2(16, 16), "Displays a grid of the specified size", (vec) => {
useGrid = true;
gridSizeX = vec.x;
gridSizeY = vec.y;
Draw();
}),
new Command("nogrid", control | shift, "g", CommandType.function, null, "Hide the grid", (_) => {
useGrid = false;
Draw();
}),
new Command("open", control, "o", CommandType.function, null, "Load a file", (_) => {
LoadFile();
CreateNewHistoryEntry();
}),
new Command("mes", 0, "m", CommandType.function, null, "Measure a distance", (_) => {
SetSelectAction(SelectAction.measure);
}),
new Command("copy", control, "c", CommandType.function, null, "Copy a region", (_) => {
SetSelectAction(SelectAction.copy);
}),
new Command("cut", control, "x", CommandType.function, null, "Copy a region, then fill it with alt color", (_) => {
SetSelectAction(SelectAction.cut);
}),
new Command("paste", control, "v", CommandType.function, null, "Paste data from clipboard", (_) => {
SetClickAction(ClickAction.paste);
}),
new Command("pastetrans", control | shift, "v", CommandType.function, null, "Paste data from clipboard as a transparent image", (_) => {
SetClickAction(ClickAction.pasteTransparent);
}),
new Command("turn", shift, "t", CommandType.function, null, "Rotate the image by π/2", (_) => {
RotateImage();
}),
new Command("mirrorx", shift, "x", CommandType.function, null, "Mirror image horizontally", (_) => {
Mirror(true);
}),
new Command("mirrory", shift, "y", CommandType.function, null, "Mirror image vertically", (_) => {
Mirror(false);
}),
new Command("recent", control, "r", CommandType.function, null, "Open a recent image", (_) => {
OpenRecentPanel();
}),
new Command("tutorial", 0, "", CommandType.function, null, "Start tutorial", (_) => {
StartTutorial();
}),
new Command("maxundoentries", 0, "", CommandType.int, 200, "Set the maximum amount of undo entries (use lower values to save memory)", (value) => {
localStorage.setItem("undoMaxEntries", value);
settings.maxUndoEntries = value;
}),
]
public name: string;
public description: string;
public type: CommandType;
public modifier: number;
public key: string;
public default: any;
public func: Function;
public constructor(name, modifier, key, type, defaultValue, description, func)
{
this.name = name;
this.modifier = modifier;
this.key = key;
this.type = type;
this.default = defaultValue;
this.description = description;
this.func = func;
}
public GetUI(highlight = false) : HTMLElement
{
let main = document.createElement("div");
main.classList.add("search-entry");
if (highlight)
main.classList.add("highlight");
main.innerHTML = `
<span>${this.name} <span class='command-type'>${this.GetTypeString()}</span></span>
<span>${this.description}</span>
<div class="command-hotkey">${this.GetHotkeyString()}</div>
`
main.addEventListener("click", () => {
if (this.type == CommandType.function)
{
this.Execute(null);
}
else
{
mainInput.value = this.name + " ";
mainInput.focus();
}
});
return main;
}
public Execute(value: any)
{
if (value == null)
value = this.default
let isContinueTutorialCommand = forceTutorialCommand == this.name;
let isAllowedByTutorial = currentTutorialState.allowedCommands.split(" ").some(cmd => cmd == this.name);
let isEscOrCommandbar = this.name == "commandbar" || this.name == "esc";
if (tutorialActive && !isContinueTutorialCommand && !isAllowedByTutorial && !isEscOrCommandbar) // Prevent executing (except for esc and commandbar)
{
infoLeft.textContent = `Can't execute command ${this.name} because the tutorial is active!`
return;
}
else if (isContinueTutorialCommand) // Continue tutorial
{
NextTutorialStep();
}
infoLeft.textContent = `Executing command: ${this.name}`
this.func(value);
commandHistory = commandHistory.filter(c => c != this);
commandHistory.push(this);
}
// TODO: better search
public GetScore(query: string): number
{
let words = query.toLowerCase().split(" ");
let res = 0;
for (let word of words)
{
let trimword = word.trim();
if (trimword.length == 0) continue;
if (this.name.includes(trimword))
res += 10;
if (this.description.toLowerCase().includes(trimword))
res += 1;
}
return res;
}
public GetHotkeyString() : string
{
if (this.key == "") return "";
let res = "";
if ((this.modifier & control) != 0)
res += "<span class='key'>Ctrl</span> + "
if ((this.modifier & shift) != 0)
res += "<span class='key'>Maj</span> + "
if ((this.modifier & alt) != 0)
res += "<span class='key'>Alt</span> + "
let keyName;
if (this.key == " ")
keyName = "Space";
else if (this.key == "Escape")
keyName = "Esc";
else
keyName = this.key.toUpperCase();
return `${res}<span class='key'>${keyName}</span>`
}
private GetTypeString() : string
{
if (this.type == CommandType.float) return "(Float)";
if (this.type == CommandType.int) return "(Int)";
if (this.type == CommandType.vec2) return "(Vector2)";
return "";
}
}