Skip to content

Commit

Permalink
Fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
Derstilon committed May 14, 2023
1 parent 020beb5 commit 16cfad8
Show file tree
Hide file tree
Showing 33 changed files with 933 additions and 833 deletions.
4 changes: 4 additions & 0 deletions src/Globals.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
declare module '*.css';

type Modify<T, R> = Omit<T, keyof R> & R;

type RemoveIndex<T> = {
[K in keyof T as string extends K ? never : number extends K ? never : K]: T[K];
};
16 changes: 9 additions & 7 deletions src/PythonConverter/PythonConverterService.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { ReactNode, useCallback, useEffect, useRef, useState } from 'react';
import * as Comlink from 'comlink';
import { createGenericContext } from '../services/GenericContext';
import { IPythonWorker } from './PythonWorker';
import { PythonWorker } from './PythonWorker';
import { EditorJson } from '../ThreeEditor/js/EditorJson';
import { SimulationInputFiles } from '../types/ResponseTypes';

Expand All @@ -17,13 +17,15 @@ export interface PythonConverterProps {
onLoad?: (pyodide: any) => void;
}

export interface IPythonConverter {
convertJSON: (editorJson: EditorJson) => Promise<Map<keyof SimulationInputFiles, string>>;
export interface PythonConverterContext {
convertJSON: (
editorJson: EditorJson
) => Promise<Map<keyof RemoveIndex<SimulationInputFiles>, string>>;
isConverterReady: boolean;
}

const [usePythonConverter, PythonConverterContextProvider] =
createGenericContext<IPythonConverter>();
createGenericContext<PythonConverterContext>();

const PYODIDE_LOADED = 'PYODIDE_LOADED';

Expand All @@ -32,10 +34,10 @@ const PYODIDE_LOADED = 'PYODIDE_LOADED';
* There can be only one instance of PythonConverter in the whole application.
*/
const PythonConverter = (props: PythonConverterProps) => {
const workerRef = useRef<Comlink.Remote<IPythonWorker>>();
const workerRef = useRef<Comlink.Remote<PythonWorker>>();

useEffect(() => {
workerRef.current = Comlink.wrap<IPythonWorker>(
workerRef.current = Comlink.wrap<PythonWorker>(
new Worker(new URL('./PythonWorker.ts', import.meta.url))
);
workerRef.current.initPyodide(
Expand Down Expand Up @@ -70,7 +72,7 @@ const PythonConverter = (props: PythonConverterProps) => {
return await workerRef.current!.convertJSON(editorJSON);
};

const value: IPythonConverter = {
const value: PythonConverterContext = {
convertJSON,
isConverterReady
};
Expand Down
8 changes: 5 additions & 3 deletions src/PythonConverter/PythonWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import { EditorJson } from '../ThreeEditor/js/EditorJson';
// CND method is suggested in https://pyodide.org/en/stable/usage/downloading-and-deploying.html
importScripts('https://cdn.jsdelivr.net/pyodide/v0.21.3/full/pyodide.js');

export interface IPythonWorker {
export interface PythonWorker {
initPyodide: (onReady: () => void) => void;
close: () => void;
runPython: <T>(string: string) => T;
checkConverter: () => boolean;
convertJSON: (editorJson: EditorJson) => Promise<Map<keyof SimulationInputFiles, string>>;
convertJSON: (
editorJson: EditorJson
) => Promise<Map<keyof RemoveIndex<SimulationInputFiles>, string>>;
}

const pythonConverterCode = `
Expand All @@ -36,7 +38,7 @@ def checkIfConverterReady():
checkIfConverterReady()
`;

class PythonWorkerBase implements IPythonWorker {
class PythonWorkerBase implements PythonWorker {
readonly isPythonWorker: true = true;
async initPyodide(onReady: () => void) {
const pyodide = await self.loadPyodide();
Expand Down
6 changes: 1 addition & 5 deletions src/ThreeEditor/CSG/CSGWorker.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as Comlink from 'comlink';
import * as THREE from 'three';

export interface IZoneWorker {
parse: (json: string) => unknown;
}

class ZoneWorker implements IZoneWorker {
export class ZoneWorker {
readonly isCSGWorker: true = true;
async parse(json: string) {
console.log('CSGWorker', await new THREE.ObjectLoader().parseAsync(JSON.parse(json)));
Expand Down
6 changes: 3 additions & 3 deletions src/ThreeEditor/Simulation/Base/SimZone.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as THREE from 'three';
import { SimulationMesh } from './SimulationMesh';
import { SimulationPropertiesType } from '../../../types/SimProperties';
import { ISimulationSceneChild, SimulationSceneContainer } from './SimulationScene';
import { SimulationPropertiesType } from '../../../types/SimulationProperties';
import { SimulationSceneChild, SimulationSceneContainer } from './SimulationScene';
import { Editor } from '../../js/Editor';
import SimulationMaterial, { SimulationMaterialJSON } from '../Materials/SimulationMaterial';

Expand Down Expand Up @@ -39,7 +39,7 @@ const _get_default = (material: SimulationMaterial) => {

export abstract class SimulationZone
extends SimulationMesh<THREE.BufferGeometry, SimulationMaterial>
implements SimulationPropertiesType, ISimulationSceneChild
implements SimulationPropertiesType, SimulationSceneChild
{
editor: Editor;
parent: SimulationSceneContainer<this> | null = null;
Expand Down
6 changes: 3 additions & 3 deletions src/ThreeEditor/Simulation/Base/SimulationElement.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as THREE from 'three';
import { Editor } from '../../js/Editor';
import { SimulationPropertiesType } from '../../../types/SimProperties';
import { ISimulationSceneChild, SimulationSceneContainer } from './SimulationScene';
import { SimulationPropertiesType } from '../../../types/SimulationProperties';
import { SimulationSceneChild, SimulationSceneContainer } from './SimulationScene';

/**
* This is the base class for all simulation elements.
Expand All @@ -10,7 +10,7 @@ import { ISimulationSceneChild, SimulationSceneContainer } from './SimulationSce
*/
export abstract class SimulationElement
extends THREE.Object3D
implements SimulationPropertiesType, ISimulationSceneChild
implements SimulationPropertiesType, SimulationSceneChild
{
editor: Editor;
parent: SimulationSceneContainer<this> | null = null;
Expand Down
6 changes: 3 additions & 3 deletions src/ThreeEditor/Simulation/Base/SimulationMesh.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as THREE from 'three';
import { Editor } from '../../js/Editor';
import { ISimulationSceneChild, SimulationSceneContainer } from './SimulationScene';
import { SimulationPropertiesType } from '../../../types/SimProperties';
import { SimulationSceneChild, SimulationSceneContainer } from './SimulationScene';
import { SimulationPropertiesType } from '../../../types/SimulationProperties';
import { SimulationElement } from './SimulationElement';

/**
Expand All @@ -12,7 +12,7 @@ export abstract class SimulationMesh<
TMaterial extends THREE.Material = THREE.MeshBasicMaterial
>
extends THREE.Mesh<TGeometry, TMaterial>
implements SimulationPropertiesType, ISimulationSceneChild, SimulationElement
implements SimulationPropertiesType, SimulationSceneChild, SimulationElement
{
editor: Editor;
parent: SimulationSceneContainer<this> | null = null;
Expand Down
6 changes: 3 additions & 3 deletions src/ThreeEditor/Simulation/Base/SimulationPoints.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import * as THREE from 'three';
import { Editor } from '../../js/Editor';
import { SimulationPropertiesType } from '../../../types/SimProperties';
import { ISimulationSceneChild, SimulationSceneContainer } from './SimulationScene';
import { SimulationPropertiesType } from '../../../types/SimulationProperties';
import { SimulationSceneChild, SimulationSceneContainer } from './SimulationScene';
import { SimulationElement } from './SimulationElement';

/**
* This is the base class for detectors that are represented by points.
*/
export abstract class SimulationPoints
extends THREE.Points
implements SimulationPropertiesType, ISimulationSceneChild, SimulationElement
implements SimulationPropertiesType, SimulationSceneChild, SimulationElement
{
private static _detectPointsMaterial: THREE.PointsMaterial = new THREE.PointsMaterial({
color: new THREE.Color('cyan'),
Expand Down
9 changes: 5 additions & 4 deletions src/ThreeEditor/Simulation/Base/SimulationScene.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as THREE from 'three';
import { SimulationPropertiesType } from '../../../types/SimProperties';
import { SimulationPropertiesType } from '../../../types/SimulationProperties';
import { Editor } from '../../js/Editor';
import { UniqueChildrenNames, getNextFreeName } from '../../../util/Name/Name';
import { SimulationElement } from './SimulationElement';

export interface ISimulationSceneChild extends THREE.Object3D {
export interface SimulationSceneChild extends THREE.Object3D {
parent: SimulationSceneContainer<this> | null;
name: string;
type: string;
Expand All @@ -15,7 +15,7 @@ export interface ISimulationSceneChild extends THREE.Object3D {
/**
* This is the base class for groups of simulation elements.
*/
export abstract class SimulationSceneContainer<TChild extends ISimulationSceneChild>
export abstract class SimulationSceneContainer<TChild extends SimulationSceneChild>
extends SimulationElement
implements SimulationPropertiesType, UniqueChildrenNames
{
Expand All @@ -40,7 +40,8 @@ export abstract class SimulationSceneContainer<TChild extends ISimulationSceneCh
});
this.children.length = 0;
}
toJSON(): Array<ReturnType<TChild['toJSON']>> {
// toJSON(): Array<ReturnType<TChild['toJSON']>> { TODO: Make it work for all SimulationSceneContainers.
toJSON(): Object {
return this.children.map(child => child.toJSON());
}
}
2 changes: 1 addition & 1 deletion src/ThreeEditor/Simulation/Detectors/DetectManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Signal } from 'signals';
import * as THREE from 'three';
import { Editor } from '../../js/Editor';
import { SimulationSceneContainer } from '../Base/SimulationScene';
import { SimulationPropertiesType } from '../../../types/SimProperties';
import { SimulationPropertiesType } from '../../../types/SimulationProperties';
import { DetectFilter, FilterJSON } from '../Scoring/DetectFilter';
import { DetectGeometry, DetectGeometryJSON, isDetectGeometry } from './DetectGeometry';
import { SimulationZone } from '../Base/SimZone';
Expand Down
2 changes: 1 addition & 1 deletion src/ThreeEditor/Simulation/Figures/FigureScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as THREE from 'three';
import { Editor } from '../../js/Editor';
import { generateSimulationInfo } from '../../../util/AdditionalGeometryData';
import { BasicFigure } from './BasicFigures';
import { SimulationPropertiesType } from '../../../types/SimProperties';
import { SimulationPropertiesType } from '../../../types/SimulationProperties';
import { UniqueChildrenNames, getNextFreeName } from '../../../util/Name/Name';

export class FigureScene
Expand Down
4 changes: 2 additions & 2 deletions src/ThreeEditor/Simulation/Zones/BooleanZone.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Signal } from 'signals';
import * as THREE from 'three';
import { debounce } from 'throttle-debounce';
import { IZoneWorker } from '../../CSG/CSGWorker';
import { ZoneWorker } from '../../CSG/CSGWorker';
import * as Comlink from 'comlink';
import { Editor } from '../../js/Editor';
import CSG from '../../js/libs/csg/three-csg';
Expand Down Expand Up @@ -44,7 +44,7 @@ export class BooleanZone extends SimulationZone {
return this._unionOperations;
}

worker?: Comlink.Remote<IZoneWorker>;
worker?: Comlink.Remote<ZoneWorker>;
readonly debouncedUpdatePreview = debounce(200, () => this.updatePreview(), { atBegin: false });

constructor(editor: Editor, name?: string) {
Expand Down
8 changes: 4 additions & 4 deletions src/ThreeEditor/Simulation/Zones/ZoneManager.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as Comlink from 'comlink';
import { Signal } from 'signals';
import * as THREE from 'three';
import { SimulationPropertiesType } from '../../../types/SimProperties';
import { SimulationPropertiesType } from '../../../types/SimulationProperties';
import { SimulationSceneContainer } from '../Base/SimulationScene';
import { Editor } from '../../js/Editor';
import { WorldZone, WorldZoneJSON } from './WorldZone/WorldZone';
import { BooleanZone, BooleanZoneJSON } from './BooleanZone';
import { IZoneWorker } from '../../CSG/CSGWorker';
import { ZoneWorker } from '../../CSG/CSGWorker';

interface ZoneManagerJSON {
uuid: string;
Expand Down Expand Up @@ -42,7 +42,7 @@ export class ZoneManager extends THREE.Scene implements SimulationPropertiesType
readonly notScalable = true;

editor: Editor;
worker: Comlink.Remote<IZoneWorker>;
worker: Comlink.Remote<ZoneWorker>;
worldZone: WorldZone;
zoneContainer: ZoneContainer;

Expand All @@ -66,7 +66,7 @@ export class ZoneManager extends THREE.Scene implements SimulationPropertiesType
light.position.set(15, 15, 15);
this.add(light);
this.add(this.zoneContainer);
this.worker = Comlink.wrap<IZoneWorker>(
this.worker = Comlink.wrap<ZoneWorker>(
new Worker(new URL('../../CSG/CSGWorker.ts', import.meta.url))
);
this.editor = editor;
Expand Down
6 changes: 3 additions & 3 deletions src/ThreeEditor/components/Select/ParticleSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import React from 'react';
import { AutoCompleteSelect } from '../../../util/genericComponents/AutoCompleteSelect';

export interface IParticleType {
export interface ParticleType {
id: number;
name: string;
}

export interface ParticleSelectProps {
onChange?: (event: React.SyntheticEvent<Element, Event>, newValue: number) => void;
particles: IParticleType[];
particles: readonly ParticleType[];
value?: number;
}

export function ParticleSelect(props: ParticleSelectProps) {
const getOptionLabel = ({ id, name }: IParticleType) => {
const getOptionLabel = ({ id, name }: ParticleType) => {
return `[${id}] ${name}`;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ChevronRightIcon from '@mui/icons-material/ChevronRight';
import VisibilityIcon from '@mui/icons-material/Visibility';
import VisibilityOffIcon from '@mui/icons-material/VisibilityOff';
import { Editor } from '../../../js/Editor';
import { SimulationPropertiesType } from '../../../../types/SimProperties';
import { SimulationPropertiesType } from '../../../../types/SimulationProperties';
import { NodeModel, TreeMethods } from '@minoru/react-dnd-treeview/dist/types';
import {
canChangeName,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { Button, Divider, Stack, ToggleButton, ToggleButtonGroup } from '@mui/material';
import { ChangeEvent } from 'react';
import { Object3D } from 'three';
import { SetValueCommand } from '../../../../js/commands/SetValueCommand';
import { Editor } from '../../../../js/Editor';
import { PARTICLE_TYPES } from '../../../../../types/Particle';
import { useSmartWatchEditorState } from '../../../../../util/hooks/signals';
import {
Beam,
BEAM_SOURCE_TYPE,
isBeam,
SadType,
Beam,
SAD_TYPE,
SIGMA_TYPE,
SadType,
SigmaType,
SIGMA_TYPE
isBeam
} from '../../../../Simulation/Physics/Beam';
import { useSmartWatchEditorState } from '../../../../../util/hooks/signals';
import { PARTICLE_TYPES } from '../../../../../types/Particle';
import { IParticleType, ParticleSelect } from '../../../Select/ParticleSelect';
import { Editor } from '../../../../js/Editor';
import { SetValueCommand } from '../../../../js/commands/SetValueCommand';
import { ParticleSelect } from '../../../Select/ParticleSelect';
import {
NumberPropertyField,
PropertyField,
Expand Down Expand Up @@ -305,7 +305,7 @@ function BeamConfigurationFields(props: { editor: Editor; object: Beam }) {
/>
<PropertyField label='Particle type'>
<ParticleSelect
particles={PARTICLE_TYPES as unknown as IParticleType[]}
particles={PARTICLE_TYPES}
value={watchedObject.particleData.id}
onChange={(_, v) =>
setValueCommand({ ...watchedObject.particleData, id: v }, 'particleData')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@ import { Euler, MathUtils, Vector3 } from 'three';
import { SetValueCommand } from '../../../../js/commands/SetValueCommand';
import { Editor } from '../../../../js/Editor';
import { Vector3PropertyField } from '../fields/PropertyField';
import { SimulationPropertiesType } from '../../../../../types/SimProperties';
import { SimulationPropertiesType } from '../../../../../types/SimulationProperties';
import { Beam, isBeam } from '../../../../Simulation/Physics/Beam';
import { isWorldZone, WorldZone } from '../../../../Simulation/Zones/WorldZone/WorldZone';
import { isWorldZone } from '../../../../Simulation/Zones/WorldZone/WorldZone';
import { isDetectGeometry } from '../../../../Simulation/Detectors/DetectGeometry';
import { SetDetectPositionCommand } from '../../../../js/commands/SetDetectPositionCommand';
import { SetPositionCommand } from '../../../../js/commands/SetPositionCommand';
import { SetRotationCommand } from '../../../../js/commands/SetRotationCommand';
import { SetBeamDirectionCommand } from '../../../../js/commands/SetBeamDirectionCommand';
import { PropertiesCategory } from './PropertiesCategory';
import { useSmartWatchEditorState } from '../../../../../util/hooks/signals';
import { ISimulationSceneChild } from '../../../../Simulation/Base/SimulationScene';
import { SimulationElement } from '../../../../Simulation/Base/SimulationElement';

export function ObjectPlacement(props: {
editor: Editor;
object: ISimulationSceneChild | Beam | WorldZone;
}) {
export function ObjectPlacement(props: { editor: Editor; object: SimulationElement }) {
const { object, editor } = props;

const { state: watchedObject } = useSmartWatchEditorState(editor, object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ import { Editor } from '../../../../js/Editor';
import { PropertiesCategory } from './PropertiesCategory';

export function BeamModifiersConfiguration(props: { editor: Editor }) {
const { editor } = props;

// const { state: watchedObject } = useSmartWatchEditorState(editor, object);

// const setValue = (key: string, value: any) =>
// editor.execute(new SetValueCommand(editor, watchedObject.object, key, value));

return (
<PropertiesCategory category='Beam Modulator'>
{/*
Expand Down
Loading

0 comments on commit 16cfad8

Please sign in to comment.