From 266aa4db49017d36493a29af055051af7c25374b Mon Sep 17 00:00:00 2001 From: Matei Adriel Date: Thu, 25 Jul 2019 14:49:24 +0300 Subject: [PATCH] basic cleaning --- src/modules/simulation-actions/constants.ts | 5 ++- .../simulation-actions/helpers/clean.ts | 35 +++++++++++++++++++ .../simulation-actions/helpers/clear.ts | 12 ------- .../helpers/wireConnectedToGate.ts | 17 +++++++++ 4 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 src/modules/simulation-actions/helpers/clean.ts delete mode 100644 src/modules/simulation-actions/helpers/clear.ts diff --git a/src/modules/simulation-actions/constants.ts b/src/modules/simulation-actions/constants.ts index 57f7268..0d4fa4c 100644 --- a/src/modules/simulation-actions/constants.ts +++ b/src/modules/simulation-actions/constants.ts @@ -6,6 +6,7 @@ import { undo } from './helpers/undo' import { createActionConfig } from './helpers/createActionConfig' import { selectAll } from './helpers/selectAll' import { deleteSelection } from './helpers/deleteSelection' +import { cleanRenderer } from './helpers/clean' export const actionIcons: Record = { clean: 'clear', @@ -38,9 +39,7 @@ export const SidebarActions: Record = { ...createActionConfig( 'clean', { - run: () => { - console.log('Cleaning') - } + run: cleanRenderer }, ['ctrl', 'delete'] ), diff --git a/src/modules/simulation-actions/helpers/clean.ts b/src/modules/simulation-actions/helpers/clean.ts new file mode 100644 index 0000000..51708d1 --- /dev/null +++ b/src/modules/simulation-actions/helpers/clean.ts @@ -0,0 +1,35 @@ +import { SimulationRenderer } from '../../simulationRenderer/classes/SimulationRenderer' +import { Simulation } from '../../simulation/classes/Simulation' +import { allWiresConnectedToGate } from '../../simulationRenderer/helpers/wireConnectedToGate' +import { deleteGate } from '../../simulationRenderer/helpers/deleteGate' +import { Gate } from '../../simulation/classes/Gate' + +/** + * Deletes all unconnected gates from a renderers simulation + * + * @param renderer The renderer to clean the simulation of + */ +export const cleanRenderer = (renderer: SimulationRenderer) => { + cleanSimulation(renderer.simulation) +} + +/** + * Deletes all unconnected gates from a simulation + * + * @param simulation The simulation to clean + */ +export const cleanSimulation = (simulation: Simulation) => { + const toDelete: Gate[] = [] + + for (const gate of simulation.gates) { + const wires = allWiresConnectedToGate(simulation, gate) + + if (!wires.length) { + toDelete.push(gate) + } + } + + for (const gate of toDelete) { + deleteGate(simulation, gate) + } +} diff --git a/src/modules/simulation-actions/helpers/clear.ts b/src/modules/simulation-actions/helpers/clear.ts deleted file mode 100644 index a50e199..0000000 --- a/src/modules/simulation-actions/helpers/clear.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { SimulationRenderer } from '../../simulationRenderer/classes/SimulationRenderer' - -/** - * Clears the simuolation of a renderer - * - * @param renderer The renderer to clear the simulation of - */ -export const clear = (renderer: SimulationRenderer) => { - renderer.simulation.dispose() - renderer.simulation.wires = [] - renderer.simulation.gates.clear() -} diff --git a/src/modules/simulationRenderer/helpers/wireConnectedToGate.ts b/src/modules/simulationRenderer/helpers/wireConnectedToGate.ts index c8cf252..0cc72ab 100644 --- a/src/modules/simulationRenderer/helpers/wireConnectedToGate.ts +++ b/src/modules/simulationRenderer/helpers/wireConnectedToGate.ts @@ -1,5 +1,22 @@ import { Gate } from '../../simulation/classes/Gate' import { Wire } from '../../simulation/classes/Wire' +import { Simulation } from '../../simulation/classes/Simulation' +/** + * Checks if a wire has a gate at any of its ends + * + * @param gate The gate to check + * @param wire The wire to check + */ export const wireConnectedToGate = (gate: Gate, wire: Wire) => wire.end.value.gate === gate || wire.start.value.gate === gate + +/** + * Finds all the wires a gate has connected to it + * + * @param simulation The simulation to check all the wires of + * @param gate The gate to find the wires for + */ +export const allWiresConnectedToGate = (simulation: Simulation, gate: Gate) => { + return simulation.wires.filter(wire => wireConnectedToGate(gate, wire)) +}