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

View file

@ -200,7 +200,6 @@ Cmdline.register_order("edit", function() {
use("editorconfig.js"); use("editorconfig.js");
use("config.js"); use("config.js");
render.set_font("fonts/c64.ttf", 8); render.set_font("fonts/c64.ttf", 8);
console.info(`set font with linegap ${render.font.linegap}`);
editor.enter_editor(); editor.enter_editor();
}); });
}, "Edit the project in this folder. Give it the name of an UR to edit that specific object.", "?UR?"); }, "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; 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 */ /* Writes debug data to buffers, and draws */
void debug_flush(HMM_Mat4 *view) void debug_flush(HMM_Mat4 *view)
{ {
@ -146,26 +166,6 @@ void debug_flush(HMM_Mat4 *view)
debug_nextpass(); 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() void debug_newframe()
{ {
point_sc = 0; 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) 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; struct rgba dummy;
HMM_Vec2 cursor = {0,0}; HMM_Vec2 cursor = {0,0};
const char *line, *wordstart; 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) { int renderText(const char *text, HMM_Vec2 pos, float scale, struct rgba color, float lw, int caret, float tracking) {
if (!use_font) { if (!use_font) {
YughError("Cannot render text before a font is set."); YughError("Cannot render text before a font is set.");
return; return pos.y;
} }
int len = strlen(text); int len = strlen(text);