diff --git a/src/modules/simulationRenderer/classes/SimulationRenderer.ts b/src/modules/simulationRenderer/classes/SimulationRenderer.ts index 6c71238..5e039a5 100644 --- a/src/modules/simulationRenderer/classes/SimulationRenderer.ts +++ b/src/modules/simulationRenderer/classes/SimulationRenderer.ts @@ -9,8 +9,8 @@ import { SelectedPins } from '../types/SelectedPins' import { currentStore } from '../../saving/stores/currentStore' import { saveStore } from '../../saving/stores/saveStore' import { - fromSimulationState, - fromCameraState + fromSimulationState, + fromCameraState } from '../../saving/helpers/fromState' import merge from 'deepmerge' import { handleScroll } from '../helpers/scaleCanvas' @@ -29,134 +29,132 @@ import { handleMouseMove } from '../helpers/handleMouseMove' import { Gate } from '../../simulation/classes/Gate' export class SimulationRenderer { - public mouseDownOutput = new Subject() - public mouseUpOutput = new Subject() - public mouseMoveOutput = new Subject() - public wheelOutput = new Subject() + public mouseDownOutput = new Subject() + public mouseUpOutput = new Subject() + public mouseMoveOutput = new Subject() + public wheelOutput = new Subject() - public selectedGates: Record> = { - temporary: new Set(), - permanent: new Set() - } + public selectedGates: Record> = { + temporary: new Set(), + permanent: new Set() + } - public options: SimulationRendererOptions - public camera = new Camera() + public options: SimulationRendererOptions + public camera = new Camera() - public selectedArea = new Transform() - public clipboard: GateInitter[] = [] - public wireClipboard: WireState[] = [] + public selectedArea = new Transform() + public clipboard: GateInitter[] = [] + public wireClipboard: WireState[] = [] - // first bit = dragging - // second bit = panning - // third bit = selecting - public mouseState = 0b000 + // first bit = dragging + // second bit = panning + // third bit = selecting + public mouseState = 0b000 - public lastMousePosition: vector2 = [0, 0] + public lastMousePosition: vector2 = [0, 0] - // this is used for spawning gates - public spawnCount = 0 + // this is used for spawning gates + public spawnCount = 0 - public selectedPins: SelectedPins = { - start: null, - end: null - } + public selectedPins: SelectedPins = { + start: null, + end: null + } - public constructor( - options: Partial = {}, - public simulation = new Simulation('project', 'default') - ) { - this.options = merge(defaultSimulationRendererOptions, options) + public constructor( + options: Partial = {}, + public simulation = new Simulation('project', 'default') + ) { + this.options = merge(defaultSimulationRendererOptions, options) - this.init() - } + this.init() + } - public init() { - this.mouseDownOutput.subscribe(handleMouseDown(this)) - this.mouseUpOutput.subscribe(handleMouseUp(this)) - this.mouseMoveOutput.subscribe(handleMouseMove(this)) + public init() { + this.mouseDownOutput.subscribe(handleMouseDown(this)) + this.mouseUpOutput.subscribe(handleMouseUp(this)) + this.mouseMoveOutput.subscribe(handleMouseMove(this)) - this.reloadSave() - } + this.reloadSave() + } - public updateWheelListener(ref: RefObject) { - if (ref.current) { - ref.current.addEventListener('wheel', (event) => { - if (!modalIsOpen() && location.pathname === '/') { - event.preventDefault() + public updateWheelListener(ref: RefObject) { + if (ref.current) { + ref.current.addEventListener('wheel', (event) => { + if (!modalIsOpen() && location.pathname === process.env.BASEURL) { + event.preventDefault() - handleScroll(event, this.camera) - } - }) + handleScroll(event, this.camera) } + }) } + } - /** - * Loads a simulation state - * - * @param save LThe state to load - */ - public loadSave(save: RendererState) { - this.simulation = fromSimulationState(save.simulation) - this.camera = fromCameraState(save.camera) - } + /** + * Loads a simulation state + * + * @param save LThe state to load + */ + public loadSave(save: RendererState) { + this.simulation = fromSimulationState(save.simulation) + this.camera = fromCameraState(save.camera) + } - public reloadSave() { - dumpSimulation(this) + public reloadSave() { + dumpSimulation(this) - const current = currentStore.get() - const save = saveStore.get(current) + const current = currentStore.get() + const save = saveStore.get(current) - if (!save) return - if (!(save.simulation || save.camera)) return + if (!save) return + if (!(save.simulation || save.camera)) return - this.loadSave(save) - } + this.loadSave(save) + } - /** - * Gets all selected gates in the simulation - * - * @throws SimulationError if an id isnt valid - * @throws SimulationError if the id doesnt have a data prop - */ - public getSelected(): Gate[] { - return setToArray(this.allSelectedIds()).map((id) => { - const gate = this.simulation.gates.get(id) + /** + * Gets all selected gates in the simulation + * + * @throws SimulationError if an id isnt valid + * @throws SimulationError if the id doesnt have a data prop + */ + public getSelected(): Gate[] { + return setToArray(this.allSelectedIds()).map((id) => { + const gate = this.simulation.gates.get(id) - if (!gate) { - throw new SimulationError(`Cannot find gate with id ${id}`) - } else if (!gate.data) { - throw new SimulationError( - `Cannot find data of gate with id ${id}` - ) - } + if (!gate) { + throw new SimulationError(`Cannot find gate with id ${id}`) + } else if (!gate.data) { + throw new SimulationError(`Cannot find data of gate with id ${id}`) + } - return gate.data - }) - } + return gate.data + }) + } - /** - * helper to merge the temporary and permanent selection - */ - public allSelectedIds() { - return new Set([ - ...setToArray(this.selectedGates.permanent), - ...setToArray(this.selectedGates.temporary) - ]) - } + /** + * helper to merge the temporary and permanent selection + */ + public allSelectedIds() { + return new Set([ + ...setToArray(this.selectedGates.permanent), + ...setToArray(this.selectedGates.temporary) + ]) + } - /** - * Helper to clear all selected sets - */ - public clearSelection() { - this.selectedGates.permanent.clear() - this.selectedGates.temporary.clear() - } + /** + * Helper to clear all selected sets + */ + public clearSelection() { + this.selectedGates.permanent.clear() + this.selectedGates.temporary.clear() + } - /** - * Clears the selected pins of the renderer - */ - public clearPinSelection() { - this.selectedPins.end = null - this.selectedPins.start = null - } + /** + * Clears the selected pins of the renderer + */ + public clearPinSelection() { + this.selectedPins.end = null + this.selectedPins.start = null + } }