This commit is contained in:
2026-04-28 10:48:04 +07:00
parent f925c61277
commit 3bbc623b2d
2 changed files with 54 additions and 48 deletions

View File

@@ -17,7 +17,7 @@ namespace Hallucinate.UI
private UIDocument _uiDocument;
private VisualElement _rootElement;
private VisualElement _cursorLayer; // Lớp trên cùng chứa trail và ripples
private VisualElement _cursorLayer;
private readonly Dictionary<Type, BaseUIController> _controllers = new Dictionary<Type, BaseUIController>();
private readonly Stack<BaseUIController> _history = new Stack<BaseUIController>();
@@ -41,6 +41,7 @@ namespace Hallucinate.UI
private MainMenuController _mainMenuController;
private List<VisualElement> _trailSegments = new List<VisualElement>();
private List<Vector2> _trailPositions = new List<Vector2>();
private void Awake()
{
@@ -55,32 +56,38 @@ namespace Hallucinate.UI
return;
}
_uiDocument = GetComponent<UIDocument>();
_rootElement = _uiDocument.rootVisualElement;
try
{
_uiDocument = GetComponent<UIDocument>();
_rootElement = _uiDocument.rootVisualElement;
// Tạo lớp Cursor trên cùng
_cursorLayer = new VisualElement();
_cursorLayer.name = "CursorLayer";
_cursorLayer.style.position = Position.Absolute;
_cursorLayer.style.width = Length.Percent(100);
_cursorLayer.style.height = Length.Percent(100);
_cursorLayer.pickingMode = PickingMode.Ignore;
_rootElement.Add(_cursorLayer);
// Tạo lớp Cursor
_cursorLayer = new VisualElement();
_cursorLayer.name = "CursorLayer";
_cursorLayer.style.position = Position.Absolute;
_cursorLayer.style.width = Length.Percent(100);
_cursorLayer.style.height = Length.Percent(100);
_cursorLayer.pickingMode = PickingMode.Ignore;
_rootElement.Add(_cursorLayer);
SetupCursorTrail();
SetupCursorTrail();
// Đăng ký ripple toàn cục
_rootElement.RegisterCallback<PointerDownEvent>(OnGlobalClick, TrickleDown.TrickleDown);
// Ripple
_rootElement.RegisterCallback<PointerDownEvent>(OnGlobalClick, TrickleDown.TrickleDown);
#if UNITY_EDITOR
if (gameIcon == null)
{
var icons = PlayerSettings.GetIconsForTargetGroup(BuildTargetGroup.Unknown);
if (icons != null && icons.Length > 0) gameIcon = icons[0];
}
if (gameIcon == null)
{
var icons = PlayerSettings.GetIconsForTargetGroup(BuildTargetGroup.Unknown);
if (icons != null && icons.Length > 0) gameIcon = icons[0];
}
#endif
InitializeControllers();
InitializeControllers();
}
catch (Exception e)
{
Debug.LogError($"[UIManager] Error during Awake: {e.Message}\n{e.StackTrace}");
}
}
private void SetupCursorTrail()
@@ -93,13 +100,16 @@ namespace Hallucinate.UI
segment.style.position = Position.Absolute;
segment.style.width = cursorSize;
segment.style.height = cursorSize;
segment.style.backgroundImage = new StyleBackground(cursorTrailSprite);
segment.style.opacity = 1f - ((float)i / trailLength);
segment.style.scale = new StyleScale(new Vector2(1f - ((float)i / trailLength), 1f - ((float)i / trailLength)));
segment.style.backgroundImage = new StyleBackground(Background.FromSprite(cursorTrailSprite));
float ratio = 1f - ((float)i / trailLength);
segment.style.opacity = ratio;
segment.style.scale = new StyleScale(new Scale(new Vector3(ratio, ratio, 1f)));
segment.pickingMode = PickingMode.Ignore;
_cursorLayer.Add(segment);
_trailSegments.Add(segment);
_trailPositions.Add(Vector2.zero);
}
}
@@ -111,10 +121,13 @@ namespace Hallucinate.UI
ripple.style.position = Position.Absolute;
ripple.style.width = cursorSize;
ripple.style.height = cursorSize;
ripple.style.borderTopLeftRadius = cursorSize;
ripple.style.borderTopRightRadius = cursorSize;
ripple.style.borderBottomLeftRadius = cursorSize;
ripple.style.borderBottomRightRadius = cursorSize;
var radius = new StyleLength(new Length(50, LengthUnit.Percent));
ripple.style.borderTopLeftRadius = radius;
ripple.style.borderTopRightRadius = radius;
ripple.style.borderBottomLeftRadius = radius;
ripple.style.borderBottomRightRadius = radius;
ripple.style.borderTopColor = rippleColor;
ripple.style.borderBottomColor = rippleColor;
ripple.style.borderLeftColor = rippleColor;
@@ -130,7 +143,6 @@ namespace Hallucinate.UI
_cursorLayer.Add(ripple);
// Hiệu ứng Ripple: To ra và nhạt dần (Sửa lỗi dùng Vector3 thay vì float)
Tween.Scale(ripple.transform, Vector3.one * 3f, duration: 0.5f, ease: Ease.OutQuad);
Tween.Custom(1f, 0f, duration: 0.5f, onValueChange: val => ripple.style.opacity = val)
.OnComplete(() => ripple.RemoveFromHierarchy());
@@ -149,22 +161,20 @@ namespace Hallucinate.UI
Vector2 mousePos = Input.mousePosition;
Vector2 uiPos = new Vector2(mousePos.x, Screen.height - mousePos.y);
// Segment đầu tiên đi theo chuột
_trailSegments[0].style.left = uiPos.x - (cursorSize / 2);
_trailSegments[0].style.top = uiPos.y - (cursorSize / 2);
// Cập nhật vị trí đầu tiên
_trailPositions[0] = uiPos;
// Các segment sau đuổi theo segment trước
for (int i = 1; i < _trailSegments.Count; i++)
// Đuổi theo mượt mà
for (int i = 1; i < _trailPositions.Count; i++)
{
float targetX = _trailSegments[i - 1].resolvedStyle.left;
float targetY = _trailSegments[i - 1].resolvedStyle.top;
_trailPositions[i] = Vector2.Lerp(_trailPositions[i], _trailPositions[i - 1], Time.deltaTime * 25f);
}
float currX = _trailSegments[i].resolvedStyle.left;
float currY = _trailSegments[i].resolvedStyle.top;
// Nội suy để mượt mà (Lerp)
_trailSegments[i].style.left = Mathf.Lerp(currX, targetX, Time.deltaTime * 20f);
_trailSegments[i].style.top = Mathf.Lerp(currY, targetY, Time.deltaTime * 20f);
// Áp dụng vào UI
for (int i = 0; i < _trailSegments.Count; i++)
{
_trailSegments[i].style.left = _trailPositions[i].x - (cursorSize / 2);
_trailSegments[i].style.top = _trailPositions[i].y - (cursorSize / 2);
}
}
@@ -196,7 +206,6 @@ namespace Hallucinate.UI
instance.style.display = DisplayStyle.None;
_rootElement.Add(instance);
// Luôn đảm bảo CursorLayer nằm trên cùng sau khi add màn hình mới
_cursorLayer.BringToFront();
var controller = new T();
@@ -214,7 +223,7 @@ namespace Hallucinate.UI
_history.Push(newScreen);
await newScreen.PlayTransitionIn();
_cursorLayer.BringToFront(); // Giữ trail luôn trên cùng
_cursorLayer.BringToFront();
}
public async Task Pop()