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');
|
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.drawDebug = false;
|
|
|
|
|
|
|
|
this.gl;
|
2019-07-23 16:33:08 +00:00
|
|
|
this.renderer;
|
|
|
|
this.sceneRenderer;
|
|
|
|
|
2019-07-16 16:34:48 +00:00
|
|
|
if (this.isWebGL)
|
|
|
|
{
|
|
|
|
this.runtime = Spine.webgl;
|
|
|
|
|
2019-07-23 16:33:08 +00:00
|
|
|
this.renderer = game.renderer;
|
2019-07-16 16:34:48 +00:00
|
|
|
this.gl = game.renderer.gl;
|
|
|
|
|
|
|
|
this.getAtlas = this.getAtlasWebGL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.runtime = Spine.canvas;
|
|
|
|
|
2019-07-23 16:33:08 +00:00
|
|
|
this.renderer = game.renderer;
|
|
|
|
|
2019-07-16 16:34:48 +00:00
|
|
|
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 ()
|
|
|
|
{
|
2019-07-23 16:33:08 +00:00
|
|
|
this.sceneRenderer = new Spine.webgl.SceneRenderer(this.renderer.canvas, this.gl, true);
|
2019-07-25 12:26:58 +00:00
|
|
|
|
|
|
|
// Monkeypatch the Spine setBlendMode functions, or batching is destroyed
|
|
|
|
|
|
|
|
var setBlendMode = function (srcBlend, dstBlend)
|
|
|
|
{
|
|
|
|
if (srcBlend !== this.srcBlend || dstBlend !== this.dstBlend)
|
|
|
|
{
|
|
|
|
var gl = this.context.gl;
|
|
|
|
|
|
|
|
this.srcBlend = srcBlend;
|
|
|
|
this.dstBlend = dstBlend;
|
|
|
|
|
|
|
|
if (this.isDrawing)
|
|
|
|
{
|
|
|
|
this.flush();
|
|
|
|
gl.blendFunc(this.srcBlend, this.dstBlend);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.sceneRenderer.batcher.setBlendMode = setBlendMode;
|
|
|
|
this.sceneRenderer.shapes.setBlendMode = setBlendMode;
|
2019-07-16 16:34:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getAtlasWebGL: function (key)
|
|
|
|
{
|
2019-07-25 12:17:17 +00:00
|
|
|
var atlasEntry = this.cache.get(key);
|
2019-07-16 16:34:48 +00:00
|
|
|
|
2019-07-25 12:17:17 +00:00
|
|
|
if (!atlasEntry)
|
2019-07-16 16:34:48 +00:00
|
|
|
{
|
|
|
|
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-25 12:17:17 +00:00
|
|
|
atlas = new Spine.TextureAtlas(atlasEntry.data, 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;
|
|
|
|
|
2019-07-23 16:33:08 +00:00
|
|
|
var gl = this.sceneRenderer.context.gl;
|
|
|
|
|
|
|
|
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
|
2019-07-16 16:34:48 +00:00
|
|
|
|
2019-07-25 12:17:17 +00:00
|
|
|
atlas = new Spine.TextureAtlas(atlasEntry.data, function (path)
|
2018-10-24 13:09:11 +00:00
|
|
|
{
|
2019-07-22 12:12:43 +00:00
|
|
|
var glTexture = new Spine.webgl.GLTexture(gl, textures.get(path).getSourceImage(), false);
|
2019-07-16 16:34:48 +00:00
|
|
|
|
|
|
|
spineTextures.add(key, glTexture);
|
|
|
|
|
|
|
|
return glTexture;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return atlas;
|
|
|
|
},
|
|
|
|
|
2019-07-25 12:17:17 +00:00
|
|
|
spineFileCallback: function (key, jsonURL, atlasURL, preMultipliedAlpha, jsonXhrSettings, atlasXhrSettings)
|
2019-07-16 16:34:48 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2019-07-25 12:17:17 +00:00
|
|
|
multifile = new SpineFile(this, key, jsonURL, atlasURL, preMultipliedAlpha, 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;
|
2019-07-22 16:28:11 +00:00
|
|
|
var split = (key.indexOf('.') !== -1);
|
2019-07-17 15:14:11 +00:00
|
|
|
|
2019-07-22 16:28:11 +00:00
|
|
|
if (split)
|
2019-07-17 15:14:11 +00:00
|
|
|
{
|
|
|
|
var parts = key.split('.');
|
|
|
|
|
|
|
|
atlasKey = parts.shift();
|
|
|
|
jsonKey = parts.join('.');
|
|
|
|
}
|
|
|
|
|
2019-07-25 12:17:17 +00:00
|
|
|
var atlasData = this.cache.get(atlasKey);
|
2019-07-17 15:14:11 +00:00
|
|
|
var atlas = this.getAtlas(atlasKey);
|
2019-07-16 16:34:48 +00:00
|
|
|
|
2019-07-25 12:17:17 +00:00
|
|
|
if (!atlas)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var preMultipliedAlpha = atlasData.preMultipliedAlpha;
|
|
|
|
|
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);
|
|
|
|
|
2019-07-22 16:28:11 +00:00
|
|
|
data = (split) ? GetValue(json, jsonKey) : json;
|
2019-07-17 15:14:11 +00:00
|
|
|
}
|
2019-07-16 16:34:48 +00:00
|
|
|
|
2019-07-22 16:28:11 +00:00
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
var skeletonData = skeletonJson.readSkeletonData(data);
|
2018-10-22 16:15:45 +00:00
|
|
|
|
2019-07-22 16:28:11 +00:00
|
|
|
var skeleton = new Spine.Skeleton(skeletonData);
|
|
|
|
|
2019-07-25 12:17:17 +00:00
|
|
|
return { skeletonData: skeletonData, skeleton: skeleton, preMultipliedAlpha: preMultipliedAlpha };
|
2019-07-22 16:28:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
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);
|
2019-07-25 12:17:17 +00:00
|
|
|
|
|
|
|
this.sceneRenderer.dispose();
|
2019-07-16 16:34:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
|
|
|
this.cache = null;
|
|
|
|
this.spineTextures = null;
|
|
|
|
this.json = null;
|
|
|
|
this.textures = null;
|
2019-07-25 12:17:17 +00:00
|
|
|
this.sceneRenderer = null;
|
2019-07-17 13:39:50 +00:00
|
|
|
this.gl = null;
|
2018-10-18 13:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2019-07-16 16:34:48 +00:00
|
|
|
module.exports = SpinePlugin;
|