Net.Like.Xue.Tokyo/Packages-Local/Com.Project.B.Unity/UX/UXControlMode.cs

190 lines
6.4 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BITKit;
using BITKit.Mod;
using BITKit.StateMachine;
using BITKit.UX;
using Cysharp.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Project.B.Player;
using Project.B.UX;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
namespace Net.Project.B.UX
{
public class UXControlMode<TPanel> :UIToolkitSubPanel<TPanel>,IUXControlMode where TPanel:IUXPanel
{
private readonly IMicroStateMachine<IPlayerControlMode> _controlMode;
private readonly IPlayerKeyMap<InputAction> _playerKeyMap;
private readonly IUXKeyMap<InputAction> _uxKeyMap;
private readonly ILogger<IUXControlMode> _logger;
public UXControlMode(IServiceProvider serviceProvider, IMicroStateMachine<IPlayerControlMode> controlMode, IPlayerKeyMap<InputAction> playerKeyMap, ILogger<IUXControlMode> logger, IUXKeyMap<InputAction> uxKeyMap):base(serviceProvider)
{
_controlMode = controlMode;
_playerKeyMap = playerKeyMap;
_logger = logger;
_uxKeyMap = uxKeyMap;
_controlMode.OnStateChanged += OnControlModeChanged;
}
private VisualElement _controlModeContainer;
#region Common
[UXBindPath("interactive-button",true)]
private OnScreenControl _interactiveButton;
[UXBindPath("crouch-button",true)]
private OnScreenControl _crouchButton;
[UXBindPath("camera-button",true)]
private OnScreenControl _cameraButton;
[UXBindPath("fire-button",true)]
private OnScreenControl _fireButton;
[UXBindPath("aim-button",true)]
private OnScreenControl _aimButton;
#endregion
#region UI
[UXBindPath("esc-button",true)]
private OnScreenControl _escButton;
[UXBindPath("inventory-button",true)]
private OnScreenControl _inventoryButton;
#endregion
#region Move
[UXBindPath("move-pad",true)]
private OnScreenControl _moveStick;
[UXBindPath("jump-button",true)]
private OnScreenControl _jumpButton;
#endregion
#region Car
[UXBindPath("drive_left-button",true)]
private OnScreenButton _driveLeftButton;
[UXBindPath("drive_right-button",true)]
private OnScreenButton _driveRightButton;
[UXBindPath("drive_accelerate-button",true)]
private OnScreenButton _driveAccelerateButton;
[UXBindPath("drive_brake-button",true)]
private OnScreenButton _driveBrakeButton;
[UXBindPath("drive_hand_brake-button",true)]
private OnScreenButton _driveHandBrakeButton;
#endregion
private VisualElement _modeContainer;
private void OnControlModeChanged(IPlayerControlMode arg1, IPlayerControlMode arg2)
{
switch (arg2)
{
case PlayerWalkMode when Touchscreen.current is not { wasUpdatedThisFrame: true }:
_modeContainer.Navigate("Non");
break;
default:
_modeContainer.Navigate(arg2.GetType().Name);
break;
}
//_logger.LogInformation($"Control Mode切换至:{arg2?.GetType()?.Name}");
}
protected override async UniTask OnInitiatedAsync()
{
var template =await ModService.LoadAsset<VisualTreeAsset>("ui_control_mode");
_controlModeContainer = Panel.As<UIToolKitPanel>().RootVisualElement.Q("control_mode-container").Create(template);
_controlModeContainer.pickingMode = PickingMode.Ignore;
UXUtils.Inject(this,_controlModeContainer);
_controlModeContainer.style.flexGrow = 1;
_modeContainer = _controlModeContainer.Q("mode-container");
if(_inventoryButton is not null)
{
_inventoryButton.ControlPath = _uxKeyMap.InventoryKey.bindings.Last().path;
}
if (_escButton is not null)
{
_escButton.ControlPath = _uxKeyMap.CancelKey.bindings.Last().path;
}
if (_jumpButton is not null)
{
var jumpKey = _playerKeyMap.JumpKey;
_jumpButton.ControlPath = jumpKey.bindings.Last().path;
}
if (_interactiveButton is not null)
{
var interactiveKey = _playerKeyMap.InteractiveKey;
_interactiveButton.ControlPath = interactiveKey.bindings.Last().path;
}
if (_moveStick is not null)
{
var moveKey = _playerKeyMap.MovementKey;
_moveStick.ControlPath = moveKey.bindings.Last().path;
}
if (_crouchButton is not null)
{
var crouchKey = _playerKeyMap.CrouchKey;
_crouchButton.ControlPath = crouchKey.bindings.Last().path;
}
if (_cameraButton is not null)
{
var cameraKey = _playerKeyMap.ToggleCameraKey;
_cameraButton.ControlPath = cameraKey.bindings.Last().path;
}
if (_fireButton is not null)
{
_fireButton.ControlPath = _playerKeyMap.FireKey.bindings.Last().path;
}
if (_aimButton is not null)
{
_aimButton.ControlPath = _playerKeyMap.AimKey.bindings.Last().path;
}
if (_driveLeftButton is not null)
{
_driveLeftButton.ControlPath = "<Gamepad>/leftStick/x";
}
if (_driveRightButton is not null)
{
_driveRightButton.ControlPath = "<Gamepad>/leftStick/x";
}
if (_driveAccelerateButton is not null)
{ _driveAccelerateButton.ControlPath = "<Gamepad>/rightTrigger";
}
if (_driveBrakeButton is not null)
{
_driveBrakeButton.ControlPath = "<Gamepad>/leftTrigger";
}
if (_driveHandBrakeButton is not null)
{
_driveHandBrakeButton.ControlPath = "<Gamepad>/buttonSouth";
}
_logger.LogInformation("Control Mode已初始化");
}
}
}