function MaskAndAction() { this.mask = null; this.action = null; } function sameDirection(a, b) { if (b > 0 && a > 0) { return true; } if (b < 0 && a < 0) { return true; } if (b == 0 && a == 0) { return true; } return false; } function JoystickActionClass() { this.xyRepetitionAllowance = 0; this.zRepetitionAllowance = 0; this.xyRepetition = 0; this.zRepetition = 0; this.xyStepQuantum = 1; this.zStepQuantum = 1; this.previousStateXStep = 0; this.previousStateYStep = 0; this.previousStateZStep = 0; this.previousStateButtons = null; this.xyActionFilterByQuadrant = true; this.startXYAction = null; this.stopXYAction = null; this.zActionFilterByDirection = true; this.startZAction = null; this.stopZAction = null; this.buttonsMaskActions = new Array(); this.setXYAction = function(startXYAction,stopXYAction) { this.startXYAction = startXYAction; this.stopXYAction = stopXYAction; } this.setZAction = function(startZAction,stopZAction) { this.startZAction = startZAction; this.stopZAction = stopZAction; } this.setXYZRepetitionAllowance = function(xy,z) { this.xyRepetitionAllowance = xy; this.zRepetitionAllowance = z; } this.setXYZStepQuantum = function(xy,z) { this.xyStepQuantum = xy; this.zStepQuantum = z; } this.enableXYZDirectionalFilter = function(filterXYByQuadrant,filterZByDirection) { this.xyActionFilterByQuadrant = filterXYByQuadrant; this.zActionFilterByDirection = filterZByDirection; } this.addButtonAction = function(mask,action) { var buttonsMaskAction = new MaskAndAction(); buttonsMaskAction.mask = mask; buttonsMaskAction.action = action; this.buttonsMaskActions[this.buttonsMaskActions.length] = buttonsMaskAction; } this.removeButtonAction = function(mask) { var updatedButtonsMaskActions = new Array(); for(var i = 0; i < this.buttonsMaskActions.length; ++ i) { var buttonsMask = this.buttonsMaskActions[i].mask; var keepAction = (buttonsMask.length != mask.length); for(var j = 0; !keepAction && j < buttonsMask.length; ++ j) { if (buttonsMask[j] != mask[j]) { keepAction = true; } } if (keepAction) { updatedButtonsMaskActions[updatedButtonsMaskActions.length] = this.buttonsMaskActions[i]; } } this.buttonsMaskActions = updatedButtonsMaskActions; } this.testAndDoXYAction = function(x,y,z,buttons) { var xStep = (0 < x ? Math.floor(x / this.xyStepQuantum) : Math.ceil(x / this.xyStepQuantum)) * this.xyStepQuantum; var yStep = (0 < y ? Math.floor(y / this.xyStepQuantum) : Math.ceil(y / this.xyStepQuantum)) * this.xyStepQuantum; if (this.previousStateXStep == xStep && this.previousStateYStep == yStep) { ++ this.xyRepetition; } else { this.xyRepetition = 0; } if (this.xyRepetition <= this.xyRepetitionAllowance) { if (null != this.startXYAction && (0 != x || 0 != y) && (!this.xyActionFilterByQuadrant || (!sameDirection(this.previousStateXStep, xStep) || !sameDirection(this.previousStateYStep, yStep)))) { var startXYAction = this.startXYAction.replace("()", "(" + x + "," + y + ")"); //alert(startXYAction); eval(startXYAction); } if (null != this.stopXYAction && (0 == x && 0 == y)) { //alert(this.stopXYAction); eval(this.stopXYAction); } } this.previousStateXStep = xStep; this.previousStateYStep = yStep; } this.testAndDoZAction = function(x,y,z,buttons) { var zStep = (0 < z ? Math.floor(z / this.zStepQuantum) : Math.ceil(z / this.zStepQuantum)) * this.zStepQuantum; if (this.previousStateZStep == zStep) { ++ this.zRepetition; } else { this.zRepetition = 0; } if (this.zRepetition <= this.zRepetitionAllowance) { if (null != this.startZAction && 0 != z && (!this.zActionFilterByDirection || !sameDirection(this.previousStateZStep, zStep))) { var startZAction = this.startZAction.replace("()", "(" + z + ")"); //alert(startZAction); eval(startZAction); } if (null != this.stopZAction && 0 == z) { //alert(this.stopZAction); eval(this.stopZAction); } } this.previousStateZStep = zStep; } this.testAndDoButtonsAction = function(x,y,z,buttons) { for(var i = 0; i < this.buttonsMaskActions.length; ++ i) { if (null == this.previousStateButtons) { this.previousStateButtons = buttons; } var buttonsMask = this.buttonsMaskActions[i].mask; var matchedMask = true; var stateChanged = false; for(var j = 0; matchedMask && j < buttonsMask.length && j < buttons.length; ++ j) { if (-1 != buttonsMask[j]) { if (buttonsMask[j] != buttons[j]) { matchedMask = false; } if (this.previousStateButtons[j] != buttons[j]) { stateChanged = true; } } } if (stateChanged && matchedMask) { //alert(this.buttonsMaskActions[i].action); eval(this.buttonsMaskActions[i].action); } } this.previousStateButtons = buttons; } this.testAndDoAction = function(x,y,z,buttons) { var executibles = new Array("this.testAndDoXYAction(x,y,z,buttons);", "this.testAndDoZAction(x,y,z,buttons);", "this.testAndDoButtonsAction(x,y,z,buttons);"); for(var executed = 0; executed < executibles.length;) { var i = Math.floor(Math.random() * executibles.length); if (i < executibles.length) { if (null != executibles[i]) { eval(executibles[i]); executibles[i] = null; ++executed; } } } } }