Skip to content

Commit 8b32254

Browse files
Add React hook for physics pausing [SYNTH-326] (#1481)
Co-authored-by: Zach Rutman <92497727+rutmanz@users.noreply.github.com>
2 parents 06d59c4 + 0bf80b8 commit 8b32254

9 files changed

Lines changed: 32 additions & 63 deletions

File tree

fission/src/ui/panels/configuring/assembly-config/interfaces/ConfigureCameraPointsInterface.tsx

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ import { useCallback, useEffect, useRef, useState } from "react"
33
import * as THREE from "three"
44
import type MirabufSceneObject from "@/mirabuf/MirabufSceneObject"
55
import EventSystem from "@/systems/EventSystem.ts"
6-
import { PAUSE_REF_ASSEMBLY_CONFIG } from "@/systems/physics/PhysicsTypes"
76
import PreferencesSystem from "@/systems/preferences/PreferencesSystem"
87
import type { CameraLook, CameraPoint } from "@/systems/preferences/PreferenceTypes"
98
import type GizmoSceneObject from "@/systems/scene/GizmoSceneObject"
10-
import World from "@/systems/World"
119
import Label from "@/ui/components/Label"
1210
import ScrollView from "@/ui/components/ScrollView"
1311
import { AddButton, DeleteButton, EditButton } from "@/ui/components/StyledComponents"
1412
import TransformGizmoControl from "@/ui/components/TransformGizmoControl"
1513
import type { ConfigurationSubpanelComponent } from "@/panels/configuring/assembly-config/ConfigTypes.ts"
14+
import { useHoldPhysicsPause } from "@/util/ReactHooks.ts"
1615

1716
const RAD_TO_DEG = 180 / Math.PI
1817
const DEG_TO_RAD = Math.PI / 180
@@ -48,12 +47,10 @@ const ListView: React.FC<ListViewProps> = ({ selectedField, points, onChange, on
4847
useEffect(() => EventSystem.listen("ConfigurationSavedEvent", saveEvent), [saveEvent])
4948
useEffect(() => {
5049
persist(points, selectedField)
51-
World.physicsSystem.holdPause(PAUSE_REF_ASSEMBLY_CONFIG)
52-
return () => {
53-
World.physicsSystem.releasePause(PAUSE_REF_ASSEMBLY_CONFIG)
54-
}
5550
}, [selectedField, points])
5651

52+
useHoldPhysicsPause()
53+
5754
return (
5855
<>
5956
{points.length > 0 ? (
@@ -134,12 +131,7 @@ const EditView: React.FC<EditViewProps> = ({ selectedField, point, onSave }) =>
134131
}, [selectedField, point.pos, lookType, name, yawDeg, pitchDeg])
135132

136133
useEffect(() => EventSystem.listen("ConfigurationSavedEvent", () => onSave(buildPoint())), [buildPoint, onSave])
137-
useEffect(() => {
138-
World.physicsSystem.holdPause(PAUSE_REF_ASSEMBLY_CONFIG)
139-
return () => {
140-
World.physicsSystem.releasePause(PAUSE_REF_ASSEMBLY_CONFIG)
141-
}
142-
}, [])
134+
useHoldPhysicsPause()
143135

144136
return (
145137
<Stack gap={2} className="bg-background-secondary rounded-md p-2">

fission/src/ui/panels/configuring/assembly-config/interfaces/ConfigureGamepieceEjectorInterface.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type { RigidNodeId } from "@/mirabuf/MirabufParser"
77
import type MirabufSceneObject from "@/mirabuf/MirabufSceneObject"
88
import type { RigidNodeAssociate } from "@/mirabuf/MirabufSceneObject"
99
import EventSystem from "@/systems/EventSystem.ts"
10-
import { PAUSE_REF_ASSEMBLY_CONFIG } from "@/systems/physics/PhysicsTypes"
1110
import type GizmoSceneObject from "@/systems/scene/GizmoSceneObject"
1211
import World from "@/systems/World"
1312
import StatefulSlider from "@/ui/components/StatefulSlider"
@@ -20,6 +19,7 @@ import {
2019
convertThreeMatrix4ToArray,
2120
} from "@/util/TypeConversions"
2221
import type { ConfigurationSubpanelComponent } from "@/panels/configuring/assembly-config/ConfigTypes.ts"
22+
import { useHoldPhysicsPause } from "@/util/ReactHooks.ts"
2323

2424
// slider constants
2525
const MIN_VELOCITY = 0.0
@@ -183,13 +183,7 @@ const ConfigureGamepieceEjectorInterface: ConfigurationSubpanelComponent = ({
183183
}
184184
}, [selectedAssembly])
185185

186-
useEffect(() => {
187-
World.physicsSystem.holdPause(PAUSE_REF_ASSEMBLY_CONFIG)
188-
189-
return () => {
190-
World.physicsSystem.releasePause(PAUSE_REF_ASSEMBLY_CONFIG)
191-
}
192-
}, [])
186+
useHoldPhysicsPause()
193187

194188
const trySetSelectedNode = useCallback(
195189
(body: Jolt.BodyID) => {

fission/src/ui/panels/configuring/assembly-config/interfaces/ConfigureGamepieceIntakeInterface.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import type { RigidNodeId } from "@/mirabuf/MirabufParser"
88
import type MirabufSceneObject from "@/mirabuf/MirabufSceneObject"
99
import type { RigidNodeAssociate } from "@/mirabuf/MirabufSceneObject"
1010
import EventSystem from "@/systems/EventSystem.ts"
11-
import { PAUSE_REF_ASSEMBLY_CONFIG } from "@/systems/physics/PhysicsTypes"
1211
import type GizmoSceneObject from "@/systems/scene/GizmoSceneObject"
1312
import World from "@/systems/World"
1413
import Checkbox from "@/ui/components/Checkbox"
@@ -22,6 +21,7 @@ import {
2221
convertThreeMatrix4ToArray,
2322
} from "@/util/TypeConversions"
2423
import type { ConfigurationSubpanelComponent } from "@/panels/configuring/assembly-config/ConfigTypes.ts"
24+
import { useHoldPhysicsPause } from "@/util/ReactHooks.ts"
2525

2626
// slider constants
2727
const MIN_ZONE_SIZE = 0.1
@@ -210,17 +210,15 @@ const ConfigureGamepieceIntakeInterface: ConfigurationSubpanelComponent = ({
210210
}
211211
}, [selectedAssembly])
212212

213-
useEffect(() => {
214-
World.physicsSystem.holdPause(PAUSE_REF_ASSEMBLY_CONFIG)
213+
useHoldPhysicsPause()
215214

215+
useEffect(() => {
216216
// Hide the visual indicator when entering configuration mode
217217
if (selectedAssembly) {
218218
selectedAssembly.setIntakeVisualIndicatorVisible(false)
219219
}
220220

221221
return () => {
222-
World.physicsSystem.releasePause(PAUSE_REF_ASSEMBLY_CONFIG)
223-
224222
// Show the visual indicator when exiting configuration mode
225223
if (selectedAssembly) {
226224
selectedAssembly.setIntakeVisualIndicatorVisible(true)

fission/src/ui/panels/configuring/assembly-config/interfaces/MoveInterface.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,12 @@ import { CloseType, useUIContext } from "@/ui/helpers/UIProviderHelpers.ts"
44
import { useEffect } from "react"
55
import World from "@/systems/World.ts"
66
import JOLT from "@/util/loading/JoltSyncLoader.ts"
7-
import { PAUSE_REF_ASSEMBLY_MOVE } from "@/systems/physics/PhysicsTypes.ts"
7+
import { useHoldPhysicsPause } from "@/util/ReactHooks.ts"
88

99
const MoveInterface: ConfigurationSubpanelComponent = ({ selectedAssembly, panel, registerCleanupFunction }) => {
1010
const { closePanel } = useUIContext()
1111

12-
// Keep physics paused while the gizmo is up, otherwise the assembly drifts
13-
// out from under the handles as it is being positioned.
14-
useEffect(() => {
15-
World.physicsSystem.holdPause(PAUSE_REF_ASSEMBLY_MOVE)
16-
return () => {
17-
World.physicsSystem.releasePause(PAUSE_REF_ASSEMBLY_MOVE)
18-
}
19-
}, [])
12+
useHoldPhysicsPause()
2013

2114
useEffect(() => {
2215
const scaleVec = new JOLT.Vec3()

fission/src/ui/panels/configuring/assembly-config/interfaces/cameras/ConfigureCameraInterface.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { Box, Divider, Stack } from "@mui/material"
22
import { useCallback, useEffect, useState } from "react"
3-
import { PAUSE_REF_ASSEMBLY_CONFIG } from "@/systems/physics/PhysicsTypes"
43
import { type CameraPreferences, defaultCameraPreferences } from "@/systems/preferences/PreferenceTypes"
5-
import World from "@/systems/World"
64
import { Button, DeleteButton, EditButton, SynthesisIcons } from "@/ui/components/StyledComponents"
75
import CameraConfigInterface from "./CameraConfigInterface"
86
import Label from "@/ui/components/Label"
97
import { SelectMenuHeader } from "@/ui/components/SelectMenu"
108
import EventSystem from "@/systems/EventSystem"
119
import type { ConfigurationSubpanelComponent } from "../../ConfigTypes"
10+
import { useHoldPhysicsPause } from "@/util/ReactHooks.ts"
1211

1312
const ConfigureCameraInterface: ConfigurationSubpanelComponent = ({
1413
selectedAssembly,
@@ -22,12 +21,7 @@ const ConfigureCameraInterface: ConfigurationSubpanelComponent = ({
2221

2322
const forceRender = useCallback(() => setVersion(v => v + 1), [])
2423

25-
useEffect(() => {
26-
World.physicsSystem.holdPause(PAUSE_REF_ASSEMBLY_CONFIG)
27-
return () => {
28-
World.physicsSystem.releasePause(PAUSE_REF_ASSEMBLY_CONFIG)
29-
}
30-
}, [])
24+
useHoldPhysicsPause()
3125

3226
useEffect(() => {
3327
const originalCameras = structuredClone(selectedAssembly.cameraPreferences)

fission/src/ui/panels/configuring/assembly-config/interfaces/zones/ManageZonesBase.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import { Box, Stack } from "@mui/material"
22
import { useCallback, useEffect, useState } from "react"
33
import type MirabufSceneObject from "@/mirabuf/MirabufSceneObject"
44
import EventSystem from "@/systems/EventSystem.ts"
5-
import { PAUSE_REF_ASSEMBLY_CONFIG } from "@/systems/physics/PhysicsTypes"
65
import type { Alliance } from "@/systems/preferences/PreferenceTypes"
7-
import World from "@/systems/World"
86
import Label from "@/ui/components/Label"
97
import { Button, DeleteButton, EditButton, SynthesisIcons } from "@/ui/components/StyledComponents"
108
import type { BaseZonePreferences } from "./ZoneConfigBase"
9+
import { useHoldPhysicsPause } from "@/util/ReactHooks.ts"
1110

1211
export type ZoneListItem = {
1312
name: string
@@ -65,12 +64,10 @@ export default function ManageZonesBase<TZone extends BaseZonePreferences>(props
6564
return EventSystem.listen("ConfigurationSavedEvent", saveEvent)
6665
}, [saveEvent])
6766

67+
useHoldPhysicsPause()
68+
6869
useEffect(() => {
6970
saveZonesGeneric(zones, selectedField, persistZones)
70-
World.physicsSystem.holdPause(PAUSE_REF_ASSEMBLY_CONFIG)
71-
return () => {
72-
World.physicsSystem.releasePause(PAUSE_REF_ASSEMBLY_CONFIG)
73-
}
7471
}, [selectedField, zones, persistZones])
7572

7673
return (

fission/src/ui/panels/configuring/assembly-config/interfaces/zones/ZoneConfigBase.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type { RigidNodeId } from "@/mirabuf/MirabufParser"
66
import EventSystem from "@/systems/EventSystem.ts"
77
import type MirabufSceneObject from "@/mirabuf/MirabufSceneObject"
88
import type { RigidNodeAssociate } from "@/mirabuf/MirabufSceneObject"
9-
import { PAUSE_REF_ASSEMBLY_CONFIG } from "@/systems/physics/PhysicsTypes"
109
import type { Alliance } from "@/systems/preferences/PreferenceTypes"
1110
import type GizmoSceneObject from "@/systems/scene/GizmoSceneObject"
1211
import World from "@/systems/World"
@@ -18,6 +17,7 @@ import {
1817
convertThreeMatrix4ToArray,
1918
} from "@/util/TypeConversions"
2019
import { deltaFieldTransformsPhysicalProp } from "@/util/threejs/MeshCreation"
20+
import { useHoldPhysicsPause } from "@/util/ReactHooks.ts"
2121

2222
/**
2323
* Saves zone configuration to selected field.
@@ -164,12 +164,7 @@ export default function ZoneConfigBase<TZone extends BaseZonePreferences>(props:
164164
return EventSystem.listen("ConfigurationSavedEvent", saveEvent)
165165
}, [saveEvent])
166166

167-
useEffect(() => {
168-
World.physicsSystem.holdPause(PAUSE_REF_ASSEMBLY_CONFIG)
169-
return () => {
170-
World.physicsSystem.releasePause(PAUSE_REF_ASSEMBLY_CONFIG)
171-
}
172-
}, [])
167+
useHoldPhysicsPause()
173168

174169
const defaultGizmoMesh = useMemo(() => {
175170
if (!selectedZone) return undefined

fission/src/ui/panels/configuring/initial-config/InitialConfigPanel.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import EventSystem from "@/systems/EventSystem.ts"
44
import InputSchemeManager from "@/systems/input/InputSchemeManager"
55
import InputSystem from "@/systems/input/InputSystem"
66
import { InputSchemeUseType } from "@/systems/input/InputTypes"
7-
import { PAUSE_REF_ASSEMBLY_MOVE } from "@/systems/physics/PhysicsTypes"
87
import type { Alliance, Station } from "@/systems/preferences/PreferenceTypes"
98
import SynthesisBrain from "@/systems/simulation/synthesis_brain/SynthesisBrain"
109
import World from "@/systems/World"
@@ -18,6 +17,7 @@ import { Box, Stack } from "@mui/material"
1817
import type React from "react"
1918
import { useCallback, useEffect, useMemo, useState } from "react"
2019
import InputSchemeSelection from "./InputSchemeSelection"
20+
import { useHoldPhysicsPause } from "@/util/ReactHooks.ts"
2121

2222
const InitialConfigPanel: React.FC<PanelImplProps<void, void>> = ({ panel }) => {
2323
// TODO: can we pass these as custom props?
@@ -28,13 +28,7 @@ const InitialConfigPanel: React.FC<PanelImplProps<void, void>> = ({ panel }) =>
2828

2929
const targetAssembly = useMemo(() => getSpotlightAssembly(), [])
3030

31-
useEffect(() => {
32-
World.physicsSystem.holdPause(PAUSE_REF_ASSEMBLY_MOVE)
33-
34-
return () => {
35-
World.physicsSystem.releasePause(PAUSE_REF_ASSEMBLY_MOVE)
36-
}
37-
}, [])
31+
useHoldPhysicsPause()
3832

3933
const closeFinish = useCallback(() => {
4034
if (targetAssembly?.miraType === MiraType.ROBOT) {

fission/src/util/ReactHooks.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { useEffect, useId } from "react"
2+
import World from "@/systems/World.ts"
3+
4+
export function useHoldPhysicsPause() {
5+
const id = useId()
6+
useEffect(() => {
7+
World.physicsSystem.holdPause(id)
8+
return () => {
9+
World.physicsSystem.releasePause(id)
10+
}
11+
})
12+
}

0 commit comments

Comments
 (0)