Created
August 28, 2014 18:44
-
-
Save lardratboy/0b64d2540026d1270014 to your computer and use it in GitHub Desktop.
Phaser bpt widget base (work in progress)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Author Brad P. Taylor (bradptaylor+github@gmail.com) license MIT | |
export class WidgetPartDef<T> { | |
constructor( public Type:T, public regex:RegExp, public required:boolean = true ) {} | |
} | |
export class Widget<T> extends Panel { | |
constructor(game, public parts:T ) { | |
super(game); | |
} | |
resolveParts = _.once( () => { | |
for ( var p in this.parts ) { | |
var part:WidgetPartDef<any> = this.parts[p]; | |
this.parts[p] = this.findFirstMatchingDecendantOfType( part.regex, 'name', part.Type ); | |
if ( (undefined === this.parts[p]) && part.required ) { | |
throw "missing " + part.Type.prototype.constructor.name; | |
} else { | |
this[ p ] = this.parts[ p ]; | |
} | |
} | |
} ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Okay this one is a bit involved because I haven't yet figured how to coax typescript generics into allowing me to extend types based on generic parameter. So I worked around this in a possibly bizarre way not sure if this is horrible or good yet... Anyway what this widget base does it provide a way to use the bpt.prefab system to manage content then automatically inject members into the widget.
Here is an example
counter_widget_contents.json
counter_spinner_widget_contents.json