forked from jummbus/jummbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMuteEditor.ts
More file actions
403 lines (352 loc) · 18.5 KB
/
MuteEditor.ts
File metadata and controls
403 lines (352 loc) · 18.5 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright (c) 2012-2022 John Nesky and contributing authors, distributed under the MIT license, see accompanying the LICENSE.md file.
import { SongDocument } from "./SongDocument";
import { HTML } from "imperative-html/dist/esm/elements-strict";
import { ColorConfig } from "./ColorConfig";
import { ChannelRow } from "./ChannelRow";
import { InputBox } from "./HTMLWrapper";
import { ChangeChannelOrder, ChangeChannelName, ChangeRemoveChannel } from "./changes";
import { Config } from "../synth/SynthConfig";
import { SongEditor } from "./SongEditor";
//namespace beepbox {
export class MuteEditor {
private _cornerFiller: HTMLDivElement = HTML.div({style: `background: ${ColorConfig.editorBackground}; position: sticky; bottom: 0; left: 0; width: 32px; height: 30px;`});
private readonly _buttons: HTMLDivElement[] = [];
private readonly _channelCounts: HTMLDivElement[] = [];
private readonly _channelNameDisplay: HTMLDivElement = HTML.div({ style: `background-color: ${ColorConfig.uiWidgetFocus}; white-space:nowrap; display: none; transform:translate(20px); width: auto; pointer-events: none; position: absolute; border-radius: 0.2em; z-index: 2;`, "color": ColorConfig.primaryText }, "");
public readonly _channelNameInput: InputBox = new InputBox(HTML.input({ style: `color: ${ColorConfig.primaryText}; background-color: ${ColorConfig.uiWidgetFocus}; margin-top: -2px; display: none; width: 6em; position: absolute; border-radius: 0.2em; z-index: 2;`, "color": ColorConfig.primaryText }, ""), this._doc, (oldValue: string, newValue: string) => new ChangeChannelName(this._doc, oldValue, newValue));
private readonly _channelDropDown: HTMLSelectElement = HTML.select({ style: "width: 0px; left: 19px; height: 19px; position:absolute; opacity:0" },
HTML.option({ value: "rename" }, "Rename..."),
HTML.option({ value: "chnUp" }, "Move Channel Up"),
HTML.option({ value: "chnDown" }, "Move Channel Down"),
HTML.option({ value: "chnMute" }, "Mute Channel"),
HTML.option({ value: "chnSolo" }, "Solo Channel"),
HTML.option({ value: "chnInsert" }, "Insert Channel Below"),
HTML.option({ value: "chnDelete" }, "Delete This Channel"),
);
public readonly container: HTMLElement = HTML.div({ class: "muteEditor", style: "position: sticky; padding-top: " + Config.barEditorHeight + "px;" }, this._channelNameDisplay, this._channelNameInput.input, this._channelDropDown);
private _editorHeight: number = 128;
private _renderedPitchChannels: number = 0;
private _renderedNoiseChannels: number = 0;
private _renderedChannelHeight: number = -1;
private _renderedModChannels: number = 0;
private _channelDropDownChannel: number = 0;
private _channelDropDownOpen: boolean = false;
private _channelDropDownLastState: boolean = false;
constructor(private _doc: SongDocument, private _editor: SongEditor) {
this.container.addEventListener("click", this._onClick);
this.container.addEventListener("mousemove", this._onMouseMove);
this.container.addEventListener("mouseleave", this._onMouseLeave);
this._channelDropDown.selectedIndex = -1;
this._channelDropDown.addEventListener("change", this._channelDropDownHandler);
this._channelDropDown.addEventListener("mousedown", this._channelDropDownGetOpenedPosition);
this._channelDropDown.addEventListener("blur", this._channelDropDownBlur);
this._channelDropDown.addEventListener("click", this._channelDropDownClick);
this._channelNameInput.input.addEventListener("change", this._channelNameInputHide);
this._channelNameInput.input.addEventListener("blur", this._channelNameInputHide);
this._channelNameInput.input.addEventListener("mousedown", this._channelNameInputClicked);
this._channelNameInput.input.addEventListener("input", this._channelNameInputWhenInput);
}
private _channelNameInputWhenInput = (): void => {
let newValue = this._channelNameInput.input.value;
if (newValue.length > 15) {
this._channelNameInput.input.value = newValue.substring(0, 15);
}
}
private _channelNameInputClicked = (event: MouseEvent): void => {
event.stopPropagation();
}
private _channelNameInputHide = (): void => {
this._channelNameInput.input.style.setProperty("display", "none");
this._channelNameDisplay.style.setProperty("display", "none");
}
private _channelDropDownClick = (event: MouseEvent): void => {
this._channelDropDownOpen = !this._channelDropDownLastState;
this._channelDropDownGetOpenedPosition(event);
//console.log("click " + this._channelDropDownOpen);
}
private _channelDropDownBlur = (): void => {
this._channelDropDownOpen = false;
this._channelNameDisplay.style.setProperty("display", "none");
//console.log("blur " + this._channelDropDownOpen);
}
private _channelDropDownGetOpenedPosition = (event: MouseEvent): void => {
this._channelDropDownLastState = this._channelDropDownOpen;
this._channelDropDownChannel = Math.floor(Math.min(this._buttons.length, Math.max(0, parseInt(this._channelDropDown.style.getPropertyValue("top")) / ChannelRow.patternHeight)));
this._doc.muteEditorChannel = this._channelDropDownChannel;
this._channelNameDisplay.style.setProperty("display", "");
// Check if channel is at limit, in which case another can't be inserted
if ((this._channelDropDownChannel < this._doc.song.pitchChannelCount && this._doc.song.pitchChannelCount == Config.pitchChannelCountMax)
|| (this._channelDropDownChannel >= this._doc.song.pitchChannelCount && this._channelDropDownChannel < this._doc.song.pitchChannelCount + this._doc.song.noiseChannelCount && this._doc.song.noiseChannelCount == Config.noiseChannelCountMax)
|| (this._channelDropDownChannel >= this._doc.song.pitchChannelCount + this._doc.song.noiseChannelCount && this._doc.song.modChannelCount == Config.modChannelCountMax)) {
this._channelDropDown.options[5].disabled = true;
}
else {
this._channelDropDown.options[5].disabled = false;
}
// Also check if a channel is eligible to move up or down based on the song's channel settings.
if (this._channelDropDownChannel == 0 || this._channelDropDownChannel == this._doc.song.pitchChannelCount || this._channelDropDownChannel == this._doc.song.pitchChannelCount + this._doc.song.noiseChannelCount) {
this._channelDropDown.options[1].disabled = true;
}
else {
this._channelDropDown.options[1].disabled = false;
}
if (this._channelDropDownChannel == this._doc.song.pitchChannelCount - 1 || this._channelDropDownChannel == this._doc.song.pitchChannelCount + this._doc.song.noiseChannelCount - 1 || this._channelDropDownChannel == this._doc.song.getChannelCount() - 1) {
this._channelDropDown.options[2].disabled = true;
}
else {
this._channelDropDown.options[2].disabled = false;
}
// Also, can't delete the last pitch channel.
if (this._doc.song.pitchChannelCount == 1 && this._channelDropDownChannel == 0) {
this._channelDropDown.options[6].disabled = true;
}
else {
this._channelDropDown.options[6].disabled = false;
}
}
private _channelDropDownHandler = (event: Event): void => {
this._channelNameDisplay.style.setProperty("display", "none");
this._channelDropDown.style.setProperty("display", "none");
this._channelDropDownOpen = false;
event.stopPropagation();
//console.log("handler " + this._channelDropDownOpen);
switch (this._channelDropDown.value) {
case "rename":
this._channelNameInput.input.style.setProperty("display", "");
this._channelNameInput.input.style.setProperty("transform", this._channelNameDisplay.style.getPropertyValue("transform"));
if (this._channelNameDisplay.textContent != null) {
this._channelNameInput.input.value = this._channelNameDisplay.textContent;
}
else {
this._channelNameInput.input.value = "";
}
this._channelNameInput.input.select();
break;
case "chnUp":
this._doc.record(new ChangeChannelOrder(this._doc, this._channelDropDownChannel, this._channelDropDownChannel, -1));
break;
case "chnDown":
this._doc.record(new ChangeChannelOrder(this._doc, this._channelDropDownChannel, this._channelDropDownChannel, 1));
break;
case "chnMute":
this._doc.song.channels[this._channelDropDownChannel].muted = !this._doc.song.channels[this._channelDropDownChannel].muted;
this.render();
break;
case "chnSolo": {
// Check for any channel not matching solo pattern
let shouldSolo: boolean = false;
for (let channel: number = 0; channel < this._doc.song.pitchChannelCount + this._doc.song.noiseChannelCount; channel++) {
if (this._doc.song.channels[channel].muted == (channel == this._channelDropDownChannel)) {
shouldSolo = true;
channel = this._doc.song.pitchChannelCount + this._doc.song.noiseChannelCount;
}
}
if (shouldSolo) {
for (let channel: number = 0; channel < this._doc.song.pitchChannelCount + this._doc.song.noiseChannelCount; channel++) {
this._doc.song.channels[channel].muted = (channel != this._channelDropDownChannel);
}
}
else {
for (let channel: number = 0; channel < this._doc.song.pitchChannelCount + this._doc.song.noiseChannelCount; channel++) {
this._doc.song.channels[channel].muted = false;
}
}
this.render();
break;
}
case "chnInsert": {
this._doc.channel = this._channelDropDownChannel;
this._doc.selection.resetBoxSelection();
this._doc.selection.insertChannel();
break;
}
case "chnDelete": {
this._doc.record(new ChangeRemoveChannel(this._doc, this._channelDropDownChannel, this._channelDropDownChannel));
break;
}
}
if (this._channelDropDown.value != "rename")
this._editor.refocusStage();
this._channelDropDown.selectedIndex = -1;
}
private _onClick = (event: MouseEvent): void => {
const index = this._buttons.indexOf(<HTMLDivElement>event.target);
if (index == -1) return;
let xPos: number = event.clientX - this._buttons[0].getBoundingClientRect().left;
if (xPos < 21.0) {
this._doc.song.channels[index].muted = !this._doc.song.channels[index].muted;
}
this._doc.notifier.changed();
}
private _onMouseMove = (event: MouseEvent): void => {
const index = this._buttons.indexOf(<HTMLDivElement>event.target);
if (index == -1) {
if (!this._channelDropDownOpen && event.target != this._channelNameDisplay && event.target != this._channelDropDown) {
this._channelNameDisplay.style.setProperty("display", "none");
this._channelDropDown.style.setProperty("display", "none");
this._channelDropDown.style.setProperty("width", "0px");
}
return;
}
if (this._channelDropDownOpen && this._channelNameDisplay.style.getPropertyValue("display") == "none" && this._channelNameInput.input.style.getPropertyValue("display") == "none") {
this._channelDropDownOpen = false;
}
let xPos: number = event.clientX - this._buttons[0].getBoundingClientRect().left;
if (xPos >= 21.0) {
if (!this._channelDropDownOpen) {
// Mouse over chn. number
this._channelDropDown.style.setProperty("display", "");
var height = ChannelRow.patternHeight;
this._channelNameDisplay.style.setProperty("transform", "translate(20px, " + (height / 4 + height * index) + "px)");
if (this._doc.song.channels[index].name != "") {
this._channelNameDisplay.textContent = this._doc.song.channels[index].name;
this._channelNameDisplay.style.setProperty("display", "");
}
else {
if (index < this._doc.song.pitchChannelCount) {
this._channelNameDisplay.textContent = "Pitch " + (index + 1);
} else if (index < this._doc.song.pitchChannelCount + this._doc.song.noiseChannelCount) {
this._channelNameDisplay.textContent = "Noise " + (index - this._doc.song.pitchChannelCount + 1);
}
else {
this._channelNameDisplay.textContent = "Mod " + (index - this._doc.song.pitchChannelCount - this._doc.song.noiseChannelCount + 1);
}
// The name set will only show up when this becomes visible, e.g. when the dropdown is opened.
this._channelNameDisplay.style.setProperty("display", "none");
}
this._channelDropDown.style.top = (Config.barEditorHeight + 2 + index * ChannelRow.patternHeight) + "px";
this._channelDropDown.style.setProperty("width", "15px");
}
}
else {
if (!this._channelDropDownOpen) {
this._channelNameDisplay.style.setProperty("display", "none");
this._channelDropDown.style.setProperty("display", "none");
this._channelDropDown.style.setProperty("width", "0px");
}
}
}
private _onMouseLeave = (event: MouseEvent): void => {
if (!this._channelDropDownOpen) {
this._channelNameDisplay.style.setProperty("display", "none");
this._channelDropDown.style.setProperty("width", "0px");
}
}
public onKeyUp(event: KeyboardEvent): void {
switch (event.keyCode) {
case 27: // esc
this._channelDropDownOpen = false;
//console.log("close");
this._channelNameDisplay.style.setProperty("display", "none");
break;
case 13: // enter
this._channelDropDownOpen = false;
//console.log("close");
this._channelNameDisplay.style.setProperty("display", "none");
break;
default:
break;
}
}
public render(): void {
if (!this._doc.prefs.enableChannelMuting) return;
let startingChannelCount: number = this._buttons.length;
if (this._buttons.length != this._doc.song.getChannelCount()) {
for (let y: number = this._buttons.length; y < this._doc.song.getChannelCount(); y++) {
const channelCountText: HTMLDivElement = HTML.div({ class: "noSelection muteButtonText", style: "display: table-cell; -webkit-text-stroke: 1.5px; vertical-align: middle; text-align: center; -webkit-user-select: none; -webkit-touch-callout: none; -moz-user-select: none; -ms-user-select: none; user-select: none; pointer-events: none; width: 12px; height: 20px; transform: translate(0px, 1px);" });
const muteButton: HTMLDivElement = HTML.div({ class: "mute-button", title: "Mute (M), Mute All (⇧M), Solo (S), Exclude (⇧S)", style: `display: block; pointer-events: none; width: 16px; height: 20px; transform: translate(2px, 1px);` });
const muteContainer: HTMLDivElement = HTML.div({ style: `align-items: center; height: 20px; margin: 0px; display: table; flex-direction: row; justify-content: space-between;` }, [
muteButton,
channelCountText,
]);
this.container.appendChild(muteContainer);
this._buttons[y] = muteContainer;
this._channelCounts[y] = channelCountText;
}
for (let y: number = this._doc.song.getChannelCount(); y < this._buttons.length; y++) {
this.container.removeChild(this._buttons[y]);
}
this._buttons.length = this._doc.song.getChannelCount();
this.container.appendChild(this._cornerFiller);
}
for (let y: number = 0; y < this._doc.song.getChannelCount(); y++) {
if (this._doc.song.channels[y].muted) {
this._buttons[y].children[0].classList.add("muted");
if (y < this._doc.song.pitchChannelCount)
this._channelCounts[y].style.color = ColorConfig.trackEditorBgPitchDim;
else if (y < this._doc.song.pitchChannelCount + this._doc.song.noiseChannelCount)
this._channelCounts[y].style.color = ColorConfig.trackEditorBgNoiseDim;
else
this._channelCounts[y].style.color = ColorConfig.trackEditorBgModDim;
} else {
this._buttons[y].children[0].classList.remove("muted");
if (y < this._doc.song.pitchChannelCount)
this._channelCounts[y].style.color = ColorConfig.trackEditorBgPitch;
else if (y < this._doc.song.pitchChannelCount + this._doc.song.noiseChannelCount)
this._channelCounts[y].style.color = ColorConfig.trackEditorBgNoise;
else
this._channelCounts[y].style.color = ColorConfig.trackEditorBgMod;
}
}
if (this._renderedChannelHeight != ChannelRow.patternHeight || startingChannelCount != this._buttons.length) {
for (let y: number = 0; y < this._doc.song.getChannelCount(); y++) {
this._buttons[y].style.marginTop = ((ChannelRow.patternHeight - 20) / 2) + "px";
this._buttons[y].style.marginBottom = ((ChannelRow.patternHeight - 20) / 2) + "px";
}
}
if (this._renderedModChannels != this._doc.song.modChannelCount || startingChannelCount != this._buttons.length) {
for (let y: number = 0; y < this._doc.song.getChannelCount(); y++) {
if (y < this._doc.song.pitchChannelCount + this._doc.song.noiseChannelCount) {
this._buttons[y].children[0].classList.remove("modMute");
}
else {
this._buttons[y].children[0].classList.add("modMute");
}
}
}
if (this._renderedModChannels != this._doc.song.modChannelCount || this._renderedPitchChannels != this._doc.song.pitchChannelCount || this._renderedNoiseChannels != this._doc.song.noiseChannelCount) {
for (let y: number = 0; y < this._doc.song.getChannelCount(); y++) {
if (y < this._doc.song.pitchChannelCount) {
let val: number = (y + 1);
this._channelCounts[y].textContent = val + "";
this._channelCounts[y].style.fontSize = (val >= 10) ? "xx-small" : "inherit";
}
else if (y < this._doc.song.pitchChannelCount + this._doc.song.noiseChannelCount) {
let val: number = (y - this._doc.song.pitchChannelCount + 1);
this._channelCounts[y].textContent = val + "";
this._channelCounts[y].style.fontSize = (val >= 10) ? "xx-small" : "inherit";
}
else {
let val: number = (y - this._doc.song.pitchChannelCount - this._doc.song.noiseChannelCount + 1);
this._channelCounts[y].textContent = val + "";
this._channelCounts[y].style.fontSize = (val >= 10) ? "xx-small" : "inherit";
}
}
this._renderedPitchChannels = this._doc.song.pitchChannelCount;
this._renderedNoiseChannels = this._doc.song.noiseChannelCount;
this._renderedModChannels = this._doc.song.modChannelCount;
}
if (startingChannelCount != this._buttons.length || this._renderedChannelHeight != ChannelRow.patternHeight) {
this._renderedChannelHeight = ChannelRow.patternHeight;
this._editorHeight = Config.barEditorHeight + this._doc.song.getChannelCount() * ChannelRow.patternHeight;
this._channelNameDisplay.style.setProperty("display", "none");
this.container.style.height = (this._editorHeight + 16) + "px";
if (ChannelRow.patternHeight < 27) {
this._channelNameDisplay.style.setProperty("margin-top", "-2px");
this._channelDropDown.style.setProperty("margin-top", "-4px");
this._channelNameInput.input.style.setProperty("margin-top", "-4px");
}
else if (ChannelRow.patternHeight < 30) {
this._channelNameDisplay.style.setProperty("margin-top", "-1px");
this._channelDropDown.style.setProperty("margin-top", "-3px");
this._channelNameInput.input.style.setProperty("margin-top", "-3px");
}
else {
this._channelNameDisplay.style.setProperty("margin-top", "0px");
this._channelDropDown.style.setProperty("margin-top", "0px");
this._channelNameInput.input.style.setProperty("margin-top", "-2px");
}
}
}
}
//}