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');
|
2018-10-23 16:47:36 +00:00
|
|
|
var ScenePlugin = require('../../../src/plugins/ScenePlugin');
|
|
|
|
var SpineFile = require('./SpineFile');
|
2018-10-22 16:15:45 +00:00
|
|
|
var SpineCanvas = require('SpineCanvas');
|
2018-10-23 16:47:36 +00:00
|
|
|
var SpineWebGL = require('SpineGL');
|
2018-10-24 11:41:56 +00:00
|
|
|
var SpineGameObject = require('./gameobject/SpineGameObject');
|
|
|
|
|
|
|
|
var runtime;
|
2018-10-18 13:59:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* TODO
|
|
|
|
*
|
|
|
|
* @class SpinePlugin
|
2018-10-23 16:47:36 +00:00
|
|
|
* @extends Phaser.Plugins.ScenePlugin
|
2018-10-18 13:59:27 +00:00
|
|
|
* @constructor
|
2018-10-23 16:47:36 +00:00
|
|
|
* @since 3.16.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.
|
|
|
|
*/
|
|
|
|
var SpinePlugin = new Class({
|
|
|
|
|
2018-10-23 16:47:36 +00:00
|
|
|
Extends: ScenePlugin,
|
2018-10-18 13:59:27 +00:00
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2018-10-23 16:47:36 +00:00
|
|
|
function SpinePlugin (scene, pluginManager)
|
2018-10-18 13:59:27 +00:00
|
|
|
{
|
2018-10-24 11:41:56 +00:00
|
|
|
console.log('SpinePlugin created');
|
|
|
|
|
2018-10-23 16:47:36 +00:00
|
|
|
ScenePlugin.call(this, scene, pluginManager);
|
|
|
|
|
|
|
|
var game = pluginManager.game;
|
|
|
|
|
|
|
|
this.canvas = game.canvas;
|
|
|
|
this.context = game.context;
|
2018-10-18 13:59:27 +00:00
|
|
|
|
2018-10-23 16:47:36 +00:00
|
|
|
// Create a custom cache to store the spine data (.atlas files)
|
|
|
|
this.cache = game.cache.addCustom('spine');
|
2018-10-18 13:59:27 +00:00
|
|
|
|
2018-10-23 16:47:36 +00:00
|
|
|
this.json = game.cache.json;
|
2018-10-18 13:59:27 +00:00
|
|
|
|
2018-10-23 16:47:36 +00:00
|
|
|
this.textures = game.textures;
|
2018-10-22 16:15:45 +00:00
|
|
|
|
2018-10-23 16:47:36 +00:00
|
|
|
// Register our file type
|
|
|
|
pluginManager.registerFileType('spine', this.spineFileCallback, scene);
|
2018-10-18 13:59:27 +00:00
|
|
|
|
2018-10-23 16:47:36 +00:00
|
|
|
// Register our game object
|
|
|
|
|
2018-10-24 11:41:56 +00:00
|
|
|
runtime = (game.config.renderType) ? SpineCanvas : SpineWebGL;
|
|
|
|
|
|
|
|
pluginManager.registerGameObject('spine', this.createSpineFactory(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
boot: function ()
|
|
|
|
{
|
|
|
|
this.skeletonRenderer = new SpineCanvas.canvas.SkeletonRenderer(this.game.context);
|
|
|
|
},
|
|
|
|
|
|
|
|
getRuntime: function ()
|
|
|
|
{
|
|
|
|
return runtime;
|
2018-10-23 16:47:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
multifile = new SpineFile(this, key, jsonURL, atlasURL, jsonXhrSettings, atlasXhrSettings);
|
|
|
|
|
|
|
|
this.addFile(multifile.files);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
2018-10-18 13:59:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-10-23 16:47:36 +00:00
|
|
|
* Creates a new Spine Game Object and adds it to the Scene.
|
2018-10-18 13:59:27 +00:00
|
|
|
*
|
2018-10-23 16:47:36 +00:00
|
|
|
* @method Phaser.GameObjects.GameObjectFactory#spineFactory
|
|
|
|
* @since 3.16.0
|
2018-10-18 13:59:27 +00:00
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*
|
2018-10-23 16:47:36 +00:00
|
|
|
* @return {Phaser.GameObjects.Spine} The Game Object that was created.
|
2018-10-18 13:59:27 +00:00
|
|
|
*/
|
2018-10-24 11:41:56 +00:00
|
|
|
createSpineFactory: function (plugin)
|
2018-10-22 16:15:45 +00:00
|
|
|
{
|
2018-10-24 11:41:56 +00:00
|
|
|
var callback = function (x, y, key, animationName, loop)
|
|
|
|
{
|
|
|
|
var spineGO = new SpineGameObject(this.scene, plugin, x, y, key, animationName, loop);
|
2018-10-22 16:15:45 +00:00
|
|
|
|
2018-10-24 11:41:56 +00:00
|
|
|
this.displayList.add(spineGO);
|
|
|
|
this.updateList.add(spineGO);
|
|
|
|
|
|
|
|
return spineGO;
|
|
|
|
};
|
2018-10-23 16:47:36 +00:00
|
|
|
|
2018-10-24 11:41:56 +00:00
|
|
|
return callback;
|
2018-10-23 16:47:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
createSkeleton: function (key)
|
|
|
|
{
|
|
|
|
var atlasData = this.cache.get(key);
|
|
|
|
|
|
|
|
if (!atlasData)
|
|
|
|
{
|
|
|
|
console.warn('No skeleton data for: ' + key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var textures = this.textures;
|
|
|
|
|
|
|
|
var atlas = new SpineCanvas.TextureAtlas(atlasData, function (path)
|
|
|
|
{
|
|
|
|
return new SpineCanvas.canvas.CanvasTexture(textures.get(path).getSourceImage());
|
|
|
|
});
|
2018-10-22 16:15:45 +00:00
|
|
|
|
|
|
|
var atlasLoader = new SpineCanvas.AtlasAttachmentLoader(atlas);
|
|
|
|
|
|
|
|
var skeletonJson = new SpineCanvas.SkeletonJson(atlasLoader);
|
|
|
|
|
2018-10-23 16:47:36 +00:00
|
|
|
var skeletonData = skeletonJson.readSkeletonData(this.json.get(key));
|
2018-10-22 16:15:45 +00:00
|
|
|
|
|
|
|
var skeleton = new SpineCanvas.Skeleton(skeletonData);
|
|
|
|
|
2018-10-24 11:41:56 +00:00
|
|
|
return { skeletonData: skeletonData, skeleton: skeleton };
|
2018-10-22 16:15:45 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getBounds: function (skeleton)
|
|
|
|
{
|
|
|
|
var offset = new SpineCanvas.Vector2();
|
|
|
|
var size = new SpineCanvas.Vector2();
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2018-10-24 11:41:56 +00:00
|
|
|
var stateData = new SpineCanvas.AnimationStateData(skeleton.data);
|
2018-10-22 16:15:45 +00:00
|
|
|
|
2018-10-24 11:41:56 +00:00
|
|
|
var state = new SpineCanvas.AnimationState(stateData);
|
2018-10-22 16:15:45 +00:00
|
|
|
|
2018-10-24 11:41:56 +00:00
|
|
|
return { stateData: stateData, state: state };
|
2018-10-22 16:15:45 +00:00
|
|
|
},
|
|
|
|
|
2018-10-18 13:59:27 +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('update', this.update, this);
|
|
|
|
eventEmitter.off('shutdown', this.shutdown, this);
|
|
|
|
|
|
|
|
this.removeAll();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
|
|
|
|
this.pluginManager = null;
|
|
|
|
this.game = null;
|
|
|
|
this.scene = null;
|
|
|
|
this.systems = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = SpinePlugin;
|