logicsimulator/components/dilswitchx4.ts

81 lines
2.7 KiB
TypeScript

// 4x DIL switch 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/>.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function DILSwitchX4Init() {
class DILSwitchX4 extends DILBase {
pressed: boolean[];
constructor(simulator: Simulator, id: string, param: BaseComponentParam) {
super(simulator, id, 8, param);
this.pressed = [];
for (let i = 0; i < 4; ++i) {
this.pressed[i] = false;
}
}
setup(canvas: SVGElement) {
super.DILBASEsetup('dilswitchx4', canvas, '');
if (this.element) {
this.element.addEventListener('mousedown', this.activate.bind(this));
const on = this.element.querySelectorAll('.on');
for (let o = 0; o < on.length; ++o) {
on[o].setAttribute('style', 'display:none');
}
}
}
update(): boolean {
let mod = false;
for (let i = 1; i <= 4; ++i) {
const oldstate = this.getStateWire('p' + i);
let newstate = WireState.float;
if (this.pressed[i - 1]) {
newstate = this.getStateWire('p' + (9 - i));
}
this.stateMapWire.set('p' + i, newstate);
this.getPin('p' + i).setState(newstate);
mod ||= newstate !== oldstate;
}
return mod;
}
activate(e: MouseEvent) {
const parent = (<SVGGElement>e.target).parentElement;
if (parent === null || /*parent === this.element ||*/ parent.id === '') {
return;
}
const id = parseInt(parent.id.substr(1)) - 1;
this.pressed[id] = !this.pressed[id];
const off = parent.querySelectorAll('.off')[0];
const on = parent.querySelectorAll('.on')[0];
if (this.pressed[id]) {
off.setAttribute('style', 'display:none');
on.removeAttribute('style');
} else {
on.setAttribute('style', 'display:none');
off.removeAttribute('style');
}
this.simulator.manualtick();
}
}
window.DILSwitchX4 = DILSwitchX4;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function DILSwitchX4Depends() {
return 'dilbase';
}