fix: fixed track unit event handling and initialisation

This commit is contained in:
Daniel Ledda
2022-04-03 15:50:01 +02:00
parent 94e601d563
commit 77b5e25e64
5 changed files with 32 additions and 20 deletions

View File

@@ -29,17 +29,17 @@ export default class BeatView extends UINode implements ISubscriber<EventTypeSub
this.title = options.title; this.title = options.title;
this.currentOrientation = options.orientation ?? "horizontal"; this.currentOrientation = options.orientation ?? "horizontal";
this.subscription = this.beat.addSubscriber(this, EventTypeSubscriptions); this.subscription = this.beat.addSubscriber(this, EventTypeSubscriptions);
this.setupBeatViews(); this.setupTrackViews();
} }
notify(publisher: unknown, event: EventTypeSubscriptions): void { notify(publisher: unknown, event: EventTypeSubscriptions): void {
if (event === BeatEvents.TrackListChanged) { if (event === BeatEvents.TrackListChanged) {
this.setupBeatViews(); this.setupTrackViews();
this.redraw(); this.redraw();
} }
} }
private setupBeatViews(): void { private setupTrackViews(): void {
const newCount = this.beat.getTrackCount(); const newCount = this.beat.getTrackCount();
for (let i = 0; i < newCount; i++) { for (let i = 0; i < newCount; i++) {
const beat = this.beat.getTrackByIndex(i); const beat = this.beat.getTrackByIndex(i);
@@ -69,11 +69,11 @@ export default class BeatView extends UINode implements ISubscriber<EventTypeSub
this.redraw(); this.redraw();
} }
setBeatGroup(newBeatGroup: Beat): void { setBeat(newBeat: Beat): void {
this.beat = newBeatGroup; this.beat = newBeat;
this.subscription.unbind(); this.subscription.unbind();
this.subscription = this.beat.addSubscriber(this, BeatEvents.TrackListChanged); this.subscription = this.beat.addSubscriber(this, BeatEvents.TrackListChanged);
this.setupBeatViews(); this.setupTrackViews();
this.redraw(); this.redraw();
} }

View File

@@ -35,7 +35,7 @@ export default class BeatSettingsView extends UINode implements ISubscriber<Even
this.setupBindings(); this.setupBindings();
} }
setBeatGroup(newBeat: Beat): void { setBeat(newBeat: Beat): void {
this.beat = newBeat; this.beat = newBeat;
this.setupBindings(); this.setupBindings();
EventTypeSubscriptions.forEach(eventType => this.notify(null, eventType)); EventTypeSubscriptions.forEach(eventType => this.notify(null, eventType));
@@ -78,7 +78,7 @@ export default class BeatSettingsView extends UINode implements ISubscriber<Even
if (this.trackSettingsViews[i]) { if (this.trackSettingsViews[i]) {
this.trackSettingsViews[i].setBeat(this.beat.getTrackByIndex(i)); this.trackSettingsViews[i].setBeat(this.beat.getTrackByIndex(i));
} else { } else {
this.trackSettingsViews.push(new TrackSettingsView({ track: this.beat.getTrackByIndex(i) })); this.trackSettingsViews.unshift(new TrackSettingsView({ track: this.beat.getTrackByIndex(i) }));
} }
} }
if (!this.trackSettingsContainer) { if (!this.trackSettingsContainer) {

View File

@@ -62,8 +62,8 @@ export default class RootView extends UINode {
setMainBeatGroup(beat: Beat): void { setMainBeatGroup(beat: Beat): void {
this.focusedBeat = beat; this.focusedBeat = beat;
this.beatSettingsView.setBeatGroup(this.focusedBeat); this.beatSettingsView.setBeat(this.focusedBeat);
this.beatView.setBeatGroup(this.focusedBeat); this.beatView.setBeat(this.focusedBeat);
this.stageTitleBarView.setBeat(this.focusedBeat); this.stageTitleBarView.setBeat(this.focusedBeat);
} }

View File

@@ -72,8 +72,8 @@ export default class TrackView extends UINode implements ISubscriber<EventTypeSu
} else { } else {
view = new TrackUnitView({trackUnit}); view = new TrackUnitView({trackUnit});
this.trackUnitViews.push(view); this.trackUnitViews.push(view);
view.onHover(() => this.onBeatViewHover(view)); view.onHover(() => this.onTrackUnitViewHover(view));
view.onMouseUp((event: MouseEvent) => this.onTrackUnitClick(event.button, i)); view.onMouseDown((event: MouseEvent) => this.onTrackUnitClick(event.button, i));
} }
} }
} }
@@ -84,15 +84,14 @@ export default class TrackView extends UINode implements ISubscriber<EventTypeSu
private onTrackUnitClick(button: number, index: number) { private onTrackUnitClick(button: number, index: number) {
if (button === 0) { if (button === 0) {
TrackView.selectingUnits = true; TrackView.selectingUnits = true;
this.track.getUnitByIndex(index)?.toggle();
} else if (button === 2) { } else if (button === 2) {
TrackView.deselectingUnits = true; TrackView.deselectingUnits = true;
this.track.getUnitByIndex(index)?.setOn(false); this.track.getUnitByIndex(index)?.setOn(false);
} }
} }
private onBeatViewHover(trackView: TrackUnitView) { private onTrackUnitViewHover(trackUnitView: TrackUnitView) {
this.lastHoveredTrackUnitView = trackView; this.lastHoveredTrackUnitView = trackUnitView;
if (TrackView.selectingUnits) { if (TrackView.selectingUnits) {
this.lastHoveredTrackUnitView.turnOn(); this.lastHoveredTrackUnitView.turnOn();
} else if (TrackView.deselectingUnits) { } else if (TrackView.deselectingUnits) {

View File

@@ -20,14 +20,14 @@ export default class TrackUnitView extends UINode implements ISubscriber<EventTy
private subscription: ISubscription | null = null; private subscription: ISubscription | null = null;
private publisher: IPublisher<TrackUnitEvent> = new Publisher<TrackUnitEvent, TrackUnitView>(this); private publisher: IPublisher<TrackUnitEvent> = new Publisher<TrackUnitEvent, TrackUnitView>(this);
private rotationTimeout: ReturnType<typeof setTimeout> | null = null; private rotationTimeout: ReturnType<typeof setTimeout> | null = null;
private mouseUpListeners: ((ev: MouseEvent) => void)[] = []; private mouseDownListeners: ((ev: MouseEvent) => void)[] = [];
private hoverListeners: ((ev: MouseEvent) => void)[] = []; private hoverListeners: ((ev: MouseEvent) => void)[] = [];
private blockNextMouseUp = false; private blockNextMouseUp = false;
constructor(options: TrackUnitUINodeOptions) { constructor(options: TrackUnitUINodeOptions) {
super(options); super(options);
this.trackUnit = options.trackUnit; this.trackUnit = options.trackUnit;
this.setupBindings(); this.setUnit(this.trackUnit);
} }
setUnit(trackUnit: TrackUnit | null): void { setUnit(trackUnit: TrackUnit | null): void {
@@ -45,8 +45,12 @@ export default class TrackUnitView extends UINode implements ISubscriber<EventTy
this.subscription?.unbind(); this.subscription?.unbind();
this.subscription = this.trackUnit.addSubscriber(this, EventTypeSubscriptions); this.subscription = this.trackUnit.addSubscriber(this, EventTypeSubscriptions);
this.hoverListeners.forEach(listener => this.getNode().removeEventListener("mouseover", listener)); this.hoverListeners.forEach(listener => this.getNode().removeEventListener("mouseover", listener));
this.mouseDownListeners.forEach(listener => this.getNode().removeEventListener("mousedown", listener));
this.redraw(); this.redraw();
this.hoverListeners.forEach(listener => this.getNode().addEventListener("mouseover", listener));
this.mouseDownListeners.forEach(listener => this.getNode().addEventListener("mousedown", listener));
this.getNode().addEventListener("mousedown", (ev) => this.handleMouseDown(ev)); this.getNode().addEventListener("mousedown", (ev) => this.handleMouseDown(ev));
this.getNode().addEventListener("mouseout", (ev) => this.handleMouseOut(ev));
this.getNode().addEventListener("mouseup", (ev) => this.handleMouseUp(ev)); this.getNode().addEventListener("mouseup", (ev) => this.handleMouseUp(ev));
this.getNode().addEventListener("touchstart", (ev) => this.handleTouchStart(ev)); this.getNode().addEventListener("touchstart", (ev) => this.handleTouchStart(ev));
this.getNode().addEventListener("touchend", (ev) => this.handleTouchEnd(ev)); this.getNode().addEventListener("touchend", (ev) => this.handleTouchEnd(ev));
@@ -56,6 +60,7 @@ export default class TrackUnitView extends UINode implements ISubscriber<EventTy
if (ev.button === 1) { if (ev.button === 1) {
this.trackUnit.rotateType(); this.trackUnit.rotateType();
} else if (ev.button === 0) { } else if (ev.button === 0) {
this.toggle();
this.rotationTimeout = this.rotationTimeout || setTimeout(() => { this.rotationTimeout = this.rotationTimeout || setTimeout(() => {
this.trackUnit.rotateType(); this.trackUnit.rotateType();
this.blockNextMouseUp = true; this.blockNextMouseUp = true;
@@ -64,13 +69,20 @@ export default class TrackUnitView extends UINode implements ISubscriber<EventTy
} }
} }
private handleMouseOut(ev: MouseEvent): void {
if (this.rotationTimeout) {
clearTimeout(this.rotationTimeout);
this.rotationTimeout = null;
}
}
private handleMouseUp(ev: MouseEvent): void { private handleMouseUp(ev: MouseEvent): void {
if (this.rotationTimeout) { if (this.rotationTimeout) {
clearTimeout(this.rotationTimeout); clearTimeout(this.rotationTimeout);
this.rotationTimeout = null; this.rotationTimeout = null;
} }
if (!this.blockNextMouseUp) { if (!this.blockNextMouseUp) {
this.mouseUpListeners.forEach(listener => listener(ev)); this.mouseDownListeners.forEach(listener => listener(ev));
} }
this.blockNextMouseUp = false; this.blockNextMouseUp = false;
} }
@@ -148,7 +160,8 @@ export default class TrackUnitView extends UINode implements ISubscriber<EventTy
this.getNode().addEventListener("mouseover", cb); this.getNode().addEventListener("mouseover", cb);
} }
onMouseUp(cb: (ev: MouseEvent) => void): void { onMouseDown(cb: (ev: MouseEvent) => void): void {
this.mouseUpListeners.push(cb); this.mouseDownListeners.push(cb);
this.getNode().addEventListener("mousedown", cb);
} }
} }