feat: added lots of ui

This commit is contained in:
Daniel Ledda
2021-08-29 22:53:15 +02:00
parent a5f5169e62
commit cdf6ef754d
22 changed files with 384 additions and 49 deletions

View File

@@ -32,7 +32,7 @@ export default class Beat implements IPublisher<BeatEvents>{
this.key = `Beat-${Beat.count}`; this.key = `Beat-${Beat.count}`;
this.name = options?.name ?? this.key; this.name = options?.name ?? this.key;
this.setTimeSignature({up: options?.timeSig?.up ?? 4, down: options?.timeSig?.down ?? 4}); this.setTimeSignature({up: options?.timeSig?.up ?? 4, down: options?.timeSig?.down ?? 4});
this.setBars(options?.bars ?? 48); this.setBars(options?.bars ?? 4);
Beat.count++; Beat.count++;
} }
@@ -61,6 +61,10 @@ export default class Beat implements IPublisher<BeatEvents>{
this.publisher.notifySubs(BeatEvents.NewBarCount); this.publisher.notifySubs(BeatEvents.NewBarCount);
} }
getUnitByIndex(index: number): BeatUnit | null {
return this.unitRecord[index] ?? null;
}
private updateBeatUnitLength() { private updateBeatUnitLength() {
const newBarCount = this.barCount * this.timeSigUp; const newBarCount = this.barCount * this.timeSigUp;
if (newBarCount < this.unitRecord.length) { if (newBarCount < this.unitRecord.length) {
@@ -146,7 +150,7 @@ export default class Beat implements IPublisher<BeatEvents>{
return this.key; return this.key;
} }
private static isValidTimeSigRange(sig: number): boolean { static isValidTimeSigRange(sig: number): boolean {
return sig >= 2 && sig <= 64; return sig >= 2 && sig <= 64;
} }

View File

@@ -6,15 +6,19 @@ type BeatGroupInitOptions = {
beats: BeatInitOptions[], beats: BeatInitOptions[],
} }
const enum BeatGroupEvents { export const enum BeatGroupEvents {
BeatOrderChanged, BeatOrderChanged,
BeatListChanged, BeatListChanged,
GlobalBarCountChanged,
GlobalTimeSigUpChanged,
} }
export default class BeatGroup implements IPublisher<BeatGroupEvents> { export default class BeatGroup implements IPublisher<BeatGroupEvents> {
private beats: Beat[] = []; private beats: Beat[] = [];
private beatKeyMap: Record<string, number> = {}; private beatKeyMap: Record<string, number> = {};
private publisher: Publisher<BeatGroupEvents> = new Publisher<BeatGroupEvents>(); private publisher: Publisher<BeatGroupEvents> = new Publisher<BeatGroupEvents>();
private lastGlobalBarCount: number;
private lastGlobalTimeSigUp: number;
constructor(options?: BeatGroupInitOptions) { constructor(options?: BeatGroupInitOptions) {
if (options?.beats) { if (options?.beats) {
@@ -23,6 +27,11 @@ export default class BeatGroup implements IPublisher<BeatGroupEvents> {
this.beats.push(newBeat); this.beats.push(newBeat);
this.beatKeyMap[newBeat.getKey()] = this.beats.length - 1; this.beatKeyMap[newBeat.getKey()] = this.beats.length - 1;
} }
this.lastGlobalBarCount = this.beats[0].getBarCount();
this.lastGlobalTimeSigUp = this.beats[0].getTimeSigUp();
} else {
this.lastGlobalBarCount = 4;
this.lastGlobalTimeSigUp = 4;
} }
} }
@@ -30,6 +39,28 @@ export default class BeatGroup implements IPublisher<BeatGroupEvents> {
return this.publisher.addSubscriber(subscriber, eventType); return this.publisher.addSubscriber(subscriber, eventType);
} }
setGlobalBarCount(barCount: number): void {
if (barCount <= 0 || (barCount | 0) !== barCount) {
return;
}
this.lastGlobalBarCount = barCount;
for (const beat of this.beats) {
beat.setBars(barCount);
}
this.publisher.notifySubs(BeatGroupEvents.GlobalBarCountChanged);
}
setGlobalTimeSigUp(timeSigUp: number): void {
if (!Beat.isValidTimeSigRange(timeSigUp)) {
return;
}
this.lastGlobalTimeSigUp = timeSigUp;
for (const beat of this.beats) {
beat.setTimeSignature({up: timeSigUp});
}
this.publisher.notifySubs(BeatGroupEvents.GlobalTimeSigUpChanged);
}
getBeatByKey(beatKey: string): Beat { getBeatByKey(beatKey: string): Beat {
if (typeof this.beatKeyMap[beatKey] === "undefined") { if (typeof this.beatKeyMap[beatKey] === "undefined") {
throw new Error(`Could not find the beat with key: ${beatKey}`); throw new Error(`Could not find the beat with key: ${beatKey}`);

View File

@@ -7,7 +7,7 @@ export enum BeatUnitType {
} }
const enum BeatUnitEvents { export const enum BeatUnitEvents {
Toggle, Toggle,
On, On,
Off, Off,

View File

@@ -44,7 +44,7 @@ export class Publisher<T extends (string | number)> implements IPublisher<T> {
sub.notify(this, eventType); sub.notify(this, eventType);
} }
for (const sub of this.getSubscribers("all")) { for (const sub of this.getSubscribers("all")) {
sub.notify(this, "all"); sub.notify(this, eventType);
} }
} }
} }

View File

@@ -26,7 +26,6 @@ mainBeatGroup.addBeat({
const appNode = document.querySelector("#app"); const appNode = document.querySelector("#app");
if (appNode) { if (appNode) {
const appRoot = new RootView({ const appRoot = new RootView({
parent: appNode as HTMLDivElement, parent: appNode as HTMLDivElement,

View File

@@ -0,0 +1,32 @@
.beat:first-child {
padding-left: 5px;
}
.beat:last-child {
padding-right: 0;
}
.beat > * {
padding-right: 1em;
padding-left: 1em;
}
.beat-settings-btn {
margin: 0;
display: inline-block;
}
.beat-unit-block {
display: inline-block;
}
.beat-title {
display: inline-block;
width: 3em;
margin: 0;
}
.beat-spacer {
display: inline-block;
width: 1em;
}

View File

@@ -0,0 +1,7 @@
.beat-settings {
display: none;
}
.beat-settings.visible {
display: block;
}

View File

@@ -1,7 +0,0 @@
.beatSettingsView {
display: none;
}
.beatSettingsView.visible {
display: block;
}

View File

@@ -1,8 +1,8 @@
import UINode, {UINodeOptions} from "../../../UINode"; import UINode, {UINodeOptions} from "../../../UINode";
import Beat, {BeatEvents} from "../../../../Beat"; import Beat, {BeatEvents} from "../../../../Beat";
import {IPublisher} from "../../../../Publisher"; import {IPublisher} from "../../../../Publisher";
import "./BeatSettingsView.css";
import ISubscriber from "../../../../Subscriber"; import ISubscriber from "../../../../Subscriber";
import "./BeatSettings.css";
export type BeatSettingsViewUINodeOptions = UINodeOptions & { export type BeatSettingsViewUINodeOptions = UINodeOptions & {
beat: Beat, beat: Beat,
@@ -22,7 +22,7 @@ export default class BeatSettingsView extends UINode implements ISubscriber {
} }
private setupBindings() { private setupBindings() {
this.beat.addSubscriber(this, BeatEvents.NewName); this.beat.addSubscriber(this, "all");
} }
notify<T extends string | number>(publisher: IPublisher<T>, event: "all" | T[] | T) { notify<T extends string | number>(publisher: IPublisher<T>, event: "all" | T[] | T) {
@@ -34,7 +34,7 @@ export default class BeatSettingsView extends UINode implements ISubscriber {
} }
} }
toggleVisible() { toggleVisible(): void {
this.visible = !this.visible; this.visible = !this.visible;
if (this.visible) { if (this.visible) {
this.node?.classList.add("visible"); this.node?.classList.add("visible");
@@ -43,26 +43,43 @@ export default class BeatSettingsView extends UINode implements ISubscriber {
} }
} }
isOpen() { isOpen(): boolean {
return this.visible; return this.visible;
} }
rebuild(): HTMLElement { rebuild(): HTMLElement {
this.timeSigUp = UINode.make("input", {}); this.timeSigUp = UINode.make("input", {
this.timeSigUp.addEventListener("input", value: this.beat.getTimeSigUp().toString(),
(event) => this.beat.setTimeSignature({ oninput: (event) => {
up: Number((event.target as HTMLInputElement).value) })); this.beat.setTimeSignature({up: Number((event.target as HTMLInputElement).value) });
this.timeSigDown = UINode.make("input", {}); },
this.timeSigDown.addEventListener("input", });
(event) => this.beat.setTimeSignature({ this.timeSigDown = UINode.make("input", {
down: Number((event.target as HTMLInputElement).value) })); value: this.beat.getTimeSigDown().toString(),
oninput: (event) => {
this.beat.setTimeSignature({down: Number((event.target as HTMLInputElement).value) });
},
});
this.barCountInput = UINode.make("input", {
value: this.beat.getBarCount().toString(),
oninput: (event) => {
this.beat.setBars(Number((event.target as HTMLInputElement).value));
},
});
this.node = UINode.make("div", { this.node = UINode.make("div", {
subs: [ subs: [
UINode.make("p", {innerText: `Settings for ${this.beat.getName()}`}), UINode.make("div", {
classes: ["beat-settings-time-sig"],
subs: [
UINode.make("label", {innerText: "Time Signature:"}),
this.timeSigUp, this.timeSigUp,
this.timeSigDown, this.timeSigDown,
]
}),
UINode.make("label", {innerText: "Bars:"}),
this.barCountInput,
], ],
classes: ["beatSettingsView"] classes: ["beat-settings"]
}); });
return this.node; return this.node;
} }

View File

@@ -0,0 +1,11 @@
.beat-unit {
width: 2em;
height: 2em;
background-color: white;
border: 0.1em solid black;
display: inline-block;
}
.beat-unit.on {
background-color: darksalmon;
}

View File

@@ -0,0 +1,46 @@
import BeatUnit, {BeatUnitEvents} from "../../../../BeatUnit";
import ISubscriber from "../../../../Subscriber";
import UINode, {UINodeOptions} from "../../../UINode";
import {IPublisher} from "../../../../Publisher";
import "./BeatUnit.css";
export type BeatUnitUINodeOptions = UINodeOptions & {
beatUnit: BeatUnit,
};
export default class BeatUnitView extends UINode implements ISubscriber {
private beatUnit: BeatUnit;
constructor(options: BeatUnitUINodeOptions) {
super(options);
this.beatUnit = options.beatUnit;
this.setupBindings();
this.rebuild();
}
private setupBindings() {
this.beatUnit.addSubscriber(this, "all");
}
notify<T extends string | number>(publisher: IPublisher<T>, event: "all" | T[] | T) {
if (event === BeatUnitEvents.On) {
this.node?.classList.add("on");
} else if (event === BeatUnitEvents.Off) {
this.node?.classList.remove("on");
}
}
rebuild(): HTMLElement {
const classes = ["beat-unit"];
if (this.beatUnit.isOn()) {
classes.push("on");
}
this.node = UINode.make("div", {
classes: classes,
onclick: () => {
this.beatUnit.toggle();
}
});
return this.node;
}
}

View File

@@ -0,0 +1,53 @@
import ISubscriber from "../../../../Subscriber";
import UINode, {UINodeOptions} from "../../../UINode";
import BeatGroup, {BeatGroupEvents} from "../../../../BeatGroup";
import {IPublisher} from "../../../../Publisher";
export type BeatUnitCollectionUINodeOptions = UINodeOptions & {
beatGroup: BeatGroup,
};
export default class BeatUnitCollectionView extends UINode implements ISubscriber {
private beatGroup: BeatGroup;
private barCountInput!: HTMLInputElement;
private timeSigUpInput!: HTMLInputElement;
constructor(options: BeatUnitCollectionUINodeOptions) {
super(options);
this.beatGroup = options.beatGroup;
this.beatGroup.addSubscriber(this, []);
this.rebuild();
}
notify<T extends string | number>(publisher: IPublisher<T>, event: "all" | T[] | T): void {
if (event === BeatGroupEvents.GlobalBarCountChanged) {
this.barCountInput.value = this.beatGroup.getBeatByIndex(0).getBarCount().toString();
} else if (event === BeatGroupEvents.GlobalTimeSigUpChanged) {
this.barCountInput.value = this.beatGroup.getBeatByIndex(0).getBarCount().toString();
}
}
rebuild(): HTMLElement {
this.barCountInput = UINode.make("input", {
type: "text",
classes: ["beat-group-settings-view-bar-count"],
value: this.beatGroup.getBeatByIndex(0).getBarCount(),
oninput: () => {
this.beatGroup.setGlobalBarCount(Number(this.barCountInput.value));
},
});
this.timeSigUpInput = UINode.make("input", {
type: "text",
value: this.beatGroup.getBeatByIndex(0).getTimeSigUp(),
classes: ["beat-group-settings-view-time-sig-up"],
oninput: () => {
this.beatGroup.setGlobalTimeSigUp(Number(this.timeSigUpInput.value));
},
});
this.node = UINode.make("div", {
subs: [
],
});
return this.node;
}
}

View File

@@ -3,6 +3,8 @@ import Beat, {BeatEvents} from "../../../Beat";
import {IPublisher} from "../../../Publisher"; import {IPublisher} from "../../../Publisher";
import BeatSettingsView from "./BeatSettings/BeatSettingsView"; import BeatSettingsView from "./BeatSettings/BeatSettingsView";
import ISubscriber from "../../../Subscriber"; import ISubscriber from "../../../Subscriber";
import BeatUnitView from "./BeatUnit/BeatUnitView";
import "./Beat.css";
export type BeatUINodeOptions = UINodeOptions & { export type BeatUINodeOptions = UINodeOptions & {
beat: Beat, beat: Beat,
@@ -13,6 +15,8 @@ export default class BeatView extends UINode implements ISubscriber {
private title!: HTMLHeadingElement; private title!: HTMLHeadingElement;
private settingsView!: BeatSettingsView; private settingsView!: BeatSettingsView;
private settingsToggleButton!: HTMLButtonElement; private settingsToggleButton!: HTMLButtonElement;
private beatUnitViews: BeatUnitView[] = [];
private beatUnitViewBlock!: HTMLElement;
constructor(options: BeatUINodeOptions) { constructor(options: BeatUINodeOptions) {
super(options); super(options);
@@ -22,12 +26,16 @@ export default class BeatView extends UINode implements ISubscriber {
} }
private setupBindings() { private setupBindings() {
this.beat.addSubscriber(this, BeatEvents.NewName); this.beat.addSubscriber(this, "all");
} }
notify<T extends string | number>(publisher: IPublisher<T>, event: "all" | T[] | T) { notify<T extends string | number>(publisher: IPublisher<T>, event: "all" | T[] | T): void {
if (event === BeatEvents.NewName) { if (event === BeatEvents.NewName) {
this.title.innerText = this.beat.getName(); this.title.innerText = this.beat.getName();
} else if (event === BeatEvents.NewTimeSig) {
this.render();
} else if (event === BeatEvents.NewBarCount) {
this.render();
} }
} }
@@ -36,15 +44,65 @@ export default class BeatView extends UINode implements ISubscriber {
this.settingsToggleButton.innerText = this.settingsView.isOpen() ? "Hide Settings" : "Show Settings"; this.settingsToggleButton.innerText = this.settingsView.isOpen() ? "Hide Settings" : "Show Settings";
} }
private makeBeatUnits() {
const beatUnitCount = this.beat.getBarCount() * this.beat.getTimeSigUp();
this.beatUnitViews = [];
for (let i = 0; i < beatUnitCount; i++) {
const beatUnit = this.beat.getUnitByIndex(i);
if (beatUnit) {
this.beatUnitViews.push(new BeatUnitView({beatUnit}));
}
}
}
private respaceBeatUnits(): void {
this.beatUnitViewBlock.querySelectorAll(".beat-spacer").forEach(spacer => spacer.remove());
const barLength = this.beat.getTimeSigUp();
const barCount = this.beat.getBarCount();
let bars = 0;
let i = -1;
let spacersInserted = false;
while (!spacersInserted) {
i += barLength;
const newSpacer = UINode.make("div", {classes: ["beat-spacer"]});
const leftNeighbour = this.beatUnitViewBlock.children.item(i);
if (leftNeighbour) {
leftNeighbour.insertAdjacentElement("afterend", newSpacer);
} else {
break;
}
i++;
bars++;
if (bars === barCount) {
spacersInserted = true;
}
}
}
rebuild(): HTMLElement { rebuild(): HTMLElement {
this.title = UINode.make("h3", {innerText: this.beat.getName()}); this.title = UINode.make("h3", {
innerText: this.beat.getName(),
classes: ["beat-title"],
});
this.makeBeatUnits();
this.settingsView = new BeatSettingsView({beat: this.beat}); this.settingsView = new BeatSettingsView({beat: this.beat});
this.settingsToggleButton = UINode.make("button", {innerText: this.settingsView.isOpen() ? "Hide Settings" : "Show Settings"}); this.settingsToggleButton = UINode.make("button", {
classes: ["beat-settings-btn"],
innerText: this.settingsView.isOpen() ? "Hide Settings" : "Show Settings",
});
this.settingsToggleButton.addEventListener("click", () => this.toggleSettings()); this.settingsToggleButton.addEventListener("click", () => this.toggleSettings());
this.beatUnitViewBlock = UINode.make("div", {
classes: ["beat-unit-block"],
subs: [
...this.beatUnitViews.map(view => view.rebuild()),
],
});
this.respaceBeatUnits();
this.node = UINode.make("div", { this.node = UINode.make("div", {
classes: ["beat"],
subs: [ subs: [
this.title, this.title,
UINode.make("p", {innerText: "I am a BeatGroup"}), this.beatUnitViewBlock,
this.settingsToggleButton, this.settingsToggleButton,
this.settingsView.rebuild(), this.settingsView.rebuild(),
], ],

View File

@@ -0,0 +1,3 @@
.beat-group {
}

View File

@@ -0,0 +1,3 @@
.beat-group-settings {
}

View File

@@ -0,0 +1,66 @@
import BeatGroup from "../../../BeatGroup";
import UINode, {UINodeOptions} from "../../UINode";
import ISubscriber from "../../../Subscriber";
import {IPublisher} from "../../../Publisher";
import {BeatGroupEvents} from "../../../BeatGroup";
export type BeatGroupSettingsUINodeOptions = UINodeOptions & {
beatGroup: BeatGroup,
};
export default class BeatGroupSettingsView extends UINode implements ISubscriber {
private beatGroup: BeatGroup;
private barCountInput!: HTMLInputElement;
private timeSigUpInput!: HTMLInputElement;
constructor(options: BeatGroupSettingsUINodeOptions) {
super(options);
this.beatGroup = options.beatGroup;
this.beatGroup.addSubscriber(this, [
BeatGroupEvents.GlobalBarCountChanged,
BeatGroupEvents.GlobalTimeSigUpChanged
]);
this.rebuild();
}
notify<T extends string | number>(publisher: IPublisher<T>, event: "all" | T[] | T): void {
if (event === BeatGroupEvents.GlobalBarCountChanged) {
this.barCountInput.value = this.beatGroup.getBeatByIndex(0).getBarCount().toString();
} else if (event === BeatGroupEvents.GlobalTimeSigUpChanged) {
this.barCountInput.value = this.beatGroup.getBeatByIndex(0).getBarCount().toString();
}
}
rebuild(): HTMLElement {
this.barCountInput = UINode.make("input", {
type: "text",
classes: ["beat-group-settings-view-bar-count"],
value: this.beatGroup.getBeatByIndex(0).getBarCount(),
oninput: () => {
this.beatGroup.setGlobalBarCount(Number(this.barCountInput.value));
},
});
this.timeSigUpInput = UINode.make("input", {
type: "text",
value: this.beatGroup.getBeatByIndex(0).getTimeSigUp(),
classes: ["beat-group-settings-view-time-sig-up"],
oninput: () => {
this.beatGroup.setGlobalTimeSigUp(Number(this.timeSigUpInput.value));
},
});
this.node = UINode.make("div", {
subs: [
UINode.make("h4", {innerText: "Settings for beat"}),
UINode.make("label", {
innerText: "Bars:",
}),
this.barCountInput,
UINode.make("label", {
innerText: "Boxes per bar:",
}),
this.timeSigUpInput,
],
});
return this.node;
}
}

View File

@@ -1,6 +1,7 @@
import UINode, {UINodeOptions} from "../UINode"; import UINode, {UINodeOptions} from "../UINode";
import BeatGroup from "../../BeatGroup"; import BeatGroup from "../../BeatGroup";
import BeatView from "./Beat/BeatView"; import BeatView from "./Beat/BeatView";
import BeatGroupSettingsView from "./BeatGroupSettings/BeatGroupSettingsView";
export type BeatGroupUINodeOptions = UINodeOptions & { export type BeatGroupUINodeOptions = UINodeOptions & {
title: string, title: string,
@@ -10,6 +11,7 @@ export type BeatGroupUINodeOptions = UINodeOptions & {
export default class BeatGroupView extends UINode { export default class BeatGroupView extends UINode {
private title: string; private title: string;
private beatGroup: BeatGroup; private beatGroup: BeatGroup;
private beatGroupSettingsView!: BeatGroupSettingsView;
constructor(options: BeatGroupUINodeOptions) { constructor(options: BeatGroupUINodeOptions) {
super(options); super(options);
@@ -22,9 +24,12 @@ export default class BeatGroupView extends UINode {
for (let i = 0; i < this.beatGroup.getBeatCount(); i++) { for (let i = 0; i < this.beatGroup.getBeatCount(); i++) {
beatViews.push(new BeatView({beat: this.beatGroup.getBeatByIndex(i)})); beatViews.push(new BeatView({beat: this.beatGroup.getBeatByIndex(i)}));
} }
this.beatGroupSettingsView = new BeatGroupSettingsView({beatGroup: this.beatGroup});
return UINode.make("div", { return UINode.make("div", {
classes: ["beat-group"],
subs: [ subs: [
UINode.make("h3", {innerText: this.title}), UINode.make("h3", {innerText: this.title}),
this.beatGroupSettingsView.rebuild(),
...beatViews.map(bv => bv.rebuild()) ...beatViews.map(bv => bv.rebuild())
], ],
}); });

View File

@@ -1,8 +1,12 @@
.rootView { .root {
margin: auto; margin: auto;
width: 80%; width: 80%;
} }
.rootView .title { .root .title {
text-align: center; text-align: center;
} }
.root input {
width: 5em;
}

View File

@@ -11,7 +11,7 @@ export type RootUINodeOptions = UINodeOptions & {
export default class RootView extends UINode { export default class RootView extends UINode {
private title: string; private title: string;
private parent: HTMLElement; protected parent: HTMLElement;
private beatGroupView: BeatGroupView; private beatGroupView: BeatGroupView;
private mainBeatGroup: BeatGroup; private mainBeatGroup: BeatGroup;
@@ -24,23 +24,13 @@ export default class RootView extends UINode {
this.rebuild(); this.rebuild();
} }
render() {
const oldNode = this.node;
this.node = this.rebuild();
if (oldNode) {
this.parent.replaceChild(oldNode, this.node);
} else {
this.parent.appendChild(this.node);
}
}
rebuild(): HTMLDivElement { rebuild(): HTMLDivElement {
return UINode.make("div", { return UINode.make("div", {
classes: ["root"],
subs: [ subs: [
UINode.make("h1", {innerText: this.title, classes: ["title"]}), UINode.make("h1", {innerText: this.title, classes: ["title"]}),
this.beatGroupView.rebuild(), this.beatGroupView.rebuild(),
], ],
classes: ["rootView"]
}); });
} }
} }

View File

@@ -12,8 +12,21 @@ type IRenderAttributes<
export default abstract class UINode { export default abstract class UINode {
protected node: HTMLElement | null = null; protected node: HTMLElement | null = null;
protected parent: HTMLElement | null = null;
constructor(options: UINodeOptions) { constructor(options: UINodeOptions) {}
render(): void {
const oldNode = this.node;
this.node = this.rebuild();
if (oldNode) {
if (!this.parent) {
this.parent = oldNode.parentElement;
}
this.parent!.replaceChild(this.node, oldNode);
} else {
this.parent!.appendChild(this.node);
}
} }
abstract rebuild(): HTMLElement; abstract rebuild(): HTMLElement;