Facilitate easier use of RenderFilters child as primary interface.

Add `autoFocus` and `autoTransfer` parameters to creation.
Add `filters` and `filtersWrapper` properties to GameObject.
This commit is contained in:
Ben Richards 2024-11-21 16:28:22 +13:00
parent 7411a4b244
commit db76e501f1
5 changed files with 62 additions and 11 deletions

View file

@ -99,6 +99,43 @@ var GameObject = new Class({
*/
this.parentContainer = null;
/**
* If this GameObject was added to a
* {@link Phaser.GameObjects.RenderFilters} object,
* causing it to be removed from the display list,
* this will contain a reference to the `RenderFilters.filters` object.
*
* This provides access to the `internal` and `external`
* filter lists that are used to manage filters.
* See the `filtersWrapper` property for the `RenderFilters` GameObject.
*
* @example
* scene.add.renderFilters(myGameObject);
* myGameObject.filters.internal.addBlur();
*
* @name Phaser.GameObjects.GameObject#filters
* @type {{ internal: Phaser.GameObjects.Components.FilterList, external: Phaser.GameObjects.Components.FilterList }}
* @default null
* @since 4.0.0
*/
this.filters = null;
/**
* If this GameObject was added to a
* {@link Phaser.GameObjects.RenderFilters} object,
* causing it to be removed from the display list,
* this will contain a reference to the `RenderFilters` GameObject.
*
* This is the GameObject itself.
* See the `filters` property for the `FilterList` objects.
*
* @name Phaser.GameObjects.GameObject#filtersWrapper
* @type {Phaser.GameObjects.RenderFilters}
* @default null
* @since 4.0.0
*/
this.filtersWrapper = null;
/**
* The name of this Game Object.
* Empty by default and never populated by Phaser, this is left for developers to use.

View file

@ -127,6 +127,8 @@ var RenderFiltersRender = require('./RenderFiltersRender');
*
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
* @param {Phaser.GameObjects.GameObject} child - The Game Object that this RenderFilters will wrap.
* @param {boolean} [autoFocus=false] - Whether the RenderFilters should automatically focus on the child every frame. Sets `autoFocus` property.
* @param {boolean} [autoTransfer=false] - Whether the RenderFilters should automatically transfer properties from the child to itself every frame. Sets `autoTransferProperties` property. If not set, it defaults to the `autoFocus` param.
*/
var RenderFilters = new Class({
@ -147,8 +149,11 @@ var RenderFilters = new Class({
RenderFiltersRender
],
initialize: function RenderFilters (scene, child)
initialize: function RenderFilters (scene, child, autoFocus, autoTransfer)
{
if (autoFocus === undefined) { autoFocus = false; }
if (autoTransfer === undefined) { autoTransfer = autoFocus; }
GameObject.call(this, scene, 'RenderFilters');
/**
@ -250,7 +255,7 @@ var RenderFilters = new Class({
* @default false
* @since 4.0.0
*/
this.autoFocus = false;
this.autoFocus = autoFocus;
/**
* Whether the RenderFilters should focus on its own context,
@ -291,7 +296,7 @@ var RenderFilters = new Class({
* @default false
* @since 4.0.0
*/
this.autoTransferProperties = false;
this.autoTransferProperties = autoTransfer;
this.setChild(child, true, false);
this.initRenderNodes(this._defaultRenderNodesMap);
@ -492,9 +497,11 @@ var RenderFilters = new Class({
this.displayList.moveAbove(this, child);
}
// Remove the new child from its current display list.
child.removeFromDisplayList();
child.filters = this.filters;
child.filtersWrapper = this;
this.child = child;
if (match)
@ -532,6 +539,9 @@ var RenderFilters = new Class({
this.child = null;
child.addToDisplayList();
child.filters = null;
child.filtersWrapper = null;
}
return child;
@ -846,12 +856,12 @@ var RenderFilters = new Class({
* Static method to replace a game object with a RenderFilters
* which wraps that object. This preserves the display order.
*
* @method Phaser.GameObjects.RenderFilters.replace
* @method Phaser.GameObjects.RenderFilters.Replace
* @since 4.0.0
* @static
* @param {Phaser.GameObjects.GameObject} child - The Game Object that is being wrapped by this RenderFilters instance.
*/
RenderFilters.replace = function (child)
RenderFilters.Replace = function (child)
{
var scene = child.scene;
if (!scene)

View file

@ -27,8 +27,10 @@ GameObjectCreator.register('renderFilters', function (config, addToScene)
if (config === undefined) { config = {}; }
var child = GetAdvancedValue(config, 'child', null);
var autoFocus = GetAdvancedValue(config, 'autoFocus', false);
var autoTransfer = GetAdvancedValue(config, 'autoTransfer', autoFocus);
var renderFilters = new RenderFilters(this.scene, child, 0, 0);
var renderFilters = new RenderFilters(this.scene, child, autoFocus, autoTransfer);
if (addToScene !== undefined)
{

View file

@ -16,14 +16,14 @@ var GameObjectFactory = require('../GameObjectFactory');
* @since 4.0.0
*
* @param {Phaser.GameObjects.GameObject} child - The Game Object that is being wrapped by this RenderFilters instance.
* @param {number} x - The horizontal position of this Game Object in the world.
* @param {number} y - The vertical position of this Game Object in the world.
* @param {boolean} [autoFocus=false] - Whether the RenderFilters should automatically focus on the child every frame. Sets `autoFocus` property.
* @param {boolean} [autoTransfer=false] - Whether the RenderFilters should automatically transfer properties from the child to itself every frame. Sets `autoTransferProperties` property. If not set, it defaults to the `autoFocus` param.
*
* @return {Phaser.GameObjects.RenderFilters} The Game Object that was created.
*/
GameObjectFactory.register('renderFilters', function (child, x, y)
GameObjectFactory.register('renderFilters', function (child, autoFocus, autoTransfer)
{
return this.displayList.add(new RenderFilters(this.scene, child, x, y));
return this.displayList.add(new RenderFilters(this.scene, child, autoFocus, autoTransfer));
});
// When registering a factory function 'this' refers to the GameObjectFactory context.

View file

@ -4,4 +4,6 @@
* @since 4.0.0
*
* @property {Phaser.GameObjects.GameObject} [child] - The child to wrap this RenderFilters instance around.
* @property {boolean} [autoFocus=false] - Whether the RenderFilters should automatically focus on the child every frame. Sets `autoFocus` property.
* @property {boolean} [autoTransfer=false] - Whether the RenderFilters should automatically transfer properties from the child to itself every frame. Sets `autoTransferProperties` property. If not set, it defaults to the `autoFocus` param.
*/