phaser/plugins/spine/src/SpinePlugin.js

112 lines
2.8 KiB
JavaScript
Raw Normal View History

/**
* @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');
var BaseSpinePlugin = require('./BaseSpinePlugin');
2018-10-22 16:15:45 +00:00
var SpineCanvas = require('SpineCanvas');
var SpineWebGL = require('SpineWebGL');
var runtime;
/**
* @classdesc
* TODO
*
* @class SpinePlugin
2018-10-23 16:47:36 +00:00
* @extends Phaser.Plugins.ScenePlugin
* @constructor
2018-10-23 16:47:36 +00:00
* @since 3.16.0
*
2018-10-23 16:47:36 +00:00
* @param {Phaser.Scene} scene - A reference to the Scene that has installed this plugin.
* @param {Phaser.Plugins.PluginManager} pluginManager - A reference to the Phaser Plugin Manager.
*/
var SpineCanvasPlugin = new Class({
Extends: BaseSpinePlugin,
initialize:
function SpineCanvasPlugin (scene, pluginManager)
{
console.log('SpinePlugin created');
BaseSpinePlugin.call(this, scene, pluginManager);
2018-10-23 16:47:36 +00:00
var game = pluginManager.game;
runtime = (game.config.renderType === 1) ? SpineCanvas : SpineWebGL;
},
boot: function ()
{
this.skeletonRenderer = (this.game.config.renderType === 1) ? SpineCanvas.canvas.SkeletonRenderer(this.game.context) : SpineWebGL;
},
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;
var useWebGL = this.game.config.renderType;
var atlas = new runtime.TextureAtlas(atlasData, function (path)
2018-10-23 16:47:36 +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
var atlasLoader = new runtime.AtlasAttachmentLoader(atlas);
2018-10-22 16:15:45 +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
var skeleton = new runtime.Skeleton(skeletonData);
2018-10-22 16:15:45 +00:00
return { skeletonData: skeletonData, skeleton: skeleton };
2018-10-22 16:15:45 +00:00
},
getBounds: function (skeleton)
{
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 };
},
createAnimationState: function (skeleton)
2018-10-22 16:15:45 +00:00
{
var stateData = new runtime.AnimationStateData(skeleton.data);
2018-10-22 16:15:45 +00:00
var state = new runtime.AnimationState(stateData);
2018-10-22 16:15:45 +00:00
return { stateData: stateData, state: state };
}
});
module.exports = SpineCanvasPlugin;