From ccbc93e27d44529e582064b664f193a9696211cc Mon Sep 17 00:00:00 2001 From: jakob Date: Sat, 16 Jun 2018 00:19:31 +0200 Subject: [PATCH] Add Comment component --- src/Container.ts | 5 +++++ src/EffectList.ts | 3 ++- src/Inert.ts | 19 +++++++++++++++++++ src/Main.ts | 3 +++ src/misc/Comment.ts | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/Inert.ts create mode 100644 src/misc/Comment.ts diff --git a/src/Container.ts b/src/Container.ts index d588617..70a164b 100644 --- a/src/Container.ts +++ b/src/Container.ts @@ -3,6 +3,7 @@ import isString from "lodash-es/isString"; import Component, { IComponentConstructor, IContainer } from "./Component"; import ComponentRegistry from "./ComponentRegistry"; import IMain from "./IMain"; +import Inert from "./Inert"; import TextureSetManager from "./webgl/TextureSetManager"; /** @@ -49,6 +50,10 @@ export default abstract class Container extends Component implements IContainer continue; } const component = new componentClass(this.main, this, opts); + if (component instanceof Inert) { + // tslint:disable-next-line:no-console + console.warn(`Inert Component: ${opts.type}. Will not affect rendering.`); + } components.push(component); } } diff --git a/src/EffectList.ts b/src/EffectList.ts index 48f0184..3bd2171 100644 --- a/src/EffectList.ts +++ b/src/EffectList.ts @@ -3,6 +3,7 @@ import Container from "./Container"; import CodeInstance from "./expr/CodeInstance"; import compileExpr from "./expr/compileExpr"; import IMain from "./IMain"; +import Inert from "./Inert"; import { BlendMode } from "./utils"; import TextureSetManager from "./webgl/TextureSetManager"; @@ -177,7 +178,7 @@ export default class EffectList extends Container { // render all the components // for (let i = 0; i < this.components.length; i++) { for (const component of this.components) { - if (component.isEnabled()) { + if (component.isEnabled() && !(component instanceof Inert)) { component.draw(); } } diff --git a/src/Inert.ts b/src/Inert.ts new file mode 100644 index 0000000..6495cd6 --- /dev/null +++ b/src/Inert.ts @@ -0,0 +1,19 @@ +import Component, { IContainer } from "./Component"; +import IMain from "./IMain"; + +/** + * Base class for passive components. + * + * A base class for component types that don't affect rendering, + * most notably Comment, but also unknown components in general. + */ +export default abstract class Inert extends Component { + protected opts: any; + + public init() { + throw new Error("Calling init on inert component."); + } + public draw() { + throw new Error("Calling draw on inert component."); + } +} diff --git a/src/Main.ts b/src/Main.ts index 9136bec..055fb13 100644 --- a/src/Main.ts +++ b/src/Main.ts @@ -8,6 +8,7 @@ import ComponentRegistry from "./ComponentRegistry"; import EffectList from "./EffectList"; import IMain from "./IMain"; import BufferSave from "./misc/BufferSave"; +import Comment from "./misc/Comment"; import GlobalVar from "./misc/GlobalVar"; import Model from "./Model"; import ClearScreen from "./render/ClearScreen"; @@ -403,6 +404,8 @@ export default class Main extends Model implements IMain { BufferSave, GlobalVar, + + Comment, ]); } diff --git a/src/misc/Comment.ts b/src/misc/Comment.ts new file mode 100644 index 0000000..23927e5 --- /dev/null +++ b/src/misc/Comment.ts @@ -0,0 +1,32 @@ +import { IContainer } from "../Component"; +import IMain from "../IMain"; +import Inert from "../Inert"; + +export interface ICommentOpts { + text: string; +} + +/** + * A component containing free text. + */ +export default class Comment extends Inert { + public static componentName: string = "Comment"; + public static componentTag: string = "misc"; + protected static optUpdateHandlers = { + text: "updateText", + }; + protected static defaultOptions: ICommentOpts = { + text: "", + }; + + protected opts: ICommentOpts; + private text: string; + + constructor(main: IMain, parent: IContainer, opts: any) { + super(main, parent, opts); + } + + public updateText() { + this.text = this.opts.text; + } +}