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 :UIToolkitSubPanel,IUXControlMode where TPanel:IUXPanel { private readonly IMicroStateMachine _controlMode; private readonly IPlayerKeyMap _playerKeyMap; private readonly IUXKeyMap _uxKeyMap; private readonly ILogger _logger; public UXControlMode(IServiceProvider serviceProvider, IMicroStateMachine controlMode, IPlayerKeyMap playerKeyMap, ILogger logger, IUXKeyMap 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("ui_control_mode"); _controlModeContainer = Panel.As().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 = "/leftStick/x"; } if (_driveRightButton is not null) { _driveRightButton.ControlPath = "/leftStick/x"; } if (_driveAccelerateButton is not null) { _driveAccelerateButton.ControlPath = "/rightTrigger"; } if (_driveBrakeButton is not null) { _driveBrakeButton.ControlPath = "/leftTrigger"; } if (_driveHandBrakeButton is not null) { _driveHandBrakeButton.ControlPath = "/buttonSouth"; } _logger.LogInformation("Control Mode已初始化"); } } }