Skip to content

Commit

Permalink
Adding way to get a constructor of a custom script using the Code Edi…
Browse files Browse the repository at this point in the history
…tor tool
  • Loading branch information
julien-moreau committed Oct 19, 2018
1 parent a916a27 commit 1ca2aae
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions assets/templates/code/tools.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface BehaviorCodeTools {
getCustomScript (objectName: string | BABYLON.Node | BABYLON.Scene | BABYLON.ParticleSystem, name: string): IScript;
getCustomMaterial (name: string): ICustomMaterial;
getCustomPostProcess (name: string): ICustomPostProcess;
getConstructor (name: string): any;
getFileByName (name: string): File;
getFileUrl (filename: string, oneTimeOnly?: boolean): string;
getPathFinder (name: string): PathFinder;
Expand Down
15 changes: 13 additions & 2 deletions src/extensions/behavior/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export default class CodeExtension extends Extension<BehaviorMetadata> implement
public assetsCaption: string = 'Scripts';

public instances: IStringDictionary<any> = { };
public scriptsConstructors: IStringDictionary<any> = { };

/**
* Constructor
Expand Down Expand Up @@ -160,6 +161,11 @@ export default class CodeExtension extends Extension<BehaviorMetadata> implement
this.datas = data;

// For each node
this.datas.scripts.forEach(s => {
const ctor = this.getConstructor(s, null);
this.scriptsConstructors[s.name] = ctor;
});

this.datas.nodes.forEach(d => {
let node: Scene | Node | IParticleSystem = d.node === 'Scene'
? this.scene
Expand Down Expand Up @@ -336,9 +342,14 @@ export default class CodeExtension extends Extension<BehaviorMetadata> implement
*/
public getConstructor (code: BehaviorCode, node: any, evaluate?: boolean): any {
let url = window.location.href;
url = url.replace(BabylonTools.GetFilename(url), '') + 'behaviors/' + (node instanceof Scene ? 'scene/' : node.name.replace(/ /g, '') + '/') + code.name.replace(/ /g, '') + '.js';
url = url.replace(BabylonTools.GetFilename(url), '') + 'behaviors/';

if (node)
url += (node instanceof Scene ? 'scene/' : node.name.replace(/ /g, '') + '/') + code.name.replace(/ /g, '') + '.js';
else
url += code.name + '.js';

const fnName = (node instanceof Scene ? 'scene' : node.name.replace(/ /g, '')) + code.name.replace(/ /g, '');
const fnName = node ? (node instanceof Scene ? 'scene' : node.name.replace(/ /g, '')) + code.name.replace(/ /g, '') : code.name.replace(/ /g, '');
const effectiveCode = template.replace('{{name}}', fnName)
.replace('{{node}}', this._getEffectiveConstructorName(node))
.replace('{{code}}', code.compiledCode || code.code);
Expand Down
12 changes: 12 additions & 0 deletions src/extensions/tools/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ export default class Tools {
}
}

/**
* Returns the constructor of a script which has the given name
* @param name the name of the script
*/
public getConstructor (name: string): any {
const ext = <CodeExtension> Extensions.Instances['BehaviorExtension'];
if (!ext)
return null;

return ext.scriptsConstructors[name];
}

/**
* Returns the post-process by giving its name
* @param name the name of the post-process
Expand Down

0 comments on commit 1ca2aae

Please sign in to comment.