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
dist
coverege
.theia
idea

View file

@ -1,9 +1,8 @@
import React, { Component, createRef, Ref, RefObject } from 'react'
import FluidCanvas, { MouseEventInfo } from './FluidCanvas'
import FluidCanvas from './FluidCanvas'
import loop from 'mainloop.js'
import { Gate } from '../../simulation/classes/Gate'
import { SimulationRenderer } from '../../simulationRenderer/classes/SimulationRenderer'
import { Subject } from 'rxjs'
import { renderSimulation } from '../../simulationRenderer/helpers/renderSimulation'
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'
export const DefaultGateTemplate: GateTemplate = {
metadata: {
name: 'Default template'
},
material: {
type: 'color',
value: 'blue'

View file

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

View file

@ -13,6 +13,7 @@ import merge from 'deepmerge'
import { getPinPosition } from '../helpers/pinPosition'
import { pointInCircle } from '../../../common/math/helpers/pointInCircle'
import { SelectedPins } from '../types/SelectedPins'
import { getRendererState } from '../../saving/helpers/getState'
export class SimulationRenderer {
public mouseDownOutput = new Subject<MouseEventInfo>()
@ -91,23 +92,24 @@ export class SimulationRenderer {
this.options.gates.pinRadius
)
) {
if (
(pin.value.type & 0b10) >> 1 &&
this.selectedPins.start === null
) {
if ((pin.value.type & 0b10) >> 1) {
this.selectedPins.start = {
wrapper: pin,
transform
}
} else if (
pin.value.type & 1 &&
this.selectedPins.end === null
) {
} else if (pin.value.type & 1) {
this.selectedPins.end = {
wrapper: pin,
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,
pinStrokeColor: '#888888',
pinStrokeWidth: 3
},
wires: {
temporaryWireColor: `rgba(128,128,128,0.5)`
}
}

View file

@ -17,7 +17,7 @@ export const renderClickedPins = (
if (pin) {
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.lineCap = 'round'

View file

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