mirror of
https://github.com/NuSkooler/enigma-bbs.git
synced 2025-06-10 22:54:37 +02:00
* Lots of minor code cleanup
* WIP work on HorizontalMenuView * Initial art for HorizontalMenuView demo
This commit is contained in:
parent
c87c0d69b7
commit
d346fe72ee
11 changed files with 114 additions and 66 deletions
|
@ -5,24 +5,91 @@ var MenuView = require('./menu_view.js').MenuView;
|
|||
var ansi = require('./ansi_term.js');
|
||||
var strUtil = require('./string_util.js');
|
||||
|
||||
function HorizontalMenuView = function(options) {
|
||||
var assert = require('assert');
|
||||
|
||||
exports.HorizontalMenuView = HorizontalMenuView;
|
||||
|
||||
function HorizontalMenuView(options) {
|
||||
options.cursor = options.cursor || 'hide';
|
||||
|
||||
MenuView.call(this, options);
|
||||
|
||||
this.dimens.height = 1; // always the case
|
||||
|
||||
var self = this;
|
||||
|
||||
this.getSpacer = function() {
|
||||
return new Array(self.itemSpacing).join(' ');
|
||||
}
|
||||
|
||||
this.performAutoScale = function() {
|
||||
if(self.autoScale.width) {
|
||||
var spacer = self.getSpacer();
|
||||
var width = self.items.join(spacer).length + (spacer.length * 2);
|
||||
assert(width <= self.client.term.termWidth - self.position.col);
|
||||
self.dimens.width = width;
|
||||
}
|
||||
};
|
||||
|
||||
this.performAutoScale();
|
||||
|
||||
this.cachePositions = function() {
|
||||
if(this.positionCacheExpired) {
|
||||
var col = self.position.col;
|
||||
var spacer = self.getSpacer();
|
||||
|
||||
self.itemColumns = [];
|
||||
for(var i = 0; i < self.items.length; ++i) {
|
||||
self.itemColumns[i] = col;
|
||||
col += spacer.length + self.items[i].length + spacer.length;
|
||||
}
|
||||
}
|
||||
|
||||
this.positionCacheExpired = false;
|
||||
};
|
||||
|
||||
this.drawItem = function(index) {
|
||||
assert(self.itemColumns.length === self.items.length);
|
||||
|
||||
var item = self.items[index];
|
||||
if(!item) {
|
||||
return;
|
||||
}
|
||||
|
||||
self.client.term.write(ansi.goto(item.row, self.itemColumns[index]));
|
||||
self.client.term.write(index === self.focusedItemIndex ? self.getFocusSGR() : self.getSGR());
|
||||
|
||||
var text = strUtil.stylizeString(item.text, item.focused ? self.focusTextStyle : self.textStyle);
|
||||
|
||||
var extraPad = self.getSpacer().length * 2;
|
||||
self.client.term.write(
|
||||
strUtil.pad(text, text.length + extraPad, this.fillChar, 'center'));
|
||||
};
|
||||
}
|
||||
|
||||
require('util').inherits(HorizontalMenuView, MenuView);
|
||||
|
||||
HorizontalMenuView.prototype.setHeight = function(height) {
|
||||
height = parseInt(height, 10);
|
||||
assert(1 === height); // nothing else allowed here
|
||||
HorizontalMenuView.super_.prototype.setHeight(this, height);
|
||||
};
|
||||
|
||||
HorizontalMenuView.prototype.redraw = function() {
|
||||
HorizontalMenuView.super_.prototype.redraw.call(this);
|
||||
|
||||
this.cachePositions();
|
||||
|
||||
for(var i = 0; i < this.items.length; ++i) {
|
||||
this.items[i].focused = this.focusedItemIndex === i;
|
||||
this.drawItem(i);
|
||||
}
|
||||
};
|
||||
|
||||
HorizontalMenuView.prototype.setPosition = function(pos) {
|
||||
HorizontalMenuView.super_.prototype.setPosition.call(this, pos);
|
||||
|
||||
|
||||
this.positionCacheExpired = true;
|
||||
};
|
||||
|
||||
HorizontalMenuView.prototype.setFocus = function(focused) {
|
||||
|
@ -34,17 +101,5 @@ HorizontalMenuView.prototype.setFocus = function(focused) {
|
|||
HorizontalMenuView.prototype.setItems = function(items) {
|
||||
HorizontalMenuView.super_.prototype.setItems.call(this, items);
|
||||
|
||||
//
|
||||
// Styles:
|
||||
// * itemPadding: n
|
||||
// *
|
||||
//
|
||||
//
|
||||
// item1 item2 itemThree itemfour!!!!!
|
||||
// ^^^^^^^^^
|
||||
//
|
||||
// item1 item2 itemThree item!!!!!
|
||||
// ^^^^^^^
|
||||
|
||||
|
||||
this.positionCacheExpired = true;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue