2017-12-22 23:00:23 +01:00
|
|
|
/// <reference path="./nodes/moduleView.ts" />
|
2017-12-18 22:04:17 +01:00
|
|
|
|
|
|
|
namespace pxsim.visuals {
|
|
|
|
|
|
|
|
export const CONTROL_WIDTH = 87.5;
|
|
|
|
export const CONTROL_HEIGHT = 175;
|
|
|
|
|
2017-12-28 01:30:42 +01:00
|
|
|
export const CONTROL_TEXT_COLOR = '#000';
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
export abstract class ControlView<T extends BaseNode> extends SimView<T> implements LayoutElement {
|
2017-12-22 23:00:23 +01:00
|
|
|
protected content: SVGSVGElement;
|
2017-12-18 22:04:17 +01:00
|
|
|
|
|
|
|
abstract getInnerView(parent: SVGSVGElement, globalDefs: SVGDefsElement): SVGElement;
|
|
|
|
|
|
|
|
constructor(protected parent: SVGSVGElement, protected globalDefs: SVGDefsElement, protected state: T, protected port: number) {
|
|
|
|
super(state);
|
2017-12-27 08:19:04 +01:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getInnerWidth(): number {
|
|
|
|
return CONTROL_WIDTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
getInnerHeight(): number {
|
|
|
|
return CONTROL_HEIGHT;
|
|
|
|
}
|
|
|
|
|
|
|
|
getPaddingRatio() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
getWiringRatio() {
|
|
|
|
return 0.5;
|
|
|
|
}
|
|
|
|
|
|
|
|
public hasClick() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-22 23:00:23 +01:00
|
|
|
buildDom(): SVGElement {
|
2017-12-28 01:30:42 +01:00
|
|
|
this.content = svg.elt("svg", { viewBox: `0 0 ${this.getInnerWidth()} ${this.getInnerHeight()}` }) as SVGSVGElement;
|
2017-12-22 23:00:23 +01:00
|
|
|
this.content.appendChild(this.getInnerView(this.parent, this.globalDefs));
|
|
|
|
return this.content;
|
2017-12-18 22:04:17 +01:00
|
|
|
}
|
|
|
|
|
2017-12-22 23:00:23 +01:00
|
|
|
public resize(width: number, height: number) {
|
|
|
|
super.resize(width, height);
|
|
|
|
this.updateDimensions(width, height);
|
|
|
|
}
|
2017-12-18 22:04:17 +01:00
|
|
|
|
2017-12-22 23:00:23 +01:00
|
|
|
private updateDimensions(width: number, height: number) {
|
|
|
|
if (this.content) {
|
|
|
|
const currentWidth = this.getInnerWidth();
|
|
|
|
const currentHeight = this.getInnerHeight();
|
|
|
|
const newHeight = currentHeight / currentWidth * width;
|
|
|
|
const newWidth = currentWidth / currentHeight * height;
|
2017-12-28 01:30:42 +01:00
|
|
|
if (newHeight > height) {
|
|
|
|
// scale width instead
|
|
|
|
this.content.setAttribute('width', `${newWidth}`);
|
|
|
|
this.content.setAttribute('height', `${height}`);
|
|
|
|
// translate to the middle (width)
|
|
|
|
this.translate(width / 2 - newWidth / 2, 0);
|
|
|
|
} else {
|
|
|
|
this.content.setAttribute('width', `${width}`);
|
|
|
|
this.content.setAttribute('height', `${newHeight}`);
|
|
|
|
// translate to the middle (height)
|
|
|
|
this.translate(0, height / 2 - newHeight / 2);
|
|
|
|
}
|
2017-12-22 23:00:23 +01:00
|
|
|
}
|
2017-12-18 22:04:17 +01:00
|
|
|
}
|
|
|
|
|
2017-12-22 23:00:23 +01:00
|
|
|
onComponentVisible() {
|
2017-12-18 22:04:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|