Mum padding and text wrap; save as works

This commit is contained in:
John Alanbrook 2023-10-06 17:38:49 +00:00
parent 272719736c
commit 9701cd2b4a
8 changed files with 117 additions and 56 deletions

View file

@ -428,6 +428,8 @@ polygon2d.inputs['C-lm'] = function() {
this.points.push(this.gameobject.world2this(Mouse.worldpos)); this.points.push(this.gameobject.world2this(Mouse.worldpos));
}; };
polygon2d.inputs['C-lm'].doc = "Add a point to location of mouse."; polygon2d.inputs['C-lm'].doc = "Add a point to location of mouse.";
polygon2d.inputs.lm = function(){};
polygon2d.inputs.lm.released = function(){};
polygon2d.inputs['S-lm'] = function() { polygon2d.inputs['S-lm'] = function() {
var idx = grab_from_points(Mouse.worldpos, this.points.map(p => this.gameobject.this2world(p)), 25); var idx = grab_from_points(Mouse.worldpos, this.points.map(p => this.gameobject.this2world(p)), 25);

View file

@ -40,7 +40,7 @@ var editor = {
/* Tries to select id */ /* Tries to select id */
do_select(go) { do_select(go) {
var obj = go >= 0 ? Game.object(go) : undefined; var obj = go >= 0 ? Game.object(go) : undefined;
if (!obj || !obj.selectable) return undefined; if (!obj || !obj._ed.selectable) return undefined;
if (obj.level !== this.edit_level) { if (obj.level !== this.edit_level) {
var testlevel = obj.level; var testlevel = obj.level;
@ -351,7 +351,7 @@ var editor = {
this.edit_level = Primum.spawn(ur.arena); this.edit_level = Primum.spawn(ur.arena);
// this.edit_level.toString = function() { return "desktop"; }; // this.edit_level.toString = function() { return "desktop"; };
editor.edit_level.selectable = false; editor.edit_level._ed.selectable = false;
}, },
_sel_comp: undefined, _sel_comp: undefined,
@ -467,13 +467,13 @@ var editor = {
} }
editor.edit_level.objects.forEach(function(obj) { editor.edit_level.objects.forEach(function(obj) {
if (!obj.selectable) if (!obj._ed.selectable)
GUI.image("icons/icons8-lock-16.png", world2screen(obj.worldpos())); GUI.image("icons/icons8-lock-16.png", world2screen(obj.worldpos()));
}); });
Debug.draw_grid(1, editor_config.grid_size, Color.Editor.grid.alpha(0.3)); Debug.draw_grid(1, editor_config.grid_size, Color.Editor.grid.alpha(0.3));
var startgrid = screen2world([-20,Window.height]).map(function(x) { return Math.snap(x, editor_config.grid_size); }); var startgrid = screen2world([-20,0]).map(function(x) { return Math.snap(x, editor_config.grid_size); });
var endgrid = screen2world([Window.width, 0]); var endgrid = screen2world([Window.width, Window.height]);
var w_step = Math.round(editor_config.ruler_mark_px/Window.width * (endgrid.x-startgrid.x)/editor_config.grid_size)*editor_config.grid_size; var w_step = Math.round(editor_config.ruler_mark_px/Window.width * (endgrid.x-startgrid.x)/editor_config.grid_size)*editor_config.grid_size;
if (w_step === 0) w_step = editor_config.grid_size; if (w_step === 0) w_step = editor_config.grid_size;
@ -539,6 +539,7 @@ var editor = {
lvl_history: [], lvl_history: [],
load(file) { load(file) {
Log.warn("LOADING " + file);
var ur = prototypes.get_ur(file); var ur = prototypes.get_ur(file);
if (!ur) return; if (!ur) return;
var obj = editor.edit_level.spawn(ur); var obj = editor.edit_level.spawn(ur);
@ -583,15 +584,24 @@ var editor = {
if (curur) { if (curur) {
notifypanel.action = editor.saveas; notifypanel.action = editor.saveas;
this.openpanel(gen_notify("Entity already exists with that name. Overwrite?", this.saveas.bind(this, sub))); this.openpanel(gen_notify("Entity already exists with that name. Delete first."));
} else { } else {
var path = sub.replaceAll('.', '/') + ".json"; var path = sub.replaceAll('.', '/') + ".json";
var saveobj = obj.level_obj(); var saveobj = obj.level_obj();
IO.slurpwrite(JSON.stringify(saveobj,null,1), path); IO.slurpwrite(JSON.stringify(saveobj,null,1), path);
var t = obj.transform();
if (obj === editor.edit_level) {
obj.clear();
var nobj = editor.edit_level.spawn(sub);
editor.selectlist = [nobj];
return;
}
var t = obj.transform();
obj.kill(); obj.kill();
editor.unselect(); editor.unselect();
editor.load(sub); obj = editor.load(sub);
obj.pos = t.pos; obj.pos = t.pos;
obj.angle = t.angle; obj.angle = t.angle;
} }
@ -747,6 +757,10 @@ editor.inputs['M-p'] = function() {
editor.inputs['M-p'].doc = "Do one time step, pausing if necessary."; editor.inputs['M-p'].doc = "Do one time step, pausing if necessary.";
editor.inputs['C-M-p'] = function() { editor.inputs['C-M-p'] = function() {
if (!Game.playing()) {
editor.start_play_ed();
Game.editor_mode(false);
}
Log.warn(`Starting edited level ...`); Log.warn(`Starting edited level ...`);
}; };
editor.inputs['C-M-p'].doc = "Start game from currently edited level."; editor.inputs['C-M-p'].doc = "Start game from currently edited level.";
@ -811,10 +825,10 @@ editor.inputs['C-z'].doc = "Undo the last change made.";
editor.inputs['C-S-z'] = function() { editor.redo(); }; editor.inputs['C-S-z'] = function() { editor.redo(); };
editor.inputs['C-S-z'].doc = "Redo the last undo."; editor.inputs['C-S-z'].doc = "Redo the last undo.";
editor.inputs.t = function() { editor.selectlist.forEach(function(x) { x.selectable = false; }); }; editor.inputs.t = function() { editor.selectlist.forEach(function(x) { x._ed.selectable = false; }); };
editor.inputs.t.doc = "Lock selected objects to make them non selectable."; editor.inputs.t.doc = "Lock selected objects to make them non selectable.";
editor.inputs['M-t'] = function() { editor.edit_level.objects.forEach(function(x) { x.selectable = true; }); }; editor.inputs['M-t'] = function() { editor.edit_level.objects.forEach(function(x) { x._ed.selectable = true; }); };
editor.inputs['M-t'].doc = "Unlock all objects in current level."; editor.inputs['M-t'].doc = "Unlock all objects in current level.";
editor.inputs['C-n'] = function() { editor.inputs['C-n'] = function() {
@ -1261,19 +1275,28 @@ var inputpanel = {
pos:[100,Window.height-50], pos:[100,Window.height-50],
wh:[350,600], wh:[350,600],
anchor: [0,1], anchor: [0,1],
padding:[5,-15],
gui() { gui() {
var win = Mum.window({width:this.wh.x,height:this.wh.y, color:Color.black.alpha(0.1), anchor:this.anchor}); this.win = Mum.window({width:this.wh.x,height:this.wh.y, color:Color.black.alpha(0.1), anchor:this.anchor, padding:this.padding});
var itms = this.guibody(); var itms = this.guibody();
if (!Array.isArray(itms)) itms = [itms]; if (!Array.isArray(itms)) itms = [itms];
win.items = itms; if (this.title)
win.draw(this.pos.slice()); this.win.items = [
Mum.column({items: [
Mum.text({str:this.title}),
...itms
]})
];
else
this.win.items = itms;
this.win.draw(this.pos.slice());
}, },
guibody() { guibody() {
return [ return [
Mum.text({str:this.value, color:Color.green}), Mum.text({str:this.value, color:Color.green}),
// Mum.button({str:"Submit", action:this.submit}) Mum.button({str:"SUBMIT", action:this.submit.bind(this)})
]; ];
}, },
@ -1339,7 +1362,7 @@ inputpanel.inputs.char = function(c) {
this.keycb(); this.keycb();
} }
inputpanel.inputs['C-d'] = function() { this.value = this.value.slice(0,this.caret) + this.value.slice(this.caret+1); }; inputpanel.inputs['C-d'] = function() { this.value = this.value.slice(0,this.caret) + this.value.slice(this.caret+1); };
inputpanel.inputs.tab = function() { this.value = tab_complete(this.value, this.assets); } inputpanel.inputs.tab = function() { this.value = tab_complete(this.value, this.assets); this.caret = this.value.length;}
inputpanel.inputs.escape = function() { this.close(); } inputpanel.inputs.escape = function() { this.close(); }
inputpanel.inputs['C-b'] = function() { inputpanel.inputs['C-b'] = function() {
if (this.caret === 0) return; if (this.caret === 0) return;
@ -1411,11 +1434,12 @@ function proto_children(name) {
load("scripts/textedit.js"); load("scripts/textedit.js");
var replpanel = Object.copy(inputpanel, { var replpanel = Object.copy(inputpanel, {
title: "REPL", title: "",
closeonsubmit:false, closeonsubmit:false,
wh: [700,300], wh: [700,300],
pos: [50,50], pos: [50,50],
anchor: [0,0], anchor: [0,0],
padding: [0,0],
guibody() { guibody() {
var log = cmd(84); var log = cmd(84);
@ -1649,6 +1673,7 @@ var openlevelpanel = Object.copy(inputpanel, {
start() { start() {
this.allassets = prototypes.list.sort(); this.allassets = prototypes.list.sort();
this.assets = this.allassets.slice(); this.assets = this.allassets.slice();
this.caret = 0;
var click_ur = function(btn) { var click_ur = function(btn) {
Log.warn(btn.str); Log.warn(btn.str);
this.value = btn.str; this.value = btn.str;
@ -1659,7 +1684,7 @@ var openlevelpanel = Object.copy(inputpanel, {
this.mumlist = []; this.mumlist = [];
this.assets.forEach(function(x) { this.assets.forEach(function(x) {
this.mumlist[x] = Mum.text({str:x, action:click_ur,selectable: true, hovered:{color:Color.red}}); this.mumlist[x] = Mum.button({str:x, action:click_ur});
}, this); }, this);
}, },
@ -1680,7 +1705,8 @@ var openlevelpanel = Object.copy(inputpanel, {
}); });
var saveaspanel = Object.copy(inputpanel, { var saveaspanel = Object.copy(inputpanel, {
title: "save level as", get title() { return `save level as: ${this.stem}.`; },
action() { action() {
var savename = ""; var savename = "";
if (this.stem) savename += this.stem + "."; if (this.stem) savename += this.stem + ".";
@ -1712,17 +1738,10 @@ var notifypanel = Object.copy(inputpanel, {
}, },
guibody() { guibody() {
Nuke.label(this.msg); return Mum.column({items: [
Nuke.newline(2); Mum.text({str:this.msg}),
if (Nuke.button("OK")) { Mum.button({str:"OK", action:this.close.bind(this)})
if ('yes' in this) ]});
this.yes();
this.close();
}
},
input_n_pressed() {
this.close();
}, },
}); });
@ -1817,7 +1836,7 @@ var entitylistpanel = Object.copy(inputpanel, {
} }
x.visible = Nuke.checkbox(x.visible); x.visible = Nuke.checkbox(x.visible);
x.selectable = Nuke.checkbox(x.selectable); x._ed.selectable = Nuke.checkbox(x._ed.selectable);
if (editor.selectlist.includes(x)) Nuke.label("T"); else Nuke.label("F"); if (editor.selectlist.includes(x)) Nuke.label("T"); else Nuke.label("F");
}); });
@ -1845,14 +1864,12 @@ limited_editor.inputs['M-p'] = function()
limited_editor.inputs['C-q'] = function() limited_editor.inputs['C-q'] = function()
{ {
Game.stop(); Game.stop();
game.stop();
Sound.killall(); Sound.killall();
Player.players[0].uncontrol(limited_editor); Player.players[0].uncontrol(limited_editor);
Player.players[0].control(editor); Player.players[0].control(editor);
Register.gui.register(editor.ed_gui, editor); Register.gui.register(editor.ed_gui, editor);
Debug.register_call(editor.ed_debug, editor); Debug.register_call(editor.ed_debug, editor);
// World.kill(); Primum.clear_all();
World.clear_all();
editor.load_json(editor.stash); editor.load_json(editor.stash);
Game.view_camera(editor.camera); Game.view_camera(editor.camera);
} }

View file

@ -693,13 +693,15 @@ var preprimum = {};
preprimum.objects = {}; preprimum.objects = {};
preprimum.worldpos = function() { return [0,0]; }; preprimum.worldpos = function() { return [0,0]; };
preprimum.worldangle = function() { return 0; }; preprimum.worldangle = function() { return 0; };
preprimum.scale = 1;
preprimum.gscale = function() { return 1; };
preprimum.pos = [0,0]; preprimum.pos = [0,0];
preprimum.angle = 0; preprimum.angle = 0;
var World = gameobject.make(preprimum); var World = gameobject.make(preprimum);
var Primum = World; var Primum = World;
Primum.level = undefined; Primum.level = undefined;
Primum.toString = function() { return "Primum"; }; Primum.toString = function() { return "Primum"; };
Primum.selectable = false; Primum._ed.selectable = false;
World.reparent = function(parent) { Log.warn("Cannot reparent the Primum."); } World.reparent = function(parent) { Log.warn("Cannot reparent the Primum."); }
/* Load configs */ /* Load configs */

View file

@ -12,8 +12,20 @@ function grab_from_points(pos, points, slop) {
var gameobject = { var gameobject = {
impl: { impl: {
get scale() { return cmd(103, this.body); }, clear() {
this.objects.forEach(function(x) {
x.kill();
});
this.objects = {};
},
gscale() { return cmd(103,this.body); },
get scale() {
if (!this.level) return this.gscale();
return this.gscale()/this.level.gscale();
},
set scale(x) { set scale(x) {
if (this.level)
x *= this.level.gscale();
var pct = x/this.scale; var pct = x/this.scale;
cmd(36, this.body, x); cmd(36, this.body, x);
@ -260,6 +272,7 @@ var gameobject = {
json_obj() { json_obj() {
var d = ediff(this,this.__proto__); var d = ediff(this,this.__proto__);
if (!d) return {};
delete d.pos; delete d.pos;
delete d.angle; delete d.angle;
delete d.velocity; delete d.velocity;
@ -381,7 +394,10 @@ var gameobject = {
Object.mixin(obj, gameobject.impl); Object.mixin(obj, gameobject.impl);
Object.hide(obj, 'components'); Object.hide(obj, 'components');
Object.hide(obj, 'objects'); Object.hide(obj, 'objects');
obj._ed = {}; obj._ed = {
selectable: true,
dirty: false,
};
Object.hide(obj, '_ed'); Object.hide(obj, '_ed');
obj.ur = this.toString(); obj.ur = this.toString();
Object.hide(obj,'ur'); Object.hide(obj,'ur');
@ -428,10 +444,8 @@ var gameobject = {
Log.warn(`No object with name ${name}. Could not rename to ${newname}.`); Log.warn(`No object with name ${name}. Could not rename to ${newname}.`);
return; return;
} }
if (this.objects[newname]) { if (this.objects[newname])
Log.warn(`Already an object with name ${newname}.`);
return; return;
}
this.objects[newname] = this.objects[name]; this.objects[newname] = this.objects[name];
delete this.objects[name]; delete this.objects[name];
@ -556,6 +570,7 @@ prototypes.from_obj = function(name, obj)
{ {
var newur = Object.copy(gameobject, obj); var newur = Object.copy(gameobject, obj);
prototypes.ur[name] = newur; prototypes.ur[name] = newur;
prototypes.list.push(name);
newur.toString = function() { return name; }; newur.toString = function() { return name; };
return prototypes.ur[name]; return prototypes.ur[name];
} }
@ -695,4 +710,3 @@ prototypes.resani = function(ur, path)
} }
return restry; return restry;
} }

View file

@ -114,9 +114,11 @@ var Mum = {
}, },
text_outline: 1, /* outline in pixels */ text_outline: 1, /* outline in pixels */
color: Color.white, color: Color.white,
margin: [5,5], /* Distance between elements for things like columns */ margin: [0,0], /* Distance between elements for things like columns */
width: 0, width: 0,
height: 0, height: 0,
max_width: Infinity,
max_height: Infinity,
image_repeat: false, image_repeat: false,
image_repeat_offset: [0,0], image_repeat_offset: [0,0],
debug: false, /* set to true to draw debug boxes */ debug: false, /* set to true to draw debug boxes */
@ -136,17 +138,20 @@ var Mum = {
extend(def) { extend(def) {
var n = Object.create(this); var n = Object.create(this);
Object.assign(n, def); Object.assign(n, def);
return function(def) { var fn = function(def) {
var p = n.make(def); var p = n.make(def);
p.prestart(); p.prestart();
p.start(); p.start();
return p; return p;
}; };
fn._int = n;
return fn;
}, },
} }
Mum.text = Mum.extend({ Mum.text = Mum.extend({
draw(cursor) { draw(cursor, cnt) {
cnt ??= Mum;
if (this.hide) return; if (this.hide) return;
if (this.selectable) gui_controls.options.push_unique(this); if (this.selectable) gui_controls.options.push_unique(this);
this.caret ??= -1; this.caret ??= -1;
@ -158,10 +163,13 @@ Mum.text = Mum.extend({
*/ */
var params = this.selected ? this.hovered : this; var params = this.selected ? this.hovered : this;
this.width = Math.min(params.max_width, cnt.max_width);
this.calc_bb(cursor); this.calc_bb(cursor);
this.height = this.wh.y;
var aa = [0,1].sub(params.anchor); var aa = [0,1].sub(params.anchor);
var pos = cursor.add(params.wh.scale(aa)).add(params.offset); var pos = cursor.add(params.wh.scale(aa)).add(params.offset);
ui_text(params.str, pos, params.font_size, params.color, params.width, params.caret); ui_text(params.str, pos, params.font_size, params.color, this.width, params.caret);
}, },
update_bb(cursor) { update_bb(cursor) {
@ -179,22 +187,34 @@ Mum.text = Mum.extend({
}, },
}); });
Mum.button = Mum.text._int.extend({
selectable: true,
color: Color.blue,
hovered:{
color: Color.red
},
action() { Log.warn("Button has no action."); },
});
Mum.window = Mum.extend({ Mum.window = Mum.extend({
start() { start() {
this.wh = [this.width, this.height]; this.wh = [this.width, this.height];
this.bb = cwh2bb([0,0], this.wh); this.bb = cwh2bb([0,0], this.wh);
}, },
draw(pos) { draw(cursor, cnt) {
var p = pos.sub(this.wh.scale(this.anchor)); cnt ??= Mum;
var p = cursor.sub(this.wh.scale(this.anchor)).add(this.padding);
GUI.window(p,this.wh, this.color); GUI.window(p,this.wh, this.color);
this.bb = bl2bb(p, this.wh); this.bb = bl2bb(p, this.wh);
var pos = [this.bb.l, this.bb.t];
GUI.flush(); GUI.flush();
GUI.scissor(p.x,p.y,this.wh.x,this.wh.y); GUI.scissor(p.x,p.y,this.wh.x,this.wh.y);
this.max_width = this.width;
var pos = [this.bb.l, this.bb.t].add(this.padding);
this.items.forEach(function(item) { this.items.forEach(function(item) {
if (item.hide) return; if (item.hide) return;
item.draw(pos.slice()); item.draw(pos.slice(),this);
}); }, this);
GUI.flush(); GUI.flush();
GUI.scissor_win(); GUI.scissor_win();
}, },
@ -229,14 +249,15 @@ Mum.image = Mum.extend({
}); });
Mum.column = Mum.extend({ Mum.column = Mum.extend({
draw(cursor) { draw(cursor, cnt) {
if (this.hide) return; if (this.hide) return;
cursor = cursor.add(this.offset); cursor = cursor.add(this.offset);
this.max_width = cnt.width;
this.items.forEach(function(item) { this.items.forEach(function(item) {
if (item.hide) return; if (item.hide) return;
item.draw(cursor); item.draw(cursor, this);
cursor.y -= item.wh.y; cursor.y -= item.height;
cursor.y -= this.padding.y; cursor.y -= this.padding.y;
}, this); }, this);
}, },

View file

@ -79,8 +79,9 @@ void font_init() {
.label = "text buffer" .label = "text buffer"
}); });
// font = MakeFont("fonts/LessPerfectDOSVGA.ttf", 16); font = MakeFont("fonts/LessPerfectDOSVGA.ttf", 16);
font = MakeFont("fonts/c64.ttf", 8); // font = MakeFont("fonts/c64.ttf", 8);
// font = MakeFont("fonts/teenytinypixels.ttf", 16);
bind_text.fs.images[0] = font->texID; bind_text.fs.images[0] = font->texID;
bind_text.fs.samplers[0] = sg_make_sampler(&(sg_sampler_desc){}); bind_text.fs.samplers[0] = sg_make_sampler(&(sg_sampler_desc){});
} }
@ -138,7 +139,7 @@ struct sFont *MakeFont(const char *fontfile, int height) {
stbtt_GetFontVMetrics(&fontinfo, &newfont->ascent, &newfont->descent, &newfont->linegap); stbtt_GetFontVMetrics(&fontinfo, &newfont->ascent, &newfont->descent, &newfont->linegap);
newfont->emscale = stbtt_ScaleForMappingEmToPixels(&fontinfo, 16); newfont->emscale = stbtt_ScaleForMappingEmToPixels(&fontinfo, 16);
newfont->linegap = (newfont->ascent - newfont->descent) * newfont->emscale; newfont->linegap = (newfont->ascent - newfont->descent) * 1.5*newfont->emscale/2;
newfont->texID = sg_make_image(&(sg_image_desc){ newfont->texID = sg_make_image(&(sg_image_desc){
.type = SG_IMAGETYPE_2D, .type = SG_IMAGETYPE_2D,

View file

@ -151,6 +151,10 @@ struct wav *make_sound(const char *wav) {
struct wav mwav; struct wav mwav;
long rawlen; long rawlen;
void *raw = slurp_file(wav, &rawlen); void *raw = slurp_file(wav, &rawlen);
if (!raw) {
YughError("Could not find file %s.", wav);
return;
}
if (!strcmp(ext, "wav")) if (!strcmp(ext, "wav"))
mwav.data = drwav_open_memory_and_read_pcm_frames_f32(raw, rawlen, &mwav.ch, &mwav.samplerate, &mwav.frames, NULL); mwav.data = drwav_open_memory_and_read_pcm_frames_f32(raw, rawlen, &mwav.ch, &mwav.samplerate, &mwav.frames, NULL);

View file

@ -112,7 +112,7 @@ void seghandle(int sig) {
if (strsignal(sig)) if (strsignal(sig))
YughCritical("CRASH! Signal: %s.", strsignal(sig)); YughCritical("CRASH! Signal: %s.", strsignal(sig));
// print_stacktrace(); js_stacktrace();
exit(1); exit(1);
#endif #endif