basic cleaning
This commit is contained in:
parent
a1b28c1317
commit
266aa4db49
|
@ -6,6 +6,7 @@ import { undo } from './helpers/undo'
|
||||||
import { createActionConfig } from './helpers/createActionConfig'
|
import { createActionConfig } from './helpers/createActionConfig'
|
||||||
import { selectAll } from './helpers/selectAll'
|
import { selectAll } from './helpers/selectAll'
|
||||||
import { deleteSelection } from './helpers/deleteSelection'
|
import { deleteSelection } from './helpers/deleteSelection'
|
||||||
|
import { cleanRenderer } from './helpers/clean'
|
||||||
|
|
||||||
export const actionIcons: Record<possibleAction, string> = {
|
export const actionIcons: Record<possibleAction, string> = {
|
||||||
clean: 'clear',
|
clean: 'clear',
|
||||||
|
@ -38,9 +39,7 @@ export const SidebarActions: Record<possibleAction, SidebarAction> = {
|
||||||
...createActionConfig(
|
...createActionConfig(
|
||||||
'clean',
|
'clean',
|
||||||
{
|
{
|
||||||
run: () => {
|
run: cleanRenderer
|
||||||
console.log('Cleaning')
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
['ctrl', 'delete']
|
['ctrl', 'delete']
|
||||||
),
|
),
|
||||||
|
|
35
src/modules/simulation-actions/helpers/clean.ts
Normal file
35
src/modules/simulation-actions/helpers/clean.ts
Normal file
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -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()
|
|
||||||
}
|
|
|
@ -1,5 +1,22 @@
|
||||||
import { Gate } from '../../simulation/classes/Gate'
|
import { Gate } from '../../simulation/classes/Gate'
|
||||||
import { Wire } from '../../simulation/classes/Wire'
|
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) =>
|
export const wireConnectedToGate = (gate: Gate, wire: Wire) =>
|
||||||
wire.end.value.gate === gate || wire.start.value.gate === gate
|
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))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue