Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Comment component #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand Down Expand Up @@ -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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry If I wasn't clear about this in my previous comment. We'll never have an Inert component in the preset so this if block will never run. What we really want to do here is to implicitly create Inert component when componentClass cannot be found in the registry. So we can just replace the continue; in the previous if block with something like componentClass = Inert;.

// tslint:disable-next-line:no-console
console.warn(`Inert Component: ${opts.type}. Will not affect rendering.`);
}
components.push(component);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/EffectList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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();
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/Inert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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;

/*tslint:disable:no-empty*/
public init() {}
public draw() {
throw new Error("Calling draw on inert component.");
}
}
3 changes: 3 additions & 0 deletions src/Main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -403,6 +404,8 @@ export default class Main extends Model implements IMain {

BufferSave,
GlobalVar,

Comment,
]);
}

Expand Down
32 changes: 32 additions & 0 deletions src/misc/Comment.ts
Original file line number Diff line number Diff line change
@@ -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() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't really need this update handler or the text private property. The private text property is not used anywhere inside this class and it's not exposed to anyone so it has no purpose. The text option is however already maintained in the opts property. When Webvs clients update them with the set method on a component it is automatically updated.

The update handlers are a way for the component to react to changes in opts and update internal state, re-compile shaders etc.

this.text = this.opts.text;
}
}