From c606b28ffe463430a873036aab9813e213d1dee3 Mon Sep 17 00:00:00 2001 From: Rafael Araujo Lehmkuhl Date: Fri, 3 Jan 2025 12:05:37 -0300 Subject: [PATCH] joystick: Improve naming and typing of interfaces --- .../mini-widgets/JoystickCommIndicator.vue | 8 +++--- src/libs/joystick/manager.ts | 28 +++++++++---------- src/stores/controller.ts | 10 +++---- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/components/mini-widgets/JoystickCommIndicator.vue b/src/components/mini-widgets/JoystickCommIndicator.vue index 5d6d701e2..73e297d62 100644 --- a/src/components/mini-widgets/JoystickCommIndicator.vue +++ b/src/components/mini-widgets/JoystickCommIndicator.vue @@ -48,7 +48,7 @@ import { computed, onMounted, ref, toRefs } from 'vue' import InteractionDialog from '@/components/InteractionDialog.vue' -import { joystickManager } from '@/libs/joystick/manager' +import { joystickManager, JoysticksMap } from '@/libs/joystick/manager' import { useControllerStore } from '@/stores/controller' import { useWidgetManagerStore } from '@/stores/widgetManager' import type { MiniWidget } from '@/types/widgets' @@ -69,12 +69,12 @@ const controllerStore = useControllerStore() const joystickConnected = ref(false) onMounted(() => { - joystickManager.onJoystickUpdate((event) => { - processJoystickEvent(event) + joystickManager.onJoystickConnectionUpdate((event) => { + processJoystickConnectionEvent(event) }) }) -const processJoystickEvent = (event: Map): void => { +const processJoystickConnectionEvent = (event: JoysticksMap): void => { joystickConnected.value = event.size !== 0 } diff --git a/src/libs/joystick/manager.ts b/src/libs/joystick/manager.ts index b4135ae4a..2f565e8c9 100644 --- a/src/libs/joystick/manager.ts +++ b/src/libs/joystick/manager.ts @@ -157,7 +157,7 @@ export type JoystickButtonEvent = { } } -export type JoysticConnectEvent = { +export type JoystickConnectEvent = { /** * Event type */ @@ -177,7 +177,7 @@ export type JoysticConnectEvent = { } } -export type JoysticDisconnectEvent = { +export type JoystickDisconnectEvent = { /** * Event type */ @@ -206,11 +206,11 @@ export type GamepadState = { export type JoysticksMap = Map -export type JoystickEvent = JoysticConnectEvent | JoysticDisconnectEvent +export type JoystickConnectionEvent = JoystickConnectEvent | JoystickDisconnectEvent export type JoystickStateEvent = JoystickAxisEvent | JoystickButtonEvent type CallbackJoystickStateEventType = (event: JoystickStateEvent) => void -type CallbackJoystickEventType = (event: JoysticksMap) => void +type CallbackJoystickConnectionEventType = (event: JoysticksMap) => void /** * Joystick Manager @@ -219,7 +219,7 @@ type CallbackJoystickEventType = (event: JoysticksMap) => void class JoystickManager { private static instance = new JoystickManager() - private callbacksJoystick: Array = [] + private callbacksJoystickConnection: Array = [] private callbacksJoystickState: Array = [] private joysticks: JoysticksMap = new Map() private enabledJoysticks: Array = [] @@ -245,8 +245,8 @@ class JoystickManager { * Callback to be used and receive joystick connection updates * @param {JoysticksMap} callback */ - onJoystickUpdate(callback: CallbackJoystickEventType): void { - this.callbacksJoystick.push(callback) + onJoystickConnectionUpdate(callback: CallbackJoystickConnectionEventType): void { + this.callbacksJoystickConnection.push(callback) } /** @@ -320,9 +320,9 @@ class JoystickManager { /** * Process joystick event internally - * @param {JoystickEvent} event + * @param {JoystickConnectionEvent} event */ - private processJoystickUpdate(event: JoystickEvent): void { + private processJoystickConnectionUpdate(event: JoystickConnectionEvent): void { const index = event.detail.index if (event.type == EventType.Connected) { @@ -338,7 +338,7 @@ class JoystickManager { this.joysticks.delete(index) } - for (const callback of this.callbacksJoystick) { + for (const callback of this.callbacksJoystickConnection) { callback(this.joysticks) } } @@ -375,14 +375,14 @@ class JoystickManager { */ private handleGamepadConnected(event: GamepadEvent): void { const gamepad = event.gamepad - const joystickEvent: JoysticConnectEvent = { + const joystickEvent: JoystickConnectEvent = { type: EventType.Connected, detail: { index: gamepad.index, gamepad: gamepad, }, } - this.processJoystickUpdate(joystickEvent) + this.processJoystickConnectionUpdate(joystickEvent) } /** @@ -390,13 +390,13 @@ class JoystickManager { * @param {GamepadEvent} event - Gamepad disconnection event */ private handleGamepadDisconnected(event: GamepadEvent): void { - const joystickEvent: JoysticDisconnectEvent = { + const joystickEvent: JoystickDisconnectEvent = { type: EventType.Disconnected, detail: { index: event.gamepad.index, }, } - this.processJoystickUpdate(joystickEvent) + this.processJoystickConnectionUpdate(joystickEvent) } /** diff --git a/src/stores/controller.ts b/src/stores/controller.ts index 96b19a7d9..91e03c896 100644 --- a/src/stores/controller.ts +++ b/src/stores/controller.ts @@ -12,7 +12,7 @@ import { import { useInteractionDialog } from '@/composables/interactionDialog' import { useBlueOsStorage } from '@/composables/settingsSyncer' import { MavType } from '@/libs/connection/m2r/messages/mavlink2rest-enum' -import { type JoystickEvent, EventType, joystickManager, JoystickModel } from '@/libs/joystick/manager' +import { joystickManager, JoystickModel, JoysticksMap, JoystickStateEvent } from '@/libs/joystick/manager' import { allAvailableAxes, allAvailableButtons } from '@/libs/joystick/protocols' import { CockpitActionsFunction, executeActionCallback } from '@/libs/joystick/protocols/cockpit-actions' import { modifierKeyActions, otherAvailableActions } from '@/libs/joystick/protocols/other' @@ -126,10 +126,10 @@ export const useControllerStore = defineStore('controller', () => { updateCallbacks.value.push(callback) } - joystickManager.onJoystickUpdate((event) => processJoystickEvent(event)) + joystickManager.onJoystickConnectionUpdate((event) => processJoystickConnectionEvent(event)) joystickManager.onJoystickStateUpdate((event) => processJoystickStateEvent(event)) - const processJoystickEvent = (event: Map): void => { + const processJoystickConnectionEvent = (event: JoysticksMap): void => { const newMap = new Map(Array.from(event).map(([index, gamepad]) => [index, new Joystick(gamepad)])) // Add new joysticks @@ -173,9 +173,9 @@ export const useControllerStore = defineStore('controller', () => { const { showDialog } = useInteractionDialog() - const processJoystickStateEvent = (event: JoystickEvent): void => { + const processJoystickStateEvent = (event: JoystickStateEvent): void => { const joystick = joysticks.value.get(event.detail.index) - if (joystick === undefined || (event.type !== EventType.Axis && event.type !== EventType.Button)) return + if (joystick === undefined) return joystick.gamepad = event.detail.gamepad const joystickModel = joystick.model || JoystickModel.Unknown