Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions entry/src/main/ets/model/StreamConfig.ets
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
*
* 参考 Android 版本 StreamConfiguration.java
*/
export const GAME_VIBRATION_STRENGTH_MIN = 0;
export const GAME_VIBRATION_STRENGTH_MAX = 200;
export const GAME_VIBRATION_STRENGTH_ORIGINAL = 100;
export const GAME_VIBRATION_STRENGTH_DEFAULT = GAME_VIBRATION_STRENGTH_ORIGINAL;
export const GAME_VIBRATION_STRENGTH_STEP = 5;

export function normalizeGameVibrationStrength(value: number): number {
if (!Number.isFinite(value)) {
return GAME_VIBRATION_STRENGTH_DEFAULT;
}
const roundedValue = Math.round(value);
return Math.max(GAME_VIBRATION_STRENGTH_MIN, Math.min(GAME_VIBRATION_STRENGTH_MAX, roundedValue));
}

export interface StreamConfig {
// 视频设置
width: number;
Expand Down Expand Up @@ -46,7 +60,7 @@ export interface StreamConfig {
attachedGamepadMask: number;
enableVibration: boolean;
vibrationMode: string; // 震动模式:自动/仅手柄/仅设备/同时
vibrateFallbackStrength: number;
gameVibrationStrength: number; // 串流游戏震动强度百分比(100=原始强度,100 以上为 app 侧放大)
enableAudioVibration: boolean; // 音频振动(低频驱动)
audioVibrationStrength: number; // 音频振动强度 (0-100)
audioVibrationSceneMode: string; // 音频振动场景模式: 游戏/电影, 音乐/节奏, 自动
Expand Down Expand Up @@ -229,7 +243,7 @@ export function getDefaultStreamConfig(): StreamConfig {
attachedGamepadMask: 0,
enableVibration: true,
vibrationMode: '自动',
vibrateFallbackStrength: 100,
gameVibrationStrength: GAME_VIBRATION_STRENGTH_DEFAULT,
enableAudioVibration: false,
audioVibrationStrength: 60,
audioVibrationSceneMode: '游戏/电影',
Expand Down
66 changes: 54 additions & 12 deletions entry/src/main/ets/pages/SettingsPageV2.ets
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ import { pasteboard } from '@kit.BasicServicesKit';
import { PreferencesUtil } from '../utils/PreferencesUtil';
import { AppColors, AppSizes, AppSpacing, AppAnimation, AppShadows } from '../common/Theme';
import { StreamingSession, DecoderCapabilities } from '../service/streaming/StreamingSession';
import { getRecommendedBitrate } from '../model/StreamConfig';
import {
getRecommendedBitrate,
GAME_VIBRATION_STRENGTH_DEFAULT,
GAME_VIBRATION_STRENGTH_MAX,
GAME_VIBRATION_STRENGTH_MIN,
GAME_VIBRATION_STRENGTH_ORIGINAL,
GAME_VIBRATION_STRENGTH_STEP,
normalizeGameVibrationStrength
} from '../model/StreamConfig';
import { PerfLabels } from '../components/PerformanceOverlayManager';
import { SettingSlider, SettingSliderConfig, LogarithmicSlider } from '../components/SettingSlider';
import { SettingsBackupService } from '../service/SettingsBackupService';
Expand Down Expand Up @@ -175,7 +183,7 @@ struct SettingsPageV2 {
// 输入 - 手柄设置
@State enableVibration: boolean = true;
@State vibrationMode: string = '自动'; // 自动/仅手柄/仅设备/同时
@State vibrateFallbackStrength: number = 100;
@State gameVibrationStrength: number = GAME_VIBRATION_STRENGTH_DEFAULT;
@State enableAudioVibration: boolean = false;
@State audioVibrationStrength: number = 60;
@State audioVibrationSceneMode: string = '游戏/电影';
Expand Down Expand Up @@ -518,7 +526,7 @@ struct SettingsPageV2 {
// 输入 - 手柄设置
this.enableVibration = await this.loadBoolean(SettingsKeys.ENABLE_VIBRATION, true);
this.vibrationMode = await PreferencesUtil.get<string>(SettingsKeys.VIBRATION_MODE, '自动');
this.vibrateFallbackStrength = await PreferencesUtil.get<number>(SettingsKeys.VIBRATE_FALLBACK_STRENGTH, 100);
this.gameVibrationStrength = await this.loadGameVibrationStrength();
this.enableAudioVibration = await this.loadBoolean(SettingsKeys.ENABLE_AUDIO_VIBRATION, false);
this.audioVibrationStrength = await PreferencesUtil.get<number>(SettingsKeys.AUDIO_VIBRATION_STRENGTH, 60);
this.audioVibrationSceneMode = await PreferencesUtil.get<string>(SettingsKeys.AUDIO_VIBRATION_SCENE_MODE, '游戏/电影');
Expand Down Expand Up @@ -641,6 +649,38 @@ struct SettingsPageV2 {
return String(val) === 'true';
}

private async loadGameVibrationStrength(): Promise<number> {
const rawValue = await PreferencesUtil.get<number | string>(
SettingsKeys.VIBRATE_FALLBACK_STRENGTH,
GAME_VIBRATION_STRENGTH_DEFAULT.toString()
);
let parsedValue = GAME_VIBRATION_STRENGTH_DEFAULT;
let shouldRepair = false;

if (typeof rawValue === 'number') {
parsedValue = Number.isFinite(rawValue) ? rawValue : GAME_VIBRATION_STRENGTH_DEFAULT;
shouldRepair = parsedValue !== rawValue;
} else {
const trimmedValue = rawValue.trim();
if (trimmedValue.length === 0) {
shouldRepair = true;
} else {
const numberValue = Number(trimmedValue);
if (Number.isFinite(numberValue)) {
parsedValue = numberValue;
} else {
shouldRepair = true;
}
}
}

const normalizedValue = normalizeGameVibrationStrength(parsedValue);
if (shouldRepair || normalizedValue !== parsedValue) {
this.saveSetting(SettingsKeys.VIBRATE_FALLBACK_STRENGTH, normalizedValue);
}
return normalizedValue;
}

private async saveSetting(key: string, value: string | boolean | number): Promise<void> {
try {
await PreferencesUtil.put(key, value);
Expand Down Expand Up @@ -1341,7 +1381,7 @@ struct SettingsPageV2 {
items: [
{
title: '振动反馈',
subtitle: '手柄振动反馈',
subtitle: '串流游戏震动反馈',
type: 'toggle',
value: this.enableVibration,
action: () => {
Expand All @@ -1361,18 +1401,20 @@ struct SettingsPageV2 {
}
},
{
title: '设备振动强度',
subtitle: '使用设备震动时的强度',
title: '震动强度',
subtitle: this.gameVibrationStrength > GAME_VIBRATION_STRENGTH_ORIGINAL ?
'超过 100% 为 app 侧放大,不突破系统/硬件上限' :
'串流游戏震动强度,100% 为原始强度',
type: 'slider',
visible: this.enableVibration,
value: this.vibrateFallbackStrength,
min: 0,
max: 100,
step: 5,
value: this.gameVibrationStrength,
min: GAME_VIBRATION_STRENGTH_MIN,
max: GAME_VIBRATION_STRENGTH_MAX,
step: GAME_VIBRATION_STRENGTH_STEP,
unit: '%',
onSliderChange: (value: number) => {
this.vibrateFallbackStrength = value;
this.saveSetting(SettingsKeys.VIBRATE_FALLBACK_STRENGTH, value);
this.gameVibrationStrength = normalizeGameVibrationStrength(value);
this.saveSetting(SettingsKeys.VIBRATE_FALLBACK_STRENGTH, this.gameVibrationStrength);
}
},
{
Expand Down
27 changes: 19 additions & 8 deletions entry/src/main/ets/service/SettingsService.ets
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import {
ScreenCombinationMode,
PerfOverlayOrientation,
PerfOverlayPosition,
getRecommendedBitrate
getRecommendedBitrate,
GAME_VIBRATION_STRENGTH_DEFAULT,
normalizeGameVibrationStrength
} from '../model/StreamConfig';

/**
Expand Down Expand Up @@ -67,7 +69,7 @@ export class SettingsKeys {
// 输入 - 手柄设置
static readonly ENABLE_VIBRATION: string = 'settings_enable_vibration';
static readonly VIBRATION_MODE: string = 'settings_vibration_mode'; // 震动模式:自动/仅手柄/仅设备/同时
static readonly VIBRATE_FALLBACK_STRENGTH: string = 'settings_vibrate_fallback_strength';
static readonly VIBRATE_FALLBACK_STRENGTH: string = 'settings_vibrate_fallback_strength'; // 历史存储键,现用于游戏震动强度
static readonly ENABLE_AUDIO_VIBRATION: string = 'settings_enable_audio_vibration'; // 音频振动(低频驱动)
static readonly AUDIO_VIBRATION_STRENGTH: string = 'settings_audio_vibration_strength'; // 音频振动强度 (0-100)
static readonly AUDIO_VIBRATION_SCENE_MODE: string = 'settings_audio_vibration_scene_mode'; // 音频振动场景模式
Expand Down Expand Up @@ -183,7 +185,7 @@ export interface InputSettings {
// 手柄
enableVibration: boolean;
vibrationMode: string; // 自动/仅手柄/仅设备/同时
vibrateFallbackStrength: number;
gameVibrationStrength: number;
enableAudioVibration: boolean; // 音频振动(低频驱动)
audioVibrationStrength: number; // 音频振动强度 (0-100)
audioVibrationSceneMode: string; // 音频振动场景模式
Expand Down Expand Up @@ -527,9 +529,14 @@ export class SettingsService {
private async getNumber(key: string, defaultValue: number): Promise<number> {
const value = await PreferencesUtil.get<number | string>(key, defaultValue.toString());
if (typeof value === 'number') {
return value;
return Number.isFinite(value) ? value : defaultValue;
}
const trimmedValue = value.trim();
if (trimmedValue.length === 0) {
return defaultValue;
}
return parseInt(value) || defaultValue;
const parsed = Number(trimmedValue);
return Number.isFinite(parsed) ? parsed : defaultValue;
}

/**
Expand Down Expand Up @@ -598,7 +605,9 @@ export class SettingsService {
// 获取输入设置
const enableVibration = await this.getBoolean(SettingsKeys.ENABLE_VIBRATION, true);
const vibrationMode = await this.getString(SettingsKeys.VIBRATION_MODE, '自动');
const vibrateFallbackStrength = await this.getNumber(SettingsKeys.VIBRATE_FALLBACK_STRENGTH, 100);
const gameVibrationStrength = normalizeGameVibrationStrength(
await this.getNumber(SettingsKeys.VIBRATE_FALLBACK_STRENGTH, GAME_VIBRATION_STRENGTH_DEFAULT)
);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
const enableAudioVibration = await this.getBoolean(SettingsKeys.ENABLE_AUDIO_VIBRATION, false);
const audioVibrationStrength = await this.getNumber(SettingsKeys.AUDIO_VIBRATION_STRENGTH, 60);
const audioVibrationSceneMode = await this.getString(SettingsKeys.AUDIO_VIBRATION_SCENE_MODE, '游戏/电影');
Expand Down Expand Up @@ -679,7 +688,7 @@ export class SettingsService {
attachedGamepadMask: 0,
enableVibration: enableVibration,
vibrationMode: vibrationMode,
vibrateFallbackStrength: vibrateFallbackStrength,
gameVibrationStrength: gameVibrationStrength,
enableAudioVibration: enableAudioVibration,
audioVibrationStrength: audioVibrationStrength,
audioVibrationSceneMode: audioVibrationSceneMode,
Expand Down Expand Up @@ -741,7 +750,9 @@ export class SettingsService {
// 手柄
enableVibration: await this.getBoolean(SettingsKeys.ENABLE_VIBRATION, true),
vibrationMode: await this.getString(SettingsKeys.VIBRATION_MODE, '自动'),
vibrateFallbackStrength: await this.getNumber(SettingsKeys.VIBRATE_FALLBACK_STRENGTH, 100),
gameVibrationStrength: normalizeGameVibrationStrength(
await this.getNumber(SettingsKeys.VIBRATE_FALLBACK_STRENGTH, GAME_VIBRATION_STRENGTH_DEFAULT)
),
enableAudioVibration: await this.getBoolean(SettingsKeys.ENABLE_AUDIO_VIBRATION, false),
audioVibrationStrength: await this.getNumber(SettingsKeys.AUDIO_VIBRATION_STRENGTH, 60),
audioVibrationSceneMode: await this.getString(SettingsKeys.AUDIO_VIBRATION_SCENE_MODE, '游戏/电影'),
Expand Down
14 changes: 9 additions & 5 deletions entry/src/main/ets/service/input/GamepadManager.ets
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import { inputDevice } from '@kit.InputKit';
import { UsbDriverService, UsbDriverListener, AbstractController, ButtonFlags, KnownUsbGamepadInfo } from '../usbdriver/index';
import {
import {
gameControllerService,
AxisType,
GameControllerDeviceInfo
Expand Down Expand Up @@ -645,12 +645,16 @@ export class GamepadManager implements UsbDriverListener {
}

/**
* 设置设备模拟震动
* @param enabled 是否启用震动
* @param strength 设备震动强度 (0-100)
* 设置串流游戏震动
* @param enabled 是否启用串流游戏震动反馈
* @param strength 串流游戏震动强度 (0-200, 100=原始强度)
* @param mode 震动模式:自动/仅手柄/仅设备/同时
*/
setDeviceVibrate(enabled: boolean, strength: number, mode: string): void {
setGameVibration(
enabled: boolean,
strength: number,
mode: string
): void {
this.vibrationService.setSettings(enabled, strength, mode);
}

Expand Down
Loading
Loading