Added helpers for getting the state of things

This commit is contained in:
Matei Adriel 2019-07-17 15:27:05 +03:00
parent 7427a02842
commit d38ce7cd1b
10 changed files with 100 additions and 12 deletions

2
.gitignore vendored
View file

@ -1,4 +1,4 @@
node_modules node_modules
dist dist
coverege coverege
.theia idea

View file

@ -1,9 +1,8 @@
import React, { Component, createRef, Ref, RefObject } from 'react' import React, { Component, createRef, Ref, RefObject } from 'react'
import FluidCanvas, { MouseEventInfo } from './FluidCanvas' import FluidCanvas from './FluidCanvas'
import loop from 'mainloop.js' import loop from 'mainloop.js'
import { Gate } from '../../simulation/classes/Gate' import { Gate } from '../../simulation/classes/Gate'
import { SimulationRenderer } from '../../simulationRenderer/classes/SimulationRenderer' import { SimulationRenderer } from '../../simulationRenderer/classes/SimulationRenderer'
import { Subject } from 'rxjs'
import { renderSimulation } from '../../simulationRenderer/helpers/renderSimulation' import { renderSimulation } from '../../simulationRenderer/helpers/renderSimulation'
import { updateSimulation } from '../../simulationRenderer/helpers/updateSimulation' import { updateSimulation } from '../../simulationRenderer/helpers/updateSimulation'

View file

@ -0,0 +1,49 @@
import { SimulationRenderer } from '../../simulationRenderer/classes/SimulationRenderer'
import { Gate } from '../../simulation/classes/Gate'
import {
GateState,
TransformState,
RendererState,
CameraState,
SimulationState
} from '../types/SimulationSave'
import { Transform } from '../../../common/math/classes/Transform'
import { Camera } from '../../simulationRenderer/classes/Camera'
import { Simulation } from '../../simulation/classes/Simulation'
export const getTransformState = (transform: Transform): TransformState => {
return {
position: transform.position,
rotation: transform.rotation,
scale: transform.scale
}
}
export const getCameraState = (camera: Camera): CameraState => {
return {
transform: getTransformState(camera.transform)
}
}
export const getSimulationState = (simulation: Simulation): SimulationState => {
return {
gates: Array.from(simulation.gates).map(getGateState)
}
}
export const getGateState = (gate: Gate): GateState => {
return {
id: gate.id,
template: gate.template.metadata.name,
transform: getTransformState(gate.transform)
}
}
export const getRendererState = (
renderer: SimulationRenderer
): RendererState => {
return {
camera: getCameraState(renderer.camera),
simulation: getSimulationState(renderer.simulation)
}
}

View file

@ -0,0 +1,26 @@
import { vector2 } from '../../../common/math/classes/Transform'
export interface TransformState {
position: vector2
scale: vector2
rotation: number
}
export interface GateState {
transform: TransformState
id: number
template: string
}
export interface CameraState {
transform: TransformState
}
export interface SimulationState {
gates: GateState[]
}
export interface RendererState {
simulation: SimulationState
camera: CameraState
}

View file

@ -1,6 +1,9 @@
import { GateTemplate } from './types/GateTemplate' import { GateTemplate } from './types/GateTemplate'
export const DefaultGateTemplate: GateTemplate = { export const DefaultGateTemplate: GateTemplate = {
metadata: {
name: 'Default template'
},
material: { material: {
type: 'color', type: 'color',
value: 'blue' value: 'blue'

View file

@ -20,4 +20,7 @@ export interface GateTemplate {
inputs: PinCount inputs: PinCount
outputs: PinCount outputs: PinCount
} }
metadata: {
name: string
}
} }

View file

@ -13,6 +13,7 @@ import merge from 'deepmerge'
import { getPinPosition } from '../helpers/pinPosition' import { getPinPosition } from '../helpers/pinPosition'
import { pointInCircle } from '../../../common/math/helpers/pointInCircle' import { pointInCircle } from '../../../common/math/helpers/pointInCircle'
import { SelectedPins } from '../types/SelectedPins' import { SelectedPins } from '../types/SelectedPins'
import { getRendererState } from '../../saving/helpers/getState'
export class SimulationRenderer { export class SimulationRenderer {
public mouseDownOutput = new Subject<MouseEventInfo>() public mouseDownOutput = new Subject<MouseEventInfo>()
@ -91,23 +92,24 @@ export class SimulationRenderer {
this.options.gates.pinRadius this.options.gates.pinRadius
) )
) { ) {
if ( if ((pin.value.type & 0b10) >> 1) {
(pin.value.type & 0b10) >> 1 &&
this.selectedPins.start === null
) {
this.selectedPins.start = { this.selectedPins.start = {
wrapper: pin, wrapper: pin,
transform transform
} }
} else if ( } else if (pin.value.type & 1) {
pin.value.type & 1 &&
this.selectedPins.end === null
) {
this.selectedPins.end = { this.selectedPins.end = {
wrapper: pin, wrapper: pin,
transform transform
} }
} }
if (this.selectedPins.start && this.selectedPins.end) {
console.log('Connecting!')
console.log(getRendererState(this))
this.selectedPins.start = null
this.selectedPins.end = null
}
} }
} }
} }

View file

@ -9,5 +9,8 @@ export const defaultSimulationRendererOptions: SimulationRendererOptions = {
pinRadius: 10, pinRadius: 10,
pinStrokeColor: '#888888', pinStrokeColor: '#888888',
pinStrokeWidth: 3 pinStrokeWidth: 3
},
wires: {
temporaryWireColor: `rgba(128,128,128,0.5)`
} }
} }

View file

@ -17,7 +17,7 @@ export const renderClickedPins = (
if (pin) { if (pin) {
const position = getPinPosition(renderer, pin.transform, pin.wrapper) const position = getPinPosition(renderer, pin.transform, pin.wrapper)
ctx.strokeStyle = 'yellow' ctx.strokeStyle = renderer.options.wires.temporaryWireColor
ctx.lineWidth = renderer.options.gates.pinRadius * 2 ctx.lineWidth = renderer.options.gates.pinRadius * 2
ctx.lineCap = 'round' ctx.lineCap = 'round'

View file

@ -8,4 +8,7 @@ export interface SimulationRendererOptions {
pinStrokeColor: string pinStrokeColor: string
pinStrokeWidth: number pinStrokeWidth: number
} }
wires: {
temporaryWireColor: string
}
} }