|
| 1 | +/** |
| 2 | + * @author Richard Davey <rich@photonstorm.com> |
| 3 | + * @copyright 2018 Photon Storm Ltd. |
| 4 | + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
| 5 | + */ |
| 6 | + |
| 7 | +// Contains the plugins that Phaser uses globally and locally. |
| 8 | +// These are the source objects, not instantiated. |
| 9 | +var inputPlugins = {}; |
| 10 | + |
| 11 | +/** |
| 12 | + * @typedef {object} InputPluginContainer |
| 13 | + * |
| 14 | + * @property {string} key - The unique name of this plugin in the input plugin cache. |
| 15 | + * @property {function} plugin - The plugin to be stored. Should be the source object, not instantiated. |
| 16 | + * @property {string} [mapping] - If this plugin is to be injected into the Input Plugin, this is the property key map used. |
| 17 | + */ |
| 18 | + |
| 19 | +var InputPluginCache = {}; |
| 20 | + |
| 21 | +/** |
| 22 | + * Static method called directly by the Core internal Plugins. |
| 23 | + * Key is a reference used to get the plugin from the plugins object (i.e. InputPlugin) |
| 24 | + * Plugin is the object to instantiate to create the plugin |
| 25 | + * Mapping is what the plugin is injected into the Scene.Systems as (i.e. input) |
| 26 | + * |
| 27 | + * @method Phaser.Input.InputPluginCache.register |
| 28 | + * @since 3.10.0 |
| 29 | + * |
| 30 | + * @param {string} key - A reference used to get this plugin from the plugin cache. |
| 31 | + * @param {function} plugin - The plugin to be stored. Should be the core object, not instantiated. |
| 32 | + * @param {string} mapping - If this plugin is to be injected into the Input Plugin, this is the property key used. |
| 33 | + */ |
| 34 | +InputPluginCache.register = function (key, plugin, mapping) |
| 35 | +{ |
| 36 | + inputPlugins[key] = { plugin: plugin, mapping: mapping }; |
| 37 | +}; |
| 38 | + |
| 39 | +/** |
| 40 | + * Returns the input plugin object from the cache based on the given key. |
| 41 | + * |
| 42 | + * @method Phaser.Input.InputPluginCache.getCore |
| 43 | + * @since 3.10.0 |
| 44 | + * |
| 45 | + * @param {string} key - The key of the input plugin to get. |
| 46 | + * |
| 47 | + * @return {InputPluginContainer} The input plugin object. |
| 48 | + */ |
| 49 | +InputPluginCache.getPlugin = function (key) |
| 50 | +{ |
| 51 | + return inputPlugins[key]; |
| 52 | +}; |
| 53 | + |
| 54 | +/** |
| 55 | + * Installs all of the registered Input Plugins into the given target. |
| 56 | + * |
| 57 | + * @method Phaser.Input.InputPluginCache.install |
| 58 | + * @since 3.10.0 |
| 59 | + * |
| 60 | + * @param {Phaser.Input.InputPlugin} target - The target InputPlugin to install the plugins into. |
| 61 | + */ |
| 62 | +InputPluginCache.install = function (target) |
| 63 | +{ |
| 64 | + for (var key in inputPlugins) |
| 65 | + { |
| 66 | + var source = inputPlugins[key].plugin; |
| 67 | + var mapping = inputPlugins[key].mapping; |
| 68 | + |
| 69 | + target[mapping] = new source(target); |
| 70 | + } |
| 71 | +}; |
| 72 | + |
| 73 | +/** |
| 74 | + * Removes an input plugin based on the given key. |
| 75 | + * |
| 76 | + * @method Phaser.Input.InputPluginCache.remove |
| 77 | + * @since 3.10.0 |
| 78 | + * |
| 79 | + * @param {string} key - The key of the input plugin to remove. |
| 80 | + */ |
| 81 | +InputPluginCache.remove = function (key) |
| 82 | +{ |
| 83 | + if (inputPlugins.hasOwnProperty(key)) |
| 84 | + { |
| 85 | + delete inputPlugins[key]; |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +module.exports = InputPluginCache; |
0 commit comments