prosperon/scripts/mum.js

256 lines
6 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 = {
2024-07-15 15:54:18 -05:00
pos: null, // If set, puts the cursor to this position before drawing the element
offset:[0,0], // Move x,y to the right and down before drawing
padding:[0,0], // Pad inwards after drawing, to prepare for the next element
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-15 15:54:18 -05:00
inset: null,
anchor: [0,1], // where to draw the item from, relative to the cursor. [0,1] is from the top left corner. [1,0] is from the bottom right
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,
},
2024-07-15 15:54:18 -05:00
border: 0, // Draw a border around the element. For text, an outline.
overflow: "wrap", // how to deal with overflow from parent element
wrap: -1,
2024-07-11 14:25:45 -05:00
text_align: "left", /* left, center, right */
2024-07-15 15:54:18 -05:00
shader: null, // Use this shader, instead of the engine provided one
2024-07-09 01:03:39 -05:00
color: Color.white,
2024-07-15 15:54:18 -05:00
opacity:1,
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-15 15:54:18 -05:00
var context = mum.base;
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];
2024-07-15 15:54:18 -05:00
mum._frame = function()
{
cursor = [0,0];
}
2024-07-09 01:03:39 -05:00
var pre = function(data)
{
2024-07-10 09:39:28 -05:00
if (data.hide) return true;
2024-07-15 15:54:18 -05:00
data.__proto__ = mum.base;
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-15 15:54:18 -05:00
context.drawpos = cursor.slice().add(context.offset);
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();
2024-07-15 15:54:18 -05:00
cursor = cursor.add(old.padding);
2024-07-10 09:39:28 -05:00
post(old);
2024-07-09 13:48:15 -05:00
}
2024-07-15 15:54:18 -05:00
mum.style = function(fn, data)
{
var oldbase = mum.base;
data.__proto__ = mum.base;
mum.base = data;
fn();
mum.base = oldbase;
}
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);
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
});
//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);
2024-07-15 15:54:18 -05:00
data.drawpos.y -= (context.bb.t-cursor.y);
data.drawpos = data.drawpos.add(context.wh.scale(aa)).add(context.offset);
2024-07-14 16:09:50 -05:00
2024-07-15 15:54:18 -05:00
context.bb = render.text_bb(str, context.scale, data.wrap, data.drawpos);
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-15 15:54:18 -05:00
context.bb = render.text(str, data.drawpos, context.scale, context.color, data.wrap);
2024-07-14 16:09:50 -05:00
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-15 15:54:18 -05:00
data.width ??= tex.width;
data.height ??= tex.height;
var aa = [0,0].sub(data.anchor);
data.drawpos = data.drawpos.add(aa.scale([data.width,data.height]));
2024-07-09 01:03:39 -05:00
2024-07-09 16:43:09 -05:00
if (context.slice)
2024-07-15 15:54:18 -05:00
render.slice9(tex, data.drawpos, context.slice, [data.width,data.height]);
2024-07-11 16:37:24 -05:00
else {
cursor.y -= tex.height*context.scale;
2024-07-15 15:54:18 -05:00
context.bb = render.image(tex, data.drawpos, [context.scale*tex.width, context.scale*tex.height]);
2024-07-11 16:37:24 -05:00
}
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 = {})
{
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);
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]});
}