Skip to content

Commit

Permalink
Rewrite touch input logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rexrainbow committed Nov 30, 2024
1 parent 25b4a01 commit 07c3333
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 296 deletions.
4 changes: 2 additions & 2 deletions examples/board-miniboard/drag.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class Demo extends Phaser.Scene {


var map = [
'0 ',
'000',
'000',
'000'
'0 0'
]
CreateMiniBoard(this, mainBoard, 500, 150, 0x6495B1, map)

Expand Down
2 changes: 2 additions & 0 deletions plugins/board/miniboard/Methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import AlignToMainBoard from './mainboard/AlignToMainBoard.js';
import SetInteractive from './input/SetInteractive.js';
import SetDraggable from './input/SetDraggable.js';
import DragEnd from './input/DragEnd.js';
import IsInTouching from './input/IsInTouching.js';

import CanMirror from './transform/CanMirror.js';
import Mirror from './transform/Mirror.js';
Expand Down Expand Up @@ -42,6 +43,7 @@ export default {
setInteractive: SetInteractive,
setDraggable: SetDraggable,
dragEnd: DragEnd,
isInTouching: IsInTouching,

setMainBoard: SetMainBoard,
canMirror: CanMirror,
Expand Down
17 changes: 4 additions & 13 deletions plugins/board/miniboard/input/DragEnd.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
var DragEnd = function (pointer) {
var dragData = this.miniboardInput.drag;
// Not dragging
if (dragData.state === 0) {
var DragEnd = function () {
if (!this.dragPointer) {
return;
}

if (pointer === undefined) {
pointer = this.miniboardInput.pointer;
}
var dragPosition = dragData.position;
var dragX = pointer.x - dragPosition.x;
var dragY = pointer.y - dragPosition.y;
dragData.state = 0;
this.emit('dragend', pointer, dragX, dragY);
this.scene.input.setDragState(this.dragPointer, 5);
return this;
}

export default DragEnd;
15 changes: 0 additions & 15 deletions plugins/board/miniboard/input/DragStart.js

This file was deleted.

11 changes: 11 additions & 0 deletions plugins/board/miniboard/input/HitAreaCallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const RectangleContains = Phaser.Geom.Rectangle.Contains;

var HitAreaCallback = function (shape, x, y, gameObject) {
if (!RectangleContains(shape, x, y)) {
return false;
}

return gameObject.isInTouching();
}

export default HitAreaCallback;
20 changes: 20 additions & 0 deletions plugins/board/miniboard/input/IsInTouching.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import PointerTest from '../../../utils/input/PointerTest.js';

var IsInTouching = function (pointer) {
if (!this.visible) {
return false;
}

return PointerTest(this, pointer, MainTest);
}

var MainTest = function (miniboard, x, y) {
miniboard.worldXYToChess(x, y, globChessArray);
var isHit = (globChessArray.length > 0);
globChessArray.length = 0;
return isHit;
}

var globChessArray = [];

export default IsInTouching;
44 changes: 0 additions & 44 deletions plugins/board/miniboard/input/OnPointerDown.js

This file was deleted.

76 changes: 0 additions & 76 deletions plugins/board/miniboard/input/OnPointerMove.js

This file was deleted.

49 changes: 0 additions & 49 deletions plugins/board/miniboard/input/OnPointerUp.js

This file was deleted.

13 changes: 13 additions & 0 deletions plugins/board/miniboard/input/RegisterDragEvents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var RegisterDragEvents = function () {
this.dragPointer = null;

this
.on('dragstart', function (pointer, dragX, dragY) {
this.dragPointer = pointer;
}, this)
.on('dragend', function (pointer, dragX, dragY, dropped) {
this.dragPointer = null;
}, this);
}

export default RegisterDragEvents;
29 changes: 29 additions & 0 deletions plugins/board/miniboard/input/RegisterPointerEvents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var RegisterPointerEvents = function () {
this
.on('pointerdown', function (pointer, localX, localY, event) {
FireTileEvent.call(this, pointer, 'gameobjectdown', 'miniboard.pointerdown');
}, this)
.on('pointerup', function (pointer, localX, localY, event) {
FireTileEvent.call(this, pointer, 'gameobjectup', 'miniboard.pointerup');
}, this)
.on('pointermove', function (pointer, localX, localY, event) {
FireTileEvent.call(this, pointer, 'gameobjectmove', 'miniboard.pointermove');
}, this)
}

var FireTileEvent = function (pointer, miniboardEvent, tileEvent) {
var gameObjects = this.worldXYToChess(pointer.worldX, pointer.worldY, globChessArray);
var gameObject;
for (var i = 0, cnt = gameObjects.length; i < cnt; i++) {
gameObject = gameObjects[i];
if (gameObject.emit) {
gameObject.emit(tileEvent, pointer);
}
this.emit(miniboardEvent, pointer, gameObject);
}
globChessArray.length = 0;
}

var globChessArray = [];

export default RegisterPointerEvents;
7 changes: 2 additions & 5 deletions plugins/board/miniboard/input/SetDraggable.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ var SetDraggable = function (enable) {
if (enable === undefined) {
enable = true;
}
this.setInteractive();
this.miniboardInput.drag.enable = enable;
if (!enable) {
this.miniboardInput.drag.state = 0;
}
this.setInteractive({ draggable: true });
this.input.draggable = enable;
return this;
}
export default SetDraggable;
Loading

0 comments on commit 07c3333

Please sign in to comment.