/
restricted_draggable.coffee
123 lines (112 loc) · 3.35 KB
/
restricted_draggable.coffee
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
111
112
113
114
115
116
117
118
119
120
121
122
123
#
# #Draggable
# @category Input
# Enable drag and drop of the entity.
# @trigger Dragging - is triggered each frame the entity is being dragged - MouseEvent
# @trigger StartDrag - is triggered when dragging begins - MouseEvent
# @trigger StopDrag - is triggered when dragging ends - MouseEvent
#
Crafty.c "RestrictedDraggable", {
_origMouseDOMPos: null
_oldX: null
_oldY: null
_dragging: false
_dir:null
#Note: the code is note tested with zoom, etc., that may distort the direction between the viewport and the coordinate on the canvas.
init: ->
this.requires "Mouse"
_ondrag: (e) ->
pos = Crafty.DOM.translate(e.clientX, e.clientY);
# ignore invalid 0 0 position - strange problem on ipad
if (pos.x == 0 || pos.y == 0)
return false;
next_x = this._oldX + (pos.x - this._origMouseDOMPos.x);
next_y = this._oldY + (pos.y - this._origMouseDOMPos.y);
vec = this.is_valid_drag_position(next_x, next_y)
if vec
this.set_pos vec
this.trigger("Dragging", e);
_ondown: (e) ->
if (e.mouseButton != Crafty.mouseButtons.LEFT)
return
this._startDrag(e);
_onup: (e) ->
if (this._dragging == true)
Crafty.removeEvent(this, Crafty.stage.elem, "mousemove", this._ondrag);
Crafty.removeEvent(this, Crafty.stage.elem, "mouseup", this._onup);
this._dragging = false;
this.trigger("StopDrag", e);
this.enableDrag();
#**@
# #._startDrag
# @comp Draggable
# Internal method for starting a drag of an entity either programatically or via Mouse click
#
# @param e - a mouse event
#/
_startDrag: (e) ->
this._origMouseDOMPos = Crafty.DOM.translate(e.clientX, e.clientY);
this._oldX = this.get_pos().x
this._oldY = this.get_pos().y
this._dragging = true;
Crafty.addEvent(this, Crafty.stage.elem, "mousemove", this._ondrag);
Crafty.addEvent(this, Crafty.stage.elem, "mouseup", this._onup);
this.trigger("StartDrag", e);
#**@
# #.stopDrag
# @comp Draggable
# @sign public this .stopDrag(void)
# @trigger StopDrag - Called right after the mouse listeners are removed
#
# Stop the entity from dragging. Essentially reproducing the drop.
#
# @see .startDrag
#/
stopDrag: ->
Crafty.removeEvent(this, Crafty.stage.elem, "mousemove", this._ondrag);
Crafty.removeEvent(this, Crafty.stage.elem, "mouseup", this._onup);
this._dragging = false;
this.trigger("StopDrag");
return this;
#**@
# #.startDrag
# @comp Draggable
# @sign public this .startDrag(void)
#
# Make the entity follow the mouse positions.
#
# @see .stopDrag
#/
startDrag: ->
if (!this._dragging)
#Use the last known position of the mouse
this._startDrag(Crafty.lastEvent);
return this
#**@
# #.enableDrag
# @comp Draggable
# @sign public this .enableDrag(void)
#
# Rebind the mouse events. Use if `.disableDrag` has been called.
#
# @see .disableDrag
#/
enableDrag: ->
this.bind("MouseDown", this._ondown);
Crafty.addEvent(this, Crafty.stage.elem, "mouseup", this._onup);
return this;
#**@
# #.disableDrag
# @comp Draggable
# @sign public this .disableDrag(void)
#
# Stops entity from being draggable. Reenable with `.enableDrag()`.
#
# @see .enableDrag
#/
disableDrag: ->
this.unbind("MouseDown", this._ondown)
if (this._dragging)
this.stopDrag()
return this
}