fix layout and 9slice bug

This commit is contained in:
John Alanbrook 2024-10-19 23:25:07 -05:00
parent 90eba900e3
commit db0c0cbc7a
2 changed files with 37 additions and 10 deletions

View file

@ -37,7 +37,7 @@ var clay_base = {
padding:0, padding:0,
margin:0, margin:0,
offset:[0,0], offset:[0,0],
size:[0,0] size:undefined,
}; };
var root_item; var root_item;
@ -185,22 +185,37 @@ var add_item = function(config)
return item; return item;
} }
clay.image = function(path, config = {}) function rectify_configs(config_array)
{ {
config.__proto__ = clay_base; if (config_array.length === 0)
config.image = path; config_array = [{}];
for (var i = config_array.length-1; i > 0; i--)
config_array[i].__proto__ = config_array[i-1];
config_array[0].__proto__ = clay_base;
var cleanobj = Object.create(config_array[config_array.length-1]);
return cleanobj;
}
clay.image = function(path, ...configs)
{
var config = rectify_configs(configs);
var image = game.texture(path); var image = game.texture(path);
config.image = path;
config.size ??= [image.texture.width, image.texture.height]; config.size ??= [image.texture.width, image.texture.height];
add_item(config); add_item(config);
} }
clay.text = function(str, config = {}) clay.text = function(str, ...configs)
{ {
config.__proto__ = clay_base; var config = rectify_configs(configs);
var tsize = render.text_size(str, config.font); var tsize = render.text_size(str, config.font);
config.size ??= [0,0];
config.size = config.size.map((x,i) => Math.max(x, tsize[i])); config.size = config.size.map((x,i) => Math.max(x, tsize[i]));
add_item(config);
config.text = str; config.text = str;
add_item(config);
} }
/* /*
@ -226,14 +241,14 @@ clay.button = function(str, action, config = {})
layout.draw_commands = function(cmds, pos = [0,0]) layout.draw_commands = function(cmds, pos = [0,0])
{ {
var mousepos = input.mouse.screenpos(); var mousepos = prosperon.camera.screen2hud(input.mouse.screenpos());
for (var cmd of cmds) { for (var cmd of cmds) {
cmd.boundingbox.x += pos.x; cmd.boundingbox.x += pos.x;
cmd.boundingbox.y += pos.y; cmd.boundingbox.y += pos.y;
cmd.content.x += pos.x; cmd.content.x += pos.x;
cmd.content.y += pos.y; cmd.content.y += pos.y;
var config = cmd.config; var config = cmd.config;
if (config.hovered && geometry.rect_point_inside(cmd.content, mousepos)) { if (config.hovered && geometry.rect_point_inside(cmd.boundingbox, mousepos)) {
config.hovered.__proto__ = config; config.hovered.__proto__ = config;
config = config.hovered; config = config.hovered;
} }

View file

@ -558,7 +558,10 @@ function shader_apply_material(shader, material = {}, old = {}) {
var bcache = new WeakMap(); var bcache = new WeakMap();
function sg_bind(mesh, ssbo) { function sg_bind(mesh, ssbo) {
if (cur.bind && cur.mesh === mesh && cur.ssbo === ssbo) { if (cur.bind && cur.mesh === mesh && cur.ssbo === ssbo) {
cur.bind.ssbo = [ssbo]; if (ssbo)
cur.bind.ssbo = [ssbo];
else
cur.bind.ssbo = undefined;
cur.bind.images = cur.images; cur.bind.images = cur.images;
cur.bind.samplers = cur.samplers; cur.bind.samplers = cur.samplers;
render.setbind(cur.bind); render.setbind(cur.bind);
@ -1226,6 +1229,14 @@ prosperon.gizmos = function () {
}); });
}; };
function screen2hud(pos)
{
var campos = this.screen2cam(pos);
campos = campos.scale(this.size);
campos.y -= this.size.y;
return campos;
}
prosperon.make_camera = function () { prosperon.make_camera = function () {
var cam = world.spawn(); var cam = world.spawn();
cam.near = 1; cam.near = 1;
@ -1237,6 +1248,7 @@ prosperon.make_camera = function () {
cam.mode = "stretch"; cam.mode = "stretch";
cam.screen2world = camscreen2world; cam.screen2world = camscreen2world;
cam.screen2cam = screen2cam; cam.screen2cam = screen2cam;
cam.screen2hud = screen2hud;
cam.extents = camextents; cam.extents = camextents;
cam.view = camviewport; cam.view = camviewport;
return cam; return cam;