Skip to content

Commit

Permalink
system migrated
Browse files Browse the repository at this point in the history
  • Loading branch information
rvilarl committed Jul 2, 2021
1 parent 1c6f5ea commit a98b63d
Showing 1 changed file with 103 additions and 27 deletions.
130 changes: 103 additions & 27 deletions src/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,98 @@

import { Element } from './element';
import { Factory } from './factory';
import { Stave } from './stave';
import { Formatter } from './formatter';
import { Formatter, FormatterOptions } from './formatter';
import { Note } from './note';
import { Stave, StaveOptions } from './stave';
import { StaveConnector } from './staveconnector';
import { RenderContext } from './types/common';
import { RuntimeError } from './util';
import { Voice } from './voice';

export interface SystemParams {
stave: Stave;
voices: Voice[];
noJustification?: boolean;
// TO REVIEW somehow StaveOptions but missing info
// eslint-disable-next-line
options?: any;
spaceAbove: number;
spaceBelow: number;
debugNoteMetrics: boolean;
}

export interface SystemParamsItems {
stave?: Stave;
voices?: Voice[];
noJustification?: boolean;
// TO REVIEW somehow StaveOptions but missing info
// eslint-disable-next-line
options?: any;
spaceAbove?: number;
spaceBelow?: number;
debugNoteMetrics?: boolean;
}

export interface SystemOptions {
factory?: Factory;
noPadding: boolean;
debugFormatter: boolean;
connector?: StaveConnector;
spaceBetweenStaves: number;
formatIterations: number;
x: number;
width: number;
y: number;
// TO REVIEW somehow FormatterOptions + alpha
// eslint-disable-next-line
details: any;
noJustification: boolean;
}

export interface SystemOptionsItems {
factory?: Factory;
noPadding?: boolean;
debugFormatter?: boolean;
connector?: StaveConnector;
spaceBetweenStaves?: number;
formatIterations?: number;
x?: number;
width?: number;
y?: number;
// TO REVIEW somehow FormatterOptions + alpha
// eslint-disable-next-line
details?: any;
noJustification?: boolean;
}

export interface DebugNoteMetrics {
y: number;
voice: Voice;
}

export class System extends Element {
constructor(params = {}) {
protected options!: SystemOptions;
protected factory!: Factory;
protected formatter?: Formatter;
protected startX?: number;
protected lastY?: number;
protected parts: SystemParams[];
protected connector?: StaveConnector;
protected debugNoteMetricsYs?: DebugNoteMetrics[];

constructor(params: SystemOptionsItems = {}) {
super();
this.setAttribute('type', 'System');
this.setOptions(params);
this.parts = [];
}

setOptions(options = {}) {
setOptions(options: SystemOptionsItems = {}): void {
this.options = {
x: 10,
y: 10,
width: 500,
connector: null,
spaceBetweenStaves: 12, // stave spaces
factory: null,
noJustification: false,
debugFormatter: false,
formatIterations: 0, // number of formatter tuning steps
Expand All @@ -40,13 +112,13 @@ export class System extends Element {
this.factory = this.options.factory || new Factory({ renderer: { el: null } });
}

setContext(context) {
setContext(context: RenderContext): this {
super.setContext(context);
this.factory.setContext(context);
return this;
}

addConnector(type = 'double') {
addConnector(type = 'double'): StaveConnector {
this.connector = this.factory.StaveConnector({
top_stave: this.parts[0].stave,
bottom_stave: this.parts[this.parts.length - 1].stave,
Expand All @@ -55,32 +127,33 @@ export class System extends Element {
return this.connector;
}

addStave(params) {
params = {
stave: null,
addStave(paramsItems: SystemParamsItems): Stave {
let stave = paramsItems.stave;
if (!stave) {
stave = this.factory.Stave({
x: this.options.x,
y: this.options.y,
width: this.options.width,
options: paramsItems.options,
});
}

const params: SystemParams = {
stave,
voices: [],
spaceAbove: 0, // stave spaces
spaceBelow: 0, // stave spaces
debugNoteMetrics: false,
...params,
...paramsItems,
options: {
left_bar: false,
...params.options,
...paramsItems.options,
},
};

if (!params.stave) {
params.stave = this.factory.Stave({
x: this.options.x,
y: this.options.y,
width: this.options.width,
options: params.options,
});
}

params.voices.forEach((voice) =>
voice
.setContext(this.context)
.setContext(this.getContext())
.setStave(params.stave)
.getTickables()
.forEach((tickable) => tickable.setStave(params.stave))
Expand All @@ -90,14 +163,14 @@ export class System extends Element {
return params.stave;
}

format() {
format(): void {
const formatter = new Formatter({ ...this.options.details });
this.formatter = formatter;

let y = this.options.y;
let startX = 0;
let allVoices = [];
const debugNoteMetricsYs = [];
let allVoices: Voice[] = [];
const debugNoteMetricsYs: DebugNoteMetrics[] = [];

// Join the voices for each stave.
this.parts.forEach((part) => {
Expand Down Expand Up @@ -132,9 +205,12 @@ export class System extends Element {
this.lastY = y;
}

draw() {
draw(): void {
// Render debugging information, if requested.
const ctx = this.checkContext();
if (!this.formatter || !this.startX || !this.lastY || !this.debugNoteMetricsYs) {
throw new RuntimeError('NoFormated', 'Format must be instatiated before draw');
}
this.setRendered();

if (this.options.debugFormatter) {
Expand Down

0 comments on commit a98b63d

Please sign in to comment.