mirror of
https://github.com/redphx/better-xcloud.git
synced 2025-08-10 15:17:45 +02:00

commit ad365d4ee854971122f0e8cb9157ed44b3aac0d8 Author: redphx <96280+redphx@users.noreply.github.com> Date: Wed May 29 17:19:57 2024 +0700 Fix not able to reconnect to WebSocket server when switching game commit ca9369318d4cbb831650e8ca631e7997dc7706cb Author: redphx <96280+redphx@users.noreply.github.com> Date: Wed May 29 17:19:23 2024 +0700 Stop emulated MKB when losing pointer capture commit 8cca1a0554c46b8f61455e79d5b16f1dff9a8014 Author: redphx <96280+redphx@users.noreply.github.com> Date: Wed May 29 17:17:42 2024 +0700 Allow fine-tuning maximum video bitrate commit 763d414d560d9d2aa6710fd60e3f80bf43a534d6 Author: redphx <96280+redphx@users.noreply.github.com> Date: Wed May 29 08:13:56 2024 +0700 Update mouse settings commit d65c5ab4e4a33ed8ad13acf0a15c4bb5ace870eb Author: redphx <96280+redphx@users.noreply.github.com> Date: Wed May 29 08:10:49 2024 +0700 Increase MKB dialog's bg opacity commit 3e72f2ad2700737c8148ef47629528954a606578 Author: redphx <96280+redphx@users.noreply.github.com> Date: Wed May 29 08:02:57 2024 +0700 Show/hide MKB dialog properly commit e7786f36508e3aa843604d9886861930bada5d60 Author: redphx <96280+redphx@users.noreply.github.com> Date: Wed May 29 07:47:21 2024 +0700 Fix connecting to WebSocket server when it's not ready commit 512d8c227a057e5c0399bf128bc1c52a88fcf853 Author: redphx <96280+redphx@users.noreply.github.com> Date: Wed May 29 07:18:06 2024 +0700 Fix arrow keys not working in Android app commit 0ce90f47f37d057d5a4fab0003e2bec8960d1eee Author: redphx <96280+redphx@users.noreply.github.com> Date: Tue May 28 17:36:56 2024 +0700 Set mouse's default sensitivities to 50 commit 16eb48660dd44497e16ca22343a880d9a2e53a30 Author: redphx <96280+redphx@users.noreply.github.com> Date: Tue May 28 17:33:37 2024 +0700 Allow emulated MKB feature in Android app commit c3d0e64f8502e19cd4f167fea4cdbdfc2e14b65e Author: redphx <96280+redphx@users.noreply.github.com> Date: Tue May 28 17:32:49 2024 +0700 Remove stick decay settings commit d289d2a0dea61a440c1bc6b9392920b8e6ab6298 Author: redphx <96280+redphx@users.noreply.github.com> Date: Tue May 28 17:21:39 2024 +0700 Remove stick decaying feature commit 76bd001d98bac53f757f4ae793b2850aad055007 Author: redphx <96280+redphx@users.noreply.github.com> Date: Tue May 28 17:21:14 2024 +0700 Update data structure commit c5d3c87da9e6624ebefb288f6d7c8d06dc00916b Author: redphx <96280+redphx@users.noreply.github.com> Date: Tue May 28 08:14:27 2024 +0700 Fix not toggling the MKB feature correctly commit 9615535cf0e4d4372e201aefb6f1231ddbc22536 Author: redphx <96280+redphx@users.noreply.github.com> Date: Mon May 27 20:51:57 2024 +0700 Handle mouse data from the app
149 lines
4.4 KiB
TypeScript
149 lines
4.4 KiB
TypeScript
import { CE, createSvgIcon } from "@utils/html";
|
|
import { ScreenshotAction } from "./action-screenshot";
|
|
import { TouchControlAction } from "./action-touch-control";
|
|
import { BxEvent } from "@utils/bx-event";
|
|
import { BxIcon } from "@utils/bx-icon";
|
|
import type { BaseGameBarAction } from "./action-base";
|
|
import { STATES } from "@utils/global";
|
|
import { PrefKey, getPref } from "@utils/preferences";
|
|
import { MicrophoneAction } from "./action-microphone";
|
|
|
|
|
|
export class GameBar {
|
|
private static instance: GameBar;
|
|
|
|
public static getInstance(): GameBar {
|
|
if (!GameBar.instance) {
|
|
GameBar.instance = new GameBar();
|
|
}
|
|
|
|
return GameBar.instance;
|
|
}
|
|
|
|
private static readonly VISIBLE_DURATION = 2000;
|
|
|
|
private $gameBar: HTMLElement;
|
|
private $container: HTMLElement;
|
|
|
|
private timeout: number | null = null;
|
|
|
|
private actions: BaseGameBarAction[] = [];
|
|
|
|
private constructor() {
|
|
let $container;
|
|
|
|
const position = getPref(PrefKey.GAME_BAR_POSITION);
|
|
|
|
const $gameBar = CE('div', {id: 'bx-game-bar', class: 'bx-gone', 'data-position': position},
|
|
$container = CE('div', {class: 'bx-game-bar-container bx-offscreen'}),
|
|
createSvgIcon(position === 'bottom-left' ? BxIcon.CARET_RIGHT : BxIcon.CARET_LEFT),
|
|
);
|
|
|
|
this.actions = [
|
|
new ScreenshotAction(),
|
|
...(STATES.hasTouchSupport && (getPref(PrefKey.STREAM_TOUCH_CONTROLLER) !== 'off') ? [new TouchControlAction()] : []),
|
|
new MicrophoneAction(),
|
|
];
|
|
|
|
// Reverse the action list if Game Bar's position is on the right side
|
|
if (position === 'bottom-right') {
|
|
this.actions.reverse();
|
|
}
|
|
|
|
// Render actions
|
|
for (const action of this.actions) {
|
|
$container.appendChild(action.render());
|
|
}
|
|
|
|
// Toggle game bar when clicking on the game bar box
|
|
$gameBar.addEventListener('click', e => {
|
|
if (e.target !== $gameBar) {
|
|
return;
|
|
}
|
|
|
|
$container.classList.contains('bx-show') ? this.hideBar() : this.showBar();
|
|
});
|
|
|
|
// Hide game bar after clicking on an action
|
|
window.addEventListener(BxEvent.GAME_BAR_ACTION_ACTIVATED, this.hideBar.bind(this));
|
|
|
|
$container.addEventListener('pointerover', this.clearHideTimeout.bind(this));
|
|
$container.addEventListener('pointerout', this.beginHideTimeout.bind(this));
|
|
|
|
// Add animation when hiding game bar
|
|
$container.addEventListener('transitionend', e => {
|
|
const classList = $container.classList;
|
|
if (classList.contains('bx-hide')) {
|
|
classList.remove('bx-offscreen', 'bx-hide');
|
|
classList.add('bx-offscreen');
|
|
}
|
|
});
|
|
|
|
document.documentElement.appendChild($gameBar);
|
|
this.$gameBar = $gameBar;
|
|
this.$container = $container;
|
|
|
|
// Enable/disable Game Bar when playing/pausing
|
|
getPref(PrefKey.GAME_BAR_POSITION) !== 'off' && window.addEventListener(BxEvent.XCLOUD_POLLING_MODE_CHANGED, ((e: Event) => {
|
|
if (!STATES.isPlaying) {
|
|
this.disable();
|
|
return;
|
|
}
|
|
|
|
// Toggle Game bar
|
|
const mode = (e as any).mode;
|
|
mode !== 'None' ? this.disable() : this.enable();
|
|
}).bind(this));
|
|
}
|
|
|
|
private beginHideTimeout() {
|
|
this.clearHideTimeout();
|
|
|
|
this.timeout = window.setTimeout(() => {
|
|
this.timeout = null;
|
|
this.hideBar();
|
|
}, GameBar.VISIBLE_DURATION);
|
|
}
|
|
|
|
private clearHideTimeout() {
|
|
this.timeout && clearTimeout(this.timeout);
|
|
this.timeout = null;
|
|
}
|
|
|
|
enable() {
|
|
this.$gameBar && this.$gameBar.classList.remove('bx-gone');
|
|
}
|
|
|
|
disable() {
|
|
this.hideBar();
|
|
this.$gameBar && this.$gameBar.classList.add('bx-gone');
|
|
}
|
|
|
|
showBar() {
|
|
if (!this.$container) {
|
|
return;
|
|
}
|
|
|
|
this.$container.classList.remove('bx-offscreen', 'bx-hide');
|
|
this.$container.classList.add('bx-show');
|
|
|
|
this.beginHideTimeout();
|
|
}
|
|
|
|
hideBar() {
|
|
if (!this.$container) {
|
|
return;
|
|
}
|
|
|
|
this.$container.classList.remove('bx-show');
|
|
this.$container.classList.add('bx-hide');
|
|
}
|
|
|
|
// Reset all states
|
|
reset() {
|
|
for (const action of this.actions) {
|
|
action.reset();
|
|
}
|
|
}
|
|
}
|