Created
December 27, 2012 16:18
-
-
Save gauntface/4389505 to your computer and use it in GitHub Desktop.
focusController.js - Move focus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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