- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
import * as event from "./../system/event.js";
import Renderable from "./../renderable/renderable.js";
/**
* @import { Draggable } from "./draggable.js";
*/
/**
* @classdesc
* a base drop target object
* @see Draggable
* @augments Renderable
*/
export class DropTarget extends Renderable {
/**
* @param {number} x - the x coordinates of the drop target
* @param {number} y - the y coordinates of the drop target
* @param {number} width - drop target width
* @param {number} height - drop target height
*/
constructor(x, y, width, height) {
super(x, y, width, height);
this.isKinematic = false;
/**
* constant for the overlaps method
* @public
* @constant
* @type {string}
* @name CHECKMETHOD_OVERLAP
* @memberof DropTarget
*/
this.CHECKMETHOD_OVERLAP = "overlaps";
/**
* constant for the contains method
* @public
* @constant
* @type {string}
* @name CHECKMETHOD_CONTAINS
* @memberof DropTarget
*/
this.CHECKMETHOD_CONTAINS = "contains";
/**
* the checkmethod we want to use
* @public
* @constant
* @type {string}
* @name checkMethod
* @default "overlaps"
* @memberof DropTarget
*/
this.checkMethod = this.CHECKMETHOD_OVERLAP;
event.on(event.DRAGEND, this.checkOnMe, this);
}
/**
* Sets the collision method which is going to be used to check a valid drop
* @name setCheckMethod
* @memberof DropTarget
* @param {string} checkMethod - the checkmethod (defaults to CHECKMETHOD_OVERLAP)
*/
setCheckMethod(checkMethod) {
// We can improve this check,
// because now you can use every method in theory
if (typeof(this.getBounds()[this.checkMethod]) === "function") {
this.checkMethod = checkMethod;
}
}
/**
* Checks if a dropped entity is dropped on the current entity
* @name checkOnMe
* @memberof DropTarget
* @param {object} e - the triggering event
* @param {Draggable} draggable - the draggable object that is dropped
*/
checkOnMe(e, draggable) {
if (draggable && this.getBounds()[this.checkMethod](draggable.getBounds())) {
// call the drop method on the current entity
this.drop(draggable);
}
}
/**
* Gets called when a draggable entity is dropped on the current entity
* @name drop
* @memberof DropTarget
* @param {Draggable} draggable - the draggable object that is dropped
*/
drop(draggable) { // eslint-disable-line no-unused-vars
}
/**
* Destructor
* @name destroy
* @memberof DropTarget
* @ignore
*/
destroy() {
event.off(event.DRAGEND, this.checkOnMe);
super.destroy();
}
}