EG/core/vr_joystick_config.py
2025-10-11 10:08:28 +08:00

279 lines
10 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
VR摇杆配置模块
提供摇杆交互的配置选项和预设:
- 不同的转向模式和灵敏度设置
- 传送参数调整
- 用户体验优化选项
"""
from enum import Enum
class TurnMode(Enum):
"""转向模式枚举"""
SMOOTH = "smooth" # 平滑转向
SNAP = "snap" # 分段转向
class JoystickProfile(Enum):
"""摇杆配置预设"""
COMFORTABLE = "comfortable" # 舒适模式 - 低灵敏度,平滑转向
STANDARD = "standard" # 标准模式 - 中等灵敏度,分段转向
GAMING = "gaming" # 游戏模式 - 高灵敏度,快速响应
ACCESSIBLE = "accessible" # 无障碍模式 - 更大死区,更高阈值
class VRJoystickConfig:
"""VR摇杆配置类"""
def __init__(self):
"""初始化默认配置"""
# 基础参数
self.deadzone = 0.15 # 摇杆死区 (0-1)
self.turn_threshold = 0.3 # 转向激活阈值
self.teleport_threshold = 0.5 # 传送激活阈值
# 转向设置
self.turn_mode = TurnMode.SNAP # 转向模式
self.turn_sensitivity = 250.0 # 转向灵敏度(度/秒)- 增加速度
self.snap_turn_angle = 30.0 # 分段转向角度(度)
self.snap_turn_cooldown = 0.3 # 分段转向间隔(秒)
# 传送设置
self.teleport_range = 20.0 # 最大传送距离
self.teleport_arc_resolution = 50 # 抛物线精度
self.teleport_initial_velocity = 10.0 # 传送初始速度
self.min_teleport_distance = 1.0 # 最小传送距离
self.player_height_offset = 1.7 # 玩家站立高度偏移(米)
# 反馈设置
self.haptic_feedback_enabled = True # 是否启用震动反馈
self.teleport_start_haptic = (0.002, 0.3) # 传送开始震动 (时长, 强度)
self.teleport_success_haptic = (0.005, 0.8) # 传送成功震动
self.teleport_fail_haptic = (0.001, 0.2) # 传送失败震动
# 可视化设置
self.show_teleport_arc = True # 显示传送抛物线
self.show_teleport_target = True # 显示传送目标标记
self.arc_valid_color = (0.2, 0.9, 0.2, 0.8) # 有效抛物线颜色
self.arc_invalid_color = (0.9, 0.2, 0.2, 0.8) # 无效抛物线颜色
def apply_profile(self, profile: JoystickProfile):
"""应用预设配置
Args:
profile: 配置预设
"""
if profile == JoystickProfile.COMFORTABLE:
self._apply_comfortable_profile()
elif profile == JoystickProfile.STANDARD:
self._apply_standard_profile()
elif profile == JoystickProfile.GAMING:
self._apply_gaming_profile()
elif profile == JoystickProfile.ACCESSIBLE:
self._apply_accessible_profile()
def _apply_comfortable_profile(self):
"""舒适模式 - 适合长时间使用"""
self.deadzone = 0.2
self.turn_threshold = 0.4
self.teleport_threshold = 0.6
self.turn_mode = TurnMode.SMOOTH
self.turn_sensitivity = 100.0 # 适中的转向速度
self.snap_turn_cooldown = 0.4
print("✓ 已应用舒适模式配置")
def _apply_standard_profile(self):
"""标准模式 - 平衡的体验"""
self.deadzone = 0.15
self.turn_threshold = 0.3
self.teleport_threshold = 0.5
self.turn_mode = TurnMode.SNAP
self.turn_sensitivity = 150.0 # 更快的转向速度
self.snap_turn_angle = 30.0
self.snap_turn_cooldown = 0.3
print("✓ 已应用标准模式配置")
def _apply_gaming_profile(self):
"""游戏模式 - 快速响应"""
self.deadzone = 0.1
self.turn_threshold = 0.2
self.teleport_threshold = 0.4
self.turn_mode = TurnMode.SMOOTH
self.turn_sensitivity = 200.0 # 高速转向
self.snap_turn_cooldown = 0.2
print("✓ 已应用游戏模式配置")
def _apply_accessible_profile(self):
"""无障碍模式 - 更容易控制"""
self.deadzone = 0.25
self.turn_threshold = 0.5
self.teleport_threshold = 0.7
self.turn_mode = TurnMode.SNAP
self.turn_sensitivity = 80.0 # 较慢但可控的转向
self.snap_turn_angle = 45.0
self.snap_turn_cooldown = 0.5
print("✓ 已应用无障碍模式配置")
def set_turn_mode(self, mode: TurnMode):
"""设置转向模式
Args:
mode: 转向模式
"""
self.turn_mode = mode
print(f"✓ 转向模式设置为: {mode.value}")
def set_turn_sensitivity(self, sensitivity: float):
"""设置转向灵敏度
Args:
sensitivity: 转向灵敏度(度/秒)
"""
self.turn_sensitivity = max(10.0, min(180.0, sensitivity))
print(f"✓ 转向灵敏度设置为: {self.turn_sensitivity}度/秒")
def set_deadzone(self, deadzone: float):
"""设置摇杆死区
Args:
deadzone: 死区大小 (0-1)
"""
self.deadzone = max(0.0, min(0.5, deadzone))
print(f"✓ 摇杆死区设置为: {self.deadzone}")
def set_teleport_range(self, range_meters: float):
"""设置传送范围
Args:
range_meters: 传送范围(米)
"""
self.teleport_range = max(5.0, min(50.0, range_meters))
print(f"✓ 传送范围设置为: {self.teleport_range}")
def set_player_height(self, height_meters: float):
"""设置玩家站立高度偏移
Args:
height_meters: 站立高度偏移推荐范围1.5-2.0米
"""
self.player_height_offset = max(1.0, min(2.5, height_meters))
print(f"✓ 玩家站立高度偏移设置为: {self.player_height_offset}")
def enable_haptic_feedback(self, enabled: bool):
"""启用或禁用震动反馈
Args:
enabled: 是否启用
"""
self.haptic_feedback_enabled = enabled
print(f"✓ 震动反馈: {'启用' if enabled else '禁用'}")
def get_config_dict(self):
"""获取配置字典
Returns:
dict: 配置参数字典
"""
return {
'deadzone': self.deadzone,
'turn_threshold': self.turn_threshold,
'teleport_threshold': self.teleport_threshold,
'turn_mode': self.turn_mode.value,
'turn_sensitivity': self.turn_sensitivity,
'snap_turn_angle': self.snap_turn_angle,
'snap_turn_cooldown': self.snap_turn_cooldown,
'teleport_range': self.teleport_range,
'haptic_feedback_enabled': self.haptic_feedback_enabled
}
def apply_to_joystick_manager(self, joystick_manager):
"""将配置应用到摇杆管理器
Args:
joystick_manager: VRJoystickManager实例
"""
try:
joystick_manager.deadzone = self.deadzone
joystick_manager.turn_threshold = self.turn_threshold
joystick_manager.teleport_threshold = self.teleport_threshold
joystick_manager.turn_sensitivity = self.turn_sensitivity
joystick_manager.smooth_turning = (self.turn_mode == TurnMode.SMOOTH)
joystick_manager.snap_turn_angle = self.snap_turn_angle
joystick_manager.snap_turn_cooldown = self.snap_turn_cooldown
# 应用传送系统配置
if hasattr(joystick_manager, 'teleport_system') and joystick_manager.teleport_system:
teleport_sys = joystick_manager.teleport_system
teleport_sys.teleport_range = self.teleport_range
teleport_sys.arc_resolution = self.teleport_arc_resolution
teleport_sys.initial_velocity = self.teleport_initial_velocity
teleport_sys.min_teleport_distance = self.min_teleport_distance
teleport_sys.player_height_offset = self.player_height_offset
print("✅ 配置已成功应用到摇杆管理器")
except Exception as e:
print(f"⚠️ 应用配置失败: {e}")
# 预定义的配置实例
COMFORTABLE_CONFIG = VRJoystickConfig()
COMFORTABLE_CONFIG.apply_profile(JoystickProfile.COMFORTABLE)
STANDARD_CONFIG = VRJoystickConfig()
STANDARD_CONFIG.apply_profile(JoystickProfile.STANDARD)
GAMING_CONFIG = VRJoystickConfig()
GAMING_CONFIG.apply_profile(JoystickProfile.GAMING)
ACCESSIBLE_CONFIG = VRJoystickConfig()
ACCESSIBLE_CONFIG.apply_profile(JoystickProfile.ACCESSIBLE)
def create_custom_config(**kwargs):
"""创建自定义配置
Args:
**kwargs: 配置参数
Returns:
VRJoystickConfig: 自定义配置实例
"""
config = VRJoystickConfig()
for key, value in kwargs.items():
if hasattr(config, key):
setattr(config, key, value)
else:
print(f"⚠️ 未知的配置参数: {key}")
return config
def print_usage_guide():
"""打印使用指南"""
print("🎮 ======= VR摇杆交互使用指南 =======")
print()
print("📋 基本操作:")
print(" • 摇杆左右移动 → 转向(旋转视角)")
print(" • 摇杆向前推 → 显示传送抛物线")
print(" • 松开摇杆 → 执行传送到落点")
print()
print("⚙️ 配置选项:")
print(" • 舒适模式 → 低灵敏度,适合长时间使用")
print(" • 标准模式 → 平衡体验,推荐大多数用户")
print(" • 游戏模式 → 高灵敏度,快速响应")
print(" • 无障碍模式 → 更大死区,容易控制")
print()
print("🎯 使用建议:")
print(" • 首次使用建议从标准模式开始")
print(" • 根据个人喜好调整转向模式(平滑/分段)")
print(" • 可以随时调整死区和灵敏度")
print(" • 传送范围可根据场景大小调整")
print()
if __name__ == "__main__":
print_usage_guide()