2023-12-18 06:45:27 -06:00
|
|
|
/*
|
|
|
|
GUI functions take screen space coordinates
|
|
|
|
*/
|
|
|
|
|
2023-09-07 16:46:35 -05:00
|
|
|
var GUI = {
|
2023-10-04 17:57:37 -05:00
|
|
|
text(str, pos, size, color, wrap, anchor, cursor) {
|
2023-09-23 12:35:02 -05:00
|
|
|
size ??= 1;
|
|
|
|
color ??= Color.white;
|
|
|
|
wrap ??= -1;
|
2023-10-03 17:16:38 -05:00
|
|
|
anchor ??= [0,1];
|
2023-10-04 17:57:37 -05:00
|
|
|
|
|
|
|
cursor ??= -1;
|
2023-09-07 16:46:35 -05:00
|
|
|
|
|
|
|
var bb = cmd(118, str, size, wrap);
|
2023-10-03 17:16:38 -05:00
|
|
|
var w = bb.r*2;
|
|
|
|
var h = bb.t*2;
|
|
|
|
|
|
|
|
//ui_text draws with an anchor on top left corner
|
|
|
|
var p = pos.slice();
|
|
|
|
p.x -= w * anchor.x;
|
|
|
|
bb.r += (w*anchor.x);
|
|
|
|
bb.l += (w*anchor.x);
|
|
|
|
p.y += h * (1 - anchor.y);
|
|
|
|
bb.t += h*(1-anchor.y);
|
|
|
|
bb.b += h*(1-anchor.y);
|
2023-10-04 17:57:37 -05:00
|
|
|
ui_text(str, p, size, color, wrap, cursor);
|
2023-09-07 16:46:35 -05:00
|
|
|
|
|
|
|
return bb;
|
|
|
|
},
|
|
|
|
|
2023-10-04 08:18:09 -05:00
|
|
|
scissor(x,y,w,h) {
|
|
|
|
cmd(140,x,y,w,h);
|
|
|
|
},
|
|
|
|
|
|
|
|
scissor_win() { cmd(140,0,0,Window.width,Window.height); },
|
|
|
|
|
2023-10-18 17:20:23 -05:00
|
|
|
image(path,pos,color) {
|
|
|
|
color ??= Color.black;
|
2023-09-13 16:49:22 -05:00
|
|
|
var wh = cmd(64,path);
|
|
|
|
gui_img(path,pos, [1.0,1.0], 0.0, 0.0, [0.0,0.0], 0.0, Color.black);
|
2023-09-07 16:46:35 -05:00
|
|
|
return cwh2bb([0,0], wh);
|
|
|
|
},
|
|
|
|
|
2023-10-04 08:18:09 -05:00
|
|
|
input_lmouse_pressed() {
|
|
|
|
if (GUI.selected)
|
|
|
|
GUI.selected.action();
|
|
|
|
},
|
2023-09-07 16:46:35 -05:00
|
|
|
|
2023-10-04 08:18:09 -05:00
|
|
|
input_s_pressed() {
|
|
|
|
if (GUI.selected?.down) {
|
|
|
|
GUI.selected.selected = false;
|
|
|
|
GUI.selected = GUI.selected.down;
|
|
|
|
GUI.selected.selected = true;
|
|
|
|
}
|
|
|
|
},
|
2023-09-07 16:46:35 -05:00
|
|
|
|
2023-10-04 08:18:09 -05:00
|
|
|
input_w_pressed() {
|
|
|
|
if (GUI.selected?.up) {
|
|
|
|
GUI.selected.selected = false;
|
|
|
|
GUI.selected = GUI.selected.up;
|
|
|
|
GUI.selected.selected = true;
|
|
|
|
}
|
2023-09-07 16:46:35 -05:00
|
|
|
},
|
|
|
|
|
2023-10-04 08:18:09 -05:00
|
|
|
input_enter_pressed() {
|
|
|
|
if (GUI.selected) {
|
|
|
|
GUI.selected.action();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-10-05 13:33:43 -05:00
|
|
|
var gui_controls = {};
|
2023-10-26 11:48:02 -05:00
|
|
|
gui_controls.toString = function() { return "GUI controls"; };
|
2023-10-05 13:33:43 -05:00
|
|
|
gui_controls.update = function() { };
|
|
|
|
|
2023-10-09 13:03:12 -05:00
|
|
|
gui_controls.set_mum = function(mum)
|
|
|
|
{
|
|
|
|
mum.selected = true;
|
|
|
|
|
|
|
|
if (this.selected && this.selected !== mum)
|
|
|
|
this.selected.selected = false;
|
|
|
|
|
|
|
|
this.selected = mum;
|
|
|
|
}
|
|
|
|
gui_controls.check_bb = function(mum)
|
|
|
|
{
|
|
|
|
if (pointinbb(mum.bb, Mouse.pos))
|
|
|
|
gui_controls.set_mum(mum);
|
|
|
|
}
|
2023-10-05 13:33:43 -05:00
|
|
|
gui_controls.inputs = {};
|
|
|
|
gui_controls.inputs.fallthru = false;
|
|
|
|
gui_controls.inputs.mouse = {};
|
|
|
|
gui_controls.inputs.mouse.move = function(pos,dpos)
|
|
|
|
{
|
2023-10-09 13:03:12 -05:00
|
|
|
}
|
|
|
|
gui_controls.inputs.mouse.scroll = function(scroll)
|
|
|
|
{
|
2023-10-05 13:33:43 -05:00
|
|
|
}
|
|
|
|
|
2023-10-26 11:48:02 -05:00
|
|
|
gui_controls.check_submit = function() {
|
2023-10-09 13:03:12 -05:00
|
|
|
if (this.selected && this.selected.action)
|
2023-10-05 13:33:43 -05:00
|
|
|
this.selected.action(this.selected);
|
2023-10-26 11:48:02 -05:00
|
|
|
}
|
2023-10-05 13:33:43 -05:00
|
|
|
|
2023-10-04 08:18:09 -05:00
|
|
|
var Mum = {
|
2023-10-04 17:57:37 -05:00
|
|
|
padding:[0,0], /* Each element inset with this padding on all sides */
|
|
|
|
offset:[0,0],
|
2023-09-07 16:46:35 -05:00
|
|
|
font: "fonts/LessPerfectDOSVGA.ttf",
|
2023-10-05 13:33:43 -05:00
|
|
|
selectable: false,
|
|
|
|
selected: false,
|
2023-09-07 16:46:35 -05:00
|
|
|
font_size: 1,
|
|
|
|
text_align: "left",
|
|
|
|
scale: 1,
|
|
|
|
angle: 0,
|
2023-10-04 17:57:37 -05:00
|
|
|
anchor: [0,1],
|
2023-10-05 13:33:43 -05:00
|
|
|
hovered: {},
|
2023-09-07 16:46:35 -05:00
|
|
|
text_shadow: {
|
|
|
|
pos: [0,0],
|
2023-09-23 12:35:02 -05:00
|
|
|
color: Color.white,
|
2023-09-07 16:46:35 -05:00
|
|
|
},
|
|
|
|
text_outline: 1, /* outline in pixels */
|
2023-09-23 12:35:02 -05:00
|
|
|
color: Color.white,
|
2023-10-06 12:38:49 -05:00
|
|
|
margin: [0,0], /* Distance between elements for things like columns */
|
2023-09-07 16:46:35 -05:00
|
|
|
width: 0,
|
|
|
|
height: 0,
|
2023-10-06 12:38:49 -05:00
|
|
|
max_width: Infinity,
|
|
|
|
max_height: Infinity,
|
2023-09-07 16:46:35 -05:00
|
|
|
image_repeat: false,
|
|
|
|
image_repeat_offset: [0,0],
|
|
|
|
debug: false, /* set to true to draw debug boxes */
|
2023-10-05 13:33:43 -05:00
|
|
|
|
2023-10-04 08:18:09 -05:00
|
|
|
make(def) {
|
|
|
|
var n = Object.create(this);
|
|
|
|
Object.assign(n, def);
|
|
|
|
return n;
|
2023-09-07 16:46:35 -05:00
|
|
|
},
|
|
|
|
|
2023-10-05 13:33:43 -05:00
|
|
|
prestart() {
|
|
|
|
this.hovered.__proto__ = this;
|
|
|
|
},
|
|
|
|
|
2023-10-04 17:57:37 -05:00
|
|
|
start() {},
|
|
|
|
|
2023-10-04 08:18:09 -05:00
|
|
|
extend(def) {
|
|
|
|
var n = Object.create(this);
|
|
|
|
Object.assign(n, def);
|
2023-10-06 12:38:49 -05:00
|
|
|
var fn = function(def) {
|
2023-10-05 13:33:43 -05:00
|
|
|
var p = n.make(def);
|
|
|
|
p.prestart();
|
|
|
|
p.start();
|
|
|
|
return p;
|
|
|
|
};
|
2023-10-06 12:38:49 -05:00
|
|
|
fn._int = n;
|
|
|
|
return fn;
|
2023-09-07 16:46:35 -05:00
|
|
|
},
|
2023-10-04 08:18:09 -05:00
|
|
|
}
|
2023-09-07 16:46:35 -05:00
|
|
|
|
2023-10-04 08:18:09 -05:00
|
|
|
Mum.text = Mum.extend({
|
2023-10-06 12:38:49 -05:00
|
|
|
draw(cursor, cnt) {
|
|
|
|
cnt ??= Mum;
|
2023-10-04 08:18:09 -05:00
|
|
|
if (this.hide) return;
|
2023-10-09 13:03:12 -05:00
|
|
|
if (this.selectable) gui_controls.check_bb(this);
|
2023-10-04 17:57:37 -05:00
|
|
|
this.caret ??= -1;
|
|
|
|
|
|
|
|
/* if (!this.bb)
|
|
|
|
this.calc_bb(cursor);
|
|
|
|
else
|
|
|
|
this.update_bb(cursor);
|
2023-10-05 13:33:43 -05:00
|
|
|
*/
|
|
|
|
var params = this.selected ? this.hovered : this;
|
|
|
|
|
2023-10-06 12:38:49 -05:00
|
|
|
this.width = Math.min(params.max_width, cnt.max_width);
|
|
|
|
|
2023-10-04 17:57:37 -05:00
|
|
|
this.calc_bb(cursor);
|
2023-10-06 12:38:49 -05:00
|
|
|
this.height = this.wh.y;
|
2023-10-05 13:33:43 -05:00
|
|
|
var aa = [0,1].sub(params.anchor);
|
|
|
|
var pos = cursor.add(params.wh.scale(aa)).add(params.offset);
|
2023-10-06 12:38:49 -05:00
|
|
|
ui_text(params.str, pos, params.font_size, params.color, this.width, params.caret);
|
2023-10-04 17:57:37 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
update_bb(cursor) {
|
|
|
|
this.bb = movebb(this.bb, cursor.sub(this.wh.scale(this.anchor)));
|
2023-10-04 08:18:09 -05:00
|
|
|
},
|
2023-10-04 17:57:37 -05:00
|
|
|
|
2023-10-04 08:18:09 -05:00
|
|
|
calc_bb(cursor) {
|
|
|
|
var bb = cmd(118,this.str, this.font_size, this.width);
|
2023-10-04 17:57:37 -05:00
|
|
|
this.wh = bb2wh(bb);
|
2023-10-05 13:33:43 -05:00
|
|
|
var pos = cursor.add(this.wh.scale([0,1].sub(this.anchor))).add(this.offset);
|
|
|
|
this.bb = movebb(bb,pos.add([this.wh.x/2,0]));
|
2023-10-04 08:18:09 -05:00
|
|
|
},
|
2023-10-04 17:57:37 -05:00
|
|
|
start() {
|
|
|
|
this.calc_bb([0,0]);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-10-06 12:38:49 -05:00
|
|
|
Mum.button = Mum.text._int.extend({
|
|
|
|
selectable: true,
|
|
|
|
color: Color.blue,
|
|
|
|
hovered:{
|
|
|
|
color: Color.red
|
|
|
|
},
|
|
|
|
action() { Log.warn("Button has no action."); },
|
|
|
|
});
|
|
|
|
|
2023-10-04 17:57:37 -05:00
|
|
|
Mum.window = Mum.extend({
|
|
|
|
start() {
|
|
|
|
this.wh = [this.width, this.height];
|
|
|
|
this.bb = cwh2bb([0,0], this.wh);
|
|
|
|
},
|
2023-10-06 12:38:49 -05:00
|
|
|
draw(cursor, cnt) {
|
|
|
|
cnt ??= Mum;
|
|
|
|
var p = cursor.sub(this.wh.scale(this.anchor)).add(this.padding);
|
2023-10-04 17:57:37 -05:00
|
|
|
GUI.window(p,this.wh, this.color);
|
|
|
|
this.bb = bl2bb(p, this.wh);
|
|
|
|
GUI.flush();
|
|
|
|
GUI.scissor(p.x,p.y,this.wh.x,this.wh.y);
|
2023-10-06 12:38:49 -05:00
|
|
|
this.max_width = this.width;
|
2023-10-09 13:03:12 -05:00
|
|
|
if (this.selectable) gui_controls.check_bb(this);
|
2023-10-06 12:38:49 -05:00
|
|
|
var pos = [this.bb.l, this.bb.t].add(this.padding);
|
2023-10-04 17:57:37 -05:00
|
|
|
this.items.forEach(function(item) {
|
|
|
|
if (item.hide) return;
|
2023-10-06 12:38:49 -05:00
|
|
|
item.draw(pos.slice(),this);
|
|
|
|
}, this);
|
2023-10-04 17:57:37 -05:00
|
|
|
GUI.flush();
|
|
|
|
GUI.scissor_win();
|
|
|
|
},
|
2023-10-04 08:18:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
Mum.image = Mum.extend({
|
|
|
|
start() {
|
|
|
|
if (!this.path) {
|
|
|
|
Log.warn("Mum image needs a path.");
|
|
|
|
this.draw = function(){};
|
|
|
|
return;
|
2023-09-07 16:46:35 -05:00
|
|
|
}
|
2023-10-04 08:18:09 -05:00
|
|
|
|
|
|
|
var tex_wh = cmd(64, this.path);
|
2023-10-04 17:57:37 -05:00
|
|
|
this.wh = tex_wh.slice();
|
|
|
|
if (this.width !== 0) this.wh.x = this.width;
|
|
|
|
if (this.height !== 0) this.wh.y = this.height;
|
2023-10-04 08:18:09 -05:00
|
|
|
|
|
|
|
this.wh = wh.scale(this.scale);
|
|
|
|
this.sendscale = [wh.x/tex_wh.x, wh.y/tex_wh.y];
|
|
|
|
},
|
|
|
|
|
|
|
|
draw(pos) {
|
|
|
|
this.calc_bb(pos);
|
|
|
|
gui_img(this.path, pos.sub(this.anchor.scale([this.width, this.height])), this.sendscale, this.angle, this.image_repeat, this.image_repeat_offset, this.color);
|
2023-09-07 16:46:35 -05:00
|
|
|
},
|
|
|
|
|
2023-10-04 08:18:09 -05:00
|
|
|
calc_bb(pos) {
|
|
|
|
this.bb = cwh2bb(this.wh.scale([0.5,0.5]), wh);
|
|
|
|
this.bb = movebb(this.bb, pos.sub(this.wh.scale(this.anchor)));
|
2023-09-07 16:46:35 -05:00
|
|
|
}
|
2023-10-04 08:18:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
Mum.column = Mum.extend({
|
2023-10-06 12:38:49 -05:00
|
|
|
draw(cursor, cnt) {
|
2023-11-07 12:45:52 -06:00
|
|
|
cnt ??= Mum;
|
2023-10-04 08:18:09 -05:00
|
|
|
if (this.hide) return;
|
2023-10-05 13:33:43 -05:00
|
|
|
cursor = cursor.add(this.offset);
|
2023-10-06 12:38:49 -05:00
|
|
|
this.max_width = cnt.width;
|
2023-10-04 08:18:09 -05:00
|
|
|
|
|
|
|
this.items.forEach(function(item) {
|
|
|
|
if (item.hide) return;
|
2023-10-06 12:38:49 -05:00
|
|
|
item.draw(cursor, this);
|
|
|
|
cursor.y -= item.height;
|
2023-10-04 17:57:37 -05:00
|
|
|
cursor.y -= this.padding.y;
|
2023-10-04 08:18:09 -05:00
|
|
|
}, this);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
GUI.window = function(pos, wh, color)
|
|
|
|
{
|
|
|
|
var p = pos.slice();
|
|
|
|
p.x += wh.x/2;
|
|
|
|
p.y += wh.y/2;
|
2023-12-18 06:45:27 -06:00
|
|
|
Shape.box(p,wh,color);
|
2023-10-04 08:18:09 -05:00
|
|
|
}
|
2023-09-07 16:46:35 -05:00
|
|
|
|
2023-10-04 08:18:09 -05:00
|
|
|
GUI.flush = function() { cmd(141); };
|
|
|
|
|
|
|
|
Mum.debug_colors = {
|
2023-09-07 16:46:35 -05:00
|
|
|
bounds: Color.red.slice(),
|
|
|
|
margin: Color.blue.slice(),
|
|
|
|
padding: Color.green.slice()
|
|
|
|
};
|
|
|
|
|
2023-10-04 08:18:09 -05:00
|
|
|
Object.values(Mum.debug_colors).forEach(function(v) { v.a = 100; });
|