mirror of
https://github.com/redphx/better-xcloud.git
synced 2025-08-06 21:28:27 +02:00
6.0
This commit is contained in:
551
src/modules/mkb/mkb-handler.ts
Normal file → Executable file
551
src/modules/mkb/mkb-handler.ts
Normal file → Executable file
@@ -1,24 +1,21 @@
|
||||
import { isFullVersion } from "@macros/build" with {type: "macro"};
|
||||
|
||||
import { MkbPreset } from "./mkb-preset";
|
||||
import { GamepadKey, MkbPresetKey, GamepadStick, MouseMapTo, WheelCode } from "@enums/mkb";
|
||||
import { createButton, ButtonStyle, CE } from "@utils/html";
|
||||
import { MkbPresetKey, MouseConstant, MouseMapTo, WheelCode } from "@/enums/mkb";
|
||||
import { BxEvent } from "@utils/bx-event";
|
||||
import { Toast } from "@utils/toast";
|
||||
import { t } from "@utils/translation";
|
||||
import { KeyHelper } from "./key-helper";
|
||||
import type { MkbStoredPreset } from "@/types/mkb";
|
||||
import { AppInterface, STATES } from "@utils/global";
|
||||
import { UserAgent } from "@utils/user-agent";
|
||||
import { BxLogger } from "@utils/bx-logger";
|
||||
import { PointerClient } from "./pointer-client";
|
||||
import { NativeMkbHandler } from "./native-mkb-handler";
|
||||
import { MkbHandler, MouseDataProvider } from "./base-mkb-handler";
|
||||
import { SettingsNavigationDialog } from "../ui/dialog/settings-dialog";
|
||||
import { NavigationDialogManager } from "../ui/dialog/navigation-dialog";
|
||||
import { PrefKey } from "@/enums/pref-keys";
|
||||
import { getPref } from "@/utils/settings-storages/global-settings-storage";
|
||||
import { MkbPresetsDb } from "@/utils/local-db/mkb-presets-db";
|
||||
import { GamepadKey, GamepadStick } from "@/enums/gamepad";
|
||||
import { MkbPopup } from "./mkb-popup";
|
||||
import type { MkbConvertedPresetData } from "@/types/presets";
|
||||
|
||||
const PointerToMouseButton = {
|
||||
1: 0,
|
||||
@@ -26,79 +23,74 @@ const PointerToMouseButton = {
|
||||
4: 1,
|
||||
}
|
||||
|
||||
export const VIRTUAL_GAMEPAD_ID = 'Xbox 360 Controller';
|
||||
export const VIRTUAL_GAMEPAD_ID = 'Better xCloud Virtual Controller';
|
||||
|
||||
class WebSocketMouseDataProvider extends MouseDataProvider {
|
||||
#pointerClient: PointerClient | undefined
|
||||
#connected = false
|
||||
private pointerClient: PointerClient | undefined
|
||||
private isConnected = false
|
||||
|
||||
init(): void {
|
||||
this.#pointerClient = PointerClient.getInstance();
|
||||
this.#connected = false;
|
||||
this.pointerClient = PointerClient.getInstance();
|
||||
this.isConnected = false;
|
||||
try {
|
||||
this.#pointerClient.start(STATES.pointerServerPort, this.mkbHandler);
|
||||
this.#connected = true;
|
||||
this.pointerClient.start(STATES.pointerServerPort, this.mkbHandler);
|
||||
this.isConnected = true;
|
||||
} catch (e) {
|
||||
Toast.show('Cannot enable Mouse & Keyboard feature');
|
||||
}
|
||||
}
|
||||
|
||||
start(): void {
|
||||
this.#connected && AppInterface.requestPointerCapture();
|
||||
this.isConnected && AppInterface.requestPointerCapture();
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
this.#connected && AppInterface.releasePointerCapture();
|
||||
this.isConnected && AppInterface.releasePointerCapture();
|
||||
}
|
||||
|
||||
destroy(): void {
|
||||
this.#connected && this.#pointerClient?.stop();
|
||||
this.isConnected && this.pointerClient?.stop();
|
||||
}
|
||||
}
|
||||
|
||||
class PointerLockMouseDataProvider extends MouseDataProvider {
|
||||
init(): void {}
|
||||
|
||||
start(): void {
|
||||
window.addEventListener('mousemove', this.#onMouseMoveEvent);
|
||||
window.addEventListener('mousedown', this.#onMouseEvent);
|
||||
window.addEventListener('mouseup', this.#onMouseEvent);
|
||||
window.addEventListener('wheel', this.#onWheelEvent, {passive: false});
|
||||
window.addEventListener('contextmenu', this.#disableContextMenu);
|
||||
start() {
|
||||
window.addEventListener('mousemove', this.onMouseMoveEvent);
|
||||
window.addEventListener('mousedown', this.onMouseEvent);
|
||||
window.addEventListener('mouseup', this.onMouseEvent);
|
||||
window.addEventListener('wheel', this.onWheelEvent, { passive: false });
|
||||
window.addEventListener('contextmenu', this.disableContextMenu);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
stop() {
|
||||
document.pointerLockElement && document.exitPointerLock();
|
||||
|
||||
window.removeEventListener('mousemove', this.#onMouseMoveEvent);
|
||||
window.removeEventListener('mousedown', this.#onMouseEvent);
|
||||
window.removeEventListener('mouseup', this.#onMouseEvent);
|
||||
window.removeEventListener('wheel', this.#onWheelEvent);
|
||||
window.removeEventListener('contextmenu', this.#disableContextMenu);
|
||||
window.removeEventListener('mousemove', this.onMouseMoveEvent);
|
||||
window.removeEventListener('mousedown', this.onMouseEvent);
|
||||
window.removeEventListener('mouseup', this.onMouseEvent);
|
||||
window.removeEventListener('wheel', this.onWheelEvent);
|
||||
window.removeEventListener('contextmenu', this.disableContextMenu);
|
||||
}
|
||||
|
||||
destroy(): void {}
|
||||
|
||||
#onMouseMoveEvent = (e: MouseEvent) => {
|
||||
private onMouseMoveEvent = (e: MouseEvent) => {
|
||||
this.mkbHandler.handleMouseMove({
|
||||
movementX: e.movementX,
|
||||
movementY: e.movementY,
|
||||
});
|
||||
}
|
||||
|
||||
#onMouseEvent = (e: MouseEvent) => {
|
||||
private onMouseEvent = (e: MouseEvent) => {
|
||||
e.preventDefault();
|
||||
|
||||
const isMouseDown = e.type === 'mousedown';
|
||||
const data: MkbMouseClick = {
|
||||
mouseButton: e.button,
|
||||
pressed: isMouseDown,
|
||||
pressed: e.type === 'mousedown',
|
||||
};
|
||||
|
||||
this.mkbHandler.handleMouseClick(data);
|
||||
}
|
||||
|
||||
#onWheelEvent = (e: WheelEvent) => {
|
||||
private onWheelEvent = (e: WheelEvent) => {
|
||||
const key = KeyHelper.getKeyFromEvent(e);
|
||||
if (!key) {
|
||||
return;
|
||||
@@ -114,7 +106,7 @@ class PointerLockMouseDataProvider extends MouseDataProvider {
|
||||
}
|
||||
}
|
||||
|
||||
#disableContextMenu = (e: Event) => e.preventDefault();
|
||||
private disableContextMenu = (e: Event) => e.preventDefault();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -122,80 +114,95 @@ This class uses some code from Yuzu emulator to handle mouse's movements
|
||||
Source: https://github.com/yuzu-emu/yuzu-mainline/blob/master/src/input_common/drivers/mouse.cpp
|
||||
*/
|
||||
export class EmulatedMkbHandler extends MkbHandler {
|
||||
private static instance: EmulatedMkbHandler;
|
||||
public static getInstance = () => EmulatedMkbHandler.instance ?? (EmulatedMkbHandler.instance = new EmulatedMkbHandler());
|
||||
private static instance: EmulatedMkbHandler | null | undefined;
|
||||
public static getInstance(): typeof EmulatedMkbHandler['instance'] {
|
||||
if (typeof EmulatedMkbHandler.instance === 'undefined') {
|
||||
if (EmulatedMkbHandler.isAllowed()) {
|
||||
EmulatedMkbHandler.instance = new EmulatedMkbHandler();
|
||||
} else {
|
||||
EmulatedMkbHandler.instance = null;
|
||||
}
|
||||
}
|
||||
|
||||
return EmulatedMkbHandler.instance;
|
||||
}
|
||||
|
||||
private static readonly LOG_TAG = 'EmulatedMkbHandler';
|
||||
|
||||
#CURRENT_PRESET_DATA = MkbPreset.convert(MkbPreset.DEFAULT_PRESET);
|
||||
static isAllowed() {
|
||||
return getPref(PrefKey.MKB_ENABLED) && (AppInterface || !UserAgent.isMobile());
|
||||
}
|
||||
|
||||
static readonly DEFAULT_PANNING_SENSITIVITY = 0.0010;
|
||||
static readonly DEFAULT_DEADZONE_COUNTERWEIGHT = 0.01;
|
||||
static readonly MAXIMUM_STICK_RANGE = 1.1;
|
||||
private PRESET!: MkbConvertedPresetData | null;
|
||||
private VIRTUAL_GAMEPAD = {
|
||||
id: VIRTUAL_GAMEPAD_ID,
|
||||
index: 0,
|
||||
connected: false,
|
||||
hapticActuators: null,
|
||||
mapping: 'standard',
|
||||
|
||||
#VIRTUAL_GAMEPAD = {
|
||||
id: VIRTUAL_GAMEPAD_ID,
|
||||
index: 3,
|
||||
connected: false,
|
||||
hapticActuators: null,
|
||||
mapping: 'standard',
|
||||
axes: [0, 0, 0, 0],
|
||||
buttons: new Array(17).fill(null).map(() => ({pressed: false, value: 0})),
|
||||
timestamp: performance.now(),
|
||||
|
||||
axes: [0, 0, 0, 0],
|
||||
buttons: new Array(17).fill(null).map(() => ({pressed: false, value: 0})),
|
||||
timestamp: performance.now(),
|
||||
vibrationActuator: null,
|
||||
};
|
||||
private nativeGetGamepads: Navigator['getGamepads'];
|
||||
|
||||
vibrationActuator: null,
|
||||
};
|
||||
#nativeGetGamepads = window.navigator.getGamepads.bind(window.navigator);
|
||||
private initialized = false;
|
||||
private enabled = false;
|
||||
private mouseDataProvider: MouseDataProvider | undefined;
|
||||
private isPolling = false;
|
||||
|
||||
#enabled = false;
|
||||
#mouseDataProvider: MouseDataProvider | undefined;
|
||||
#isPolling = false;
|
||||
private prevWheelCode = null;
|
||||
private wheelStoppedTimeoutId: number | null = null;
|
||||
|
||||
#prevWheelCode = null;
|
||||
#wheelStoppedTimeout?: number | null;
|
||||
private detectMouseStoppedTimeoutId: number | null = null;
|
||||
|
||||
#detectMouseStoppedTimeout?: number | null;
|
||||
private escKeyDownTime: number = -1;
|
||||
|
||||
#$message?: HTMLElement;
|
||||
private LEFT_STICK_X: GamepadKey[] = [];
|
||||
private LEFT_STICK_Y: GamepadKey[] = [];
|
||||
private RIGHT_STICK_X: GamepadKey[] = [];
|
||||
private RIGHT_STICK_Y: GamepadKey[] = [];
|
||||
|
||||
#escKeyDownTime: number = -1;
|
||||
private popup: MkbPopup;
|
||||
|
||||
#STICK_MAP: {[key in GamepadKey]?: [GamepadKey[], number, number]};
|
||||
#LEFT_STICK_X: GamepadKey[] = [];
|
||||
#LEFT_STICK_Y: GamepadKey[] = [];
|
||||
#RIGHT_STICK_X: GamepadKey[] = [];
|
||||
#RIGHT_STICK_Y: GamepadKey[] = [];
|
||||
private STICK_MAP: {[key in GamepadKey]?: [GamepadKey[], number, number]} = {
|
||||
[GamepadKey.LS_LEFT]: [this.LEFT_STICK_X, 0, -1],
|
||||
[GamepadKey.LS_RIGHT]: [this.LEFT_STICK_X, 0, 1],
|
||||
[GamepadKey.LS_UP]: [this.LEFT_STICK_Y, 1, -1],
|
||||
[GamepadKey.LS_DOWN]: [this.LEFT_STICK_Y, 1, 1],
|
||||
|
||||
[GamepadKey.RS_LEFT]: [this.RIGHT_STICK_X, 2, -1],
|
||||
[GamepadKey.RS_RIGHT]: [this.RIGHT_STICK_X, 2, 1],
|
||||
[GamepadKey.RS_UP]: [this.RIGHT_STICK_Y, 3, -1],
|
||||
[GamepadKey.RS_DOWN]: [this.RIGHT_STICK_Y, 3, 1],
|
||||
};
|
||||
|
||||
private constructor() {
|
||||
super();
|
||||
BxLogger.info(EmulatedMkbHandler.LOG_TAG, 'constructor()');
|
||||
|
||||
this.#STICK_MAP = {
|
||||
[GamepadKey.LS_LEFT]: [this.#LEFT_STICK_X, 0, -1],
|
||||
[GamepadKey.LS_RIGHT]: [this.#LEFT_STICK_X, 0, 1],
|
||||
[GamepadKey.LS_UP]: [this.#LEFT_STICK_Y, 1, -1],
|
||||
[GamepadKey.LS_DOWN]: [this.#LEFT_STICK_Y, 1, 1],
|
||||
this.nativeGetGamepads = window.navigator.getGamepads.bind(window.navigator);
|
||||
|
||||
[GamepadKey.RS_LEFT]: [this.#RIGHT_STICK_X, 2, -1],
|
||||
[GamepadKey.RS_RIGHT]: [this.#RIGHT_STICK_X, 2, 1],
|
||||
[GamepadKey.RS_UP]: [this.#RIGHT_STICK_Y, 3, -1],
|
||||
[GamepadKey.RS_DOWN]: [this.#RIGHT_STICK_Y, 3, 1],
|
||||
};
|
||||
this.popup = MkbPopup.getInstance();
|
||||
this.popup.attachMkbHandler(this);
|
||||
}
|
||||
|
||||
isEnabled = () => this.#enabled;
|
||||
isEnabled = () => this.enabled;
|
||||
|
||||
#patchedGetGamepads = () => {
|
||||
const gamepads = this.#nativeGetGamepads() || [];
|
||||
(gamepads as any)[this.#VIRTUAL_GAMEPAD.index] = this.#VIRTUAL_GAMEPAD;
|
||||
private patchedGetGamepads = () => {
|
||||
const gamepads = (this.nativeGetGamepads() || []) as any;
|
||||
|
||||
gamepads[this.VIRTUAL_GAMEPAD.index] = this.VIRTUAL_GAMEPAD;
|
||||
return gamepads;
|
||||
}
|
||||
|
||||
#getVirtualGamepad = () => this.#VIRTUAL_GAMEPAD;
|
||||
private getVirtualGamepad = () => this.VIRTUAL_GAMEPAD;
|
||||
|
||||
#updateStick(stick: GamepadStick, x: number, y: number) {
|
||||
const virtualGamepad = this.#getVirtualGamepad();
|
||||
private updateStick(stick: GamepadStick, x: number, y: number) {
|
||||
const virtualGamepad = this.getVirtualGamepad();
|
||||
virtualGamepad.axes[stick * 2] = x;
|
||||
virtualGamepad.axes[stick * 2 + 1] = y;
|
||||
|
||||
@@ -212,10 +219,10 @@ export class EmulatedMkbHandler extends MkbHandler {
|
||||
}
|
||||
*/
|
||||
|
||||
#vectorLength = (x: number, y: number): number => Math.sqrt(x ** 2 + y ** 2);
|
||||
private vectorLength = (x: number, y: number): number => Math.sqrt(x ** 2 + y ** 2);
|
||||
|
||||
#resetGamepad = () => {
|
||||
const gamepad = this.#getVirtualGamepad();
|
||||
private resetGamepad() {
|
||||
const gamepad = this.getVirtualGamepad();
|
||||
|
||||
// Reset axes
|
||||
gamepad.axes = [0, 0, 0, 0];
|
||||
@@ -229,11 +236,11 @@ export class EmulatedMkbHandler extends MkbHandler {
|
||||
gamepad.timestamp = performance.now();
|
||||
}
|
||||
|
||||
#pressButton = (buttonIndex: GamepadKey, pressed: boolean) => {
|
||||
const virtualGamepad = this.#getVirtualGamepad();
|
||||
private pressButton(buttonIndex: GamepadKey, pressed: boolean) {
|
||||
const virtualGamepad = this.getVirtualGamepad();
|
||||
|
||||
if (buttonIndex >= 100) {
|
||||
let [valueArr, axisIndex] = this.#STICK_MAP[buttonIndex]!;
|
||||
let [valueArr, axisIndex] = this.STICK_MAP[buttonIndex]!;
|
||||
valueArr = valueArr as number[];
|
||||
axisIndex = axisIndex as number;
|
||||
|
||||
@@ -249,7 +256,7 @@ export class EmulatedMkbHandler extends MkbHandler {
|
||||
let value;
|
||||
if (valueArr.length) {
|
||||
// Get value of the last key of the axis
|
||||
value = this.#STICK_MAP[valueArr[valueArr.length - 1]]![2] as number;
|
||||
value = this.STICK_MAP[valueArr[valueArr.length - 1]]![2] as number;
|
||||
} else {
|
||||
value = 0;
|
||||
}
|
||||
@@ -263,41 +270,35 @@ export class EmulatedMkbHandler extends MkbHandler {
|
||||
virtualGamepad.timestamp = performance.now();
|
||||
}
|
||||
|
||||
#onKeyboardEvent = (e: KeyboardEvent) => {
|
||||
private onKeyboardEvent = (e: KeyboardEvent) => {
|
||||
const isKeyDown = e.type === 'keydown';
|
||||
|
||||
// Toggle MKB feature
|
||||
if (e.code === 'F8') {
|
||||
if (!isKeyDown) {
|
||||
e.preventDefault();
|
||||
this.toggle();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Hijack the Esc button
|
||||
if (e.code === 'Escape') {
|
||||
e.preventDefault();
|
||||
|
||||
// Hold the Esc for 1 second to disable MKB
|
||||
if (this.#enabled && isKeyDown) {
|
||||
if (this.#escKeyDownTime === -1) {
|
||||
this.#escKeyDownTime = performance.now();
|
||||
} else if (performance.now() - this.#escKeyDownTime >= 1000) {
|
||||
if (this.enabled && isKeyDown) {
|
||||
if (this.escKeyDownTime === -1) {
|
||||
this.escKeyDownTime = performance.now();
|
||||
} else if (performance.now() - this.escKeyDownTime >= 1000) {
|
||||
this.stop();
|
||||
}
|
||||
} else {
|
||||
this.#escKeyDownTime = -1;
|
||||
this.escKeyDownTime = -1;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.#isPolling) {
|
||||
if (!this.isPolling || !this.PRESET) {
|
||||
return;
|
||||
}
|
||||
|
||||
const buttonIndex = this.#CURRENT_PRESET_DATA.mapping[e.code || e.key]!;
|
||||
if (window.BX_STREAM_SETTINGS.xCloudPollingMode !== 'none') {
|
||||
return;
|
||||
}
|
||||
|
||||
const buttonIndex = this.PRESET.mapping[e.code || e.key]!;
|
||||
if (typeof buttonIndex === 'undefined') {
|
||||
return;
|
||||
}
|
||||
@@ -308,19 +309,23 @@ export class EmulatedMkbHandler extends MkbHandler {
|
||||
}
|
||||
|
||||
e.preventDefault();
|
||||
this.#pressButton(buttonIndex, isKeyDown);
|
||||
this.pressButton(buttonIndex, isKeyDown);
|
||||
}
|
||||
|
||||
#onMouseStopped = () => {
|
||||
private onMouseStopped = () => {
|
||||
// Reset stick position
|
||||
this.#detectMouseStoppedTimeout = null;
|
||||
this.detectMouseStoppedTimeoutId = null;
|
||||
|
||||
const mouseMapTo = this.#CURRENT_PRESET_DATA.mouse[MkbPresetKey.MOUSE_MAP_TO];
|
||||
if (!this.PRESET) {
|
||||
return;
|
||||
}
|
||||
|
||||
const mouseMapTo = this.PRESET.mouse[MkbPresetKey.MOUSE_MAP_TO];
|
||||
const analog = mouseMapTo === MouseMapTo.LS ? GamepadStick.LEFT : GamepadStick.RIGHT;
|
||||
this.#updateStick(analog, 0, 0);
|
||||
this.updateStick(analog, 0, 0);
|
||||
}
|
||||
|
||||
handleMouseClick = (data: MkbMouseClick) => {
|
||||
handleMouseClick(data: MkbMouseClick) {
|
||||
let mouseButton;
|
||||
if (typeof data.mouseButton !== 'undefined') {
|
||||
mouseButton = data.mouseButton;
|
||||
@@ -331,51 +336,54 @@ export class EmulatedMkbHandler extends MkbHandler {
|
||||
const keyCode = 'Mouse' + mouseButton;
|
||||
const key = {
|
||||
code: keyCode,
|
||||
name: KeyHelper.codeToKeyName(keyCode),
|
||||
};
|
||||
|
||||
if (!key.name) {
|
||||
if (!this.PRESET) {
|
||||
return;
|
||||
}
|
||||
|
||||
const buttonIndex = this.#CURRENT_PRESET_DATA.mapping[key.code]!;
|
||||
const buttonIndex = this.PRESET.mapping[key.code]!;
|
||||
if (typeof buttonIndex === 'undefined') {
|
||||
return;
|
||||
}
|
||||
|
||||
this.#pressButton(buttonIndex, data.pressed);
|
||||
this.pressButton(buttonIndex, data.pressed);
|
||||
}
|
||||
|
||||
handleMouseMove = (data: MkbMouseMove) => {
|
||||
handleMouseMove(data: MkbMouseMove) {
|
||||
const preset = this.PRESET;
|
||||
if (!preset) {
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: optimize this
|
||||
const mouseMapTo = this.#CURRENT_PRESET_DATA.mouse[MkbPresetKey.MOUSE_MAP_TO];
|
||||
const mouseMapTo = preset.mouse[MkbPresetKey.MOUSE_MAP_TO];
|
||||
if (mouseMapTo === MouseMapTo.OFF) {
|
||||
// Ignore mouse movements
|
||||
return;
|
||||
}
|
||||
|
||||
this.#detectMouseStoppedTimeout && clearTimeout(this.#detectMouseStoppedTimeout);
|
||||
this.#detectMouseStoppedTimeout = window.setTimeout(this.#onMouseStopped.bind(this), 50);
|
||||
this.detectMouseStoppedTimeoutId && clearTimeout(this.detectMouseStoppedTimeoutId);
|
||||
this.detectMouseStoppedTimeoutId = window.setTimeout(this.onMouseStopped, 50);
|
||||
|
||||
const deadzoneCounterweight = this.#CURRENT_PRESET_DATA.mouse[MkbPresetKey.MOUSE_DEADZONE_COUNTERWEIGHT];
|
||||
const deadzoneCounterweight = preset.mouse[MkbPresetKey.MOUSE_DEADZONE_COUNTERWEIGHT];
|
||||
|
||||
let x = data.movementX * this.#CURRENT_PRESET_DATA.mouse[MkbPresetKey.MOUSE_SENSITIVITY_X];
|
||||
let y = data.movementY * this.#CURRENT_PRESET_DATA.mouse[MkbPresetKey.MOUSE_SENSITIVITY_Y];
|
||||
let x = data.movementX * preset.mouse[MkbPresetKey.MOUSE_SENSITIVITY_X];
|
||||
let y = data.movementY * preset.mouse[MkbPresetKey.MOUSE_SENSITIVITY_Y];
|
||||
|
||||
let length = this.#vectorLength(x, y);
|
||||
let length = this.vectorLength(x, y);
|
||||
if (length !== 0 && length < deadzoneCounterweight) {
|
||||
x *= deadzoneCounterweight / length;
|
||||
y *= deadzoneCounterweight / length;
|
||||
} else if (length > EmulatedMkbHandler.MAXIMUM_STICK_RANGE) {
|
||||
x *= EmulatedMkbHandler.MAXIMUM_STICK_RANGE / length;
|
||||
y *= EmulatedMkbHandler.MAXIMUM_STICK_RANGE / length;
|
||||
} else if (length > MouseConstant.MAXIMUM_STICK_RANGE) {
|
||||
x *= MouseConstant.MAXIMUM_STICK_RANGE / length;
|
||||
y *= MouseConstant.MAXIMUM_STICK_RANGE / length;
|
||||
}
|
||||
|
||||
const analog = mouseMapTo === MouseMapTo.LS ? GamepadStick.LEFT : GamepadStick.RIGHT;
|
||||
this.#updateStick(analog, x, y);
|
||||
this.updateStick(analog, x, y);
|
||||
}
|
||||
|
||||
handleMouseWheel = (data: MkbMouseWheel): boolean => {
|
||||
handleMouseWheel(data: MkbMouseWheel): boolean {
|
||||
let code = '';
|
||||
if (data.vertical < 0) {
|
||||
code = WheelCode.SCROLL_UP;
|
||||
@@ -391,136 +399,69 @@ export class EmulatedMkbHandler extends MkbHandler {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this.PRESET) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const key = {
|
||||
code: code,
|
||||
name: KeyHelper.codeToKeyName(code),
|
||||
};
|
||||
|
||||
const buttonIndex = this.#CURRENT_PRESET_DATA.mapping[key.code]!;
|
||||
const buttonIndex = this.PRESET.mapping[key.code]!;
|
||||
if (typeof buttonIndex === 'undefined') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.#prevWheelCode === null || this.#prevWheelCode === key.code) {
|
||||
this.#wheelStoppedTimeout && clearTimeout(this.#wheelStoppedTimeout);
|
||||
this.#pressButton(buttonIndex, true);
|
||||
if (this.prevWheelCode === null || this.prevWheelCode === key.code) {
|
||||
this.wheelStoppedTimeoutId && clearTimeout(this.wheelStoppedTimeoutId);
|
||||
this.pressButton(buttonIndex, true);
|
||||
}
|
||||
|
||||
this.#wheelStoppedTimeout = window.setTimeout(() => {
|
||||
this.#prevWheelCode = null;
|
||||
this.#pressButton(buttonIndex, false);
|
||||
this.wheelStoppedTimeoutId = window.setTimeout(() => {
|
||||
this.prevWheelCode = null;
|
||||
this.pressButton(buttonIndex, false);
|
||||
}, 20);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
toggle = (force?: boolean) => {
|
||||
if (typeof force !== 'undefined') {
|
||||
this.#enabled = force;
|
||||
} else {
|
||||
this.#enabled = !this.#enabled;
|
||||
toggle(force?: boolean) {
|
||||
if (!this.initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.#enabled) {
|
||||
if (typeof force !== 'undefined') {
|
||||
this.enabled = force;
|
||||
} else {
|
||||
this.enabled = !this.enabled;
|
||||
}
|
||||
|
||||
if (this.enabled) {
|
||||
document.body.requestPointerLock();
|
||||
} else {
|
||||
document.pointerLockElement && document.exitPointerLock();
|
||||
}
|
||||
}
|
||||
|
||||
#getCurrentPreset = (): Promise<MkbStoredPreset> => {
|
||||
return new Promise(resolve => {
|
||||
const presetId = getPref(PrefKey.MKB_DEFAULT_PRESET_ID);
|
||||
MkbPresetsDb.getInstance().getPreset(presetId).then((preset: MkbStoredPreset) => {
|
||||
resolve(preset);
|
||||
});
|
||||
});
|
||||
refreshPresetData() {
|
||||
this.PRESET = window.BX_STREAM_SETTINGS.mkbPreset;
|
||||
this.resetGamepad();
|
||||
}
|
||||
|
||||
refreshPresetData = () => {
|
||||
this.#getCurrentPreset().then((preset: MkbStoredPreset) => {
|
||||
this.#CURRENT_PRESET_DATA = MkbPreset.convert(preset ? preset.data : MkbPreset.DEFAULT_PRESET);
|
||||
this.#resetGamepad();
|
||||
});
|
||||
waitForMouseData(showPopup: boolean) {
|
||||
this.popup.toggleVisibility(showPopup);
|
||||
}
|
||||
|
||||
waitForMouseData = (wait: boolean) => {
|
||||
this.#$message && this.#$message.classList.toggle('bx-gone', !wait);
|
||||
private onPollingModeChanged = (e: Event) => {
|
||||
const move = window.BX_STREAM_SETTINGS.xCloudPollingMode !== 'none';
|
||||
this.popup.moveOffscreen(move);
|
||||
}
|
||||
|
||||
#onPollingModeChanged = (e: Event) => {
|
||||
if (!this.#$message) {
|
||||
return;
|
||||
}
|
||||
|
||||
const mode = (e as any).mode;
|
||||
if (mode === 'none') {
|
||||
this.#$message.classList.remove('bx-offscreen');
|
||||
} else {
|
||||
this.#$message.classList.add('bx-offscreen');
|
||||
}
|
||||
}
|
||||
|
||||
#onDialogShown = () => {
|
||||
private onDialogShown = () => {
|
||||
document.pointerLockElement && document.exitPointerLock();
|
||||
}
|
||||
|
||||
#initMessage = () => {
|
||||
if (!this.#$message) {
|
||||
this.#$message = CE('div', {'class': 'bx-mkb-pointer-lock-msg bx-gone'},
|
||||
CE('div', {},
|
||||
CE('p', {}, t('virtual-controller')),
|
||||
CE('p', {}, t('press-key-to-toggle-mkb', {key: 'F8'})),
|
||||
),
|
||||
|
||||
CE('div', {'data-type': 'virtual'},
|
||||
createButton({
|
||||
style: ButtonStyle.PRIMARY | ButtonStyle.TALL | ButtonStyle.FULL_WIDTH,
|
||||
label: t('activate'),
|
||||
onClick: ((e: Event) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
this.toggle(true);
|
||||
}).bind(this),
|
||||
}),
|
||||
|
||||
CE('div', {},
|
||||
createButton({
|
||||
label: t('ignore'),
|
||||
style: ButtonStyle.GHOST,
|
||||
onClick: e => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
this.toggle(false);
|
||||
this.waitForMouseData(false);
|
||||
},
|
||||
}),
|
||||
|
||||
createButton({
|
||||
label: t('edit'),
|
||||
onClick: e => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
// Show Settings dialog & focus the MKB tab
|
||||
const dialog = SettingsNavigationDialog.getInstance();
|
||||
dialog.focusTab('mkb');
|
||||
NavigationDialogManager.getInstance().show(dialog);
|
||||
},
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (!this.#$message.isConnected) {
|
||||
document.documentElement.appendChild(this.#$message);
|
||||
}
|
||||
}
|
||||
|
||||
#onPointerLockChange = () => {
|
||||
private onPointerLockChange = () => {
|
||||
if (document.pointerLockElement) {
|
||||
this.start();
|
||||
} else {
|
||||
@@ -528,58 +469,64 @@ export class EmulatedMkbHandler extends MkbHandler {
|
||||
}
|
||||
}
|
||||
|
||||
#onPointerLockError = (e: Event) => {
|
||||
private onPointerLockError = (e: Event) => {
|
||||
console.log(e);
|
||||
this.stop();
|
||||
}
|
||||
|
||||
#onPointerLockRequested = () => {
|
||||
private onPointerLockRequested = () => {
|
||||
this.start();
|
||||
}
|
||||
|
||||
#onPointerLockExited = () => {
|
||||
this.#mouseDataProvider?.stop();
|
||||
private onPointerLockExited = () => {
|
||||
this.mouseDataProvider?.stop();
|
||||
}
|
||||
|
||||
handleEvent(event: Event) {
|
||||
switch (event.type) {
|
||||
case BxEvent.POINTER_LOCK_REQUESTED:
|
||||
this.#onPointerLockRequested();
|
||||
this.onPointerLockRequested();
|
||||
break;
|
||||
case BxEvent.POINTER_LOCK_EXITED:
|
||||
this.#onPointerLockExited();
|
||||
this.onPointerLockExited();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
init = () => {
|
||||
init() {
|
||||
if (!STATES.browser.capabilities.mkb) {
|
||||
this.initialized = false;
|
||||
return;
|
||||
}
|
||||
|
||||
this.initialized = true;
|
||||
|
||||
this.refreshPresetData();
|
||||
this.#enabled = false;
|
||||
this.enabled = false;
|
||||
|
||||
if (AppInterface) {
|
||||
this.#mouseDataProvider = new WebSocketMouseDataProvider(this);
|
||||
this.mouseDataProvider = new WebSocketMouseDataProvider(this);
|
||||
} else {
|
||||
this.#mouseDataProvider = new PointerLockMouseDataProvider(this);
|
||||
this.mouseDataProvider = new PointerLockMouseDataProvider(this);
|
||||
}
|
||||
this.#mouseDataProvider.init();
|
||||
this.mouseDataProvider.init();
|
||||
|
||||
window.addEventListener('keydown', this.#onKeyboardEvent);
|
||||
window.addEventListener('keyup', this.#onKeyboardEvent);
|
||||
window.addEventListener('keydown', this.onKeyboardEvent);
|
||||
window.addEventListener('keyup', this.onKeyboardEvent);
|
||||
|
||||
window.addEventListener(BxEvent.XCLOUD_POLLING_MODE_CHANGED, this.#onPollingModeChanged);
|
||||
window.addEventListener(BxEvent.XCLOUD_DIALOG_SHOWN, this.#onDialogShown);
|
||||
window.addEventListener(BxEvent.XCLOUD_POLLING_MODE_CHANGED, this.onPollingModeChanged);
|
||||
window.addEventListener(BxEvent.XCLOUD_DIALOG_SHOWN, this.onDialogShown);
|
||||
|
||||
if (AppInterface) {
|
||||
// Android app doesn't support PointerLock API so we need to use a different method
|
||||
window.addEventListener(BxEvent.POINTER_LOCK_REQUESTED, this);
|
||||
window.addEventListener(BxEvent.POINTER_LOCK_EXITED, this);
|
||||
} else {
|
||||
document.addEventListener('pointerlockchange', this.#onPointerLockChange);
|
||||
document.addEventListener('pointerlockerror', this.#onPointerLockError);
|
||||
document.addEventListener('pointerlockchange', this.onPointerLockChange);
|
||||
document.addEventListener('pointerlockerror', this.onPointerLockError);
|
||||
}
|
||||
|
||||
this.#initMessage();
|
||||
this.#$message?.classList.add('bx-gone');
|
||||
MkbPopup.getInstance().reset();
|
||||
|
||||
if (AppInterface) {
|
||||
Toast.show(t('press-key-to-toggle-mkb', {key: `<b>F8</b>`}), t('virtual-controller'), {html: true});
|
||||
@@ -589,51 +536,62 @@ export class EmulatedMkbHandler extends MkbHandler {
|
||||
}
|
||||
}
|
||||
|
||||
destroy = () => {
|
||||
this.#isPolling = false;
|
||||
this.#enabled = false;
|
||||
destroy() {
|
||||
if (!this.initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.initialized = false;
|
||||
this.isPolling = false;
|
||||
this.enabled = false;
|
||||
this.stop();
|
||||
|
||||
this.waitForMouseData(false);
|
||||
document.pointerLockElement && document.exitPointerLock();
|
||||
|
||||
window.removeEventListener('keydown', this.#onKeyboardEvent);
|
||||
window.removeEventListener('keyup', this.#onKeyboardEvent);
|
||||
window.removeEventListener('keydown', this.onKeyboardEvent);
|
||||
window.removeEventListener('keyup', this.onKeyboardEvent);
|
||||
|
||||
if (AppInterface) {
|
||||
window.removeEventListener(BxEvent.POINTER_LOCK_REQUESTED, this);
|
||||
window.removeEventListener(BxEvent.POINTER_LOCK_EXITED, this);
|
||||
} else {
|
||||
document.removeEventListener('pointerlockchange', this.#onPointerLockChange);
|
||||
document.removeEventListener('pointerlockerror', this.#onPointerLockError);
|
||||
document.removeEventListener('pointerlockchange', this.onPointerLockChange);
|
||||
document.removeEventListener('pointerlockerror', this.onPointerLockError);
|
||||
}
|
||||
|
||||
window.removeEventListener(BxEvent.XCLOUD_POLLING_MODE_CHANGED, this.#onPollingModeChanged);
|
||||
window.removeEventListener(BxEvent.XCLOUD_DIALOG_SHOWN, this.#onDialogShown);
|
||||
window.removeEventListener(BxEvent.XCLOUD_POLLING_MODE_CHANGED, this.onPollingModeChanged);
|
||||
window.removeEventListener(BxEvent.XCLOUD_DIALOG_SHOWN, this.onDialogShown);
|
||||
|
||||
this.#mouseDataProvider?.destroy();
|
||||
this.mouseDataProvider?.destroy();
|
||||
|
||||
window.removeEventListener(BxEvent.XCLOUD_POLLING_MODE_CHANGED, this.#onPollingModeChanged);
|
||||
window.removeEventListener(BxEvent.XCLOUD_POLLING_MODE_CHANGED, this.onPollingModeChanged);
|
||||
}
|
||||
|
||||
start = () => {
|
||||
if (!this.#enabled) {
|
||||
this.#enabled = true;
|
||||
Toast.show(t('virtual-controller'), t('enabled'), {instant: true});
|
||||
updateGamepadSlots() {
|
||||
// Set gamepad slot
|
||||
this.VIRTUAL_GAMEPAD.index = getPref<number>(PrefKey.MKB_P1_SLOT) - 1;
|
||||
}
|
||||
|
||||
start() {
|
||||
if (!this.enabled) {
|
||||
this.enabled = true;
|
||||
Toast.show(t('virtual-controller'), t('enabled'), { instant: true });
|
||||
}
|
||||
|
||||
this.#isPolling = true;
|
||||
this.#escKeyDownTime = -1;
|
||||
this.isPolling = true;
|
||||
this.escKeyDownTime = -1;
|
||||
|
||||
this.#resetGamepad();
|
||||
window.navigator.getGamepads = this.#patchedGetGamepads;
|
||||
this.resetGamepad();
|
||||
this.updateGamepadSlots();
|
||||
window.navigator.getGamepads = this.patchedGetGamepads;
|
||||
|
||||
this.waitForMouseData(false);
|
||||
|
||||
this.#mouseDataProvider?.start();
|
||||
this.mouseDataProvider?.start();
|
||||
|
||||
// Dispatch "gamepadconnected" event
|
||||
const virtualGamepad = this.#getVirtualGamepad();
|
||||
const virtualGamepad = this.getVirtualGamepad();
|
||||
virtualGamepad.connected = true;
|
||||
virtualGamepad.timestamp = performance.now();
|
||||
|
||||
@@ -643,46 +601,51 @@ export class EmulatedMkbHandler extends MkbHandler {
|
||||
|
||||
window.BX_EXPOSED.stopTakRendering = true;
|
||||
|
||||
Toast.show(t('virtual-controller'), t('enabled'), {instant: true});
|
||||
Toast.show(t('virtual-controller'), t('enabled'), { instant: true });
|
||||
}
|
||||
|
||||
stop = () => {
|
||||
this.#enabled = false;
|
||||
this.#isPolling = false;
|
||||
this.#escKeyDownTime = -1;
|
||||
stop() {
|
||||
this.enabled = false;
|
||||
this.isPolling = false;
|
||||
this.escKeyDownTime = -1;
|
||||
|
||||
const virtualGamepad = this.#getVirtualGamepad();
|
||||
const virtualGamepad = this.getVirtualGamepad();
|
||||
if (virtualGamepad.connected) {
|
||||
// Dispatch "gamepaddisconnected" event
|
||||
this.#resetGamepad();
|
||||
this.resetGamepad();
|
||||
|
||||
virtualGamepad.connected = false;
|
||||
virtualGamepad.timestamp = performance.now();
|
||||
|
||||
BxEvent.dispatch(window, 'gamepaddisconnected', {
|
||||
gamepad: virtualGamepad,
|
||||
});
|
||||
gamepad: virtualGamepad,
|
||||
});
|
||||
|
||||
window.navigator.getGamepads = this.#nativeGetGamepads;
|
||||
window.navigator.getGamepads = this.nativeGetGamepads;
|
||||
}
|
||||
|
||||
this.waitForMouseData(true);
|
||||
this.#mouseDataProvider?.stop();
|
||||
this.mouseDataProvider?.stop();
|
||||
|
||||
// Toast.show(t('virtual-controller'), t('disabled'), {instant: true});
|
||||
}
|
||||
|
||||
static setupEvents() {
|
||||
isFullVersion() && window.addEventListener(BxEvent.STREAM_PLAYING, () => {
|
||||
if (STATES.currentStream.titleInfo?.details.hasMkbSupport) {
|
||||
// Enable native MKB in Android app
|
||||
if (AppInterface && getPref(PrefKey.NATIVE_MKB_ENABLED) === 'on') {
|
||||
AppInterface && NativeMkbHandler.getInstance().init();
|
||||
if (isFullVersion()) {
|
||||
window.addEventListener(BxEvent.STREAM_PLAYING, () => {
|
||||
if (STATES.currentStream.titleInfo?.details.hasMkbSupport) {
|
||||
// Enable native MKB in Android app
|
||||
NativeMkbHandler.getInstance()?.init();
|
||||
} else {
|
||||
EmulatedMkbHandler.getInstance()?.init();
|
||||
}
|
||||
} else if (getPref(PrefKey.MKB_ENABLED) && (AppInterface || !UserAgent.isMobile())) {
|
||||
BxLogger.info(EmulatedMkbHandler.LOG_TAG, 'Emulate MKB');
|
||||
EmulatedMkbHandler.getInstance().init();
|
||||
});
|
||||
|
||||
if (EmulatedMkbHandler.isAllowed()) {
|
||||
window.addEventListener(BxEvent.MKB_UPDATED, () => {
|
||||
EmulatedMkbHandler.getInstance()?.refreshPresetData();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user