0
0
mirror of https://github.com/Doodle3D/doodle3d-client.git synced 2024-11-22 09:17:56 +01:00
This commit is contained in:
peteruithoven 2014-01-31 14:34:44 +01:00
parent fcf966b2e3
commit 735cff2edd

View File

@ -8,14 +8,14 @@
// prototype inheritance // prototype inheritance
// http://robertnyman.com/2008/10/06/javascript-inheritance-how-and-why/ // http://robertnyman.com/2008/10/06/javascript-inheritance-how-and-why/
Button.prototype = new jQuery; Button.prototype = new jQuery();
function Button() { function Button() {
this.enabled = true; this.enabled = true;
var _clickEnabled = true; var _clickEnabled = true;
var _downTimerFPS = 20; var _downTimerFPS = 20;
var _timer = undefined; var _timer;
var _x,_y; var _x,_y;
var _isDown = false; var _isDown = false;
@ -27,46 +27,49 @@ function Button() {
// prevent multiple event handlers etc // prevent multiple event handlers etc
// make sure you do a more general conversion last // make sure you do a more general conversion last
if(this.data("isButton")) return; if(this.data("isButton")) {
else this.data("isButton",true); return;
} else {
this.data("isButton",true);
}
this.enable = function() { this.enable = function() {
if(_self.enabled) return; if(_self.enabled) { return; }
_self.removeClass("disabled"); _self.removeClass("disabled");
_self.enabled = true; _self.enabled = true;
} };
this.disable = function() { this.disable = function() {
if(!_self.enabled) return; if(!_self.enabled) { return; }
_self.addClass("disabled"); _self.addClass("disabled");
_self.enabled = false; _self.enabled = false;
} };
function updateCursor(e) { function updateCursor(e) {
// retrieve cursor position relative to element // retrieve cursor position relative to element
if (e.offsetX != undefined) { if (e.offsetX !== undefined) {
_x = e.offsetX; _x = e.offsetX;
_y = e.offsetY; _y = e.offsetY;
} else if(e.pageX != undefined) { } else {
// http://www.quirksmode.org/mobile/tableViewport_desktop.html#t11
var offset = _self.offset(); var offset = _self.offset();
_x = e.pageX - offset.left; if(e.pageX !== undefined) {
_y = e.pageY - offset.top; // http://www.quirksmode.org/mobile/tableViewport_desktop.html#t11
} else if(e.originalEvent != undefined && e.originalEvent.pageX != undefined) { _x = e.pageX - offset.left;
//http://css-tricks.com/the-javascript-behind-touch-friendly-sliders/ _y = e.pageY - offset.top;
var offset = _self.offset(); } else if(e.originalEvent !== undefined && e.originalEvent.pageX !== undefined) {
_x = e.originalEvent.pageX - offset.left; //http://css-tricks.com/the-javascript-behind-touch-friendly-sliders/
_y = e.originalEvent.pageY - offset.top; _x = e.originalEvent.pageX - offset.left;
} _y = e.originalEvent.pageY - offset.top;
}
//android+chrome-specific hack //android+chrome-specific hack
if (e.originalEvent.changedTouches != undefined) { if (e.originalEvent.changedTouches !== undefined) {
var offset = _self.offset(); _x = e.originalEvent.changedTouches[0].pageX - offset.left;
_x = e.originalEvent.changedTouches[0].pageX - offset.left; _y = e.originalEvent.changedTouches[0].pageY - offset.top;
_y = e.originalEvent.changedTouches[0].pageY - offset.top; }
} }
} }
function startDownTimer() { function startDownTimer() {
if (_timer==undefined) { if (_timer === undefined) {
_timer = setInterval(onDownTimerInterval, 1000/_downTimerFPS); _timer = setInterval(onDownTimerInterval, 1000/_downTimerFPS);
_isDown = true; _isDown = true;
} }
@ -79,8 +82,8 @@ function Button() {
// _y = undefined; // _y = undefined;
} }
function onDownTimerInterval() { function onDownTimerInterval() {
if(!_self.enabled) return; if(!_self.enabled) { return; }
if (_x!=undefined && _y!=undefined) { if (_x !== undefined && _y !== undefined) {
_self.trigger("onButtonHold",{x:_x,y:_y}); _self.trigger("onButtonHold",{x:_x,y:_y});
} else { } else {
console.log("Button: warning... _x or _y not set..."); console.log("Button: warning... _x or _y not set...");
@ -92,7 +95,7 @@ function Button() {
stopDownTimer(); stopDownTimer();
}); });
this.on("touchstart", function(e) { this.on("touchstart", function(e) {
if(!_self.enabled) return; if(!_self.enabled) { return; }
_clickEnabled = false; _clickEnabled = false;
updateCursor(e); updateCursor(e);
startDownTimer(); startDownTimer();
@ -104,12 +107,12 @@ function Button() {
stopDownTimer(); stopDownTimer();
}); });
this.on("touchmove", function(e) { this.on("touchmove", function(e) {
if(!_self.enabled) return; if(!_self.enabled) { return; }
updateCursor(e); updateCursor(e);
startDownTimer(); startDownTimer();
}); });
this.mousedown(function(e) { this.mousedown(function(e) {
if(!_self.enabled) return; if(!_self.enabled) { return; }
updateCursor(e); updateCursor(e);
startDownTimer(); startDownTimer();
}); });
@ -118,7 +121,7 @@ function Button() {
stopDownTimer(); stopDownTimer();
}); });
this.mousemove(function(e) { this.mousemove(function(e) {
if(!_self.enabled) return; if(!_self.enabled) { return; }
updateCursor(e); updateCursor(e);
//if (_isDown) mousedrag(e); //if (_isDown) mousedrag(e);
}); });
@ -129,7 +132,7 @@ function Button() {
e.preventDefault(); e.preventDefault();
}); });
this.click(function(e) { this.click(function(e) {
if(!_self.enabled || !_clickEnabled) return; if(!_self.enabled || !_clickEnabled) { return; }
updateCursor(e); updateCursor(e);
stopDownTimer(); stopDownTimer();
_self.trigger("onButtonClick",{x:_x,y:_y}); _self.trigger("onButtonClick",{x:_x,y:_y});