Skip to content

Commit

Permalink
joystick: Improve naming and typing of interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaellehmkuhl committed Jan 17, 2025
1 parent 6ce5da3 commit c606b28
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/components/mini-widgets/JoystickCommIndicator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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<number, Gamepad>): void => {
const processJoystickConnectionEvent = (event: JoysticksMap): void => {
joystickConnected.value = event.size !== 0
}
Expand Down
28 changes: 14 additions & 14 deletions src/libs/joystick/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export type JoystickButtonEvent = {
}
}

export type JoysticConnectEvent = {
export type JoystickConnectEvent = {
/**
* Event type
*/
Expand All @@ -177,7 +177,7 @@ export type JoysticConnectEvent = {
}
}

export type JoysticDisconnectEvent = {
export type JoystickDisconnectEvent = {
/**
* Event type
*/
Expand Down Expand Up @@ -206,11 +206,11 @@ export type GamepadState = {

export type JoysticksMap = Map<number, Gamepad>

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
Expand All @@ -219,7 +219,7 @@ type CallbackJoystickEventType = (event: JoysticksMap) => void
class JoystickManager {
private static instance = new JoystickManager()

private callbacksJoystick: Array<CallbackJoystickEventType> = []
private callbacksJoystickConnection: Array<CallbackJoystickConnectionEventType> = []
private callbacksJoystickState: Array<CallbackJoystickStateEventType> = []
private joysticks: JoysticksMap = new Map()
private enabledJoysticks: Array<string> = []
Expand All @@ -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)
}

/**
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -375,28 +375,28 @@ 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)
}

/**
* Handle gamepad disconnection event
* @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)
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/stores/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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<number, Gamepad>): void => {
const processJoystickConnectionEvent = (event: JoysticksMap): void => {
const newMap = new Map(Array.from(event).map(([index, gamepad]) => [index, new Joystick(gamepad)]))

// Add new joysticks
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c606b28

Please sign in to comment.