Added TouchController class

This commit is contained in:
BKcore
2013-01-23 22:21:48 +01:00
parent caa452b215
commit b7ea7a25a0
3 changed files with 273 additions and 0 deletions
+103
View File
@@ -0,0 +1,103 @@
###
TouchController (stick + buttons) for touch devices
Based on the touch demo by Seb Lee-Delisle <http://seb.ly/>
@class bkcore.TouchController
@author Thibaut 'BKcore' Despoulain <http://bkcore.com>
###
class TouchController
###
Creates a new TouchController
@param dom DOMElement The element that will listen to touch events
@param stickMargin int The left margin in px for stick detection
@param buttonCallback function Callback for non-stick touches
###
constructor: (@dom, @stickMargin, @buttonCallback) ->
@active = true
@touches = null
@stickID = -1
@stickPos = new Vec2(0, 0)
@stickStartPos = new Vec2(0, 0)
@stickVector = new Vec2(0, 0)
@dom.addEventListener('touchstart', ((e)=> @touchStart(e)), false)
@dom.addEventListener('touchmove', ((e)=> @touchMove(e)), false)
@dom.addEventListener('touchend', ((e)=> @touchEnd(e)), false)
###
@private
###
touchStart: (event) ->
for i in [0..event.changedTouches.length]
touch = event.changedTouches[i]
if @stickID < 0 and touch.clientX < @stickMargin
@stickID = touch.identifier
@stickStartPos.set(touch.clientX, touch.clientY)
@stickPos.copy(@stickStartPos)
@stickVector.set(0, 0)
continue
else
@buttonCallback?(touch, event)
@touches = event.touches
false
###
@private
###
touchMove: (event) ->
event.preventDefault()
for i in [0..event.changedTouches.length]
touch = event.changedTouches[i]
if @stickID is touch.identifier
@stickPos.set(touch.clientX, touch.clientY)
@stickVector.copy(@stickPos).substract(@stickStartPos)
break
@touches = event.touches
false
###
@private
###
touchEnd: (event) ->
@touches = event.touches
for i in [0..event.changedTouches.length]
touch = event.changedTouches[i]
if @stickID is touch.identifier
@stickID = -1
@stickVector.set(0, 0)
break
false
###
Internal class used for vector2
@class Vec2
@private
###
class Vec2
constructor: (@x = 0, @y = 0) ->
substract: (vec) ->
@x -= vec.x
@y -= vec.y
@
copy: (vec) ->
@x = vec.x
@y = vec.y
@
set: (x, y) ->
@x = x
@y = y
@
###
Exports
@package bkcore
###
exports = exports ? @
exports.bkcore ||= {}
exports.bkcore.TouchController = TouchController
+162
View File
@@ -0,0 +1,162 @@
// Generated by CoffeeScript 1.4.0
/*
TouchController (stick + buttons) for touch devices
Based on the touch demo by Seb Lee-Delisle <http://seb.ly/>
@class bkcore.TouchController
@author Thibaut 'BKcore' Despoulain <http://bkcore.com>
*/
(function() {
var TouchController, Vec2, exports;
TouchController = (function() {
/*
Creates a new TouchController
@param dom DOMElement The element that will listen to touch events
@param stickMargin int The left margin in px for stick detection
@param buttonCallback function Callback for non-stick touches
*/
function TouchController(dom, stickMargin, buttonCallback) {
var _this = this;
this.dom = dom;
this.stickMargin = stickMargin;
this.buttonCallback = buttonCallback;
this.active = true;
this.touches = null;
this.stickID = -1;
this.stickPos = new Vec2(0, 0);
this.stickStartPos = new Vec2(0, 0);
this.stickVector = new Vec2(0, 0);
this.dom.addEventListener('touchstart', (function(e) {
return _this.touchStart(e);
}), false);
this.dom.addEventListener('touchmove', (function(e) {
return _this.touchMove(e);
}), false);
this.dom.addEventListener('touchend', (function(e) {
return _this.touchEnd(e);
}), false);
}
/*
@private
*/
TouchController.prototype.touchStart = function(event) {
var i, touch, _i, _ref;
for (i = _i = 0, _ref = event.changedTouches.length; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
touch = event.changedTouches[i];
if (this.stickID < 0 && touch.clientX < this.stickMargin) {
this.stickID = touch.identifier;
this.stickStartPos.set(touch.clientX, touch.clientY);
this.stickPos.copy(this.stickStartPos);
this.stickVector.set(0, 0);
continue;
} else {
if (typeof this.buttonCallback === "function") {
this.buttonCallback(touch, event);
}
}
}
this.touches = event.touches;
return false;
};
/*
@private
*/
TouchController.prototype.touchMove = function(event) {
var i, touch, _i, _ref;
event.preventDefault();
for (i = _i = 0, _ref = event.changedTouches.length; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
touch = event.changedTouches[i];
if (this.stickID === touch.identifier) {
this.stickPos.set(touch.clientX, touch.clientY);
this.stickVector.copy(this.stickPos).substract(this.stickStartPos);
break;
}
}
this.touches = event.touches;
return false;
};
/*
@private
*/
TouchController.prototype.touchEnd = function(event) {
var i, touch, _i, _ref;
this.touches = event.touches;
for (i = _i = 0, _ref = event.changedTouches.length; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
touch = event.changedTouches[i];
if (this.stickID === touch.identifier) {
this.stickID = -1;
this.stickVector.set(0, 0);
break;
}
}
return false;
};
return TouchController;
})();
/*
Internal class used for vector2
@class Vec2
@private
*/
Vec2 = (function() {
function Vec2(x, y) {
this.x = x != null ? x : 0;
this.y = y != null ? y : 0;
}
Vec2.prototype.substract = function(vec) {
this.x -= vec.x;
this.y -= vec.y;
return this;
};
Vec2.prototype.copy = function(vec) {
this.x = vec.x;
this.y = vec.y;
return this;
};
Vec2.prototype.set = function(x, y) {
this.x = x;
this.y = y;
return this;
};
return Vec2;
})();
/*
Exports
@package bkcore
*/
exports = exports != null ? exports : this;
exports.bkcore || (exports.bkcore = {});
exports.bkcore.TouchController = TouchController;
}).call(this);
+8
View File
@@ -0,0 +1,8 @@
* {
-webkit-touch-callout: none; /* prevent callout to copy image, etc when tap to hold */
-webkit-text-size-adjust: none; /* prevent webkit from resizing text to fit */
/* make transparent link selection, adjust last value opacity 0 to 1.0 */
-webkit-tap-highlight-color: rgba(0,0,0,0);
-webkit-user-select: none; /* prevent copy paste, to allow, change 'none' to 'text' */
-webkit-tap-highlight-color: rgba(0,0,0,0);
}