Add ur chaining

This commit is contained in:
John Alanbrook 2024-04-11 07:38:28 -05:00
parent 33ac36ae5c
commit 0b4231c0b6
4 changed files with 78 additions and 82 deletions

View file

@ -641,62 +641,37 @@ ur {
/* Apply an ur u to an entity e */
/* u is given as */
function apply_ur(u, e) {
function apply_ur(u, ent) {
if (typeof u !== 'string') {
console.warn("Must give u as a string.");
return;
}
var urs = u.split('.');
var config = {};
var topur = ur;
for (var i = 0; i < urs.length; i++) {
topur = topur[urs[i]];
if (!topur) {
console.warn(`Ur given by ${u} does not exist. Stopped at ${urs[i]}.`);
return;
}
if (topur.text) {
var script = Resources.replstrs(topur.text);
use(topur.text, e, script);
}
if (topur.data) {
var jss = Resources.replstrs(topur.data);
Object.merge(config, json.decode(jss));
}
if (!urs.every(u => ur[u])) {
console.error(`Attempted to make ur combo ${u} but not every ur in the chain exists.`);
return;
}
Object.merge(e, config);
}
for (var u of urs) {
var text = u.text;
var data = u.data;
if (typeof text === 'string')
use(text, ent);
else if (Array.isArray(text))
text.forEach(path => use(path,ent));
function file2fqn(file) {
var fqn = file.strip_ext();
if (fqn.folder_same_name())
fqn = fqn.up_path();
fqn = fqn.replace('/', '.');
var topur;
if (topur = Object.access(ur, fqn)) return topur;
var fqnlast = fqn.split('.').last();
if (topur = Object.access(ur, fqn.tolast('.'))) {
topur[fqnlast] = {
name: fqn
};
ur._list.push(fqn);
return Object.access(ur, fqn);
if (typeof data === 'string')
Object.merge(ent, json.decode(Resources.replstrs(data)));
else if (Array.isArray(data)) {
data.forEach(function(path)) {
if (typeof path === 'string')
Object.merge(ent, json.decode(Resources.replstrs(data)));
else if (typeof path === 'object')
Object.merge(ent,path);
});
}
}
fqn = fqnlast;
ur[fqn] = {
name: fqn
};
ur._list.push(fqn);
return ur[fqn];
}
var getur = function(text, data)
@ -712,30 +687,52 @@ var getur = function(text, data)
return ur[urstr];
}
var ur_from_file = function(file) {
var urname = file.name();
if (ur[urname]) {
console.warn(`Tried to make another ur with the name ${urname} from ${file}, but it already exists.`);
return undefined;
}
var newur = {
name: urname
};
ur[urname] = newur;
ur._list.push(urname);
return newur;
}
game.loadurs = function() {
ur = {};
ur._list = [];
/* FIND ALL URS IN A PROJECT */
for (var file of io.glob("**.ur")) {
var urname = file.name();
if (ur[urname]) {
console.warn(`Tried to make another ur with the name ${urname} from ${file}, but it already exists.`);
continue;
}
var newur = ur_from_file(file);
if (!newur) continue;
var urjson = json.decode(io.slurp(file));
urjson.name = urname;
ur[urname] = urjson;
Object.assign(newur, urjson);
}
for (var file of io.glob("**.jso")) {
if (file[0] === '.' || file[0] === '_') continue;
var topur = file2fqn(file);
topur.text = file;
var newur = ur_from_file(file);
if (!newur) continue;
var datastr = file.set_ext(".json");
var data;
if (io.exists(datastr))
data = datastr;
Object.assign(newur, {
text: file,
data: datastr
};
}
for (var file of io.glob("**.json")) {
if (file[0] === '.' || file[0] === '_') continue;
var topur = file2fqn(file);
topur.data = file;
var newur = ur_from_file(file);
if (!newur) continue;
Object.assign(newur, {
data: file
});
}
};

View file

@ -200,7 +200,6 @@ Cmdline.register_order("edit", function() {
use("editorconfig.js");
use("config.js");
render.set_font("fonts/c64.ttf", 8);
console.info(`set font with linegap ${render.font.linegap}`);
editor.enter_editor();
});
}, "Edit the project in this folder. Give it the name of an UR to edit that specific object.", "?UR?");

View file

@ -109,6 +109,26 @@ struct circle_vertex {
float fill;
};
void debug_nextpass()
{
point_sc = point_c;
point_c = 0;
circle_sc = circle_count;
circle_count = 0;
line_sv = line_v;
line_v = 0;
line_sc = line_c;
line_c = 0;
poly_sc = poly_c;
poly_c = 0;
poly_sv = poly_v;
poly_v = 0;
}
/* Writes debug data to buffers, and draws */
void debug_flush(HMM_Mat4 *view)
{
@ -146,26 +166,6 @@ void debug_flush(HMM_Mat4 *view)
debug_nextpass();
}
void debug_nextpass()
{
point_sc = point_c;
point_c = 0;
circle_sc = circle_count;
circle_count = 0;
line_sv = line_v;
line_v = 0;
line_sc = line_c;
line_c = 0;
poly_sc = poly_c;
poly_c = 0;
poly_sv = poly_v;
poly_v = 0;
}
void debug_newframe()
{
point_sc = 0;

View file

@ -288,7 +288,7 @@ const char *esc_color(const char *c, struct rgba *color, struct rgba defc)
struct boundingbox text_bb(const char *text, float scale, float lw, float tracking)
{
if (!use_font) return;
if (!use_font) return cwh2bb((HMM_Vec2){0,0}, (HMM_Vec2){0,0});
struct rgba dummy;
HMM_Vec2 cursor = {0,0};
const char *line, *wordstart;
@ -342,7 +342,7 @@ void check_caret(int caret, int l, HMM_Vec2 pos, float scale, struct rgba color)
int renderText(const char *text, HMM_Vec2 pos, float scale, struct rgba color, float lw, int caret, float tracking) {
if (!use_font) {
YughError("Cannot render text before a font is set.");
return;
return pos.y;
}
int len = strlen(text);