From 2f2f57a46f427a3e85455a2fe7062877bbbd010d Mon Sep 17 00:00:00 2001 From: Butterscotch! Date: Thu, 3 Nov 2022 16:35:25 -0400 Subject: [PATCH] Add a mounting rotation button to the GUI (#22) * Add support for mounting reset * Add reset mounting button * Don't display negative timer on reset buttons * Fix reset button timer going negative * Update package-lock.json --- package-lock.json | 4 +- src/components/MainLayout.tsx | 1 + src/components/commons/icon/ResetIcon.tsx | 26 ++++++++++ src/components/home/ResetButton.tsx | 61 ++++++++++++++++------- 4 files changed, 71 insertions(+), 21 deletions(-) diff --git a/package-lock.json b/package-lock.json index d0a5a0098..039577c72 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14786,7 +14786,7 @@ }, "node_modules/solarxr-protocol": { "version": "1.0.0", - "resolved": "git+ssh://git@github.com/SlimeVR/SolarXR-Protocol.git#f588c2e4adc70480d7dfe23f77621d1f2183ed47", + "resolved": "git+ssh://git@github.com/SlimeVR/SolarXR-Protocol.git#c6db24ca7f99dea8c093106cd30e36a3451d9f7c", "license": "(MIT OR Apache-2.0)", "dependencies": { "flatbuffers": "^2.0.6" @@ -25346,7 +25346,7 @@ } }, "solarxr-protocol": { - "version": "git+ssh://git@github.com/SlimeVR/SolarXR-Protocol.git#f588c2e4adc70480d7dfe23f77621d1f2183ed47", + "version": "git+ssh://git@github.com/SlimeVR/SolarXR-Protocol.git#c6db24ca7f99dea8c093106cd30e36a3451d9f7c", "from": "solarxr-protocol@github:SlimeVR/SolarXR-Protocol", "requires": { "flatbuffers": "^2.0.6" diff --git a/src/components/MainLayout.tsx b/src/components/MainLayout.tsx index ba7bbd4cb..f0b13312d 100644 --- a/src/components/MainLayout.tsx +++ b/src/components/MainLayout.tsx @@ -44,6 +44,7 @@ export function MainLayoutRoute({
+
diff --git a/src/components/commons/icon/ResetIcon.tsx b/src/components/commons/icon/ResetIcon.tsx index c6f7c067a..4c7778e92 100644 --- a/src/components/commons/icon/ResetIcon.tsx +++ b/src/components/commons/icon/ResetIcon.tsx @@ -54,3 +54,29 @@ export function QuickResetIcon({ width = 33 }: { width?: number }) { ); } + +export function MountingResetIcon({ width = 33 }: { width?: number }) { + return ( + + + + + ); +} diff --git a/src/components/home/ResetButton.tsx b/src/components/home/ResetButton.tsx index 5e7691ba0..6758f9815 100644 --- a/src/components/home/ResetButton.tsx +++ b/src/components/home/ResetButton.tsx @@ -2,7 +2,11 @@ import { useRef, useState } from 'react'; import { ResetRequestT, ResetType, RpcMessage } from 'solarxr-protocol'; import { useWebsocketAPI } from '../../hooks/websocket-api'; import { BigButton } from '../commons/BigButton'; -import { QuickResetIcon, ResetIcon } from '../commons/icon/ResetIcon'; +import { + MountingResetIcon, + QuickResetIcon, + ResetIcon, +} from '../commons/icon/ResetIcon'; export function ResetButton({ type }: { type: ResetType }) { const timerid = useRef(null); @@ -10,21 +14,52 @@ export function ResetButton({ type }: { type: ResetType }) { const [timer, setTimer] = useState(0); const { sendRPCPacket } = useWebsocketAPI(); + const getText = () => { + switch (type) { + case ResetType.Quick: + return 'Quick Reset'; + case ResetType.Mounting: + return 'Reset Mounting'; + } + return 'Reset'; + }; + + const getIcon = () => { + switch (type) { + case ResetType.Quick: + return ; + case ResetType.Mounting: + return ; + } + return ; + }; + const reset = () => { const req = new ResetRequestT(); req.resetType = type; setReseting(true); + setTimer(0); if (type !== ResetType.Quick) { if (timerid.current) clearInterval(timerid.current); timerid.current = setInterval(() => { setTimer((timer) => { - if (timer + 1 === 3) { + const newTimer = timer + 1; + if (newTimer >= 3) { + // Stop the current interval if (timerid.current) clearInterval(timerid.current); - sendRPCPacket(RpcMessage.ResetRequest, req); - setTimer(0); + + // Only actually reset on exactly 0 so it doesn't repeatedly reset if bugged + if (newTimer === 3) sendRPCPacket(RpcMessage.ResetRequest, req); + else + console.warn( + `Reset timer is still running after 3 seconds (newTimer = ${newTimer})` + ); + + // Reset the state + // Don't reset the timer in-case the interval keeps running setReseting(false); } - return timer + 1; + return newTimer; }); }, 1000); } else { @@ -35,20 +70,8 @@ export function ResetButton({ type }: { type: ResetType }) { return ( - ) : ( - - ) - } + text={!reseting || timer >= 3 ? getText() : `${3 - timer}`} + icon={getIcon()} onClick={reset} disabled={reseting} >