-
Notifications
You must be signed in to change notification settings - Fork 5
feat: Suppot live control #184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hhvrc
wants to merge
6
commits into
develop
Choose a base branch
from
feature/live-control
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+570
−8
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5dc5b60
Initial impl
hhvrc 76cff85
Merge branch 'develop' into feature/live-control
hhvrc f4cd322
Refactor writeFlash fileArray parameter syntax
hhvrc 688c998
Downgrade packageManager from pnpm@10.33.0 to pnpm@10.32.1
hhvrc 80a5d0f
Merge branch 'develop' into feature/live-control
hhvrc 68191ec
more stuff
hhvrc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
src/lib/components/ControlModules/LiveControlModule.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| <script lang="ts"> | ||
| import type { ShockerResponse } from '$lib/api/internal/v1'; | ||
| import { LoaderCircle, Pause, Play, Volume2, Waves, Zap } from '@lucide/svelte'; | ||
| import { shockersV1Api } from '$lib/api'; | ||
| import { handleApiError } from '$lib/errorhandling/apiErrorHandling'; | ||
| import { buttonVariants } from '$lib/components/ui/button/button.svelte'; | ||
| import { ControlType } from '$lib/signalr/models/ControlType'; | ||
| import { | ||
| type LiveShockerState, | ||
| type LiveDeviceConnection, | ||
| } from '$lib/state/live-control-state.svelte'; | ||
| import { cn } from '$lib/utils'; | ||
| import LiveSlider from './impl/LiveSlider.svelte'; | ||
| import ShockerMenu from './impl/ShockerMenu.svelte'; | ||
|
|
||
| interface Props { | ||
| shocker: ShockerResponse; | ||
| liveState: LiveShockerState; | ||
| connection: LiveDeviceConnection; | ||
| } | ||
|
|
||
| let { shocker, liveState, connection }: Props = $props(); | ||
|
|
||
| let resuming = $state(false); | ||
|
|
||
| async function resume() { | ||
| resuming = true; | ||
| try { | ||
| const result = await shockersV1Api.shockerPauseShocker(shocker.id, { pause: false }); | ||
| shocker.isPaused = result.data; | ||
| } catch (error) { | ||
| handleApiError(error); | ||
| } finally { | ||
| resuming = false; | ||
| } | ||
| } | ||
|
|
||
| const types = [ | ||
| { type: ControlType.Sound, Icon: Volume2, label: 'Sound' }, | ||
| { type: ControlType.Vibrate, Icon: Waves, label: 'Vibrate' }, | ||
| { type: ControlType.Shock, Icon: Zap, label: 'Shock' }, | ||
| ] as const; | ||
|
|
||
| const buttonClasses = buttonVariants({ variant: 'secondary', size: 'default' }); | ||
| </script> | ||
|
|
||
| <div | ||
| class="border-surface-400-500-token relative flex flex-col items-center justify-center gap-2 overflow-hidden rounded-md border p-2" | ||
| > | ||
| {#if shocker.isPaused} | ||
| <button | ||
| class="group absolute inset-0 z-10 flex cursor-pointer flex-col items-center justify-center gap-2 rounded-md bg-black/60 backdrop-blur-sm transition-colors hover:bg-black/50" | ||
| onclick={resume} | ||
| disabled={resuming} | ||
| > | ||
| {#if resuming} | ||
| <LoaderCircle class="size-8 animate-spin text-white" /> | ||
| {:else} | ||
| <Pause class="size-8 text-white/60 group-hover:hidden" /> | ||
| <Play class="hidden size-8 text-white group-hover:block" /> | ||
| {/if} | ||
| <span class="text-sm font-semibold text-white"> | ||
| {#if resuming}Resuming...{:else}<span class="group-hover:hidden">Paused</span><span | ||
| class="hidden group-hover:inline">Resume</span | ||
| >{/if} | ||
| </span> | ||
| </button> | ||
| {/if} | ||
| <!-- Title --> | ||
| <h2 class="flex w-full justify-between px-4 text-center text-lg font-bold"> | ||
| <span>{shocker.name}</span> | ||
| <ShockerMenu {shocker} /> | ||
| </h2> | ||
|
|
||
| <!-- Type Selector --> | ||
| <div class="flex w-full gap-2"> | ||
| {#each types as { type, Icon, label } (type)} | ||
| <button | ||
| class={cn(buttonClasses, 'flex-1', { | ||
| 'border-primary bg-primary/20': liveState.type === type, | ||
| })} | ||
| title={label} | ||
| aria-label={label} | ||
| aria-pressed={liveState.type === type} | ||
| onclick={() => (liveState.type = type)} | ||
| > | ||
| <Icon /> | ||
| </button> | ||
| {/each} | ||
| </div> | ||
|
|
||
| <!-- Live Slider --> | ||
| <div class="h-[200px] w-full"> | ||
| <LiveSlider {liveState} onRelease={() => connection.sendFrame(shocker.id, 0, liveState.type)} /> | ||
| </div> | ||
| </div> |
109 changes: 109 additions & 0 deletions
109
src/lib/components/ControlModules/impl/LiveSlider.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| <script lang="ts"> | ||
| import type { LiveShockerState } from '$lib/state/live-control-state.svelte'; | ||
|
|
||
| interface Props { | ||
| liveState: LiveShockerState; | ||
| maxIntensity?: number; | ||
| onRelease?: () => void; | ||
| } | ||
|
|
||
| let { liveState, maxIntensity = 100, onRelease }: Props = $props(); | ||
|
|
||
| let container: HTMLDivElement | undefined = $state(); | ||
| let y = $state(1); | ||
|
|
||
| let intensity = $derived(Math.round((1 - y) * maxIntensity)); | ||
|
|
||
| function startDrag(event: PointerEvent) { | ||
| if (!container) return; | ||
| liveState.isDragging = true; | ||
| container.setPointerCapture(event.pointerId); | ||
| updatePosition(event); | ||
| } | ||
|
|
||
| function onPointerMove(event: PointerEvent) { | ||
| if (!liveState.isDragging || !container) return; | ||
| updatePosition(event); | ||
| } | ||
|
|
||
| function stopDrag() { | ||
| liveState.isDragging = false; | ||
| y = 1; | ||
| liveState.intensity = 0; | ||
| onRelease?.(); | ||
| } | ||
|
|
||
| function updatePosition(event: PointerEvent) { | ||
| if (!container) return; | ||
| const rect = container.getBoundingClientRect(); | ||
| y = Math.min(1, Math.max(0, (event.clientY - rect.top) / rect.height)); | ||
| liveState.intensity = intensity; | ||
| } | ||
|
|
||
| const STEP = 0.05; | ||
|
|
||
| function onKeydown(event: KeyboardEvent) { | ||
| let handled = true; | ||
| switch (event.key) { | ||
| case 'ArrowUp': | ||
| case 'ArrowRight': | ||
| y = Math.max(0, y - STEP); | ||
| break; | ||
| case 'ArrowDown': | ||
| case 'ArrowLeft': | ||
| y = Math.min(1, y + STEP); | ||
| break; | ||
| case 'Home': | ||
| y = 0; | ||
| break; | ||
| case 'End': | ||
| y = 1; | ||
| break; | ||
| default: | ||
| handled = false; | ||
| } | ||
| if (handled) { | ||
| event.preventDefault(); | ||
| liveState.intensity = intensity; | ||
| } | ||
| } | ||
| </script> | ||
|
|
||
| <div class="relative h-full w-full p-4 select-none"> | ||
| <div | ||
| bind:this={container} | ||
| class="border-border relative h-full w-full cursor-pointer overflow-hidden rounded-md border" | ||
| onpointerdown={startDrag} | ||
| onpointermove={onPointerMove} | ||
| onpointerup={stopDrag} | ||
| onpointercancel={stopDrag} | ||
| onkeydown={onKeydown} | ||
| role="slider" | ||
| aria-valuenow={intensity} | ||
| aria-valuemin={0} | ||
| aria-valuemax={maxIntensity} | ||
| aria-label="Live intensity" | ||
| tabindex="0" | ||
| > | ||
| <!-- Fill from bottom --> | ||
| <div | ||
| class="bg-muted pointer-events-none absolute bottom-0 left-0 w-full transition-none" | ||
| style="height: {(1 - y) * 100}%" | ||
| ></div> | ||
|
|
||
| <!-- Handle --> | ||
| <div | ||
| class="pointer-events-none absolute -translate-x-1/2 -translate-y-1/2 rounded-full transition-none | ||
| {liveState.isDragging | ||
| ? 'border-border bg-primary h-12 w-12 border' | ||
| : 'bg-primary h-10 w-10 border-2 border-transparent'}" | ||
| style="left: 50%; top: {y * 100}%" | ||
| > | ||
| <span | ||
| class="text-primary-foreground flex h-full items-center justify-center text-sm font-medium" | ||
| > | ||
| {intensity}% | ||
| </span> | ||
| </div> | ||
| </div> | ||
| </div> | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the user releases the slider,
stopDrag()setsliveState.intensity = 0, but the gateway send loop only transmits frames whileliveState.isDraggingis true. This means no message is sent at release time, so the last non-zero intensity frame may remain effective until the server times out. Consider sending an explicit final frame with intensity 0 (and/or ControlType.Stop) on pointerup/cancel.