Path: blob/main/projects/HexGL/bkcore.coffee/controllers/TouchController.js
4627 views
// Generated by CoffeeScript 1.7.112/*3TouchController (stick + buttons) for touch devices4Based on the touch demo by Seb Lee-Delisle <http://seb.ly/>56@class bkcore.controllers.TouchController7@author Thibaut 'BKcore' Despoulain <http://bkcore.com>8*/910(function() {11var TouchController, Vec2, exports, _base;1213TouchController = (function() {14TouchController.isCompatible = function() {15return 'ontouchstart' in document.documentElement;16};171819/*20Creates a new TouchController2122@param dom DOMElement The element that will listen to touch events23@param stickMargin int The left margin in px for stick detection24@param buttonCallback function Callback for non-stick touches25*/2627function TouchController(dom, stickMargin, buttonCallback) {28this.dom = dom;29this.stickMargin = stickMargin != null ? stickMargin : 200;30this.buttonCallback = buttonCallback != null ? buttonCallback : null;31this.active = true;32this.touches = null;33this.stickID = -1;34this.stickPos = new Vec2(0, 0);35this.stickStartPos = new Vec2(0, 0);36this.stickVector = new Vec2(0, 0);37this.dom.addEventListener('touchstart', ((function(_this) {38return function(e) {39return _this.touchStart(e);40};41})(this)), false);42this.dom.addEventListener('touchmove', ((function(_this) {43return function(e) {44return _this.touchMove(e);45};46})(this)), false);47this.dom.addEventListener('touchend', ((function(_this) {48return function(e) {49return _this.touchEnd(e);50};51})(this)), false);52}535455/*56@private57*/5859TouchController.prototype.touchStart = function(event) {60var touch, _i, _len, _ref;61if (!this.active) {62return;63}64_ref = event.changedTouches;65for (_i = 0, _len = _ref.length; _i < _len; _i++) {66touch = _ref[_i];67if (this.stickID < 0 && touch.clientX < this.stickMargin) {68this.stickID = touch.identifier;69this.stickStartPos.set(touch.clientX, touch.clientY);70this.stickPos.copy(this.stickStartPos);71this.stickVector.set(0, 0);72continue;73} else {74if (typeof this.buttonCallback === "function") {75this.buttonCallback(true, touch, event);76}77}78}79this.touches = event.touches;80return false;81};828384/*85@private86*/8788TouchController.prototype.touchMove = function(event) {89var touch, _i, _len, _ref;90event.preventDefault();91if (!this.active) {92return;93}94_ref = event.changedTouches;95for (_i = 0, _len = _ref.length; _i < _len; _i++) {96touch = _ref[_i];97if (this.stickID === touch.identifier && touch.clientX < this.stickMargin) {98this.stickPos.set(touch.clientX, touch.clientY);99this.stickVector.copy(this.stickPos).substract(this.stickStartPos);100break;101}102}103this.touches = event.touches;104return false;105};106107108/*109@private110*/111112TouchController.prototype.touchEnd = function(event) {113var touch, _i, _len, _ref;114if (!this.active) {115return;116}117this.touches = event.touches;118_ref = event.changedTouches;119for (_i = 0, _len = _ref.length; _i < _len; _i++) {120touch = _ref[_i];121if (this.stickID === touch.identifier) {122this.stickID = -1;123this.stickVector.set(0, 0);124break;125} else {126if (typeof this.buttonCallback === "function") {127this.buttonCallback(false, touch, event);128}129}130}131return false;132};133134return TouchController;135136})();137138139/*140Internal class used for vector2141@class Vec2142@private143*/144145Vec2 = (function() {146function Vec2(x, y) {147this.x = x != null ? x : 0;148this.y = y != null ? y : 0;149}150151Vec2.prototype.substract = function(vec) {152this.x -= vec.x;153this.y -= vec.y;154return this;155};156157Vec2.prototype.copy = function(vec) {158this.x = vec.x;159this.y = vec.y;160return this;161};162163Vec2.prototype.set = function(x, y) {164this.x = x;165this.y = y;166return this;167};168169return Vec2;170171})();172173174/*175Exports176@package bkcore177*/178179exports = exports != null ? exports : this;180181exports.bkcore || (exports.bkcore = {});182183(_base = exports.bkcore).controllers || (_base.controllers = {});184185exports.bkcore.controllers.TouchController = TouchController;186187}).call(this);188189190