Skip to content

Commit

Permalink
DualController
Browse files Browse the repository at this point in the history
  • Loading branch information
brandon-lockaby committed Sep 20, 2011
1 parent 9f773b7 commit 97117f4
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
70 changes: 70 additions & 0 deletions DualController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright 2011 Brandon Lockaby
This file is part of JSPDP.
https://github.com/brandon-lockaby/JSPDP
JSPDP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
JSPDP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with JSPDP. If not, see <http:https://www.gnu.org/licenses/>.
*/

JSPDP.DualController = function() {
}

var proto = (JSPDP.DualController.prototype = new JSPDP.TableauUI());

proto.init = function(settings) {
JSPDP.TableauUI.prototype.init.call(this, settings);

this.keyboardController = new JSPDP.KeyboardController().init(settings);
this.touchController = new JSPDP.TouchController().init(settings);

this.touchController.onSelect.subscribe(this.handleSelect.bind(this));
this.tableau.onSwap.subscribe(this.handleSwap.bind(this));

return this;
};

proto.handleSelect = function(selection) {
var row = selection.tableau_pos.row;
var col = selection.tableau_pos.col;

if(col > 0 && col > this.keyboardController.position.col) {
--col;
}

if(!this.tableau.bounds(row, col + 1)) --col;
if(!this.tableau.bounds(row, col)) return;

this.keyboardController.moveTo(row, col);
};

proto.handleSwap = function(panels) {
var row = panels[0].row;
var col = panels[0].col;
if(panels[1].col < col) col = panels[1].col;
this.keyboardController.moveTo(row, col);
};

proto.refresh = function() {
this.keyboardController.refresh();
};

proto.update = function() {
return this.keyboardController.update();
};

proto.draw = function(ctx) {
this.keyboardController.draw(ctx);
};
8 changes: 8 additions & 0 deletions KeyboardController.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ proto.init = function(settings) {
return this;
};

proto.moveTo = function(row, col) {
if(!this.tableau.bounds(row, col)) return false;
this.position.row = row;
this.position.col = col;
this.moved = true;
return true;
};

proto.onKeydown = function(event) {
for(var i = 0; i < this.buttons.length; i++) {
if(event.keyCode == this.buttons[i].key) {
Expand Down
3 changes: 3 additions & 0 deletions TouchController.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ proto.lastSwapTick = -9001;
proto.init = function(settings) {
JSPDP.TableauUI.prototype.init.call(this, settings);

this.onSelect = new JSPDP.Event();

this.tableau.onActionPhase.subscribe(this.onActionPhase.bind(this));
if(this.tableau instanceof JSPDP.RisingTableau) {
this.tableau.onRow.subscribe(this.handleRow.bind(this));
Expand All @@ -53,6 +55,7 @@ proto.onMousedown = function(event) {
tableau_pos: tableau_pos
};
this.tableau_pos = tableau_pos;
this.onSelect.fire(this.selection);
}
else {
this.selection = null;
Expand Down

0 comments on commit 97117f4

Please sign in to comment.