This commit is contained in:
John Alanbrook 2024-07-15 15:54:18 -05:00
parent 2f8827fd78
commit fb88641883
6 changed files with 46 additions and 44 deletions

View file

@ -41,6 +41,7 @@ function keycode(name) { return charCodeAt(name); }
function keyname_extd(key) function keyname_extd(key)
{ {
if (typeof key === 'string') return key; if (typeof key === 'string') return key;
if (key > 289 && key < 302) { if (key > 289 && key < 302) {
var num = key-289; var num = key-289;
return `f${num}`; return `f${num}`;

View file

@ -13,16 +13,17 @@ mum.inputs.lm = function()
} }
mum.base = { mum.base = {
padding:[0,0], /* Each element inset with this padding on all sides */ pos: null, // If set, puts the cursor to this position before drawing the element
offset:[0,0], offset:[0,0], // Move x,y to the right and down before drawing
pos: null, padding:[0,0], // Pad inwards after drawing, to prepare for the next element
font: "fonts/c64.ttf", font: "fonts/c64.ttf",
selectable: false, selectable: false,
selected: false, selected: false,
font_size: 16, font_size: 16,
scale: 1, scale: 1,
angle: 0, angle: 0,
anchor: [0,1], // where to draw the item from, relative to the cursor. 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
background_image: null, background_image: null,
slice: null, slice: null,
hover: { hover: {
@ -32,13 +33,13 @@ mum.base = {
pos: [0,0], pos: [0,0],
color: Color.white, color: Color.white,
}, },
text_outline: 1, /* outline in pixels */ border: 0, // Draw a border around the element. For text, an outline.
overflow: "wrap", // how to deal with overflow from parent element
wrap: -1,
text_align: "left", /* left, center, right */ text_align: "left", /* left, center, right */
text_shader: null, shader: null, // Use this shader, instead of the engine provided one
overflow: "wrap", // clip, wrap, break
color: Color.white, color: Color.white,
margin: [0,0], /* Distance between elements for things like columns */ opacity:1,
size: null,
width:0, width:0,
height:0, height:0,
max_width: Infinity, max_width: Infinity,
@ -57,37 +58,50 @@ mum.debug = false;
var post = function() {}; var post = function() {};
var posts = []; var posts = [];
mum.style = mum.base; var context = mum.base;
var context = mum.style;
var container = undefined; var container = undefined;
var contexts = []; var contexts = [];
var cursor = [0,0]; var cursor = [0,0];
mum._frame = function()
{
cursor = [0,0];
}
var pre = function(data) var pre = function(data)
{ {
if (data.hide) return true; if (data.hide) return true;
data.__proto__ = mum.style; data.__proto__ = mum.base;
if (context) if (context)
contexts.push(context); contexts.push(context);
context = data; context = data;
if (context.pos) cursor = context.pos.slice(); if (context.pos) cursor = context.pos.slice();
context.drawpos = cursor.slice().add(context.offset);
} }
var end = function() var end = function()
{ {
var old = context; var old = context;
context = contexts.pop(); context = contexts.pop();
cursor = cursor.add(old.padding);
post(old); post(old);
} }
mum.style = function(fn, data)
{
var oldbase = mum.base;
data.__proto__ = mum.base;
mum.base = data;
fn();
mum.base = oldbase;
}
mum.list = function(fn, data = {}) mum.list = function(fn, data = {})
{ {
if (pre(data)) return; if (pre(data)) return;
var aa = [0,1].sub(context.anchor); 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); cursor = cursor.add([context.width,context.height].scale(aa)).add(context.offset).add(context.padding);
posts.push(post); posts.push(post);
@ -101,8 +115,6 @@ mum.list = function(fn, data = {})
r:cursor.x+context.width r:cursor.x+context.width
}); });
cursor.x += context.width/2;
//if (context.background_image) mum.image(null, Object.create(context)) //if (context.background_image) mum.image(null, Object.create(context))
if (context.background_image) { if (context.background_image) {
var imgpos = context.pos.slice(); var imgpos = context.pos.slice();
@ -151,11 +163,10 @@ mum.label = function(str, data = {})
var aa = [0,1].sub(context.anchor); var aa = [0,1].sub(context.anchor);
var pos = cursor.slice(); data.drawpos.y -= (context.bb.t-cursor.y);
pos.y -= (context.bb.t-cursor.y); data.drawpos = data.drawpos.add(context.wh.scale(aa)).add(context.offset);
pos = pos.add(context.wh.scale(aa)).add(context.offset);
context.bb = render.text_bb(str, context.scale, -1, pos); context.bb = render.text_bb(str, context.scale, data.wrap, data.drawpos);
if (context.action && bbox.pointin(context.bb, input.mouse.screenpos())) { if (context.action && bbox.pointin(context.bb, input.mouse.screenpos())) {
if (context.hover) { if (context.hover) {
@ -165,7 +176,7 @@ mum.label = function(str, data = {})
} }
} }
context.bb = render.text(str, pos, context.scale, context.color); context.bb = render.text(str, data.drawpos, context.scale, context.color, data.wrap);
if (show_debug()) if (show_debug())
render.boundingbox(context.bb); render.boundingbox(context.bb);
@ -180,12 +191,18 @@ mum.image = function(path, data = {})
var tex = path; var tex = path;
if (typeof path === 'string') if (typeof path === 'string')
tex = game.texture(path); tex = game.texture(path);
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]));
if (context.slice) if (context.slice)
render.slice9(tex, cursor, context.slice, context.scale); render.slice9(tex, data.drawpos, context.slice, [data.width,data.height]);
else { else {
cursor.y -= tex.height*context.scale; cursor.y -= tex.height*context.scale;
context.bb = render.image(tex, cursor, [context.scale*tex.width, context.scale*tex.height]); context.bb = render.image(tex, data.drawpos, [context.scale*tex.width, context.scale*tex.height]);
} }
end(); end();
@ -220,17 +237,11 @@ mum.button = function(str, data = {padding:[4,4], color:Color.black})
mum.window = function(fn, data = {}) mum.window = function(fn, data = {})
{ {
data = Object.assign({
size:[400,400],
color: Color.black
}, data);
if (pre(data)) return; if (pre(data)) return;
render.rectangle(cursor, cursor.add(context.size), context.color); render.rectangle(cursor, cursor.add(context.size), context.color);
cursor.y += context.height; cursor.y += context.height;
cursor = cursor.add(context.padding); cursor = cursor.add(context.padding);
context.pos = cursor.slice();
fn(); fn();
end(); end();
} }

View file

@ -607,6 +607,8 @@ render.image = function(tex, pos, scale = [tex.width, tex.height], rotation = 0,
return bb; return bb;
} }
// pos is the lower left corner, scale is the width and height
render.slice9 = function(tex, pos, bb, scale = [tex.width,tex.height], color = Color.white) render.slice9 = function(tex, pos, bb, scale = [tex.width,tex.height], color = Color.white)
{ {
var t = os.make_transform(); var t = os.make_transform();

View file

@ -58,9 +58,9 @@ void mesh_add_material(primitive *prim, cgltf_material *mat)
cgltf_buffer_view *buf = img->buffer_view; cgltf_buffer_view *buf = img->buffer_view;
pmat->diffuse = texture_fromdata(buf->buffer->data, buf->size); pmat->diffuse = texture_fromdata(buf->buffer->data, buf->size);
} else { } else {
char *path = makepath(dirname(cpath), img->uri); // char *path = makepath(dirname(cpath), img->uri);
pmat->diffuse = texture_from_file(path); // pmat->diffuse = texture_from_file(path);
free(path); // free(path);
} }
} else } else
pmat->diffuse = texture_from_file("icons/moon.gif"); pmat->diffuse = texture_from_file("icons/moon.gif");

View file

@ -126,17 +126,6 @@ char *dirname(const char *path)
return dir; return dir;
} }
char *makepath(char *dir, char *file)
{
int d = strlen(dir) + strlen(file) + 2;
char *path = malloc(d);
path[0] = 0;
strncat(path, dir, d);
strncat(path, "/", d);
strncat(path, file, d);
return path;
}
char *seprint(char *fmt, ...) char *seprint(char *fmt, ...)
{ {
va_list args; va_list args;

View file

@ -12,7 +12,6 @@ extern int LOADED_GAME;
void resources_init(); void resources_init();
char *get_filename_from_path(char *path, int extension); char *get_filename_from_path(char *path, int extension);
char *dirname(const char *path); char *dirname(const char *path);
char *makepath(char *dir, char *file);
char *str_replace_ext(const char *s, const char *newext); char *str_replace_ext(const char *s, const char *newext);
FILE *res_open(char *path, const char *tag); FILE *res_open(char *path, const char *tag);
char **ls(const char *path); char **ls(const char *path);