From 1ca2aae817cd1a85a894985fa95f604532339bd3 Mon Sep 17 00:00:00 2001 From: Julien MOREAU-MATHIS Date: Fri, 19 Oct 2018 19:33:00 +0200 Subject: [PATCH] Adding way to get a constructor of a custom script using the Code Editor tool --- assets/templates/code/tools.d.ts | 1 + src/extensions/behavior/code.ts | 15 +++++++++++++-- src/extensions/tools/tools.ts | 12 ++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/assets/templates/code/tools.d.ts b/assets/templates/code/tools.d.ts index 5f2e549ce..5b3bb92a9 100644 --- a/assets/templates/code/tools.d.ts +++ b/assets/templates/code/tools.d.ts @@ -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; diff --git a/src/extensions/behavior/code.ts b/src/extensions/behavior/code.ts index 3242c1cd0..7b01759ff 100644 --- a/src/extensions/behavior/code.ts +++ b/src/extensions/behavior/code.ts @@ -64,6 +64,7 @@ export default class CodeExtension extends Extension implement public assetsCaption: string = 'Scripts'; public instances: IStringDictionary = { }; + public scriptsConstructors: IStringDictionary = { }; /** * Constructor @@ -160,6 +161,11 @@ export default class CodeExtension extends Extension 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 @@ -336,9 +342,14 @@ export default class CodeExtension extends Extension 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); diff --git a/src/extensions/tools/tools.ts b/src/extensions/tools/tools.ts index b7ea5832a..8cd718f5b 100644 --- a/src/extensions/tools/tools.ts +++ b/src/extensions/tools/tools.ts @@ -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 = 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