using System; using System.Collections; using System.Collections.Generic; using Unity.Mathematics; using UnityEngine; using UnityEngine.UIElements; namespace BITKit.UX { public class OnScreenGamepad : OnScreenControl { public new class UxmlFactory : UxmlFactory { } protected override string ControlPathInternal { get; set; } private readonly Dictionary _buttonMap = new(); public OnScreenGamepad() { IsEnabled.AddListener(OnActive); for (var y = 1; y >=-1; y--) { var row = this.Create(); row.style.flexGrow = 1; row.style.flexDirection = FlexDirection.Row; for (var x = -1; x <= 1; x++) { var dir = (x, y) switch { (-1, 1) => "NW", // 西北 (0, 1) => "N", // 北 (1, 1) => "NE", // 东北 (-1, 0) => "W", // 西 (0, 0) => "C", // 中心 (1, 0) => "E", // 东 (-1, -1) => "SW", // 西南 (0, -1) => "S", // 南 (1, -1) => "SE", // 东南 _ => "NULL" // 其他无效位置 }; var value = (x, y) switch { (-1, -1) => new float2(-0.707f, -0.707f), // NW 西北 (0, -1) =>new float2 (0f, -1f), // N 北 (1, -1) =>new float2 (0.707f, -0.707f), // NE 东北 (-1, 0) => new float2(-1f, 0f), // W 西 (0, 0) =>new float2 (0f, 0f), // C 中心 (1, 0) =>new float2 (1f, 0f), // E 东 (-1, 1) => new float2(-0.707f, 0.707f), // SW 西南 (0, 1) =>new float2 (0f, 1f), // S 南 (1, 1) => new float2(0.707f, 0.707f), // SE 东南 _ => new float2(0f, 0f) // 默认返回 (0, 0) 无效位置 }; var button = row.Create(); _buttonMap.TryAdd(new Vector2Int(x, y), button); button.style.flexGrow = 1; button.AddToClassList("gamepad-button"); button.AddToClassList($"gamepad-button--{dir.ToLower()}"); button.AddManipulator(new Clickable(()=>{})); button.RegisterCallback(_ => { SendValueToControl((Vector2)value); }); button.RegisterCallback(_ => { SendValueToControl((Vector2)default); }); // var label = button.Create