diff --git a/examples/ui-scrollablepanel/drag-item.js b/examples/ui-scrollablepanel/drag-item.js index ec8917e8f0..7177c00218 100644 --- a/examples/ui-scrollablepanel/drag-item.js +++ b/examples/ui-scrollablepanel/drag-item.js @@ -62,31 +62,6 @@ class Demo extends Phaser.Scene { } }) .layout() - - scrollablePanel.setChildrenInteractive({ - targets: [ - scrollablePanel.getByName('table', true), - ] - }) - .on('child.pressstart', function (child) { - var item = child.getData('item'); - if (!item || !item.textureKey) { - return; - } - - var icon = child.getElement('icon'); - if (this.rexUI.isInTouching(icon)) { - // Create a new game object for dragging - var dragObject = this.add.image(icon.x, icon.y, ''); - dragObject.setTexture(item.textureKey).setTint(item.color); - // Force dragging - dragObject.drag = this.plugins.get('rexDrag').add(dragObject).drag() - - // icon has be removed, set it to invisible - item.textureKey = undefined; - child.setChildVisible(icon, false); - } - }, this) } update() { } @@ -107,12 +82,7 @@ var CreateGrid = function (scene, items, col) { config.expand = true; var item = items[(y * col) + x]; - var cellContainer = CreateCellContainer(scene).setData('item', item); - if (item) { - cellContainer.getElement('icon').setTexture(item.textureKey).setTint(item.color); - cellContainer.getElement('id').setText(item.id); - } - return cellContainer; + return CreateCellContainer(scene, item); }, name: 'table' @@ -120,18 +90,55 @@ var CreateGrid = function (scene, items, col) { } var CreateCellContainer = function (scene, item) { - return scene.rexUI.add.overlapSizer({ + if (!item) { + return; + } + + var background = scene.rexUI.add.roundRectangle(0, 0, 20, 20, 0).setStrokeStyle(2, COLOR_DARK); + var icon = scene.add.image(0, 0, item.textureKey).setTint(item.color); + var text = scene.add.text(0, 0, item.id).setText(item.id) + var container = scene.rexUI.add.overlapSizer({ height: 80 }) - .addBackground(scene.rexUI.add.roundRectangle(0, 0, 20, 20, 0).setStrokeStyle(2, COLOR_DARK)) + .addBackground(background) .add( - scene.add.image(0, 0, ''), + icon, { key: 'icon', align: 'center', expand: false } ) .add( - scene.add.text(0, 0, ''), + text, { key: 'id', align: 'left-top', expand: false } ) + + icon + .setInteractive({ draggable: true }) + .on('dragstart', function (pointer, dragX, dragY) { + var isDragging = CanDrag(icon, pointer.worldX, pointer.worldY); + icon.setData('isDragging', isDragging); + if (!isDragging) { + return; + } + + var parentSizer = scene.rexUI.getParentSizer(icon); + if (parentSizer) { + parentSizer.remove(icon); + parentSizer.removeChildrenMap('icon'); + } + }) + .on('drag', function (pointer, dragX, dragY) { + if (icon.getData('isDragging')) { + icon.setPosition(dragX, dragY); + } + }) + .on('dragend', function (pointer, dragX, dragY, dropped) { + icon.setData('isDragging', false); + }); + + return container +} + +var CanDrag = function (gameObject, x, y) { + return !gameObject.mask } var CreateItems = function (count) { diff --git a/plugins/utils/mask/defaultmaskgraphics/DefaultMaskGraphics.js b/plugins/utils/mask/defaultmaskgraphics/DefaultMaskGraphics.js index 4bc84e11e0..84cad64426 100644 --- a/plugins/utils/mask/defaultmaskgraphics/DefaultMaskGraphics.js +++ b/plugins/utils/mask/defaultmaskgraphics/DefaultMaskGraphics.js @@ -1,7 +1,5 @@ -import DrawShape from './DrawShape.js'; +import Methods from './methods/Methods.js'; import GetBoundsConfig from '../../bounds/GetBoundsConfig.js'; -import IsKeyValueEqual from '../../object/IsKeyValueEqual.js'; -import Clone from '../../object/Clone.js'; const Graphics = Phaser.GameObjects.Graphics; @@ -27,88 +25,16 @@ class DefaultMaskGraphics extends Graphics { super.destroy(); return this; } - - setPosition(x, y) { - var parent = this.parent; - if (x === undefined) { - x = parent.x; - } - if (y === undefined) { - y = parent.y; - } - super.setPosition(x, y); - return this; - } - - resize(width, height, padding) { - var parent = this.parent; - if (width === undefined) { - width = parent.width; - } - if (height === undefined) { - height = parent.height; - } - - if (padding === undefined) { - padding = this.padding; - } else if (typeof (padding) === 'number') { - padding = GetBoundsConfig(padding); - } - - var isSizeChanged = (this.width !== width) || (this.height !== height); - var isPaddingChanged = (this.padding !== padding) && !IsKeyValueEqual(this.padding, padding); - if (!isSizeChanged && !isPaddingChanged) { - return this; - } - - this.width = width; - this.height = height; - - if (isPaddingChanged) { - Clone(padding, this.padding); - } - - // Graphics does not have originX, originY properties - this.originX = parent.originX; - this.originY = parent.originY; - - DrawShape.call(this, - width, height, padding, - parent.originX, parent.originY - ); - - return this; - } - - setOrigin(originX, originY) { - if (originY === undefined) { - originY = originX; - } - - var parent = this.parent; - if (originX === undefined) { - originX = parent.originX; - } - if (originY === undefined) { - originY = parent.originY; - } - if ((this.originX === originX) && (this.originY === originY)) { - return this; - } - - this.originX = originX; - this.originY = originY; - - DrawShape.call(this, - this.width, this.height, this.padding, - originX, originY, - ); - return this; - } } const SHAPEMODE = { rectangle: 0, circle: 1, } + +Object.assign( + DefaultMaskGraphics.prototype, + Methods +) + export default DefaultMaskGraphics; diff --git a/plugins/utils/mask/defaultmaskgraphics/DrawShape.js b/plugins/utils/mask/defaultmaskgraphics/DrawShape.js deleted file mode 100644 index e38071bbee..0000000000 --- a/plugins/utils/mask/defaultmaskgraphics/DrawShape.js +++ /dev/null @@ -1,26 +0,0 @@ -var DrawShape = function (width, height, padding, originX, originY) { - this.clear().fillStyle(0xffffff); - switch (this.shapeType) { - case 1: // circle - // Assume that all padding are the same value in this circle shape - padding = padding.left; - var radius = Math.min(width, height) / 2; - this.fillCircle( - -width * (originX - 0.5), // centerX - -height * (originY - 0.5), // centerY - radius + padding // radius - ); - break; - - default: // 0|'rectangle' - this.fillRect( - -(width * originX) - padding.left, // x - -(height * originY) - padding.top, // y - width + padding.left + padding.right, // width - height + padding.top + padding.bottom // height - ); - break; - } -} - -export default DrawShape; \ No newline at end of file diff --git a/plugins/utils/mask/defaultmaskgraphics/methods/Contains.js b/plugins/utils/mask/defaultmaskgraphics/methods/Contains.js new file mode 100644 index 0000000000..c9f9b23883 --- /dev/null +++ b/plugins/utils/mask/defaultmaskgraphics/methods/Contains.js @@ -0,0 +1,7 @@ +var Contains = function (x, y) { + x -= this.x; + y -= this.y; + return this.geom.contains(x, y); +} + +export default Contains; \ No newline at end of file diff --git a/plugins/utils/mask/defaultmaskgraphics/methods/DrawShape.js b/plugins/utils/mask/defaultmaskgraphics/methods/DrawShape.js new file mode 100644 index 0000000000..2583f29029 --- /dev/null +++ b/plugins/utils/mask/defaultmaskgraphics/methods/DrawShape.js @@ -0,0 +1,19 @@ +import GetGeom from './GetGeom.js'; + +var DrawShape = function (width, height, padding, originX, originY) { + this.geom = GetGeom(this.shapeType, width, height, padding, originX, originY, this.geom); + + this.clear().fillStyle(0xffffff); + switch (this.shapeType) { + case 1: // circle + // Assume that all padding are the same value in this circle shape + this.fillCircleShape(this.geom); + break; + + default: // 0|'rectangle' + this.fillRectShape(this.geom); + break; + } +} + +export default DrawShape; \ No newline at end of file diff --git a/plugins/utils/mask/defaultmaskgraphics/methods/GetGeom.js b/plugins/utils/mask/defaultmaskgraphics/methods/GetGeom.js new file mode 100644 index 0000000000..be2f1ae343 --- /dev/null +++ b/plugins/utils/mask/defaultmaskgraphics/methods/GetGeom.js @@ -0,0 +1,35 @@ +const RectangleGeom = Phaser.Geom.Rectangle; +const CircleGemo = Phaser.Geom.Circle; + +var GetGeom = function (shapeType, width, height, padding, originX, originY, out) { + switch (shapeType) { + case 1: // circle + // Assume that all padding are the same value in this circle shape + padding = padding.left; + var centerX = -width * (originX - 0.5); + var centerY = -height * (originY - 0.5); + var radius = Math.min(width, height) / 2 + padding; + + if ((out === undefined) || !(out instanceof (CircleGemo))) { + out = new CircleGemo(); + } + out.setTo(centerX, centerY, radius); + break; + + default: // 0|'rectangle' + var topLeftX = -(width * originX) - padding.left; + var topLeftY = -(height * originY) - padding.top; + var rectWidth = width + padding.left + padding.right; + var rectHeight = height + padding.top + padding.bottom; + + if ((out === undefined) || !(out instanceof (RectangleGeom))) { + out = new RectangleGeom(); + } + out.setTo(topLeftX, topLeftY, rectWidth, rectHeight); + break; + } + + return out; +} + +export default GetGeom; \ No newline at end of file diff --git a/plugins/utils/mask/defaultmaskgraphics/methods/Methods.js b/plugins/utils/mask/defaultmaskgraphics/methods/Methods.js new file mode 100644 index 0000000000..d5fb03da71 --- /dev/null +++ b/plugins/utils/mask/defaultmaskgraphics/methods/Methods.js @@ -0,0 +1,13 @@ +import SetPosition from './SetPosition.js'; +import Resize from './Resize.js'; +import SetOrigin from './SetOrigin.js'; +import Contains from './Contains.js'; + +var Methods = { + setPosition: SetPosition, + resize: Resize, + setOrigin: SetOrigin, + contains: Contains, +} + +export default Methods; \ No newline at end of file diff --git a/plugins/utils/mask/defaultmaskgraphics/methods/Resize.js b/plugins/utils/mask/defaultmaskgraphics/methods/Resize.js new file mode 100644 index 0000000000..4a35255512 --- /dev/null +++ b/plugins/utils/mask/defaultmaskgraphics/methods/Resize.js @@ -0,0 +1,45 @@ +import DrawShape from './DrawShape.js'; +import IsKeyValueEqual from '../../../object/IsKeyValueEqual.js'; +import Clone from '../../../object/Clone.js'; + +var Resize = function (width, height, padding) { + var parent = this.parent; + if (width === undefined) { + width = parent.width; + } + if (height === undefined) { + height = parent.height; + } + + if (padding === undefined) { + padding = this.padding; + } else if (typeof (padding) === 'number') { + padding = GetBoundsConfig(padding); + } + + var isSizeChanged = (this.width !== width) || (this.height !== height); + var isPaddingChanged = (this.padding !== padding) && !IsKeyValueEqual(this.padding, padding); + if (!isSizeChanged && !isPaddingChanged) { + return this; + } + + this.width = width; + this.height = height; + + if (isPaddingChanged) { + Clone(padding, this.padding); + } + + // Graphics does not have originX, originY properties + this.originX = parent.originX; + this.originY = parent.originY; + + DrawShape.call(this, + width, height, padding, + parent.originX, parent.originY + ); + + return this; +} + +export default Resize; \ No newline at end of file diff --git a/plugins/utils/mask/defaultmaskgraphics/methods/SetOrigin.js b/plugins/utils/mask/defaultmaskgraphics/methods/SetOrigin.js new file mode 100644 index 0000000000..e73df60755 --- /dev/null +++ b/plugins/utils/mask/defaultmaskgraphics/methods/SetOrigin.js @@ -0,0 +1,29 @@ +import DrawShape from './DrawShape.js'; + +var SetOrigin = function (originX, originY) { + if (originY === undefined) { + originY = originX; + } + + var parent = this.parent; + if (originX === undefined) { + originX = parent.originX; + } + if (originY === undefined) { + originY = parent.originY; + } + if ((this.originX === originX) && (this.originY === originY)) { + return this; + } + + this.originX = originX; + this.originY = originY; + + DrawShape.call(this, + this.width, this.height, this.padding, + originX, originY, + ); + return this; +} + +export default SetOrigin; \ No newline at end of file diff --git a/plugins/utils/mask/defaultmaskgraphics/methods/SetPosition.js b/plugins/utils/mask/defaultmaskgraphics/methods/SetPosition.js new file mode 100644 index 0000000000..af2a654f1f --- /dev/null +++ b/plugins/utils/mask/defaultmaskgraphics/methods/SetPosition.js @@ -0,0 +1,16 @@ +const SetPositionBase = Phaser.GameObjects.Graphics.prototype.setPosition; + +var SetPosition = function (x, y) { + var parent = this.parent; + if (x === undefined) { + x = parent.x; + } + if (y === undefined) { + y = parent.y; + } + + SetPositionBase.call(this, x, y); + return this; +} + +export default SetPosition; \ No newline at end of file