prosperon/scripts/mum.js

245 lines
5.5 KiB
JavaScript
Raw Normal View History

2024-07-09 01:03:39 -05:00
globalThis.mum = {};
2024-07-10 09:39:28 -05:00
var panel;
2024-07-09 13:48:15 -05:00
var selected = undefined;
mum.inputs = {};
mum.inputs.lm = function()
{
if (!selected) return;
2024-07-11 14:25:45 -05:00
if (!selected.action) return;
2024-07-09 13:48:15 -05:00
selected.action();
}
2024-07-09 01:03:39 -05:00
mum.base = {
padding:[0,0], /* Each element inset with this padding on all sides */
offset:[0,0],
2024-07-11 16:37:24 -05:00
pos: null,
2024-07-09 01:03:39 -05:00
font: "fonts/c64.ttf",
selectable: false,
selected: false,
font_size: 16,
scale: 1,
angle: 0,
2024-07-14 16:09:50 -05:00
anchor: [0,1], // where to draw the item from, relative to the cursor.
2024-07-09 01:03:39 -05:00
background_image: null,
2024-07-09 16:43:09 -05:00
slice: null,
2024-07-11 16:37:24 -05:00
hover: {
color: Color.red,
},
2024-07-09 01:03:39 -05:00
text_shadow: {
pos: [0,0],
color: Color.white,
},
text_outline: 1, /* outline in pixels */
2024-07-11 14:25:45 -05:00
text_align: "left", /* left, center, right */
text_shader: null,
2024-07-14 16:09:50 -05:00
overflow: "wrap", // clip, wrap, break
2024-07-09 01:03:39 -05:00
color: Color.white,
margin: [0,0], /* Distance between elements for things like columns */
2024-07-10 09:39:28 -05:00
size: null,
2024-07-14 16:09:50 -05:00
width:0,
height:0,
2024-07-09 01:03:39 -05:00
max_width: Infinity,
max_height: Infinity,
image_repeat: false,
image_repeat_offset: [0,0],
debug: false, /* set to true to draw debug boxes */
hide: false,
2024-07-09 13:48:15 -05:00
tooltip: null,
2024-07-09 01:03:39 -05:00
}
2024-07-11 16:37:24 -05:00
function show_debug() { return prosperon.debug && mum.debug; }
2024-07-10 09:39:28 -05:00
mum.debug = false;
2024-07-09 01:03:39 -05:00
var post = function() {};
var posts = [];
2024-07-11 16:37:24 -05:00
mum.style = mum.base;
var context = mum.style;
2024-07-14 16:09:50 -05:00
var container = undefined;
2024-07-09 01:03:39 -05:00
var contexts = [];
var cursor = [0,0];
var pre = function(data)
{
2024-07-10 09:39:28 -05:00
if (data.hide) return true;
2024-07-11 16:37:24 -05:00
data.__proto__ = mum.style;
2024-07-10 09:39:28 -05:00
if (context)
contexts.push(context);
2024-07-09 01:03:39 -05:00
context = data;
2024-07-11 16:37:24 -05:00
if (context.pos) cursor = context.pos.slice();
2024-07-09 01:03:39 -05:00
}
2024-07-10 09:39:28 -05:00
var end = function()
2024-07-09 13:48:15 -05:00
{
2024-07-10 09:39:28 -05:00
var old = context;
context = contexts.pop();
post(old);
2024-07-09 13:48:15 -05:00
}
2024-07-09 01:03:39 -05:00
mum.list = function(fn, data = {})
{
if (pre(data)) return;
2024-07-14 16:09:50 -05:00
var aa = [0,1].sub(context.anchor);
if (context.pos) cursor = context.pos.slice();
cursor = cursor.add([context.width,context.height].scale(aa)).add(context.offset).add(context.padding);
2024-07-09 01:03:39 -05:00
posts.push(post);
2024-07-10 09:39:28 -05:00
post = mum.list.post;
2024-07-09 01:03:39 -05:00
2024-07-14 16:09:50 -05:00
if (show_debug())
render.boundingbox({
t:cursor.y,
b:cursor.y-context.height,
l:cursor.x,
r:cursor.x+context.width
});
cursor.x += context.width/2;
//if (context.background_image) mum.image(null, Object.create(context))
if (context.background_image) {
var imgpos = context.pos.slice();
imgpos.y -= context.height/2;
imgpos.x -= context.width/2;
var imgscale = [context.width,context.height];
if (context.slice)
render.slice9(game.texture(context.background_image), imgpos, context.slice, imgscale);
else
render.image(game.texture(context.background_image), imgpos, [context.width,context.height]);
}
2024-07-09 01:03:39 -05:00
fn();
2024-07-14 16:09:50 -05:00
2024-07-10 09:39:28 -05:00
context.bb.l -= context.padding.x;
context.bb.r += context.padding.x;
context.bb.t += context.padding.y;
context.bb.b -= context.padding.y;
2024-07-11 16:37:24 -05:00
if (show_debug())
2024-07-10 09:39:28 -05:00
render.boundingbox(context.bb);
2024-07-09 01:03:39 -05:00
post = posts.pop();
end();
}
2024-07-10 09:39:28 -05:00
mum.list.post = function(e)
{
cursor.y -= (e.bb.t - e.bb.b);
cursor.y -= e.padding.y;
if (context.bb)
context.bb = bbox.expand(context.bb,e.bb)
else
context.bb = e.bb;
}
mum.label = function(str, data = {})
{
if (pre(data)) return;
render.set_font(context.font, context.font_size);
2024-07-14 16:09:50 -05:00
2024-07-10 09:39:28 -05:00
context.bb = render.text_bb(str, context.scale, -1, cursor);
2024-07-14 16:09:50 -05:00
context.wh = bbox.towh(context.bb);
2024-07-10 09:39:28 -05:00
2024-07-14 16:09:50 -05:00
var aa = [0,1].sub(context.anchor);
var pos = cursor.slice();
pos.y -= (context.bb.t-cursor.y);
pos = pos.add(context.wh.scale(aa)).add(context.offset);
context.bb = render.text_bb(str, context.scale, -1, pos);
2024-07-10 09:39:28 -05:00
2024-07-11 16:37:24 -05:00
if (context.action && bbox.pointin(context.bb, input.mouse.screenpos())) {
2024-07-10 09:39:28 -05:00
if (context.hover) {
context.hover.__proto__ = context;
context = context.hover;
selected = context;
}
}
2024-07-14 16:09:50 -05:00
context.bb = render.text(str, pos, context.scale, context.color);
if (show_debug())
render.boundingbox(context.bb);
2024-07-10 09:39:28 -05:00
end();
}
2024-07-09 01:03:39 -05:00
mum.image = function(path, data = {})
{
if (pre(data)) return;
2024-07-14 16:09:50 -05:00
path ??= context.background_image;
2024-07-11 16:37:24 -05:00
var tex = path;
if (typeof path === 'string')
tex = game.texture(path);
2024-07-09 01:03:39 -05:00
2024-07-09 16:43:09 -05:00
if (context.slice)
2024-07-10 09:39:28 -05:00
render.slice9(tex, cursor, context.slice, context.scale);
2024-07-11 16:37:24 -05:00
else {
cursor.y -= tex.height*context.scale;
context.bb = render.image(tex, cursor, [context.scale*tex.width, context.scale*tex.height]);
}
2024-07-09 01:03:39 -05:00
end();
}
2024-07-09 13:48:15 -05:00
var btnbb;
var btnpost = function()
{
btnbb = context.bb;
}
2024-07-11 16:37:24 -05:00
mum.button = function(str, data = {padding:[4,4], color:Color.black})
{
2024-07-09 01:03:39 -05:00
if (pre(data)) return;
2024-07-09 13:48:15 -05:00
posts.push(post);
post = btnpost;
if (typeof str === 'string')
2024-07-10 09:39:28 -05:00
render.text(str, cursor.add(context.padding), context.scale, context.color);
2024-07-09 13:48:15 -05:00
else
str();
2024-07-11 16:37:24 -05:00
if (data.action && data.hover && bbox.pointin(btnbb, input.mouse.screenpos())) {
2024-07-09 13:48:15 -05:00
data.hover.__proto__ = data;
context = data.hover;
}
render.rectangle([btnbb.l-context.padding.x, btnbb.b-context.padding.y], [btnbb.r+context.padding.y, btnbb.t+context.padding.y], context.color);
context.bb = btnbb;
post = posts.pop();
end();
}
mum.window = function(fn, data = {})
{
data = Object.assign({
2024-07-10 09:39:28 -05:00
size:[400,400],
2024-07-09 13:48:15 -05:00
color: Color.black
}, data);
if (pre(data)) return;
2024-07-10 09:39:28 -05:00
render.rectangle(cursor, cursor.add(context.size), context.color);
2024-07-09 13:48:15 -05:00
cursor.y += context.height;
cursor = cursor.add(context.padding);
context.pos = cursor.slice();
fn();
2024-07-09 01:03:39 -05:00
end();
}
2024-07-14 16:09:50 -05:00
mum.ex_hud = function()
{
mum.label("TOP LEFT", {pos:[0,game.size.y], anchor:[0,1]});
mum.label("BOTTOM LEFT", {pos:[0,0], anchor:[0,0]});
mum.label("TOP RIGHT", {pos:game.size, anchor:[1,1]});
mum.label("BOTTOM RIGHT", {pos:[game.size.x, 0], anchor:[1,0]});
}