@osbjs/osbjs
Version:
a minimalist osu! storyboarding framework
85 lines (84 loc) • 4.04 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Storyboard = void 0;
const fs_1 = require("fs");
const Enums_1 = require("../Enums");
const Components_1 = require("../Components");
const chalk_1 = require("chalk");
const path_1 = require("path");
class Storyboard {
constructor(filename, path = './storyboard') {
this.layers = {
background: [],
foreground: [],
fail: [],
pass: [],
overlay: [],
sample: [],
};
this.filename = filename;
this.path = path;
}
_extractEachLayerOsbString() {
const bg = this.layers.background.map((component) => component.getOsbString()).join(''), fg = this.layers.foreground.map((component) => component.getOsbString()).join(''), f = this.layers.fail.map((component) => component.getOsbString()).join(''), p = this.layers.pass.map((component) => component.getOsbString()).join(''), s = this.layers.sample.map((component) => component.getOsbString()).join(''), ov = this.layers.overlay.map((component) => component.getOsbString()).join('');
return { bg, fg, f, p, s, ov };
}
getOsbString() {
const { bg, fg, f, p, s, ov } = this._extractEachLayerOsbString();
let str = `[Events]\n//Background and Video events\n//Storyboard Layer 0 (Background)\n${bg}//Storyboard Layer 1 (Fail)\n${f}//Storyboard Layer 2 (Pass)\n${p}//Storyboard Layer 3 (Foreground)\n${fg}//Storyboard Layer 4 (Overlay)\n${ov}//Storyboard Sound Samples\n${s}`;
return str;
}
_addToLayer(component) {
if (component instanceof Components_1.Sprite || component instanceof Components_1.Animation) {
switch (component.layer) {
case Enums_1.Layer.Background:
this.layers.background.push(component);
break;
case Enums_1.Layer.Foreground:
this.layers.foreground.push(component);
break;
case Enums_1.Layer.Fail:
this.layers.fail.push(component);
break;
case Enums_1.Layer.Pass:
this.layers.pass.push(component);
break;
case Enums_1.Layer.Overlay:
this.layers.overlay.push(component);
default:
break;
}
}
else if (component instanceof Components_1.Sample) {
this.layers.sample.push(component);
}
else {
this.layers.background = this.layers.background.concat(component.layers.background);
this.layers.foreground = this.layers.foreground.concat(component.layers.foreground);
this.layers.fail = this.layers.fail.concat(component.layers.fail);
this.layers.pass = this.layers.pass.concat(component.layers.pass);
this.layers.overlay = this.layers.overlay.concat(component.layers.overlay);
}
}
/**
* Register components to this storyboard. You can supply as many components as you want.
* Note: later component will have higher z-index therefore it might appear on top of earlier components if their active time overlap.
* @param components Component instances
*/
registerComponents(...components) {
components.forEach((component) => {
component.generate();
this._addToLayer(component);
});
}
/**
* Generate storyboard. Call this after every component is registered.
*/
generate() {
if (!(0, fs_1.existsSync)(this.path))
(0, fs_1.mkdirSync)(this.path, { recursive: true });
(0, fs_1.writeFileSync)((0, path_1.join)(this.path, this.filename), this.getOsbString());
console.log((0, chalk_1.green) `Storyboard generated in ${this.path}/${this.filename}`);
}
}
exports.Storyboard = Storyboard;