logicsimulator/components/label.ts

83 lines
2.5 KiB
TypeScript

// Label component for the LogicSimulator
// Copyright (C) 2022 Sascha Nitsch
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
interface LabelParam extends BaseComponentParam {
label?: string;
width: number;
attach: string;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class Label extends BaseComponent {
private mod: boolean;
private labelText: string;
private width: number;
private attach: string;
private state: boolean | null;
constructor(simulator: Simulator, id: string, param: LabelParam) {
super(simulator, id, param);
this.pins.set('q', new TriState(this, 0, 0));
this.mod = false;
this.labelText = param.label || '';
this.width = param.width;
this.attach = param.attach || '';
this.state = null;
}
setup(canvas: SVGElement) {
super.doSetup('label', canvas);
if (this.element) {
this.element.querySelectorAll('.txt')[0].innerHTML = this.labelText;
}
const width = this.width ? this.width : 50;
if (this.attach !== '' && this.element) {
const g = this.element.querySelectorAll('g');
switch (this.attach) {
case 'e':
g[1].setAttribute('transform', 'translate(-' + width + ',0)');
break;
case 's':
g[1].setAttribute('transform', 'rotate(-90)');
break;
}
}
if (this.width && this.element) {
this.element.querySelectorAll('.outer')[0].setAttribute('width', width.toString());
}
}
io() {
const s = this.state;
this.state = this.tri(this.getPin('q').getAndReset());
this.mod = this.state !== s;
return false;
}
update(): boolean {
if (this.mod && this.element) {
this.element.classList.remove('low');
this.element.classList.remove('high');
if (this.state === true) {
this.element.classList.add('high');
}
if (this.state === false) {
this.element.classList.add('low');
}
}
return false;
}
}