2018-10-18 13:59:27 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
var Class = require('../../../src/utils/Class');
|
2019-07-17 15:14:11 +00:00
|
|
|
var GetValue = require('../../../src/utils/object/GetValue');
|
2019-07-16 16:34:48 +00:00
|
|
|
var ScenePlugin = require('../../../src/plugins/ScenePlugin');
|
|
|
|
var SpineFile = require('./SpineFile');
|
|
|
|
var Spine = require('Spine');
|
|
|
|
var SpineGameObject = require('./gameobject/SpineGameObject');
|
|
|
|
var Matrix4 = require('../../../src/math/Matrix4');
|
2018-10-18 13:59:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @classdesc
|
2019-07-16 16:34:48 +00:00
|
|
|
* TODO
|
2018-10-18 13:59:27 +00:00
|
|
|
*
|
|
|
|
* @class SpinePlugin
|
2018-10-23 16:47:36 +00:00
|
|
|
* @extends Phaser.Plugins.ScenePlugin
|
2018-10-18 13:59:27 +00:00
|
|
|
* @constructor
|
2019-07-16 16:34:48 +00:00
|
|
|
* @since 3.19.0
|
2018-10-18 13:59:27 +00:00
|
|
|
*
|
2018-10-23 16:47:36 +00:00
|
|
|
* @param {Phaser.Scene} scene - A reference to the Scene that has installed this plugin.
|
2018-10-18 13:59:27 +00:00
|
|
|
* @param {Phaser.Plugins.PluginManager} pluginManager - A reference to the Phaser Plugin Manager.
|
|
|
|
*/
|
2019-07-16 16:34:48 +00:00
|
|
|
var SpinePlugin = new Class({
|
2018-10-18 13:59:27 +00:00
|
|
|
|
2019-07-16 16:34:48 +00:00
|
|
|
Extends: ScenePlugin,
|
2018-10-18 13:59:27 +00:00
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2019-07-16 16:34:48 +00:00
|
|
|
function SpinePlugin (scene, pluginManager)
|
2018-10-18 13:59:27 +00:00
|
|
|
{
|
2019-07-16 16:34:48 +00:00
|
|
|
ScenePlugin.call(this, scene, pluginManager);
|
2018-10-23 16:47:36 +00:00
|
|
|
|
|
|
|
var game = pluginManager.game;
|
|
|
|
|
2019-07-16 16:34:48 +00:00
|
|
|
this.isWebGL = (game.config.renderType === 2);
|
|
|
|
|
|
|
|
// Create a custom cache to store the spine data (.atlas files)
|
|
|
|
this.cache = game.cache.addCustom('spine');
|
|
|
|
|
|
|
|
this.spineTextures = game.cache.addCustom('spineTextures');
|
|
|
|
|
|
|
|
this.json = game.cache.json;
|
|
|
|
|
|
|
|
this.textures = game.textures;
|
|
|
|
|
|
|
|
this.skeletonRenderer;
|
|
|
|
|
|
|
|
this.drawDebug = false;
|
|
|
|
|
|
|
|
this.gl;
|
|
|
|
this.mvp;
|
|
|
|
this.shader;
|
|
|
|
this.batcher;
|
|
|
|
this.debugRenderer;
|
|
|
|
this.debugShader;
|
|
|
|
|
2019-07-17 15:14:11 +00:00
|
|
|
console.log('SpinePlugin created', '- WebGL:', this.isWebGL);
|
2019-07-16 16:34:48 +00:00
|
|
|
|
|
|
|
if (this.isWebGL)
|
|
|
|
{
|
|
|
|
this.runtime = Spine.webgl;
|
|
|
|
|
|
|
|
this.gl = game.renderer.gl;
|
|
|
|
|
|
|
|
this.getAtlas = this.getAtlasWebGL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.runtime = Spine.canvas;
|
|
|
|
|
|
|
|
this.getAtlas = this.getAtlasCanvas;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register our file type
|
|
|
|
pluginManager.registerFileType('spine', this.spineFileCallback, scene);
|
|
|
|
|
|
|
|
// Register our game object
|
|
|
|
pluginManager.registerGameObject('spine', this.createSpineFactory(this));
|
2018-10-24 11:41:56 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
boot: function ()
|
|
|
|
{
|
2019-07-16 16:34:48 +00:00
|
|
|
if (this.isWebGL)
|
|
|
|
{
|
|
|
|
this.bootWebGL();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.bootCanvas();
|
|
|
|
}
|
2019-07-17 13:39:50 +00:00
|
|
|
|
|
|
|
var eventEmitter = this.systems.events;
|
|
|
|
|
|
|
|
eventEmitter.once('shutdown', this.shutdown, this);
|
|
|
|
eventEmitter.once('destroy', this.destroy, this);
|
2018-10-24 11:41:56 +00:00
|
|
|
},
|
|
|
|
|
2019-07-16 16:34:48 +00:00
|
|
|
bootCanvas: function ()
|
2018-10-24 11:41:56 +00:00
|
|
|
{
|
2019-07-16 16:34:48 +00:00
|
|
|
this.skeletonRenderer = new this.runtime.SkeletonRenderer(this.scene.sys.context);
|
2018-10-23 16:47:36 +00:00
|
|
|
},
|
|
|
|
|
2019-07-16 16:34:48 +00:00
|
|
|
getAtlasCanvas: function (key)
|
2018-10-23 16:47:36 +00:00
|
|
|
{
|
|
|
|
var atlasData = this.cache.get(key);
|
|
|
|
|
|
|
|
if (!atlasData)
|
|
|
|
{
|
2019-07-16 16:34:48 +00:00
|
|
|
console.warn('No atlas data for: ' + key);
|
2018-10-23 16:47:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-16 16:34:48 +00:00
|
|
|
var atlas;
|
|
|
|
var spineTextures = this.spineTextures;
|
|
|
|
|
|
|
|
if (spineTextures.has(key))
|
|
|
|
{
|
|
|
|
atlas = new Spine.TextureAtlas(atlasData, function ()
|
|
|
|
{
|
|
|
|
return spineTextures.get(key);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var textures = this.textures;
|
|
|
|
|
|
|
|
atlas = new Spine.TextureAtlas(atlasData, function (path)
|
|
|
|
{
|
|
|
|
var canvasTexture = new Spine.canvas.CanvasTexture(textures.get(path).getSourceImage());
|
|
|
|
|
|
|
|
spineTextures.add(key, canvasTexture);
|
|
|
|
|
|
|
|
return canvasTexture;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return atlas;
|
|
|
|
},
|
|
|
|
|
|
|
|
bootWebGL: function ()
|
|
|
|
{
|
|
|
|
var gl = this.gl;
|
|
|
|
var runtime = this.runtime;
|
|
|
|
|
|
|
|
this.mvp = new Matrix4();
|
|
|
|
|
|
|
|
// Create a simple shader, mesh, model-view-projection matrix and SkeletonRenderer.
|
|
|
|
this.shader = runtime.Shader.newTwoColoredTextured(gl);
|
|
|
|
this.batcher = new runtime.PolygonBatcher(gl);
|
|
|
|
|
|
|
|
this.skeletonRenderer = new runtime.SkeletonRenderer(gl);
|
|
|
|
|
|
|
|
this.shapes = new runtime.ShapeRenderer(gl);
|
|
|
|
|
|
|
|
this.debugRenderer = new runtime.SkeletonDebugRenderer(gl);
|
|
|
|
|
|
|
|
this.debugShader = runtime.Shader.newColored(gl);
|
|
|
|
},
|
|
|
|
|
|
|
|
getAtlasWebGL: function (key)
|
|
|
|
{
|
|
|
|
var atlasData = this.cache.get(key);
|
|
|
|
|
|
|
|
if (!atlasData)
|
|
|
|
{
|
|
|
|
console.warn('No atlas data for: ' + key);
|
|
|
|
return;
|
|
|
|
}
|
2018-10-23 16:47:36 +00:00
|
|
|
|
2019-07-16 16:34:48 +00:00
|
|
|
var atlas;
|
|
|
|
var spineTextures = this.spineTextures;
|
2018-10-24 13:09:11 +00:00
|
|
|
|
2019-07-16 16:34:48 +00:00
|
|
|
if (spineTextures.has(key))
|
2018-10-23 16:47:36 +00:00
|
|
|
{
|
2019-07-16 16:34:48 +00:00
|
|
|
atlas = new Spine.TextureAtlas(atlasData, function ()
|
2018-10-24 13:09:11 +00:00
|
|
|
{
|
2019-07-16 16:34:48 +00:00
|
|
|
return spineTextures.get(key);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var textures = this.textures;
|
|
|
|
|
|
|
|
var gl = this.gl;
|
|
|
|
|
|
|
|
atlas = new Spine.TextureAtlas(atlasData, function (path)
|
2018-10-24 13:09:11 +00:00
|
|
|
{
|
2019-07-16 16:34:48 +00:00
|
|
|
var glTexture = new Spine.webgl.GLTexture(gl, textures.get(path).getSourceImage());
|
|
|
|
|
|
|
|
spineTextures.add(key, glTexture);
|
|
|
|
|
|
|
|
return glTexture;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return atlas;
|
|
|
|
},
|
|
|
|
|
|
|
|
spineFileCallback: function (key, jsonURL, atlasURL, jsonXhrSettings, atlasXhrSettings)
|
|
|
|
{
|
|
|
|
var multifile;
|
|
|
|
|
|
|
|
if (Array.isArray(key))
|
|
|
|
{
|
|
|
|
for (var i = 0; i < key.length; i++)
|
|
|
|
{
|
|
|
|
multifile = new SpineFile(this, key[i]);
|
|
|
|
|
|
|
|
this.addFile(multifile.files);
|
2018-10-24 13:09:11 +00:00
|
|
|
}
|
2019-07-16 16:34:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
multifile = new SpineFile(this, key, jsonURL, atlasURL, jsonXhrSettings, atlasXhrSettings);
|
2018-10-22 16:15:45 +00:00
|
|
|
|
2019-07-16 16:34:48 +00:00
|
|
|
this.addFile(multifile.files);
|
|
|
|
}
|
2018-10-22 16:15:45 +00:00
|
|
|
|
2019-07-16 16:34:48 +00:00
|
|
|
return this;
|
|
|
|
},
|
2018-10-22 16:15:45 +00:00
|
|
|
|
2019-07-16 16:34:48 +00:00
|
|
|
/**
|
|
|
|
* Creates a new Spine Game Object and adds it to the Scene.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.GameObjectFactory#spineFactory
|
|
|
|
* @since 3.16.0
|
|
|
|
*
|
|
|
|
* @param {number} x - The horizontal position of this Game Object.
|
|
|
|
* @param {number} y - The vertical position of this Game Object.
|
|
|
|
* @param {string} texture - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
|
|
|
|
* @param {(string|integer)} [frame] - An optional frame from the Texture this Game Object is rendering with.
|
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.Spine} The Game Object that was created.
|
|
|
|
*/
|
|
|
|
createSpineFactory: function (plugin)
|
|
|
|
{
|
|
|
|
var callback = function (x, y, key, animationName, loop)
|
|
|
|
{
|
|
|
|
var spineGO = new SpineGameObject(this.scene, plugin, x, y, key, animationName, loop);
|
|
|
|
|
|
|
|
this.displayList.add(spineGO);
|
|
|
|
this.updateList.add(spineGO);
|
|
|
|
|
|
|
|
return spineGO;
|
|
|
|
};
|
|
|
|
|
|
|
|
return callback;
|
|
|
|
},
|
|
|
|
|
|
|
|
getRuntime: function ()
|
|
|
|
{
|
|
|
|
return this.runtime;
|
|
|
|
},
|
|
|
|
|
|
|
|
createSkeleton: function (key, skeletonJSON)
|
|
|
|
{
|
2019-07-17 15:14:11 +00:00
|
|
|
var atlasKey = key;
|
|
|
|
var jsonKey = key;
|
|
|
|
|
|
|
|
if (key.indexOf('.'))
|
|
|
|
{
|
|
|
|
var parts = key.split('.');
|
|
|
|
|
|
|
|
atlasKey = parts.shift();
|
|
|
|
jsonKey = parts.join('.');
|
|
|
|
}
|
|
|
|
|
|
|
|
var atlas = this.getAtlas(atlasKey);
|
2019-07-16 16:34:48 +00:00
|
|
|
|
|
|
|
var atlasLoader = new Spine.AtlasAttachmentLoader(atlas);
|
|
|
|
|
|
|
|
var skeletonJson = new Spine.SkeletonJson(atlasLoader);
|
|
|
|
|
2019-07-17 15:14:11 +00:00
|
|
|
var data;
|
|
|
|
|
|
|
|
if (skeletonJSON)
|
|
|
|
{
|
|
|
|
data = skeletonJSON;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var json = this.json.get(atlasKey);
|
|
|
|
|
|
|
|
data = GetValue(json, jsonKey);
|
|
|
|
}
|
2019-07-16 16:34:48 +00:00
|
|
|
|
|
|
|
var skeletonData = skeletonJson.readSkeletonData(data);
|
2018-10-22 16:15:45 +00:00
|
|
|
|
2019-07-16 16:34:48 +00:00
|
|
|
var skeleton = new Spine.Skeleton(skeletonData);
|
2018-10-22 16:15:45 +00:00
|
|
|
|
2018-10-24 11:41:56 +00:00
|
|
|
return { skeletonData: skeletonData, skeleton: skeleton };
|
2018-10-22 16:15:45 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getBounds: function (skeleton)
|
|
|
|
{
|
2019-07-16 16:34:48 +00:00
|
|
|
var offset = new Spine.Vector2();
|
|
|
|
var size = new Spine.Vector2();
|
2018-10-22 16:15:45 +00:00
|
|
|
|
|
|
|
skeleton.getBounds(offset, size, []);
|
|
|
|
|
|
|
|
return { offset: offset, size: size };
|
|
|
|
},
|
|
|
|
|
2018-10-24 11:41:56 +00:00
|
|
|
createAnimationState: function (skeleton)
|
2018-10-22 16:15:45 +00:00
|
|
|
{
|
2019-07-16 16:34:48 +00:00
|
|
|
var stateData = new Spine.AnimationStateData(skeleton.data);
|
2018-10-22 16:15:45 +00:00
|
|
|
|
2019-07-16 16:34:48 +00:00
|
|
|
var state = new Spine.AnimationState(stateData);
|
2018-10-22 16:15:45 +00:00
|
|
|
|
2018-10-24 11:41:56 +00:00
|
|
|
return { stateData: stateData, state: state };
|
2019-07-16 16:34:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Scene that owns this plugin is shutting down.
|
|
|
|
* We need to kill and reset all internal properties as well as stop listening to Scene events.
|
|
|
|
*
|
|
|
|
* @method Camera3DPlugin#shutdown
|
|
|
|
* @private
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
shutdown: function ()
|
|
|
|
{
|
|
|
|
var eventEmitter = this.systems.events;
|
|
|
|
|
|
|
|
eventEmitter.off('shutdown', this.shutdown, this);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Scene that owns this plugin is being destroyed.
|
|
|
|
* We need to shutdown and then kill off all external references.
|
|
|
|
*
|
|
|
|
* @method Camera3DPlugin#destroy
|
|
|
|
* @private
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
this.shutdown();
|
|
|
|
|
2019-07-17 13:39:50 +00:00
|
|
|
this.pluginManager.removeGameObject('spine', true, true);
|
|
|
|
|
2019-07-16 16:34:48 +00:00
|
|
|
this.pluginManager = null;
|
|
|
|
this.game = null;
|
|
|
|
this.scene = null;
|
|
|
|
this.systems = null;
|
2019-07-17 13:39:50 +00:00
|
|
|
|
|
|
|
// Create a custom cache to store the spine data (.atlas files)
|
|
|
|
this.cache = null;
|
|
|
|
this.spineTextures = null;
|
|
|
|
this.json = null;
|
|
|
|
this.textures = null;
|
|
|
|
this.skeletonRenderer = null;
|
|
|
|
this.gl = null;
|
|
|
|
this.mvp = null;
|
|
|
|
this.shader = null;
|
|
|
|
this.batcher = null;
|
|
|
|
this.debugRenderer = null;
|
|
|
|
this.debugShader = null;
|
2018-10-18 13:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2019-07-16 16:34:48 +00:00
|
|
|
module.exports = SpinePlugin;
|