grid-apps-cmms/src/mode/cam/client.js

421 lines
14 KiB
JavaScript
Raw Normal View History

2020-11-05 18:29:04 -05:00
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
"use strict";
(function() {
let KIRI = self.kiri,
BASE = self.base,
2020-11-05 18:29:04 -05:00
CAM = KIRI.driver.CAM,
PRO = CAM.process,
newPolygon = BASE.newPolygon,
2020-11-14 11:12:12 -05:00
newPoint = BASE.newPoint,
2020-11-24 11:01:29 -05:00
isAnimate,
2020-11-14 11:12:12 -05:00
isArrange,
isCamMode,
isParsed,
2020-11-14 11:12:12 -05:00
camStock,
2020-11-25 15:26:24 -05:00
API, FDM, SPACE, STACKS, MODES, VIEWS, UI, UC;
2020-11-05 18:29:04 -05:00
2020-11-25 16:46:51 -05:00
let zaxis = {x: 0, y: 0, z: 1},
func = {};
2020-11-05 18:29:04 -05:00
CAM.init = function(kiri, api) {
2020-11-25 15:26:24 -05:00
FDM = KIRI.driver.FDM;
2020-11-14 11:12:12 -05:00
// console.log({kiri,api})
STACKS = api.const.STACKS;
2020-11-14 11:12:12 -05:00
SPACE = api.const.SPACE;
MODES = api.const.MODES;
VIEWS = api.const.VIEWS;
UI = api.ui;
UC = api.uc;
API = api;
2020-11-05 18:29:04 -05:00
api.event.on("mode.set", (mode) => {
2020-11-14 11:12:12 -05:00
isCamMode = mode === 'CAM';
$('set-tools').style.display = isCamMode ? '' : 'none';
kiri.space.platform.setColor(isCamMode ? 0xeeeeee : 0xcccccc);
updateStock(undefined, 'internal');
if (!isCamMode) func.tclear();
2020-11-14 11:12:12 -05:00
});
api.event.on("view.set", (mode) => {
isArrange = (mode === VIEWS.ARRANGE);
2020-11-24 11:01:29 -05:00
isAnimate = false;
CAM.animate_clear(api);
2020-11-05 18:29:04 -05:00
});
2020-11-14 11:12:12 -05:00
2020-11-05 18:29:04 -05:00
api.event.on("settings.saved", (settings) => {
const proc = settings.process;
2020-11-14 11:12:12 -05:00
// show/hide dots in enabled process pop buttons
// api.ui.camTabs.marker.style.display = proc.camTabsOn ? 'flex' : 'none';
2020-11-05 18:29:04 -05:00
api.ui.camRough.marker.style.display = proc.camRoughOn ? 'flex' : 'none';
api.ui.camDrill.marker.style.display =
proc.camDrillingOn || proc.camDrillReg !== 'none' ? 'flex' : 'none';
api.ui.camOutline.marker.style.display = proc.camOutlineOn ? 'flex' : 'none';
api.ui.camContour.marker.style.display =
proc.camContourXOn || proc.camContourYOn ? 'flex' : 'none';
2020-11-14 11:12:12 -05:00
updateStock(settings, 'settings.saved.internal');
2020-11-05 18:29:04 -05:00
});
2020-11-25 16:46:51 -05:00
api.event.on("settings.load", (settings) => {
if (!isCamMode) return;
restoreTabs(api.widgets.all());
});
2020-11-14 11:12:12 -05:00
api.event.on([
"init-done",
// update stock when modes change?
"slice.end",
"preview.end",
// update stock when preferences change
// "boolean.click",
"boolean.update",
// update stock when objects move
"platform.layout",
"selection.drag",
"selection.move",
// force bounds update, too
"selection.scale",
"selection.rotate"
], updateStock);
api.event.on("preview.end", () => {
isParsed = false;
2020-11-25 15:26:24 -05:00
if (isCamMode && camStock) {
STACKS.getStack("bounds").button("animate", animate);
}
});
api.event.on("code.loaded", (info) => {
if (isCamMode && camStock) {
isParsed = true;
STACKS.getStack("parse", SPACE.platform.world).button("animate", animate);
}
});
2020-11-25 15:26:24 -05:00
api.event.on("button.click", target => {
switch (target) {
case api.ui.tabAdd:
return func.tadd();
case api.ui.tabDun:
return func.tdone();
case api.ui.tabClr:
api.uc.confirm("clear tabs?").then(ok => {
if (ok) func.tclear();
});
break;
}
});
api.event.on("cam.tabs.add", func.tadd = () => {
alert = api.show.alert("&lt;esc&gt; key cancels editing tabs");
api.feature.hover = true;
});
api.event.on("cam.tabs.done", func.tdone = () => {
delbox('tabb');
2020-11-25 15:26:24 -05:00
api.hide.alert(alert);
api.feature.hover = false;
});
api.event.on("cam.tabs.clear", func.tclear = () => {
func.tdone();
API.widgets.all().forEach(widget => {
clearTabs(widget);
});
API.conf.save();
2020-11-25 15:26:24 -05:00
});
api.event.on("slice.begin", () => {
if (isCamMode) {
func.tdone();
2020-11-25 15:26:24 -05:00
}
});
api.event.on("key.esc", () => {
if (isCamMode) {
func.tdone();
}
});
api.event.on("selection.scale", () => {
if (isCamMode) {
func.tclear();
2020-11-25 15:26:24 -05:00
}
});
api.event.on("widget.rotate", rot => {
if (!isCamMode) {
return;
}
const {widget, x, y, z} = rot;
if (x || y) {
2020-11-25 16:46:51 -05:00
clearTabs(widget);
2020-11-25 15:26:24 -05:00
} else {
2020-11-25 16:46:51 -05:00
let tabs = API.widgets.annotate(widget.id).tab || [];
tabs.forEach(rec => {
let { id, pos, rot } = rec;
2020-11-25 16:46:51 -05:00
let tab = widget.tabs[id];
let m4 = new THREE.Matrix4().makeRotationFromEuler(new THREE.Euler(0, 0, z || 0));
// update position vector
let vc = new THREE.Vector3(pos.x, pos.y, pos.z).applyMatrix4(m4);
// update rotation quaternion
rec.rot = new THREE.Quaternion().multiplyQuaternions(
new THREE.Quaternion(rot._x, rot._y, rot._z, rot._w),
new THREE.Quaternion().setFromRotationMatrix(m4)
);
2020-11-25 16:46:51 -05:00
tab.box.geometry.applyMatrix4(m4);
tab.box.position.x = pos.x = vc.x;
tab.box.position.y = pos.y = vc.y;
tab.box.position.z = pos.z = vc.z;
});
SPACE.update();
2020-11-25 15:26:24 -05:00
}
});
api.event.on("mouse.hover.up", int => {
if (!isCamMode) {
return;
}
delbox('tabb');
2020-11-25 15:26:24 -05:00
if (lastTab) {
const {widget, box, id} = lastTab;
widget.adds.remove(box);
widget.mesh.remove(box);
delete widget.tabs[id];
let ta = API.widgets.annotate(widget.id).tab;
let ix = 0;
ta.forEach((rec,i) => {
if (rec.id === id) {
ix = i;
}
});
ta.splice(ix,1);
API.conf.save();
return;
}
if (!iw) return;
let ip = iw.track.pos;
let wa = api.widgets.annotate(iw.id);
let wt = (wa.tab = wa.tab || []);
2020-11-25 16:46:51 -05:00
let pos = {
x: showTab.pos.x - ip.x,
y: -showTab.pos.z - ip.y,
z: showTab.pos.y + ip.z,
}
let id = Date.now();
let { dim, rot } = showTab;
let rec = { pos, dim, rot, id };
2020-11-25 15:26:24 -05:00
wt.push(Object.clone(rec));
addWidgetTab(iw, rec);
API.conf.save();
});
2020-11-25 16:46:51 -05:00
let showTab, lastTab, tab, iw, ic;
2020-11-25 15:26:24 -05:00
api.event.on("mouse.hover", data => {
if (!isCamMode) {
return;
}
delbox('tabb');
2020-11-25 15:26:24 -05:00
const { int, type, point } = data;
const object = int ? int.object : null;
const tab = int ? object.tab : null;
if (lastTab) {
lastTab.box.material.color.r = 0;
lastTab = null;
}
if (tab) {
tab.box.material.color.r = 0.5;
lastTab = tab;
return;
}
if (type !== 'widget') {
iw = null;
return;
}
let n = int.face.normal;
iw = int.object.widget;
ic = int.point;
// only near vertical faces
if (Math.abs(n.z) > 0.1) {
return;
}
2020-11-25 16:46:51 -05:00
showTab = createTabBox(iw, ic, n);
2020-11-25 15:26:24 -05:00
});
2020-11-14 11:12:12 -05:00
};
2020-11-25 16:46:51 -05:00
function createTabBox(iw, ic, n) {
const { track } = iw;
const { stock, bounds, process } = API.conf.get();
const { camTabsWidth, camTabsHeight } = process;
2020-11-26 10:22:18 -05:00
const sz = stock.z || bounds.max.z;
const zp = sz - track.box.d + process.camZBottom - process.camZTopOffset + camTabsHeight / 2;
2020-11-25 16:46:51 -05:00
ic.x += n.x * 2.5; // offset from part
ic.z -= n.y * 2.5; // offset swap z,y
ic.y = zp; // offset swap in world space y,z
const rot = new THREE.Quaternion().setFromAxisAngle(zaxis, Math.atan2(n.y, n.x));
const pos = { x:ic.x, y:ic.y, z:ic.z };
const dim = { x:5, y:camTabsWidth, z:camTabsHeight };
const tab = addbox(pos, 0x0000dd, 'tabb', dim, { rotate: rot });
2020-11-25 16:46:51 -05:00
return { pos, dim, rot, tab, width: camTabsWidth, height: camTabsHeight };
}
function addWidgetTab(widget, rec) {
const { pos, dim, rot, id } = rec;
2020-11-25 15:26:24 -05:00
const tabs = widget.tabs = (widget.tabs || {});
// prevent duplicate restore from repeated settings load calls
if (!tabs[id]) {
pos.box = addbox(
2020-11-25 16:46:51 -05:00
pos, 0x0000dd, id,
dim, { group: widget.mesh, rotate: rot }
2020-11-25 15:26:24 -05:00
);
pos.box.tab = Object.assign({widget, id}, pos);
2020-11-25 15:26:24 -05:00
widget.adds.push(pos.box);
2020-11-25 16:46:51 -05:00
tabs[id] = pos;
2020-11-25 15:26:24 -05:00
}
}
2020-11-25 16:46:51 -05:00
function restoreTabs(widgets) {
widgets.forEach(widget => {
const tabs = API.widgets.annotate(widget.id).tab || [];
tabs.forEach(rec => {
rec = Object.clone(rec);
rec.rot = new THREE.Quaternion(rec.rot._x ,rec.rot._y, rec.rot._z, rec.rot._w);
2020-11-25 16:46:51 -05:00
addWidgetTab(widget, rec);
});
});
}
function clearTabs(widget) {
Object.values(widget.tabs || {}).forEach(rec => {
widget.adds.remove(rec.box);
widget.mesh.remove(rec.box);
});
widget.tabs = {};
delete API.widgets.annotate(widget.id).tab;
}
2020-11-25 15:26:24 -05:00
function addbox() { return FDM.addbox(...arguments)};
function delbox() { return FDM.delbox(...arguments)};
function animate() {
2020-11-24 11:01:29 -05:00
isAnimate = true;
API.widgets.opacity(isParsed ? 0 : 0.75);
2020-11-24 00:48:45 -05:00
API.hide.slider();
STACKS.clear();
2020-11-24 00:11:49 -05:00
CAM.animate(API);
}
function updateStock(args, event) {
2020-11-24 11:01:29 -05:00
if (isAnimate) {
return;
}
STACKS.remove('bounds');
if (isCamMode && isArrange) {
STACKS.clear();
}
2020-11-14 11:12:12 -05:00
if (!isCamMode) {
if (camStock) {
SPACE.platform.remove(camStock);
camStock = null;
}
2020-11-14 11:12:12 -05:00
return;
}
2020-11-14 11:12:12 -05:00
const refresh = (event === "selection.scale" || event === 'selection.rotate');
let settings = API.conf.get();
let widgets = API.widgets.all();
let proc = settings.process;
let enabled = UI.camStockOn.checked;
let offset = UI.camStockOffset.checked;
let stockSet = offset || (proc.camStockX && proc.camStockY && proc.camStockZ > 0);
let topZ = API.platform.top_z();
let delta = 0;
let csox = 0;
let csoy = 0;
let stock = settings.stock = { };
// create/inject cam stock if stock size other than default
if (enabled && stockSet && widgets.length) {
let csx = proc.camStockX;
let csy = proc.camStockY;
let csz = proc.camStockZ;
2020-11-14 11:12:12 -05:00
if (offset) {
let min = { x: Infinity, y: Infinity, z: 0 };
let max = { x: -Infinity, y: -Infinity, z: -Infinity };
widgets.forEach(function(widget) {
let wbnd = widget.getBoundingBox(refresh);
let wpos = widget.track.pos;
min = {
x: Math.min(min.x, wpos.x + wbnd.min.x),
y: Math.min(min.y, wpos.y + wbnd.min.y),
z: 0
};
max = {
x: Math.max(max.x, wpos.x + wbnd.max.x),
y: Math.max(max.y, wpos.y + wbnd.max.y),
z: Math.max(max.z, wbnd.max.z)
};
});
csx += max.x - min.x;
csy += max.y - min.y;
csz += max.z - min.z;
csox = min.x + ((max.x - min.x) / 2);
csoy = min.y + ((max.y - min.y) / 2);
}
if (!camStock) {
let geo = new THREE.BoxGeometry(1, 1, 1);
let mat = new THREE.MeshBasicMaterial({
color: 0x777777,
opacity: 0.2,
transparent: true,
side:THREE.DoubleSide
});
camStock = new THREE.Mesh(geo, mat);
camStock.renderOrder = 2;
SPACE.platform.add(camStock);
}
stock = settings.stock = {
x: csx,
y: csy,
z: csz,
center: {
x: csox,
y: csoy,
z: csz / 2
}
};
camStock.scale.x = csx + 0.005;
camStock.scale.y = csy + 0.005;
camStock.scale.z = csz + 0.005;
camStock.position.x = csox;
camStock.position.y = csoy;
camStock.position.z = csz / 2;
delta = csz - topZ;
} else if (camStock) {
SPACE.platform.remove(camStock);
camStock = null;
delta = 0;
}
const {x, y, z} = stock;
2020-11-14 12:07:05 -05:00
if (x && y && z && !STACKS.getStack('bounds')) {
2020-11-14 11:12:12 -05:00
const render = new KIRI.Render().setLayer('bounds', { face: 0xaaaaaa, line: 0xaaaaaa });
const stack = STACKS.create('bounds', SPACE.platform.world);
stack.add(render.addPolys([
newPolygon().centerRectangle({x:csox, y:csoy, z:0}, x, y),
newPolygon().centerRectangle({x:csox, y:csoy, z}, x, y)
], { thin: true } ));
const hx = x/2, hy = y/2;
2020-11-26 10:22:18 -05:00
const sz = stock.z || 0;
2020-11-14 11:12:12 -05:00
stack.add(render.addLines([
2020-11-26 10:22:18 -05:00
newPoint(csox + hx, csoy - hy, 0), newPoint(csox + hx, csoy - hy, sz),
newPoint(csox + hx, csoy + hy, 0), newPoint(csox + hx, csoy + hy, sz),
newPoint(csox - hx, csoy - hy, 0), newPoint(csox - hx, csoy - hy, sz),
newPoint(csox - hx, csoy + hy, 0), newPoint(csox - hx, csoy + hy, sz),
2020-11-14 11:12:12 -05:00
], { thin: true }));
}
API.platform.update_top_z(delta);
API.platform.update_origin();
SPACE.update();
}
2020-11-05 18:29:04 -05:00
})();