|
| 1 | +import type { Context } from '@model/context'; |
| 2 | +import type { AudioService } from '@service/audio'; |
| 3 | +import type { Command } from './command'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Command that routes pointer/touch movement into guidance sonification. |
| 7 | + * |
| 8 | + * This keeps pointer input handling in the command flow rather than |
| 9 | + * calling AudioService directly from input-binding services. |
| 10 | + */ |
| 11 | +export class PointerGuidanceCommand implements Command { |
| 12 | + private readonly context: Context; |
| 13 | + private readonly audioService: AudioService; |
| 14 | + |
| 15 | + /** |
| 16 | + * Creates an instance of PointerGuidanceCommand. |
| 17 | + * |
| 18 | + * @param context - Application context used for point navigation and guidance lookup |
| 19 | + * @param audioService - Audio service that renders guidance beeps |
| 20 | + */ |
| 21 | + public constructor(context: Context, audioService: AudioService) { |
| 22 | + this.context = context; |
| 23 | + this.audioService = audioService; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Executes pointer/touch guidance behavior. |
| 28 | + * |
| 29 | + * If an event with client coordinates is provided, updates nearest point |
| 30 | + * navigation and plays directional guidance. If no event is provided, |
| 31 | + * guidance is reset (used for pointer leave / unregister). |
| 32 | + * |
| 33 | + * @param event - Optional pointer/mouse event containing clientX/clientY |
| 34 | + */ |
| 35 | + public execute(event?: Event): void { |
| 36 | + if (!event || !this.hasClientCoordinates(event)) { |
| 37 | + this.audioService.playTouchGuidance(null); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + const { clientX, clientY } = event; |
| 42 | + this.context.moveToPoint(clientX, clientY); |
| 43 | + |
| 44 | + const guidance = this.context.getTouchGuidance(clientX, clientY); |
| 45 | + this.audioService.playTouchGuidance(guidance); |
| 46 | + } |
| 47 | + |
| 48 | + private hasClientCoordinates( |
| 49 | + event: Event, |
| 50 | + ): event is Event & { clientX: number; clientY: number } { |
| 51 | + const candidate = event as Partial<{ clientX: unknown; clientY: unknown }>; |
| 52 | + return typeof candidate.clientX === 'number' && typeof candidate.clientY === 'number'; |
| 53 | + } |
| 54 | +} |
0 commit comments