Update
This commit is contained in:
@@ -16,49 +16,142 @@ namespace UI
|
||||
public string screenName;
|
||||
public UIDocument document;
|
||||
public bool isOverlay;
|
||||
public Texture2D customCursor;
|
||||
public bool isActive; // For Editor and Initial state
|
||||
public bool isActive;
|
||||
}
|
||||
|
||||
public List<ScreenData> screens = new List<ScreenData>();
|
||||
|
||||
[Header("Settings")]
|
||||
public string initialScreen = "MainMenu";
|
||||
public float focusRadius = 300f;
|
||||
|
||||
[Header("Cursor Settings")]
|
||||
private VisualElement _customCursor;
|
||||
private List<VisualElement> _trailPool = new List<VisualElement>();
|
||||
private int _trailIndex = 0;
|
||||
public int trailCount = 15;
|
||||
public float focusRadius = 500f;
|
||||
|
||||
[Header("Editor Preview")]
|
||||
[Range(0f, 1f)]
|
||||
public float globalOpacity = 1f;
|
||||
|
||||
private Stack<string> _navigationStack = new Stack<string>();
|
||||
private string _currentScreenName;
|
||||
private VisualElement _lastHoveredElement;
|
||||
private bool _isSettingsOpen = false;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance == null) Instance = this;
|
||||
else { Destroy(gameObject); return; }
|
||||
|
||||
// Initialize all screens based on isActive or hidden
|
||||
foreach (var screen in screens)
|
||||
SetupCursor();
|
||||
foreach (var s in screens)
|
||||
{
|
||||
if (screen.document != null)
|
||||
{
|
||||
// Ensure the root has the screen-root class for transitions
|
||||
var root = screen.document.rootVisualElement;
|
||||
if (root != null)
|
||||
{
|
||||
root.AddToClassList("screen-root");
|
||||
root.style.display = DisplayStyle.None;
|
||||
}
|
||||
}
|
||||
if (s.document != null) s.document.rootVisualElement.style.display = DisplayStyle.None;
|
||||
}
|
||||
|
||||
ShowScreen(initialScreen);
|
||||
}
|
||||
|
||||
private void SetupCursor()
|
||||
{
|
||||
UIDocument doc = GetComponent<UIDocument>();
|
||||
if (doc == null && screens.Count > 0) doc = screens[0].document;
|
||||
if (doc == null) return;
|
||||
|
||||
var root = doc.rootVisualElement;
|
||||
|
||||
_customCursor = new VisualElement();
|
||||
_customCursor.style.width = 25;
|
||||
_customCursor.style.height = 25;
|
||||
_customCursor.style.backgroundColor = Color.white;
|
||||
_customCursor.style.borderTopLeftRadius = 13; _customCursor.style.borderTopRightRadius = 13;
|
||||
_customCursor.style.borderBottomLeftRadius = 13; _customCursor.style.borderBottomRightRadius = 13;
|
||||
_customCursor.style.position = Position.Absolute;
|
||||
_customCursor.pickingMode = PickingMode.Ignore;
|
||||
root.Add(_customCursor);
|
||||
|
||||
for (int i = 0; i < trailCount; i++)
|
||||
{
|
||||
var trail = new VisualElement();
|
||||
trail.style.width = 18; trail.style.height = 18;
|
||||
trail.style.backgroundColor = new Color(1, 1, 1, 0.4f);
|
||||
trail.style.borderTopLeftRadius = 9; trail.style.borderTopRightRadius = 9;
|
||||
trail.style.borderBottomLeftRadius = 9; trail.style.borderBottomRightRadius = 9;
|
||||
trail.style.position = Position.Absolute;
|
||||
trail.pickingMode = PickingMode.Ignore;
|
||||
root.Add(trail);
|
||||
_trailPool.Add(trail);
|
||||
}
|
||||
_customCursor.BringToFront();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
HandleGlobalInputs();
|
||||
HandleCursorlessFocus();
|
||||
Vector2 mousePos = Input.mousePosition;
|
||||
bool restrictY = (_currentScreenName == "MainMenu" && !_isSettingsOpen);
|
||||
float targetY = restrictY ? Screen.height / 2f : mousePos.y;
|
||||
Vector2 uiPos = new Vector2(mousePos.x, Screen.height - targetY);
|
||||
|
||||
if (_customCursor != null)
|
||||
{
|
||||
_customCursor.style.left = uiPos.x - 12.5f;
|
||||
_customCursor.style.top = uiPos.y - 12.5f;
|
||||
}
|
||||
|
||||
if (_trailPool.Count > 0)
|
||||
{
|
||||
var currentTrail = _trailPool[_trailIndex];
|
||||
currentTrail.style.left = uiPos.x - 9;
|
||||
currentTrail.style.top = uiPos.y - 9;
|
||||
currentTrail.style.opacity = 0.5f;
|
||||
foreach(var t in _trailPool) t.style.opacity = Mathf.Max(0, t.style.opacity.value - Time.deltaTime * 4f);
|
||||
_trailIndex = (_trailIndex + 1) % _trailPool.Count;
|
||||
}
|
||||
|
||||
HandleVirtualInput(uiPos);
|
||||
|
||||
if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && Input.GetKeyDown(KeyCode.O))
|
||||
ToggleSettings();
|
||||
}
|
||||
|
||||
private void HandleVirtualInput(Vector2 uiPos)
|
||||
{
|
||||
UIDocument activeDoc = null;
|
||||
var settings = screens.Find(s => s.screenName == "Settings");
|
||||
if (_isSettingsOpen) activeDoc = settings.document;
|
||||
else activeDoc = screens.Find(s => s.screenName == _currentScreenName)?.document;
|
||||
|
||||
if (activeDoc == null) return;
|
||||
|
||||
VisualElement bestElement = null;
|
||||
float minDistance = float.MaxValue;
|
||||
|
||||
var interactables = activeDoc.rootVisualElement.Query<VisualElement>()
|
||||
.Where(e => e.focusable && e.pickingMode != PickingMode.Ignore).ToList();
|
||||
|
||||
foreach (var element in interactables)
|
||||
{
|
||||
Rect worldBounds = element.worldBound;
|
||||
float dist = Vector2.Distance(uiPos, worldBounds.center);
|
||||
if (dist < minDistance && dist < focusRadius) {
|
||||
minDistance = dist;
|
||||
bestElement = element;
|
||||
}
|
||||
}
|
||||
|
||||
if (bestElement != _lastHoveredElement)
|
||||
{
|
||||
_lastHoveredElement?.RemoveFromClassList("hover");
|
||||
bestElement?.AddToClassList("hover");
|
||||
_lastHoveredElement = bestElement;
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonDown(0) && _lastHoveredElement != null)
|
||||
{
|
||||
using (var clickEvent = ClickEvent.GetPooled()) {
|
||||
clickEvent.target = _lastHoveredElement;
|
||||
_lastHoveredElement.SendEvent(clickEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Editor Support Methods ---
|
||||
@@ -87,132 +180,42 @@ namespace UI
|
||||
|
||||
// --- Runtime Logic ---
|
||||
|
||||
private void HandleGlobalInputs()
|
||||
{
|
||||
if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && Input.GetKeyDown(KeyCode.O))
|
||||
{
|
||||
ToggleSettings();
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Escape))
|
||||
{
|
||||
if (_navigationStack.Count > 1)
|
||||
{
|
||||
GoBack();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleCursorlessFocus()
|
||||
{
|
||||
if (string.IsNullOrEmpty(_currentScreenName)) return;
|
||||
|
||||
var screen = screens.Find(s => s.screenName == _currentScreenName);
|
||||
if (screen == null || screen.document == null) return;
|
||||
|
||||
Vector2 mousePos = Input.mousePosition;
|
||||
Vector2 uiMousePos = new Vector2(mousePos.x, Screen.height - mousePos.y);
|
||||
|
||||
VisualElement bestElement = null;
|
||||
float minDistance = float.MaxValue;
|
||||
|
||||
var interactiveElements = screen.document.rootVisualElement.Query<VisualElement>()
|
||||
.Where(e => e.focusable && e.pickingMode == PickingMode.Position).ToList();
|
||||
|
||||
foreach (var element in interactiveElements)
|
||||
{
|
||||
Rect worldBounds = element.worldBound;
|
||||
Vector2 center = worldBounds.center;
|
||||
float dist = Vector2.Distance(uiMousePos, center);
|
||||
|
||||
if (dist < minDistance && dist < focusRadius)
|
||||
{
|
||||
minDistance = dist;
|
||||
bestElement = element;
|
||||
}
|
||||
}
|
||||
|
||||
if (bestElement != _lastHoveredElement)
|
||||
{
|
||||
_lastHoveredElement?.RemoveFromClassList("hover");
|
||||
bestElement?.AddToClassList("hover");
|
||||
_lastHoveredElement = bestElement;
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonDown(0) && _lastHoveredElement != null)
|
||||
{
|
||||
using (var clickEvent = ClickEvent.GetPooled())
|
||||
{
|
||||
clickEvent.target = _lastHoveredElement;
|
||||
_lastHoveredElement.SendEvent(clickEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ShowScreen(string name)
|
||||
{
|
||||
var nextData = screens.Find(s => s.screenName == name);
|
||||
if (nextData == null) return;
|
||||
var screen = screens.Find(s => s.screenName == name);
|
||||
if (screen == null) return;
|
||||
|
||||
// Hide all first for a clean state at runtime
|
||||
foreach (var s in screens)
|
||||
if (!screen.isOverlay)
|
||||
{
|
||||
if (s.document != null) s.document.rootVisualElement.style.display = DisplayStyle.None;
|
||||
s.isActive = false;
|
||||
foreach(var s in screens) if(!s.isOverlay) s.document.rootVisualElement.style.display = DisplayStyle.None;
|
||||
_navigationStack.Push(name);
|
||||
_currentScreenName = name;
|
||||
}
|
||||
|
||||
_navigationStack.Push(name);
|
||||
_currentScreenName = name;
|
||||
nextData.isActive = true;
|
||||
|
||||
nextData.document.rootVisualElement.style.display = DisplayStyle.Flex;
|
||||
nextData.document.rootVisualElement.style.opacity = globalOpacity;
|
||||
ApplyCursorSettings(nextData);
|
||||
screen.document.rootVisualElement.style.display = DisplayStyle.Flex;
|
||||
screen.isActive = true;
|
||||
UnityEngine.Cursor.visible = false;
|
||||
}
|
||||
|
||||
public void GoBack()
|
||||
{
|
||||
if (_navigationStack.Count <= 1) return;
|
||||
|
||||
string current = _navigationStack.Pop();
|
||||
var currentData = screens.Find(s => s.screenName == current);
|
||||
if (currentData != null) currentData.document.rootVisualElement.style.display = DisplayStyle.None;
|
||||
|
||||
_currentScreenName = _navigationStack.Peek();
|
||||
var prevData = screens.Find(s => s.screenName == _currentScreenName);
|
||||
if (prevData != null)
|
||||
{
|
||||
prevData.document.rootVisualElement.style.display = DisplayStyle.Flex;
|
||||
ApplyCursorSettings(prevData);
|
||||
}
|
||||
var prev = screens.Find(s => s.screenName == _currentScreenName);
|
||||
if (prev != null) prev.document.rootVisualElement.style.display = DisplayStyle.Flex;
|
||||
}
|
||||
|
||||
public void ToggleSettings()
|
||||
{
|
||||
var settings = screens.Find(s => s.screenName == "Settings");
|
||||
if (settings == null) return;
|
||||
|
||||
bool isShowing = settings.document.rootVisualElement.style.display == DisplayStyle.Flex;
|
||||
settings.document.rootVisualElement.style.display = isShowing ? DisplayStyle.None : DisplayStyle.Flex;
|
||||
|
||||
if (!isShowing)
|
||||
{
|
||||
settings.document.sortingOrder = 999;
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyCursorSettings(ScreenData data)
|
||||
{
|
||||
if (data.screenName == "HUD")
|
||||
{
|
||||
UnityEngine.Cursor.visible = false;
|
||||
UnityEngine.Cursor.lockState = CursorLockMode.Locked;
|
||||
}
|
||||
else
|
||||
{
|
||||
UnityEngine.Cursor.visible = false;
|
||||
UnityEngine.Cursor.lockState = CursorLockMode.None;
|
||||
}
|
||||
_isSettingsOpen = settings.document.rootVisualElement.style.display == DisplayStyle.None;
|
||||
settings.document.rootVisualElement.style.display = _isSettingsOpen ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
settings.isActive = _isSettingsOpen;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user