Skip to content
Snippets Groups Projects
Commit 1f539bc5 authored by deuce's avatar deuce
Browse files

Remove procedural interface... it's too pug-nasty to use reliably, and

I can't find a way to pass a single numeric variable by reference.

Oh well, JS is supposed to do objects anyways.  :-)
parent bf26e98e
Branches
Tags
No related merge requests found
...@@ -51,6 +51,7 @@ function Lightbar(items) ...@@ -51,6 +51,7 @@ function Lightbar(items)
this.getval=Lightbar_getval; this.getval=Lightbar_getval;
this.clear=Lightbar_clearitems; this.clear=Lightbar_clearitems;
this.add=Lightbar_additem; this.add=Lightbar_additem;
this.failsafe_getval=Lightbar_failsafe_getval;
if(items==undefined) if(items==undefined)
this.items=new Array(); this.items=new Array();
else else
...@@ -81,120 +82,85 @@ function Lightbar_clearitems() ...@@ -81,120 +82,85 @@ function Lightbar_clearitems()
this.items=new Array(); this.items=new Array();
} }
function Lightbar_getval(current) function Lightbar_failsafe_getval()
{ {
var retval; var retval;
if(current!=undefined)
this.current=current;
retval=lightbar_func(this.xpos, this.ypos, this.items, this.direction, this.fg
, this.bg, this.hfg, this.hbg, this.kfg, this.khfg, this.current
, this.align);
if(retval==null) {
for(i=0; i<this.items.length; i++) {
if(this.items[i] != undefined && this.items[i].text != undefined) {
writeln((i+1)+": "+this.items[i].text);
}
}
write("Choose an option: ");
i=console.getnum(i);
if(this.items[i]==undefined)
return(null);
if(this.items[i].retval==undefined)
return(undefined);
retval=this.items[i].retval;
}
for(i=0; i<this.items.length; i++) { for(i=0; i<this.items.length; i++) {
if(retval==this.items[i].retval) { if(this.items[i] != undefined && this.items[i].text != undefined) {
this.current=i; writeln((i+1)+": "+this.items[i].text);
break;
} }
} }
write("Choose an option: ");
i=console.getnum(i);
if(this.items[i]==undefined)
return(null);
if(this.items[i].retval==undefined)
return(undefined);
this.current=i;
retval=this.items[i].retval;
return(retval); return(retval);
} }
/* /*
* Super-Overlord Lightbar function... draws and returns selected value. * Super-Overlord Lightbar method... draws and returns selected value.
* Parameters:
* xpos: Horizontal position of lightbar menu (1-based)
* xpos: Vertical position of lightbar menu (1-based)
* items: an array of objects each having the following properties:
* text - The displayed text. A | prefixes a hotkey
* retval - The value to return if this is selected
* OPTIONAL Properties:
* width - The width of this item. If not specified, is the width of
* the text. Otherwise, the text is truncated or padded with
* spaces to fit the width.
* direction: 0 for vertical, 1 for horizontal.
* Horizontal menus always have one space of padding added between
* items.
* fg: Foreground colour of a non-current item
* bg: Background colour of a non-current item
* hfg: Foreground colour of a current item
* hbg: Background colour of a current item
* kfg: Hotkey forground colour for non-current item
* khfg: Hotkey foreground colour for current item
* current: Index of currently highlighted item (ToDo: This should be passed by reference (how?)!)
* align: If width is greater than the text length, a zero indicates the text
* should be left-aligned, a 1 indicates it should be right-aligned, and
* a 2 indicates it should be centered.
* Return Values:
* null for error, retval from the selected item for non-errors
*/ */
function lightbar_func(xpos, ypos, items, direction, fg, bg, hfg, hbg, kfg, khfg, current, align) function Lightbar_getval(current)
{ {
var attr=bg<<4|fg; var attr=this.bg<<4|this.fg;
var cattr=hbg<<4|hfg; var cattr=this.hbg<<4|this.hfg;
var kattr=bg<<4|kfg; var kattr=this.bg<<4|this.kfg;
var kcattr=hbg<<4|khfg; var kcattr=this.hbg<<4|this.khfg;
var ret=undefined; var ret=undefined;
if(current!=undefined)
this.current=current;
if(!(user.settings & USER_ANSI)) { if(!(user.settings & USER_ANSI)) {
alert("ANSI not supported for lightbar!"); return(this.failsafe_getval());
return(null);
} }
if(!(user.settings & USER_COLOR)) { if(!(user.settings & USER_COLOR)) {
alert("Colour not supported for lightbar!"); return(this.failsafe_getval());
return(null);
} }
if(direction < 0 || direction > 1) { if(this.direction < 0 || this.direction > 1) {
alert("Unknown lightbar direction!"); alert("Unknown lightbar direction!");
return(null); return(this.failsafe_getval());
} }
/* Check that a vertical lightbar fits on the screen */ /* Check that a vertical lightbar fits on the screen */
if(direction==0 && (ypos+items.length-1 > console.screen_rows)) { if(this.direction==0 && (this.ypos+this.items.length-1 > console.screen_rows)) {
alert("Screen too short for lightbar!"); alert("Screen too short for lightbar!");
return(null); return(this.failsafe_getval());
} }
/* Ensure current is valid */ /* Ensure current is valid */
if(current<0 || current >= items.length) { if(this.current<0 || this.current >= this.items.length) {
alert("current parameter is out of range!"); alert("current parameter is out of range!");
return(null); this.current=0;
if(this.items.length<=0)
return(null);
} }
/* Check that a horizontal lightbar fits on the screen */ /* Check that a horizontal lightbar fits on the screen */
if(direction==1) { if(this.direction==1) {
var end=xpos; var end=this.xpos;
var i; var i;
for(i=0; i<items.length; i++) { for(i=0; i<this.items.length; i++) {
if(items[i].width==undefined) { if(this.items[i].width==undefined) {
if(items[i]==undefined) { if(this.items[i]==undefined) {
alert("Sparse items array!"); alert("Sparse items array!");
return(null); return(null);
} }
if(items[i].text==undefined) { if(this.items[i].text==undefined) {
alert("No text for item "+i+"!"); alert("No text for item "+i+"!");
return(null); return(this.failsafe_getval());
} }
var cleaned=items[i].text; var cleaned=this.items[i].text;
cleaned=cleaned.replace(/\|/g,''); cleaned=cleaned.replace(/\|/g,'');
end+=cleaned.length+1; end+=cleaned.length+1;
} }
else { else {
end+=items[i].width end+=this.items[i].width
} }
} }
} }
...@@ -205,35 +171,35 @@ function lightbar_func(xpos, ypos, items, direction, fg, bg, hfg, hbg, kfg, khfg ...@@ -205,35 +171,35 @@ function lightbar_func(xpos, ypos, items, direction, fg, bg, hfg, hbg, kfg, khfg
var j; var j;
/* Draw items */ /* Draw items */
var curx=xpos; var curx=this.xpos;
var cury=ypos; var cury=this.ypos;
var cursx=xpos; var cursx=this.xpos;
var cursy=ypos; var cursy=this.ypos;
for(i=0; i<items.length; i++) { for(i=0; i<this.items.length; i++) {
var width; var width;
var cleaned=items[i].text; var cleaned=this.items[i].text;
cleaned=cleaned.replace(/\|/g,''); cleaned=cleaned.replace(/\|/g,'');
width=cleaned.length; width=cleaned.length;
if(items[i]==undefined) { if(this.items[i]==undefined) {
alert("Sparse items array!"); alert("Sparse items array!");
return(null); return(this.failsafe_getval());
} }
if(items[i].text==undefined) { if(this.items[i].text==undefined) {
alert("No text for item "+i+"!"); alert("No text for item "+i+"!");
return(null); return(this.failsafe_getval());
} }
if(items[i].width!=undefined) if(this.items[i].width!=undefined)
width=items[i].width; width=this.items[i].width;
console.gotoxy(curx, cury); console.gotoxy(curx, cury);
if(i==current) { if(i==this.current) {
cursx=curx; cursx=curx;
cursy=cury; cursy=cury;
} }
j=0; j=0;
if(cleaned.length < width) { if(cleaned.length < width) {
if(align==1) { if(this.align==1) {
if(current==i) if(this.current==i)
console.attributes=cattr; console.attributes=cattr;
else else
console.attributes=attr; console.attributes=attr;
...@@ -241,7 +207,7 @@ function lightbar_func(xpos, ypos, items, direction, fg, bg, hfg, hbg, kfg, khfg ...@@ -241,7 +207,7 @@ function lightbar_func(xpos, ypos, items, direction, fg, bg, hfg, hbg, kfg, khfg
console.write(' '); console.write(' ');
} }
if(align==2) { if(align==2) {
if(current==i) if(this.current==i)
console.attributes=cattr; console.attributes=cattr;
else else
console.attributes=attr; console.attributes=attr;
...@@ -249,25 +215,25 @@ function lightbar_func(xpos, ypos, items, direction, fg, bg, hfg, hbg, kfg, khfg ...@@ -249,25 +215,25 @@ function lightbar_func(xpos, ypos, items, direction, fg, bg, hfg, hbg, kfg, khfg
console.write(' '); console.write(' ');
} }
} }
for(; j<items[i].text.length; j++) { for(; j<this.items[i].text.length; j++) {
if(width > -1 && j > width) if(width > -1 && j > width)
break; break;
if(items[i].text.substr(j,1)=='|') { if(this.items[i].text.substr(j,1)=='|') {
if(current==i) if(this.current==i)
console.attributes=kcattr; console.attributes=kcattr;
else else
console.attributes=kattr; console.attributes=kattr;
j++; j++;
} }
else { else {
if(current==i) if(this.current==i)
console.attributes=cattr; console.attributes=cattr;
else else
console.attributes=attr; console.attributes=attr;
} }
console.write(items[i].text.substr(j,1)); console.write(this.items[i].text.substr(j,1));
} }
if(current==i) if(this.current==i)
console.attributes=cattr; console.attributes=cattr;
else else
console.attributes=attr; console.attributes=attr;
...@@ -275,7 +241,7 @@ function lightbar_func(xpos, ypos, items, direction, fg, bg, hfg, hbg, kfg, khfg ...@@ -275,7 +241,7 @@ function lightbar_func(xpos, ypos, items, direction, fg, bg, hfg, hbg, kfg, khfg
console.write(" "); console.write(" ");
j++; j++;
} }
if(direction==0) if(this.direction==0)
cury++; cury++;
else { else {
console.attributes=attr; console.attributes=attr;
...@@ -291,53 +257,53 @@ function lightbar_func(xpos, ypos, items, direction, fg, bg, hfg, hbg, kfg, khfg ...@@ -291,53 +257,53 @@ function lightbar_func(xpos, ypos, items, direction, fg, bg, hfg, hbg, kfg, khfg
var key=console.getkey(K_UPPER); var key=console.getkey(K_UPPER);
switch(key) { switch(key) {
case KEY_UP: case KEY_UP:
if(direction==0) { if(this.direction==0) {
if(current==0) if(this.current==0)
current=items.length; this.current=this.items.length;
current--; this.current--;
} }
break; break;
case KEY_DOWN: case KEY_DOWN:
if(direction==0) { if(this.direction==0) {
current++; this.current++;
if(current==items.length) if(this.current==this.items.length)
current=0; this.current=0;
} }
break; break;
case KEY_LEFT: case KEY_LEFT:
if(direction==1) { if(this.direction==1) {
if(current==0) if(this.current==0)
current=items.length; this.current=this.items.length;
current--; this.current--;
} }
break; break;
case KEY_RIGHT: case KEY_RIGHT:
if(direction==1) { if(this.direction==1) {
current++; this.current++;
if(current==items.length) if(this.current==this.items.length)
current=0; this.current=0;
} }
break; break;
case KEY_HOME: case KEY_HOME:
current=0; this.current=0;
break; break;
case KEY_END: case KEY_END:
current=items.length-1; this.current=this.items.length-1;
break; break;
case '\r': case '\r':
case '\n': case '\n':
if(items[current].retval==undefined) if(this.items[this.current].retval==undefined)
return(undefined); return(undefined);
return(items[current].retval); return(this.items[this.current].retval);
break; break;
default: default:
for(i=0; i<items.length; i++) { for(i=0; i<this.items.length; i++) {
if(items[i].text.indexOf('|'+key)!=-1) { if(this.items[i].text.indexOf('|'+key)!=-1) {
current=i; this.current=i;
if(items[current].retval==undefined) if(this.items[this.current].retval==undefined)
return(undefined); return(undefined);
/* Let it go through once more to highlight */ /* Let it go through once more to highlight */
ret=items[current].retval; ret=this.items[this.current].retval;
break; break;
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment