Skip to content

Instantly share code, notes, and snippets.

@gauntface
Created December 27, 2012 16:18
Show Gist options
  • Save gauntface/4389505 to your computer and use it in GitHub Desktop.
Save gauntface/4389505 to your computer and use it in GitHub Desktop.
focusController.js - Move focus
/**
* This will take a direction vector and move the focus to the most
* appropriate item or make no change if there are no items to move to
* @param {array} direction A direction vector [x, y]
*/
this.moveFocus = function (direction) {
// We need an item to move down from
// TODO: Should initialise focus if not initialised
if(!currentlyFocusedItem) {
return;
}
var minItemDistance;
var newItem;
var minItem;
var minItemIndex;
for(var i = 0; i < focusableItems.length; i++) {
newItem = focusableItems[i];
if(newItem == currentlyFocusedItem) {
continue;
}
var itemDistance = calculateElementDistance(currentlyFocusedItem, newItem, direction);
if(itemDistance >= 0 && (minItemDistance === undefined ||
itemDistance < minItemDistance)) {
minItemDistance = itemDistance;
minItem = newItem;
minItemIndex = i;
}
}
if(minItemIndex >= 0) {
this.handleFocusChangeToItem(minItemIndex);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment