Skip to content

Commit

Permalink
Merge pull request #1001 from ronyeh/migration/notesubgroup
Browse files Browse the repository at this point in the history
Migrate NoteSubGroup
  • Loading branch information
0xfe authored May 31, 2021
2 parents 94fc2d8 + a8e1171 commit 64223d0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/notesubgroup.js → src/notesubgroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ import { Flow } from './tables';
import { Modifier } from './modifier';
import { Formatter } from './formatter';
import { Voice } from './voice';
import { ModifierContextState } from './modifiercontext';
import { Note } from './note';
import { RenderContext } from './types/common';

export class NoteSubGroup extends Modifier {
static get CATEGORY() {
static get CATEGORY(): string {
return 'notesubgroup';
}

// Arrange groups inside a `ModifierContext`
static format(groups, state) {
static format(groups: NoteSubGroup[], state: ModifierContextState): boolean {
if (!groups || groups.length === 0) return false;

let width = 0;
Expand All @@ -33,16 +36,19 @@ export class NoteSubGroup extends Modifier {
return true;
}

constructor(subNotes) {
protected subNotes: Note[];
protected preFormatted: boolean;
protected formatter: Formatter;
protected voice: Voice;

constructor(subNotes: Note[]) {
super();
this.setAttribute('type', 'NoteSubGroup');

this.note = null;
this.index = null;
this.position = Modifier.Position.LEFT;
this.subNotes = subNotes;
this.subNotes.forEach((subNote) => {
subNote.ignore_ticks = false;
subNote.setIgnoreTicks(false);
});
this.width = 0;
this.preFormatted = false;
Expand All @@ -55,45 +61,44 @@ export class NoteSubGroup extends Modifier {
}).setStrict(false);

this.voice.addTickables(this.subNotes);

return this;
}

getCategory() {
getCategory(): string {
return NoteSubGroup.CATEGORY;
}

preFormat() {
preFormat(): void {
if (this.preFormatted) return;

this.formatter.joinVoices([this.voice]).format([this.voice], 0);
this.setWidth(this.formatter.getMinTotalWidth());
this.preFormatted = true;
}

setNote(note) {
setNote(note: Note): this {
this.note = note;
return this;
}
setWidth(width) {

setWidth(width: number): this {
this.width = width;
return this;
}
getWidth() {

getWidth(): number {
return this.width;
}

draw() {
this.checkContext();

draw(): void {
const ctx: RenderContext = this.checkContext();
const note = this.getNote();

if (!(note && this.index !== null)) {
throw new Vex.RuntimeError('NoAttachedNote', "Can't draw notes without a parent note and parent note index.");
if (!note) {
throw new Vex.RuntimeError('NoAttachedNote', "Can't draw notes without a parent note.");
}

this.setRendered();
this.alignSubNotesWithNote(this.subNotes, note); // Modifier function

// Draw notes
this.subNotes.forEach((subNote) => subNote.setContext(this.context).drawWithStyle());
this.subNotes.forEach((subNote) => subNote.setContext(ctx).drawWithStyle());
}
}
4 changes: 4 additions & 0 deletions src/tickable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ export abstract class Tickable extends Element {
return this.ignore_ticks;
}

setIgnoreTicks(flag: boolean): void {
this.ignore_ticks = flag;
}

/** Sets width of note. Used by the formatter for positioning. */
setWidth(width: number): void {
this.width = width;
Expand Down

0 comments on commit 64223d0

Please sign in to comment.