Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/bkcore.coffee/controllers/TouchController.js
4627 views
1
// Generated by CoffeeScript 1.7.1
2
3
/*
4
TouchController (stick + buttons) for touch devices
5
Based on the touch demo by Seb Lee-Delisle <http://seb.ly/>
6
7
@class bkcore.controllers.TouchController
8
@author Thibaut 'BKcore' Despoulain <http://bkcore.com>
9
*/
10
11
(function() {
12
var TouchController, Vec2, exports, _base;
13
14
TouchController = (function() {
15
TouchController.isCompatible = function() {
16
return 'ontouchstart' in document.documentElement;
17
};
18
19
20
/*
21
Creates a new TouchController
22
23
@param dom DOMElement The element that will listen to touch events
24
@param stickMargin int The left margin in px for stick detection
25
@param buttonCallback function Callback for non-stick touches
26
*/
27
28
function TouchController(dom, stickMargin, buttonCallback) {
29
this.dom = dom;
30
this.stickMargin = stickMargin != null ? stickMargin : 200;
31
this.buttonCallback = buttonCallback != null ? buttonCallback : null;
32
this.active = true;
33
this.touches = null;
34
this.stickID = -1;
35
this.stickPos = new Vec2(0, 0);
36
this.stickStartPos = new Vec2(0, 0);
37
this.stickVector = new Vec2(0, 0);
38
this.dom.addEventListener('touchstart', ((function(_this) {
39
return function(e) {
40
return _this.touchStart(e);
41
};
42
})(this)), false);
43
this.dom.addEventListener('touchmove', ((function(_this) {
44
return function(e) {
45
return _this.touchMove(e);
46
};
47
})(this)), false);
48
this.dom.addEventListener('touchend', ((function(_this) {
49
return function(e) {
50
return _this.touchEnd(e);
51
};
52
})(this)), false);
53
}
54
55
56
/*
57
@private
58
*/
59
60
TouchController.prototype.touchStart = function(event) {
61
var touch, _i, _len, _ref;
62
if (!this.active) {
63
return;
64
}
65
_ref = event.changedTouches;
66
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
67
touch = _ref[_i];
68
if (this.stickID < 0 && touch.clientX < this.stickMargin) {
69
this.stickID = touch.identifier;
70
this.stickStartPos.set(touch.clientX, touch.clientY);
71
this.stickPos.copy(this.stickStartPos);
72
this.stickVector.set(0, 0);
73
continue;
74
} else {
75
if (typeof this.buttonCallback === "function") {
76
this.buttonCallback(true, touch, event);
77
}
78
}
79
}
80
this.touches = event.touches;
81
return false;
82
};
83
84
85
/*
86
@private
87
*/
88
89
TouchController.prototype.touchMove = function(event) {
90
var touch, _i, _len, _ref;
91
event.preventDefault();
92
if (!this.active) {
93
return;
94
}
95
_ref = event.changedTouches;
96
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
97
touch = _ref[_i];
98
if (this.stickID === touch.identifier && touch.clientX < this.stickMargin) {
99
this.stickPos.set(touch.clientX, touch.clientY);
100
this.stickVector.copy(this.stickPos).substract(this.stickStartPos);
101
break;
102
}
103
}
104
this.touches = event.touches;
105
return false;
106
};
107
108
109
/*
110
@private
111
*/
112
113
TouchController.prototype.touchEnd = function(event) {
114
var touch, _i, _len, _ref;
115
if (!this.active) {
116
return;
117
}
118
this.touches = event.touches;
119
_ref = event.changedTouches;
120
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
121
touch = _ref[_i];
122
if (this.stickID === touch.identifier) {
123
this.stickID = -1;
124
this.stickVector.set(0, 0);
125
break;
126
} else {
127
if (typeof this.buttonCallback === "function") {
128
this.buttonCallback(false, touch, event);
129
}
130
}
131
}
132
return false;
133
};
134
135
return TouchController;
136
137
})();
138
139
140
/*
141
Internal class used for vector2
142
@class Vec2
143
@private
144
*/
145
146
Vec2 = (function() {
147
function Vec2(x, y) {
148
this.x = x != null ? x : 0;
149
this.y = y != null ? y : 0;
150
}
151
152
Vec2.prototype.substract = function(vec) {
153
this.x -= vec.x;
154
this.y -= vec.y;
155
return this;
156
};
157
158
Vec2.prototype.copy = function(vec) {
159
this.x = vec.x;
160
this.y = vec.y;
161
return this;
162
};
163
164
Vec2.prototype.set = function(x, y) {
165
this.x = x;
166
this.y = y;
167
return this;
168
};
169
170
return Vec2;
171
172
})();
173
174
175
/*
176
Exports
177
@package bkcore
178
*/
179
180
exports = exports != null ? exports : this;
181
182
exports.bkcore || (exports.bkcore = {});
183
184
(_base = exports.bkcore).controllers || (_base.controllers = {});
185
186
exports.bkcore.controllers.TouchController = TouchController;
187
188
}).call(this);
189
190