fix model/group center (inverted y). add html dom builder

This commit is contained in:
Stewart Allen 2021-12-31 10:44:21 -05:00
commit 3dbd1e85a8
3 changed files with 123 additions and 16 deletions

View file

@ -32,16 +32,19 @@ mesh.group = class MeshGroup {
floor() { floor() {
let b = this.bounds(); let b = this.bounds();
this.move(0, 0, -b.min.z); this.move(0, 0, -b.min.z);
return this;
} }
centerXY() { centerXY() {
let b = this.bounds(); let b = this.bounds();
this.move(-b.center.x, -b.center.y, 0); this.move(-b.center.x, b.center.y, 0);
return this;
} }
move(x = 0, y = 0, z = 0) { move(x = 0, y = 0, z = 0) {
let pos = this.position(); let pos = this.position();
pos.set(pos.x + x, pos.y + y, pos.z + z); pos.set(pos.x + x, pos.y + y, pos.z + z);
return this;
} }
position() { position() {

View file

@ -45,16 +45,19 @@ mesh.model = class MeshModel {
floor() { floor() {
let b = this.bounds(); let b = this.bounds();
this.move(0, 0, -b.min.z); this.move(0, 0, -b.min.z);
return this;
} }
centerXY() { centerXY() {
let b = this.bounds(); let b = this.bounds();
this.move(-b.center.x, -b.center.y, 0); this.move(-b.center.x, b.center.y, 0);
return this;
} }
move(x = 0, y = 0, z = 0) { move(x = 0, y = 0, z = 0) {
let pos = this.position(); let pos = this.position();
pos.set(pos.x + x, pos.y + y, pos.z + z); pos.set(pos.x + x, pos.y + y, pos.z + z);
return this;
} }
position() { position() {

View file

@ -2,22 +2,123 @@
"use strict"; "use strict";
gapp.register('moto.webui'); (function() {
// some ui helpers that need to go into a ui class gapp.register('moto.webui', [
function $(id) { "moto.license", // dep: moto.license
return document.getElementById(id); "add.array", // dep: add.array
]);
let moto = self.moto = self.moto || {};
if (moto.webui) return;
// window ui helpers
Object.assign(self, {
$: (id) => {
return document.getElementById(id);
},
$d: (id, v) => {
$(id).style.display = v;
},
$h: (id, h) => {
$(id).innerHTML = h;
},
estop: (evt) => {
evt.stopPropagation();
evt.preventDefault();
}
});
let nextid = 1;
function build(data, context) {
if (Array.isArray(data)) {
let html = [];
for (let d of data) {
html.appendAll(build(d, context));
}
return html;
}
let { type, attr, innr } = data;
let elid;
let html = [];
let text
html.push(`<${type}`);
let func = {};
for (let [key, val] of Object.entries(attr || {})) {
let tov = typeof val;
if (key === '_') {
text = val;
} else if (key === 'id') {
elid = val;
} else if (tov === 'function') {
func[key] = val;
elid = elid || `_${nextid++}`;
} else if (tov === 'object') {
html.push(` ${key}="${val.join(' ')}"`);
} else {
html.push(` ${key}="${val}"`);
}
}
if (elid) {
html.push(` id="${elid}"`);
context.push({elid, func});
}
html.push('>');
if (innr) {
let snips = [];
if (innr.length) {
for (let i of innr) {
snips.push(build(i, context));
}
} else {
snips.push(build(i, context));
}
html.appendAll(snips.flat());
}
if (text) {
html.push(text);
}
html.push(`</${type}>`);
return html;
} }
function $d(id, v) { // core html builder funtions
$(id).style.display = v; let h = self.h = moto.webui = {
} bind: (el, data, opt = {}) => {
let ctx = [];
let html = build(data, ctx).join('');
if (opt.append) {
el.innerHTML += html;
} else {
el.innerHTML = html;
}
for (let bind of ctx) {
let { elid, func } = bind;
let et = $(elid);
for (let [name, fn] of Object.entries(func)) {
et[name] = fn;
}
}
},
function $h(id, h) { el: (type, attr, innr) => {
$(id).innerHTML = h; if (Array.isArray(attr)) {
} innr = attr;
attr = {};
}
return { type, attr, innr };
}
};
function estop(evt) { // add common element types
evt.stopPropagation(); ["a", "div", "span", "label", "input", "button"].forEach(type => {
evt.preventDefault(); h[type] = (attr, innr) => {
} return h.el(type, attr, innr);
}
});
})();