2017-12-18 22:04:17 +01:00
|
|
|
namespace pxsim.visuals {
|
|
|
|
export abstract class View {
|
|
|
|
protected element: SVGGElement;
|
|
|
|
protected rendered = false;
|
|
|
|
protected visible = false;
|
2017-12-22 23:00:23 +01:00
|
|
|
protected selected: boolean;
|
2017-12-18 22:04:17 +01:00
|
|
|
protected width: number = 0;
|
2017-12-22 23:00:23 +01:00
|
|
|
protected height: number = 0;
|
2017-12-18 22:04:17 +01:00
|
|
|
protected left: number = 0;
|
|
|
|
protected top: number = 0;
|
|
|
|
protected scaleFactor: number = 1;
|
|
|
|
|
2017-12-22 23:00:23 +01:00
|
|
|
private changed: boolean;
|
|
|
|
private hover: boolean = false;
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
protected theme: IBoardTheme;
|
|
|
|
|
2017-12-22 23:00:23 +01:00
|
|
|
protected abstract buildDom(): SVGElement;
|
2017-12-18 22:04:17 +01:00
|
|
|
public abstract getInnerWidth(): number;
|
|
|
|
public abstract getInnerHeight(): number;
|
|
|
|
|
|
|
|
public inject(parent: SVGElement, width?: number, visible = true) {
|
|
|
|
this.width = width;
|
|
|
|
parent.appendChild(this.getView());
|
|
|
|
|
|
|
|
if (visible) {
|
|
|
|
this.visible = true;
|
|
|
|
this.onComponentInjected();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public getWidth() {
|
|
|
|
return this.scaleFactor == undefined ? this.getInnerWidth() : this.getInnerWidth() * this.scaleFactor;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getHeight() {
|
|
|
|
return this.scaleFactor == undefined ? this.getInnerHeight() : this.getInnerHeight() * this.scaleFactor;
|
|
|
|
}
|
|
|
|
|
|
|
|
public onComponentInjected() {
|
|
|
|
// To be overridden by sub class
|
|
|
|
}
|
|
|
|
|
|
|
|
public onComponentVisible() {
|
|
|
|
// To be overridden by sub class
|
|
|
|
}
|
|
|
|
|
|
|
|
public onComponentHidden() {
|
|
|
|
// To be overridden by sub class
|
|
|
|
}
|
|
|
|
|
|
|
|
public translate(x: number, y: number, applyImmediately = true) {
|
|
|
|
this.left = x;
|
|
|
|
this.top = y;
|
|
|
|
|
|
|
|
if (applyImmediately) {
|
|
|
|
this.updateTransform();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public scale(scaleFactor: number, applyImmediately = true) {
|
|
|
|
this.scaleFactor = scaleFactor;
|
|
|
|
|
|
|
|
if (applyImmediately) {
|
|
|
|
this.updateTransform();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public updateState() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public updateTheme(theme: IBoardTheme) {
|
|
|
|
this.theme = theme;
|
|
|
|
this.updateThemeCore();
|
|
|
|
}
|
|
|
|
|
|
|
|
public updateThemeCore() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public setVisible(visible: boolean) {
|
|
|
|
if (this.rendered) {
|
|
|
|
this.getView().style.display = visible ? 'block' : 'none';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public hasClick() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private onClickHandler: (ev: any) => void;
|
2017-12-22 23:00:23 +01:00
|
|
|
public registerClick(handler: (ev: any) => void, zoom?: boolean) {
|
2017-12-18 22:04:17 +01:00
|
|
|
this.onClickHandler = handler;
|
2017-12-22 23:00:23 +01:00
|
|
|
if (zoom) {
|
|
|
|
this.getView().addEventListener(pointerEvents.up, (ev: any) => {
|
|
|
|
if (!this.getSelected()) {
|
|
|
|
this.onClickHandler(ev);
|
|
|
|
this.setHover(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.getView().addEventListener(pointerEvents.move, () => {
|
|
|
|
if (!this.getSelected()) {
|
|
|
|
this.setHover(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.getView().addEventListener(pointerEvents.leave, () => {
|
|
|
|
this.setHover(false);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.getView().addEventListener(pointerEvents.up, this.onClickHandler);
|
|
|
|
}
|
2017-12-18 22:04:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public dispose() {
|
|
|
|
if (this.onClickHandler) this.getView().removeEventListener(pointerEvents.up, this.onClickHandler)
|
|
|
|
View.dispose(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getView() {
|
|
|
|
if (!this.rendered) {
|
|
|
|
this.element = svg.elt("g") as SVGGElement;
|
|
|
|
View.track(this);
|
|
|
|
|
2017-12-22 23:00:23 +01:00
|
|
|
const content = this.buildDom();
|
2017-12-18 22:04:17 +01:00
|
|
|
if (content) {
|
|
|
|
this.element.appendChild(content);
|
|
|
|
}
|
|
|
|
this.updateTransform();
|
|
|
|
this.rendered = true;
|
|
|
|
}
|
|
|
|
return this.element;
|
|
|
|
}
|
|
|
|
|
2017-12-22 23:00:23 +01:00
|
|
|
public resize(width: number, height: number) {
|
2017-12-18 22:04:17 +01:00
|
|
|
this.width = width;
|
2017-12-22 23:00:23 +01:00
|
|
|
this.height = height;
|
2017-12-18 22:04:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private updateTransform() {
|
|
|
|
if (this.rendered) {
|
2017-12-22 23:00:23 +01:00
|
|
|
let left = this.left;
|
|
|
|
let top = this.top;
|
|
|
|
let scaleFactor = this.scaleFactor;
|
|
|
|
if (this.hover) {
|
|
|
|
const hoverScaleFactor = scaleFactor + 0.05;
|
|
|
|
// Scale around center of module
|
|
|
|
const centerX = this.getWidth() / 2;
|
|
|
|
const centerY = this.getHeight() / 2;
|
|
|
|
left = left - centerX * (hoverScaleFactor - 1);
|
|
|
|
top = top - centerY * (hoverScaleFactor - 1);
|
|
|
|
scaleFactor = hoverScaleFactor;
|
|
|
|
}
|
2017-12-18 22:04:17 +01:00
|
|
|
|
2017-12-22 23:00:23 +01:00
|
|
|
let transform = `translate(${left} ${top})`;
|
|
|
|
|
|
|
|
if (scaleFactor !== 1) {
|
|
|
|
transform += ` scale(${scaleFactor})`;
|
2017-12-18 22:04:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.element.setAttribute("transform", transform);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static currentId = 0;
|
|
|
|
private static allViews: Map<View> = {};
|
|
|
|
|
|
|
|
protected static getInstance(element: Element) {
|
|
|
|
if (element.hasAttribute("ref-id")) {
|
|
|
|
return View.allViews[element.getAttribute("ref-id")];
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static track(view: View) {
|
|
|
|
const myId = "id-" + (View.currentId++);
|
|
|
|
view.element.setAttribute("ref-id", myId);
|
|
|
|
View.allViews[myId] = view;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static dispose(view: View) {
|
|
|
|
if (view.element) {
|
|
|
|
const id = view.element.getAttribute("ref-id");
|
|
|
|
// TODO: Remove from DOM
|
|
|
|
view.element.parentNode.removeChild(view.element);
|
|
|
|
delete View.allViews[id];
|
|
|
|
}
|
|
|
|
}
|
2017-12-22 23:00:23 +01:00
|
|
|
|
|
|
|
///////// HOVERED STATE /////////////
|
|
|
|
|
|
|
|
public getHover() {
|
|
|
|
return this.hover;
|
|
|
|
}
|
|
|
|
|
|
|
|
public setHover(hover: boolean) {
|
|
|
|
if (this.hover != hover) {
|
|
|
|
this.hover = hover;
|
|
|
|
this.updateTransform();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////// SELECTED STATE /////////////
|
|
|
|
|
|
|
|
public getSelected() {
|
|
|
|
return this.selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
public setSelected(selected: boolean) {
|
2018-01-31 07:22:21 +01:00
|
|
|
if (this.selected != selected) {
|
|
|
|
this.selected = selected;
|
|
|
|
this.setChangedState();
|
|
|
|
}
|
2017-12-22 23:00:23 +01:00
|
|
|
}
|
|
|
|
|
2018-04-16 20:50:12 +02:00
|
|
|
public hasClose() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-22 23:00:23 +01:00
|
|
|
protected setChangedState() {
|
|
|
|
this.changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public didChange() {
|
|
|
|
const res = this.changed;
|
|
|
|
this.changed = false;
|
|
|
|
return res;
|
|
|
|
}
|
2017-12-18 22:04:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export abstract class SimView<T extends BaseNode> extends View implements LayoutElement {
|
|
|
|
constructor(protected state: T) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
public getId() {
|
|
|
|
return this.state.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getPort() {
|
|
|
|
return this.state.port;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getPaddingRatio() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getWiringRatio() {
|
|
|
|
return 0.5;
|
|
|
|
}
|
|
|
|
|
|
|
|
public setSelected(selected: boolean) { }
|
|
|
|
|
|
|
|
protected getView() {
|
|
|
|
return super.getView();
|
|
|
|
}
|
2018-03-28 22:36:52 +02:00
|
|
|
|
|
|
|
public kill() {
|
|
|
|
}
|
2017-12-18 22:04:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export class ViewContainer extends View {
|
|
|
|
public getInnerWidth() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getInnerHeight() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public addView(view: View) {
|
|
|
|
view.inject(this.element);
|
|
|
|
}
|
|
|
|
|
|
|
|
public clear() {
|
|
|
|
forEachElement(this.element.childNodes, e => {
|
|
|
|
this.element.removeChild(e);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public onComponentInjected() {
|
|
|
|
const observer = new MutationObserver(records => {
|
|
|
|
records.forEach(r => {
|
|
|
|
forEachElement(r.addedNodes, node => {
|
|
|
|
const instance = View.getInstance(node);
|
|
|
|
if (instance) {
|
|
|
|
instance.onComponentVisible();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
forEachElement(r.removedNodes, node => {
|
|
|
|
const instance = View.getInstance(node);
|
|
|
|
if (instance) {
|
|
|
|
instance.onComponentHidden();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
observer.observe(this.element, {
|
|
|
|
childList: true,
|
|
|
|
subtree: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-22 23:00:23 +01:00
|
|
|
protected buildDom(): SVGElement {
|
2017-12-18 22:04:17 +01:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function forEachElement(nodes: NodeList, cb: (e: Element) => void) {
|
|
|
|
for (let i = 0; i < nodes.length; i++) {
|
|
|
|
const node = nodes[i];
|
|
|
|
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
|
|
cb(node as Element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|