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

Fix naming convention for parsing from File or snippet #12791

Merged
merged 2 commits into from
Jul 25, 2022
Merged
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
10 changes: 9 additions & 1 deletion packages/dev/core/src/Animations/animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1506,7 +1506,7 @@ export class Animation {
* @param snippetId defines the snippet to load
* @returns a promise that will resolve to the new animation or a new array of animations
*/
public static CreateFromSnippetAsync(snippetId: string): Promise<Animation | Array<Animation>> {
public static ParseFromSnippetAsync(snippetId: string): Promise<Animation | Array<Animation>> {
return new Promise((resolve, reject) => {
const request = new WebRequest();
request.addEventListener("readystatechange", () => {
Expand Down Expand Up @@ -1542,6 +1542,14 @@ export class Animation {
request.send();
});
}

/**
* Creates an animation or an array of animations from a snippet saved by the Inspector
* @deprecated Please use ParseFromSnippetAsync instead
* @param snippetId defines the snippet to load
* @returns a promise that will resolve to the new animation or a new array of animations
*/
public static CreateFromSnippetAsync = Animation.ParseFromSnippetAsync;
}

RegisterClass("BABYLON.Animation", Animation);
Expand Down
51 changes: 27 additions & 24 deletions packages/dev/core/src/Materials/Node/nodeMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1729,17 +1729,13 @@ export class NodeMaterial extends PushMaterial {

/**
* Loads the current Node Material from a url pointing to a file save by the Node Material Editor
* @deprecated Please use NodeMaterial.ParseFromFileAsync instead
* @param url defines the url to load from
* @param rootUrl defines the root URL for nested url in the node material
* @returns a promise that will fulfil when the material is fully loaded
*/
public loadAsync(url: string, rootUrl: string = "") {
return this.getScene()
._loadFileAsync(url)
.then((data) => {
const serializationObject = JSON.parse(data as string);
this.loadFromSerialization(serializationObject, rootUrl);
});
public async loadAsync(url: string, rootUrl: string = "") {
return NodeMaterial.ParseFromFileAsync(url, rootUrl, this.getScene());
}

private _gatherBlocks(rootNode: NodeMaterialBlock, list: NodeMaterialBlock[]) {
Expand Down Expand Up @@ -1899,7 +1895,7 @@ export class NodeMaterial extends PushMaterial {
* @param rootUrl defines the root URL to use to load textures and relative dependencies
* @param merge defines whether or not the source must be merged or replace the current content
*/
public loadFromSerialization(source: any, rootUrl: string = "", merge = false) {
public parseSerializedObject(source: any, rootUrl: string = "", merge = false) {
if (!merge) {
this.clear();
}
Expand Down Expand Up @@ -1987,6 +1983,17 @@ export class NodeMaterial extends PushMaterial {
}
}

/**
* Clear the current graph and load a new one from a serialization object
* @param source defines the JSON representation of the material
* @param rootUrl defines the root URL to use to load textures and relative dependencies
* @param merge defines whether or not the source must be merged or replace the current content
* @deprecated Please use the parseSerializedObject method instead
*/
public loadFromSerialization(source: any, rootUrl: string = "", merge = false) {
this.parseSerializedObject(source, rootUrl, merge);
}

/**
* Makes a duplicate of the current material.
* @param name defines the name to use for the new material
Expand All @@ -1999,7 +2006,7 @@ export class NodeMaterial extends PushMaterial {
clone.id = name;
clone.name = name;

clone.loadFromSerialization(serializationObject);
clone.parseSerializedObject(serializationObject);
clone._buildId = this._buildId;
clone.build(false, !shareEffect);

Expand All @@ -2016,7 +2023,7 @@ export class NodeMaterial extends PushMaterial {
public static Parse(source: any, scene: Scene, rootUrl: string = ""): NodeMaterial {
const nodeMaterial = SerializationHelper.Parse(() => new NodeMaterial(source.name, scene), source, scene, rootUrl);

nodeMaterial.loadFromSerialization(source, rootUrl);
nodeMaterial.parseSerializedObject(source, rootUrl);
nodeMaterial.build();

return nodeMaterial;
Expand All @@ -2031,20 +2038,16 @@ export class NodeMaterial extends PushMaterial {
* @param skipBuild defines whether to build the node material
* @returns a promise that will resolve to the new node material
*/
public static ParseFromFileAsync(name: string, url: string, scene: Scene, rootUrl: string = "", skipBuild: boolean = false): Promise<NodeMaterial> {
public static async ParseFromFileAsync(name: string, url: string, scene: Scene, rootUrl: string = "", skipBuild: boolean = false): Promise<NodeMaterial> {
const material = new NodeMaterial(name, scene);

return new Promise((resolve, reject) => {
return material
.loadAsync(url, rootUrl)
.then(() => {
if (!skipBuild) {
material.build();
}
resolve(material);
})
.catch(reject);
});
const data = await scene._loadFileAsync(url);
const serializationObject = JSON.parse(data as string);
material.parseSerializedObject(serializationObject, rootUrl);
if (!skipBuild) {
material.build();
}
return material;
}

/**
Expand All @@ -2064,7 +2067,7 @@ export class NodeMaterial extends PushMaterial {
skipBuild: boolean = false
): Promise<NodeMaterial> {
if (snippetId === "_BLANK") {
return Promise.resolve(this.CreateDefault("blank", scene));
return Promise.resolve(NodeMaterial.CreateDefault("blank", scene));
}

return new Promise((resolve, reject) => {
Expand All @@ -2080,7 +2083,7 @@ export class NodeMaterial extends PushMaterial {
nodeMaterial.uniqueId = scene.getUniqueId();
}

nodeMaterial.loadFromSerialization(serializationObject);
nodeMaterial.parseSerializedObject(serializationObject);
nodeMaterial.snippetId = snippetId;

try {
Expand Down
12 changes: 11 additions & 1 deletion packages/dev/core/src/Materials/shaderMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1730,7 +1730,7 @@ export class ShaderMaterial extends PushMaterial {
* @param rootUrl defines the root URL to use to load textures and relative dependencies
* @returns a promise that will resolve to the new ShaderMaterial
*/
public static CreateFromSnippetAsync(snippetId: string, scene: Scene, rootUrl: string = ""): Promise<ShaderMaterial> {
public static ParseFromSnippetAsync(snippetId: string, scene: Scene, rootUrl: string = ""): Promise<ShaderMaterial> {
return new Promise((resolve, reject) => {
const request = new WebRequest();
request.addEventListener("readystatechange", () => {
Expand All @@ -1753,6 +1753,16 @@ export class ShaderMaterial extends PushMaterial {
request.send();
});
}

/**
* Creates a ShaderMaterial from a snippet saved by the Inspector
* @deprecated Please use ParseFromSnippetAsync instead
* @param snippetId defines the snippet to load
* @param scene defines the hosting scene
* @param rootUrl defines the root URL to use to load textures and relative dependencies
* @returns a promise that will resolve to the new ShaderMaterial
*/
public static CreateFromSnippetAsync = ShaderMaterial.ParseFromSnippetAsync;
}

RegisterClass("BABYLON.ShaderMaterial", ShaderMaterial);
14 changes: 13 additions & 1 deletion packages/dev/core/src/Particles/particleHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class ParticleHelper {
* @param capacity defines the system capacity (if null or undefined the sotred capacity will be used)
* @returns a promise that will resolve to the new particle system
*/
public static CreateFromSnippetAsync(snippetId: string, scene: Scene, gpu: boolean = false, rootUrl: string = "", capacity?: number): Promise<IParticleSystem> {
public static ParseFromSnippetAsync(snippetId: string, scene: Scene, gpu: boolean = false, rootUrl: string = "", capacity?: number): Promise<IParticleSystem> {
if (snippetId === "_BLANK") {
const system = this.CreateDefault(null);
system.start();
Expand Down Expand Up @@ -207,4 +207,16 @@ export class ParticleHelper {
request.send();
});
}

/**
* Creates a particle system from a snippet saved by the particle system editor
* @deprecated Please use ParseFromSnippetAsync instead
* @param snippetId defines the snippet to load (can be set to _BLANK to create a default one)
* @param scene defines the hosting scene
* @param gpu If the system will use gpu
* @param rootUrl defines the root URL to use to load textures and relative dependencies
* @param capacity defines the system capacity (if null or undefined the sotred capacity will be used)
* @returns a promise that will resolve to the new particle system
*/
public static CreateFromSnippetAsync = ParticleHelper.ParseFromSnippetAsync;
}
12 changes: 11 additions & 1 deletion packages/dev/core/src/Sprites/spriteManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ export class SpriteManager implements ISpriteManager {
* @param rootUrl defines the root URL to use to load textures and relative dependencies
* @returns a promise that will resolve to the new sprite manager
*/
public static CreateFromSnippetAsync(snippetId: string, scene: Scene, rootUrl: string = ""): Promise<SpriteManager> {
public static ParseFromSnippetAsync(snippetId: string, scene: Scene, rootUrl: string = ""): Promise<SpriteManager> {
if (snippetId === "_BLANK") {
return Promise.resolve(new SpriteManager("Default sprite manager", "//playground.babylonjs.com/textures/player.png", 500, 64, scene));
}
Expand All @@ -755,4 +755,14 @@ export class SpriteManager implements ISpriteManager {
request.send();
});
}

/**
* Creates a sprite manager from a snippet saved by the sprite editor
* @deprecated Please use ParseFromSnippetAsync instead
* @param snippetId defines the snippet to load (can be set to _BLANK to create a default one)
* @param scene defines the hosting scene
* @param rootUrl defines the root URL to use to load textures and relative dependencies
* @returns a promise that will resolve to the new sprite manager
*/
public static CreateFromSnippetAsync = SpriteManager.ParseFromSnippetAsync;
}
74 changes: 46 additions & 28 deletions packages/dev/gui/src/2D/advancedDynamicTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,7 @@ export class AdvancedDynamicTexture extends DynamicTexture {
* @param serializedObject define the JSON serialized object to restore from
* @param scaleToSize defines whether to scale to texture to the saved size
*/
public parseContent(serializedObject: any, scaleToSize?: boolean) {
public parseSerializedObject(serializedObject: any, scaleToSize?: boolean) {
this._rootContainer = Control.Parse(serializedObject.root, this) as Container;
if (scaleToSize) {
const width = serializedObject.width;
Expand All @@ -1148,38 +1148,54 @@ export class AdvancedDynamicTexture extends DynamicTexture {
}
}

/**
* Recreate the content of the ADT from a JSON object
* @param serializedObject define the JSON serialized object to restore from
* @param scaleToSize defines whether to scale to texture to the saved size
* @deprecated Please use parseSerializedObject instead
*/
public parseContent = this.parseSerializedObject;

/**
* Recreate the content of the ADT from a snippet saved by the GUI editor
* @param snippetId defines the snippet to load
* @param scaleToSize defines whether to scale to texture to the saved size
* @param appendToAdt if provided the snippet will be appended to the adt. Otherwise a fullscreen ADT will be created.
* @returns a promise that will resolve on success
*/
public parseFromSnippetAsync(snippetId: string, scaleToSize?: boolean): Promise<void> {
public static async ParseFromSnippetAsync(snippetId: string, scaleToSize?: boolean, appendToAdt?: AdvancedDynamicTexture): Promise<AdvancedDynamicTexture> {
const adt = appendToAdt ?? AdvancedDynamicTexture.CreateFullscreenUI("ADT from snippet");
if (snippetId === "_BLANK") {
return Promise.resolve();
return adt;
}

return new Promise((resolve, reject) => {
const request = new WebRequest();
request.addEventListener("readystatechange", () => {
if (request.readyState == 4) {
if (request.status == 200) {
const snippet = JSON.parse(JSON.parse(request.responseText).jsonPayload);
const serializationObject = JSON.parse(snippet.gui);

this.parseContent(serializationObject, scaleToSize);
this.snippetId = snippetId;
const serialized = await AdvancedDynamicTexture._LoadURLContentAsync(AdvancedDynamicTexture.SnippetUrl + "/" + snippetId.replace(/#/g, "/"), true);
adt.parseSerializedObject(serialized, scaleToSize);
return adt;
}

resolve();
} else {
reject("Unable to load the snippet " + snippetId);
}
}
});
/**
* Recreate the content of the ADT from a snippet saved by the GUI editor
* @param snippetId defines the snippet to load
* @param scaleToSize defines whether to scale to texture to the saved size
* @returns a promise that will resolve on success
*/
public parseFromSnippetAsync(snippetId: string, scaleToSize?: boolean): Promise<AdvancedDynamicTexture> {
return AdvancedDynamicTexture.ParseFromSnippetAsync(snippetId, scaleToSize, this);
}

request.open("GET", AdvancedDynamicTexture.SnippetUrl + "/" + snippetId.replace(/#/g, "/"));
request.send();
});
/**
* Recreate the content of the ADT from a url json
* @param url defines the url to load
* @param scaleToSize defines whether to scale to texture to the saved size
* @param appendToAdt if provided the snippet will be appended to the adt. Otherwise a fullscreen ADT will be created.
* @returns a promise that will resolve on success
*/
public static async ParseFromFileAsync(url: string, scaleToSize?: boolean, appendToAdt?: AdvancedDynamicTexture): Promise<AdvancedDynamicTexture> {
const adt = appendToAdt ?? AdvancedDynamicTexture.CreateFullscreenUI("ADT from URL");
const serialized = await AdvancedDynamicTexture._LoadURLContentAsync(url);
adt.parseSerializedObject(serialized, scaleToSize);
return adt;
}

/**
Expand All @@ -1188,21 +1204,23 @@ export class AdvancedDynamicTexture extends DynamicTexture {
* @param scaleToSize defines whether to scale to texture to the saved size
* @returns a promise that will resolve on success
*/
public parseFromURLAsync(url: string, scaleToSize?: boolean): Promise<void> {
public parseFromURLAsync(url: string, scaleToSize?: boolean): Promise<AdvancedDynamicTexture> {
return AdvancedDynamicTexture.ParseFromFileAsync(url, scaleToSize, this);
}

private static _LoadURLContentAsync(url: string, snippet: boolean = false): Promise<any> {
if (url === "") {
return Promise.resolve();
return Promise.reject("No URL provided");
}

return new Promise((resolve, reject) => {
const request = new WebRequest();
request.addEventListener("readystatechange", () => {
if (request.readyState == 4) {
if (request.status == 200) {
const gui = request.responseText;
const gui = snippet ? JSON.parse(JSON.parse(request.responseText).jsonPayload).gui : request.responseText;
const serializationObject = JSON.parse(gui);
this.parseContent(serializationObject, scaleToSize);

resolve();
resolve(serializationObject);
} else {
reject("Unable to load");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class LoadAnimationComponent extends React.Component<ILoadAnimationCompon
const context = this.props.context;
const snippetId = this._textInput.current!.value;

Animation.CreateFromSnippetAsync(snippetId)
Animation.ParseFromSnippetAsync(snippetId)
.then((animations) => {
context.snippetId = snippetId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export class ParticleSystemPropertyGridComponent extends React.Component<IPartic
system.dispose();
this.props.globalState.onSelectionChangedObservable.notifyObservers(null);

ParticleHelper.CreateFromSnippetAsync(snippedId, scene, isGpu)
ParticleHelper.ParseFromSnippetAsync(snippedId, scene, isGpu)
.then((newSystem) => {
this.props.globalState.onSelectionChangedObservable.notifyObservers(newSystem);
})
Expand Down Expand Up @@ -264,8 +264,8 @@ export class ParticleSystemPropertyGridComponent extends React.Component<IPartic

if (windowAsAny.Playground && oldId) {
windowAsAny.Playground.onRequestCodeChangeObservable.notifyObservers({
regex: new RegExp(`ParticleHelper.CreateFromSnippetAsync\\("${oldId}`, "g"),
replace: `ParticleHelper.CreateFromSnippetAsync("${system.snippetId}`,
regex: new RegExp(`ParticleHelper.ParseFromSnippetAsync\\("${oldId}`, "g"),
replace: `ParticleHelper.ParseFromSnippetAsync("${system.snippetId}`,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class SpriteManagerPropertyGridComponent extends React.Component<ISpriteM
spriteManager.dispose();
this.props.globalState.onSelectionChangedObservable.notifyObservers(null);

SpriteManager.CreateFromSnippetAsync(snippedId, scene)
SpriteManager.ParseFromSnippetAsync(snippedId, scene)
.then((newManager) => {
this.props.globalState.onSelectionChangedObservable.notifyObservers(newManager);
})
Expand Down Expand Up @@ -123,8 +123,8 @@ export class SpriteManagerPropertyGridComponent extends React.Component<ISpriteM

if (windowAsAny.Playground && oldId) {
windowAsAny.Playground.onRequestCodeChangeObservable.notifyObservers({
regex: new RegExp(`SpriteManager.CreateFromSnippetAsync\\("${oldId}`, "g"),
replace: `SpriteManager.CreateFromSnippetAsync("${spriteManager.snippetId}`,
regex: new RegExp(`SpriteManager.ParseFromSnippetAsync\\("${oldId}`, "g"),
replace: `SpriteManager.ParseFromSnippetAsync("${spriteManager.snippetId}`,
});
}

Expand Down
Loading