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-24 13:09:11 +00:00
|
|
|
var BaseSpinePlugin = require('./BaseSpinePlugin');
|
2018-10-22 16:15:45 +00:00
|
|
|
var SpineCanvas = require('SpineCanvas');
|
2018-10-24 13:09:11 +00:00
|
|
|
var SpineWebGL = require('SpineWebGL');
|
2018-10-24 11:41:56 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2018-10-24 13:09:11 +00:00
|
|
|
var SpineCanvasPlugin = new Class({
|
2018-10-18 13:59:27 +00:00
|
|
|
|
2018-10-24 13:09:11 +00:00
|
|
|
Extends: BaseSpinePlugin,
|
2018-10-18 13:59:27 +00:00
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2018-10-24 13:09:11 +00:00
|
|
|
function SpineCanvasPlugin (scene, pluginManager)
|
2018-10-18 13:59:27 +00:00
|
|
|
{
|
2018-10-24 11:41:56 +00:00
|
|
|
console.log('SpinePlugin created');
|
|
|
|
|
2018-10-24 13:09:11 +00:00
|
|
|
BaseSpinePlugin.call(this, scene, pluginManager);
|
2018-10-23 16:47:36 +00:00
|
|
|
|
|
|
|
var game = pluginManager.game;
|
|
|
|
|
2018-10-24 13:09:11 +00:00
|
|
|
runtime = (game.config.renderType === 1) ? SpineCanvas : SpineWebGL;
|
2018-10-24 11:41:56 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
boot: function ()
|
|
|
|
{
|
2018-10-24 13:09:11 +00:00
|
|
|
this.skeletonRenderer = (this.game.config.renderType === 1) ? SpineCanvas.canvas.SkeletonRenderer(this.game.context) : SpineWebGL;
|
2018-10-24 11:41:56 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getRuntime: function ()
|
|
|
|
{
|
|
|
|
return runtime;
|
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;
|
|
|
|
|
2018-10-24 13:09:11 +00:00
|
|
|
var useWebGL = this.game.config.renderType;
|
|
|
|
|
|
|
|
var atlas = new runtime.TextureAtlas(atlasData, function (path)
|
2018-10-23 16:47:36 +00:00
|
|
|
{
|
2018-10-24 13:09:11 +00:00
|
|
|
if (useWebGL)
|
|
|
|
{
|
|
|
|
// return new SpineCanvas.canvas.CanvasTexture(textures.get(path).getSourceImage());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return new SpineCanvas.canvas.CanvasTexture(textures.get(path).getSourceImage());
|
|
|
|
}
|
2018-10-23 16:47:36 +00:00
|
|
|
});
|
2018-10-22 16:15:45 +00:00
|
|
|
|
2018-10-24 13:09:11 +00:00
|
|
|
var atlasLoader = new runtime.AtlasAttachmentLoader(atlas);
|
2018-10-22 16:15:45 +00:00
|
|
|
|
2018-10-24 13:09:11 +00:00
|
|
|
var skeletonJson = new runtime.SkeletonJson(atlasLoader);
|
2018-10-22 16:15:45 +00:00
|
|
|
|
2018-10-23 16:47:36 +00:00
|
|
|
var skeletonData = skeletonJson.readSkeletonData(this.json.get(key));
|
2018-10-22 16:15:45 +00:00
|
|
|
|
2018-10-24 13:09:11 +00:00
|
|
|
var skeleton = new runtime.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)
|
|
|
|
{
|
2018-10-24 13:09:11 +00:00
|
|
|
var offset = new runtime.Vector2();
|
|
|
|
var size = new runtime.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
|
|
|
{
|
2018-10-24 13:09:11 +00:00
|
|
|
var stateData = new runtime.AnimationStateData(skeleton.data);
|
2018-10-22 16:15:45 +00:00
|
|
|
|
2018-10-24 13:09:11 +00:00
|
|
|
var state = new runtime.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-18 13:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-10-24 13:09:11 +00:00
|
|
|
module.exports = SpineCanvasPlugin;
|