From c1469a1f9c4ddbd4cf4263bd3aa9d8319a5195ca Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Wed, 1 Jul 2015 20:18:34 -0600 Subject: [PATCH] * isSpecialKeyMapped() renamed to isKeyMapped() * Functional HorizontalMenuView * Minor updates --- core/edit_text_view.js | 4 +-- core/horizontal_menu_view.js | 44 ++++++++++++++++++++++++ core/mask_edit_text_view.js | 4 +-- core/multi_line_edit_text_view.js | 8 ++--- core/multi_line_edit_text_view2.js | 2 +- core/spinner_menu_view.js | 6 ++-- core/toggle_menu_view.js | 8 ++--- core/vertical_menu_view.js | 5 +-- core/view.js | 6 ++-- mods/art/demo_horizontal_menu_view1.ans | Bin 346 -> 423 bytes mods/menu.json | 17 +++++++-- 11 files changed, 81 insertions(+), 23 deletions(-) diff --git a/core/edit_text_view.js b/core/edit_text_view.js index 9a424712..efe2c812 100644 --- a/core/edit_text_view.js +++ b/core/edit_text_view.js @@ -32,7 +32,7 @@ require('util').inherits(EditTextView, TextView); EditTextView.prototype.onKeyPress = function(ch, key) { if(key) { - if(this.isSpecialKeyMapped('backspace', key.name)) { + if(this.isKeyMapped('backspace', key.name)) { if(this.text.length > 0) { this.text = this.text.substr(0, this.text.length - 1); @@ -47,7 +47,7 @@ EditTextView.prototype.onKeyPress = function(ch, key) { } return; - } else if(this.isSpecialKeyMapped('clearLine', key.name)) { + } else if(this.isKeyMapped('clearLine', key.name)) { this.text = ''; this.cursorPos.col = 0; this.setFocus(true); // resetting focus will redraw & adjust cursor diff --git a/core/horizontal_menu_view.js b/core/horizontal_menu_view.js index d80f918c..9faab4d4 100644 --- a/core/horizontal_menu_view.js +++ b/core/horizontal_menu_view.js @@ -10,6 +10,8 @@ var _ = require('lodash'); exports.HorizontalMenuView = HorizontalMenuView; +// :TODO: Update this to allow scrolling if number of items cannot fit in width (similar to VerticalMenuView) + function HorizontalMenuView(options) { options.cursor = options.cursor || 'hide'; @@ -107,4 +109,46 @@ HorizontalMenuView.prototype.setItems = function(items) { HorizontalMenuView.super_.prototype.setItems.call(this, items); this.positionCacheExpired = true; +}; + +HorizontalMenuView.prototype.onKeyPress = function(ch, key) { + if(key) { + var prevFocusedItemIndex = this.focusedItemIndex; + + if(this.isKeyMapped('left', key.name)) { + if(0 === this.focusedItemIndex) { + this.focusedItemIndex = this.items.length - 1; + } else { + this.focusedItemIndex--; + } + + } else if(this.isKeyMapped('right', key.name)) { + if(this.items.length - 1 === this.focusedItemIndex) { + this.focusedItemIndex = 0; + } else { + this.focusedItemIndex++; + } + } + + if(prevFocusedItemIndex !== this.focusedItemIndex) { + // :TODO: Optimize this in cases where we only need to redraw two items. Always the case now, somtimes + // if this is changed to allow scrolling + this.redraw(); + return; + } + } + + if(ch && this.hotKeys) { + var keyIndex = this.hotKeys[this.caseInsensitiveHotKeys ? ch.toLowerCase() : ch]; + if(_.isNumber(keyIndex)) { + this.focusedItemIndex = keyIndex; + this.redraw(); + } + } + + HorizontalMenuView.super_.prototype.onKeyPress.call(this, ch, key); +}; + +HorizontalMenuView.prototype.getData = function() { + return this.focusedItemIndex; }; \ No newline at end of file diff --git a/core/mask_edit_text_view.js b/core/mask_edit_text_view.js index b5cbb670..c6dd8bcd 100644 --- a/core/mask_edit_text_view.js +++ b/core/mask_edit_text_view.js @@ -106,7 +106,7 @@ MaskEditTextView.prototype.setMaskPattern = function(pattern) { MaskEditTextView.prototype.onKeyPress = function(ch, key) { if(key) { - if(this.isSpecialKeyMapped('backspace', key.name)) { + if(this.isKeyMapped('backspace', key.name)) { if(this.text.length > 0) { this.patternArrayPos--; assert(this.patternArrayPos >= 0); @@ -128,7 +128,7 @@ MaskEditTextView.prototype.onKeyPress = function(ch, key) { } return; - } else if(this.isSpecialKeyMapped('clearLine', key.name)) { + } else if(this.isKeyMapped('clearLine', key.name)) { this.text = ''; this.patternArrayPos = 0; this.setFocus(true); // redraw + adjust cursor diff --git a/core/multi_line_edit_text_view.js b/core/multi_line_edit_text_view.js index 4466c0e4..cbb0d9af 100644 --- a/core/multi_line_edit_text_view.js +++ b/core/multi_line_edit_text_view.js @@ -499,13 +499,13 @@ MultiLineEditTextView.prototype.onKeyPress = function(key, isSpecial) { }; MultiLineEditTextView.prototype.onSpecialKeyPress = function(keyName) { - if(this.isSpecialKeyMapped('up', keyName)) { + if(this.isKeyMapped('up', keyName)) { this.cursorUp(); - } else if(this.isSpecialKeyMapped('down', keyName)) { + } else if(this.isKeyMapped('down', keyName)) { this.cursorDown(); - } else if(this.isSpecialKeyMapped('left', keyName)) { + } else if(this.isKeyMapped('left', keyName)) { this.cursorLeft(); - } else if(this.isSpecialKeyMapped('right', keyName)) { + } else if(this.isKeyMapped('right', keyName)) { this.cursorRight(); } diff --git a/core/multi_line_edit_text_view2.js b/core/multi_line_edit_text_view2.js index 92c4b37b..557b9de5 100644 --- a/core/multi_line_edit_text_view2.js +++ b/core/multi_line_edit_text_view2.js @@ -1036,7 +1036,7 @@ MultiLineEditTextView2.prototype.onKeyPress = function(ch, key) { if(key) { HANDLED_SPECIAL_KEYS.forEach(function aKey(specialKey) { - if(self.isSpecialKeyMapped(specialKey, key.name)) { + if(self.isKeyMapped(specialKey, key.name)) { self[_.camelCase('keyPress ' + specialKey)](); handled = true; } diff --git a/core/spinner_menu_view.js b/core/spinner_menu_view.js index adc5fd94..8050015b 100644 --- a/core/spinner_menu_view.js +++ b/core/spinner_menu_view.js @@ -26,7 +26,7 @@ function SpinnerMenuView(options) { */ this.updateSelection = function() { - assert(!self.positionCacheExpired); + //assert(!self.positionCacheExpired); assert(this.focusedItemIndex >= 0 && this.focusedItemIndex <= self.items.length); @@ -66,7 +66,7 @@ SpinnerMenuView.prototype.setFocus = function(focused) { SpinnerMenuView.prototype.onKeyPress = function(ch, key) { if(key) { - if(this.isSpecialKeyMapped('up', key.name)) { + if(this.isKeyMapped('up', key.name)) { if(0 === this.focusedItemIndex) { this.focusedItemIndex = this.items.length - 1; } else { @@ -75,7 +75,7 @@ SpinnerMenuView.prototype.onKeyPress = function(ch, key) { this.updateSelection(); return; - } else if(this.isSpecialKeyMapped('down', key.name)) { + } else if(this.isKeyMapped('down', key.name)) { if(this.items.length - 1 === this.focusedItemIndex) { this.focusedItemIndex = 0; } else { diff --git a/core/toggle_menu_view.js b/core/toggle_menu_view.js index aef63c9a..f9ad4f82 100644 --- a/core/toggle_menu_view.js +++ b/core/toggle_menu_view.js @@ -25,7 +25,7 @@ function ToggleMenuView (options) { */ this.updateSelection = function() { - assert(!self.positionCacheExpired); + //assert(!self.positionCacheExpired); assert(this.focusedItemIndex >= 0 && this.focusedItemIndex <= self.items.length); self.redraw(); @@ -71,14 +71,14 @@ ToggleMenuView.prototype.setFocus = function(focused) { ToggleMenuView.prototype.onKeyPress = function(ch, key) { if(key) { var needsUpdate; - if(this.isSpecialKeyMapped('right', key.name) || this.isSpecialKeyMapped('down', key.name)) { + if(this.isKeyMapped('right', key.name) || this.isKeyMapped('down', key.name)) { if(this.items.length - 1 === this.focusedItemIndex) { this.focusedItemIndex = 0; } else { this.focusedItemIndex++; } needsUpdate = true; - } else if(this.isSpecialKeyMapped('left', key.name) || this.isSpecialKeyMapped('up', key.name)) { + } else if(this.isKeyMapped('left', key.name) || this.isKeyMapped('up', key.name)) { if(0 === this.focusedItemIndex) { this.focusedItemIndex = this.items.length - 1; } else { @@ -95,7 +95,7 @@ ToggleMenuView.prototype.onKeyPress = function(ch, key) { if(ch && this.hotKeys) { var keyIndex = this.hotKeys[this.caseInsensitiveHotKeys ? ch.toLowerCase() : ch]; - if(!_.isUndefined(keyIndex)) { + if(_.isNumber(keyIndex)) { this.focusedItemIndex = keyIndex; this.updateSelection(); } diff --git a/core/vertical_menu_view.js b/core/vertical_menu_view.js index bc90cc2f..836ec048 100644 --- a/core/vertical_menu_view.js +++ b/core/vertical_menu_view.js @@ -109,7 +109,7 @@ VerticalMenuView.prototype.onKeyPress = function(ch, key) { if(key) { var prevFocusedItemIndex = this.focusedItemIndex; - if(this.isSpecialKeyMapped('up', key.name)) { + if(this.isKeyMapped('up', key.name)) { if(0 === this.focusedItemIndex) { this.focusedItemIndex = this.items.length - 1; @@ -125,7 +125,7 @@ VerticalMenuView.prototype.onKeyPress = function(ch, key) { this.viewWindow.bottom--; } } - } else if(this.isSpecialKeyMapped('down', key.name)) { + } else if(this.isKeyMapped('down', key.name)) { if(this.items.length - 1 === this.focusedItemIndex) { this.focusedItemIndex = 0; @@ -144,6 +144,7 @@ VerticalMenuView.prototype.onKeyPress = function(ch, key) { } if(prevFocusedItemIndex !== this.focusedItemIndex) { + // :TODO: Optimize this for cases where no scrolling occured & only two items need updated this.redraw(); } } diff --git a/core/view.js b/core/view.js index f77b40b9..76a6345f 100644 --- a/core/view.js +++ b/core/view.js @@ -80,7 +80,7 @@ function View(options) { this.specialKeyMap = options.specialKeyMap || VIEW_SPECIAL_KEY_MAP_DEFAULT; } - this.isSpecialKeyMapped = function(keySet, keyName) { + this.isKeyMapped = function(keySet, keyName) { return _.has(this.specialKeyMap, keySet) && this.specialKeyMap[keySet].indexOf(keyName) > -1; }; @@ -241,9 +241,9 @@ View.prototype.onKeyPress = function(ch, key) { if(key) { assert(this.specialKeyMap, 'No special key map defined'); - if(this.isSpecialKeyMapped('accept', key.name)) { + if(this.isKeyMapped('accept', key.name)) { this.emit('action', 'accept'); - } else if(this.isSpecialKeyMapped('next', key.name)) { + } else if(this.isKeyMapped('next', key.name)) { this.emit('action', 'next'); } } diff --git a/mods/art/demo_horizontal_menu_view1.ans b/mods/art/demo_horizontal_menu_view1.ans index a18f1530d56bf1d18a64da1a01d70895f64e6c4d..0e486d39ad6d839c89a9273b76cc9d558abfb3e8 100644 GIT binary patch delta 97 zcmcb`w48ZDJF~HQ?!;DICj}sojy5*Ub<0mKEmjCgtte3lE~(5(m5w%bmX0>GHa5t$ s0;({u2C7l@@HGO7nV4DwwS#yXX_^z)ba1LMGB7YQ1~9Nswq=wB0BheFmH+?% delta 26 icmZ3^e2Zy9`@|mYiT|58Z!$12Ffs-(uubk_lm!5ZL