修改UITool包

This commit is contained in:
MuHua-123
2025-03-03 17:47:30 +08:00
parent fb849c9c25
commit 850ba360bb
38 changed files with 591 additions and 255 deletions
@@ -0,0 +1,124 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace MuHua {
/// <summary>
/// 滚动视图
/// </summary>
public class UIScrollView {
/// <summary> 绑定的元素 </summary>
public readonly VisualElement element;
/// <summary> 绑定的画布 </summary>
public readonly VisualElement canvas;
/// <summary> 元素方向 </summary>
public readonly UIDirection direction;
/// <summary> 值改变时 </summary>
public event Action<Vector2> ValueChanged;
public Vector2 value;
public bool isDrag;
public Vector3 originalPosition;
public Vector3 pointerPosition;
private UIScroller horizontal;
private UIScroller vertical;
public VisualElement Viewport => element.Q<VisualElement>("Viewport");
public VisualElement Container => element.Q<VisualElement>("Container");
public VisualElement ScrollerHorizontal => element.Q<VisualElement>("ScrollerHorizontal");
public VisualElement ScrollerVertical => element.Q<VisualElement>("ScrollerVertical");
public UIScrollView(VisualElement element, VisualElement canvas, UIDirection direction = UIDirection.HorizontalAndVertical, UIDirection sh = UIDirection.FromLeftToRight, UIDirection sv = UIDirection.FromTopToBottom) {
this.element = element;
this.canvas = canvas;
this.direction = direction;
element.generateVisualContent += ElementGenerateVisualContent;
if (sh == UIDirection.FromLeftToRight) { horizontal = new UIScroller(ScrollerHorizontal, canvas, sh); }
if (sh == UIDirection.FromRightToLeft) { horizontal = new UIScroller(ScrollerHorizontal, canvas, sh); }
if (sv == UIDirection.FromTopToBottom) { vertical = new UIScroller(ScrollerVertical, canvas, sv); }
if (sv == UIDirection.FromBottomToTop) { vertical = new UIScroller(ScrollerVertical, canvas, sv); }
//设置事件
horizontal.ValueChanged += (x) => { UpdateValue(new Vector2(x, value.y)); };
vertical.ValueChanged += (y) => { UpdateValue(new Vector2(value.x, y)); };
Viewport.RegisterCallback<WheelEvent>(ViewportWheel);
Viewport.RegisterCallback<PointerDownEvent>(DraggerDown);
Viewport.RegisterCallback<MouseCaptureEvent>((evt) => isDrag = false);
canvas.RegisterCallback<PointerUpEvent>((evt) => isDrag = false);
canvas.RegisterCallback<PointerLeaveEvent>((evt) => isDrag = false);
}
/// <summary> 视图原始更新 </summary>
private void ElementGenerateVisualContent(MeshGenerationContext context) {
float width = Mathf.Clamp01(Viewport.resolvedStyle.width / Container.resolvedStyle.width);
float height = Mathf.Clamp01(Viewport.resolvedStyle.height / Container.resolvedStyle.height);
horizontal.dragger.style.width = Length.Percent(width * 100);
vertical.dragger.style.height = Length.Percent(height * 100);
}
/// <summary> 视图滚轮滑动 </summary>
private void ViewportWheel(WheelEvent evt) {
float wheel = Mathf.Clamp(evt.delta.y, -1, 1);
if (direction == UIDirection.Horizontal) {
UpdateValue(new Vector2(value.x - wheel, value.y));
} else {
UpdateValue(new Vector2(value.x, value.y - wheel));
}
}
private void DraggerDown(PointerDownEvent evt) {
isDrag = true;
originalPosition = Container.transform.position;
Vector3 mousePosition = UITool.GetMousePosition();
pointerPosition = new Vector3(mousePosition.x, Screen.height - mousePosition.y);
}
/// <summary> 更新状态 </summary>
public void Update() {
horizontal.Update();
vertical.Update();
Vector2 original = value;
float maxX = Viewport.resolvedStyle.width < Container.resolvedStyle.width ? 1 : 0;
float maxY = Viewport.resolvedStyle.height < Container.resolvedStyle.height ? 1 : 0;
if (value.x < 0) { value.x = Mathf.Lerp(value.x, 0, Time.deltaTime * 10); }
if (value.x > maxX) { value.x = Mathf.Lerp(value.x, maxX, Time.deltaTime * 10); }
if (value.y < 0) { value.y = Mathf.Lerp(value.y, 0, Time.deltaTime * 10); }
if (value.y > maxY) { value.y = Mathf.Lerp(value.y, maxY, Time.deltaTime * 10); }
if (original != value) { UpdateValue(value); }
if (!isDrag) { return; }
Vector3 mousePosition = UITool.GetMousePosition();
Vector3 differ = new Vector3(mousePosition.x, Screen.height - mousePosition.y) - pointerPosition;
Vector3 offset = differ + originalPosition;
float maxWidth = Viewport.resolvedStyle.width - Container.resolvedStyle.width;
float maxHeight = Viewport.resolvedStyle.height - Container.resolvedStyle.height;
float x = offset.x / maxWidth;
float y = offset.y / maxHeight;
UpdateValue(new Vector2(x, y));
}
/// <summary> 更新值(0-1) </summary>
public void UpdateValue(Vector2 value, bool send = true) {
if (direction == UIDirection.Horizontal) { value.y = 0; }
if (direction == UIDirection.Vertical) { value.x = 0; }
this.value = value;
if (send) { ValueChanged?.Invoke(value); }
float maxWidth = Viewport.resolvedStyle.width - Container.resolvedStyle.width;
float maxHeight = Viewport.resolvedStyle.height - Container.resolvedStyle.height;
float xPos = maxWidth * value.x;
float yPos = maxHeight * value.y;
Container.transform.position = new Vector3(xPos, yPos);
if (horizontal.value != value.x) { horizontal.UpdateValue(value.x, false); }
if (vertical.value != value.y) { vertical.UpdateValue(value.y, false); }
}
}
}
@@ -0,0 +1,187 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace MuHua {
/// <summary>
/// 滑块
/// </summary>
public class UIScroller {
/// <summary> 绑定的元素 </summary>
public readonly VisualElement element;
/// <summary> 绑定的画布 </summary>
public readonly VisualElement canvas;
/// <summary> 元素方向 </summary>
public readonly UIDirection direction;
/// <summary> 值改变时 </summary>
public event Action<float> ValueChanged;
public float value;
public bool isDragger;
public float originalPosition;
public float pointerPosition;
private UIScrollerFunc scrollerFunc;
public VisualElement dragger => element.Q<VisualElement>("Dragger");
public UIScroller(VisualElement element, VisualElement canvas, UIDirection direction = UIDirection.FromLeftToRight) {
this.element = element;
this.canvas = canvas;
this.direction = direction;
if (direction == UIDirection.FromLeftToRight) { scrollerFunc = new FromLeftToRight(this); }
if (direction == UIDirection.FromRightToLeft) { scrollerFunc = new FromRightToLeft(this); }
if (direction == UIDirection.FromTopToBottom) { scrollerFunc = new FromTopToBottom(this); }
if (direction == UIDirection.FromBottomToTop) { scrollerFunc = new FromBottomToTop(this); }
//设置事件
dragger.RegisterCallback<PointerDownEvent>(DraggerDown);
element.RegisterCallback<PointerDownEvent>(ElementDown);
canvas.RegisterCallback<PointerUpEvent>((evt) => isDragger = false);
canvas.RegisterCallback<PointerLeaveEvent>((evt) => isDragger = false);
}
public virtual void DraggerDown(PointerDownEvent evt) => scrollerFunc.DraggerDown(evt);
public virtual void ElementDown(PointerDownEvent evt) => scrollerFunc.ElementDown(evt);
/// <summary> 更新状态 </summary>
public virtual void Update() => scrollerFunc.Update();
/// <summary> 更新值(0-1) </summary>
public virtual void UpdateValue(float value, bool send = true) => scrollerFunc.UpdateValue(value, send);
public abstract class UIScrollerFunc {
public readonly UIScroller scroller;
public UIScrollerFunc(UIScroller scroller) => this.scroller = scroller;
public abstract void DraggerDown(PointerDownEvent evt);
public abstract void ElementDown(PointerDownEvent evt);
/// <summary> 更新状态 </summary>
public abstract void Update();
/// <summary> 更新值(0-1) </summary>
public abstract void UpdateValue(float value, bool send = true);
}
public class FromLeftToRight : UIScrollerFunc {
public FromLeftToRight(UIScroller scroller) : base(scroller) { }
public override void DraggerDown(PointerDownEvent evt) {
scroller.isDragger = true;
scroller.originalPosition = scroller.dragger.transform.position.x;
scroller.pointerPosition = UITool.GetMousePosition().x;
}
public override void ElementDown(PointerDownEvent evt) {
float offset = evt.localPosition.x - scroller.dragger.resolvedStyle.width * 0.5f;
float max = scroller.element.resolvedStyle.width - scroller.dragger.resolvedStyle.width;
float value = Mathf.InverseLerp(0, max, offset);
UpdateValue(value);
}
public override void Update() {
if (!scroller.isDragger) { return; }
float differ = UITool.GetMousePosition().x - scroller.pointerPosition;
float offset = differ + scroller.originalPosition;
float max = scroller.element.resolvedStyle.width - scroller.dragger.resolvedStyle.width;
float value = Mathf.InverseLerp(0, max, offset);
UpdateValue(value);
}
public override void UpdateValue(float value, bool send = true) {
scroller.value = value;
if (send) { scroller.ValueChanged?.Invoke(value); }
float max = scroller.element.resolvedStyle.width - scroller.dragger.resolvedStyle.width;
float x = Mathf.Lerp(0, max, value);
scroller.dragger.transform.position = new Vector3(x, 0);
}
}
public class FromRightToLeft : UIScrollerFunc {
public FromRightToLeft(UIScroller scroller) : base(scroller) { }
public override void DraggerDown(PointerDownEvent evt) {
scroller.isDragger = true;
scroller.originalPosition = scroller.dragger.transform.position.x;
scroller.pointerPosition = UITool.GetMousePosition().x;
}
public override void ElementDown(PointerDownEvent evt) {
float offset = evt.localPosition.x - scroller.dragger.resolvedStyle.width * 0.5f;
float max = scroller.element.resolvedStyle.width - scroller.dragger.resolvedStyle.width;
float value = Mathf.InverseLerp(max, 0, offset);
UpdateValue(value);
}
public override void Update() {
if (!scroller.isDragger) { return; }
float differ = UITool.GetMousePosition().x - scroller.pointerPosition;
float offset = differ + scroller.originalPosition;
float max = scroller.element.resolvedStyle.width - scroller.dragger.resolvedStyle.width;
float value = Mathf.InverseLerp(max, 0, offset);
UpdateValue(value);
}
public override void UpdateValue(float value, bool send = true) {
scroller.value = value;
if (send) { scroller.ValueChanged?.Invoke(value); }
float max = scroller.element.resolvedStyle.width - scroller.dragger.resolvedStyle.width;
float x = Mathf.Lerp(max, 0, value);
scroller.dragger.transform.position = new Vector3(x, 0);
}
}
public class FromTopToBottom : UIScrollerFunc {
public FromTopToBottom(UIScroller scroller) : base(scroller) { }
public override void DraggerDown(PointerDownEvent evt) {
scroller.isDragger = true;
scroller.originalPosition = scroller.dragger.transform.position.y;
scroller.pointerPosition = Screen.height - UITool.GetMousePosition().y;
}
public override void ElementDown(PointerDownEvent evt) {
float offset = evt.localPosition.y - scroller.dragger.resolvedStyle.height * 0.5f;
float max = scroller.element.resolvedStyle.height - scroller.dragger.resolvedStyle.height;
float value = Mathf.InverseLerp(0, max, offset);
UpdateValue(value);
}
public override void Update() {
if (!scroller.isDragger) { return; }
float differ = Screen.height - UITool.GetMousePosition().y - scroller.pointerPosition;
float offset = differ + scroller.originalPosition;
float max = scroller.element.resolvedStyle.height - scroller.dragger.resolvedStyle.height;
float value = Mathf.InverseLerp(0, max, offset);
UpdateValue(value);
}
public override void UpdateValue(float value, bool send = true) {
scroller.value = value;
if (send) { scroller.ValueChanged?.Invoke(value); }
float max = scroller.element.resolvedStyle.height - scroller.dragger.resolvedStyle.height;
float y = Mathf.Lerp(0, max, value);
scroller.dragger.transform.position = new Vector3(0, y);
}
}
public class FromBottomToTop : UIScrollerFunc {
public FromBottomToTop(UIScroller scroller) : base(scroller) { }
public override void DraggerDown(PointerDownEvent evt) {
scroller.isDragger = true;
scroller.originalPosition = scroller.dragger.transform.position.y;
scroller.pointerPosition = Screen.height - UITool.GetMousePosition().y;
}
public override void ElementDown(PointerDownEvent evt) {
float offset = evt.localPosition.y - scroller.dragger.resolvedStyle.height * 0.5f;
float max = scroller.element.resolvedStyle.height - scroller.dragger.resolvedStyle.height;
float value = Mathf.InverseLerp(max, 0, offset);
UpdateValue(value);
}
public override void Update() {
if (!scroller.isDragger) { return; }
float differ = Screen.height - UITool.GetMousePosition().y - scroller.pointerPosition;
float offset = differ + scroller.originalPosition;
float max = scroller.element.resolvedStyle.height - scroller.dragger.resolvedStyle.height;
float value = Mathf.InverseLerp(max, 0, offset);
UpdateValue(value);
}
public override void UpdateValue(float value, bool send = true) {
scroller.value = value;
if (send) { scroller.ValueChanged?.Invoke(value); }
float max = scroller.element.resolvedStyle.height - scroller.dragger.resolvedStyle.height;
float y = Mathf.Lerp(max, 0, value);
scroller.dragger.transform.position = new Vector3(0, y);
}
}
}
}
@@ -0,0 +1,19 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 方向
/// </summary>
public enum UIDirection {
FromLeftToRight = 0,
FromRightToLeft = 1,
FromTopToBottom = 2,
FromBottomToTop = 3,
HorizontalAndVertical = 4,
Horizontal = 5,
Vertical = 6,
}
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: d15576153e2ca2d40b07796a726007bf
guid: c2b0340184fec6c45815d13d6a1f7321
MonoImporter:
externalObjects: {}
serializedVersion: 2
@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 347543776b7c9134c8f51ee8930b55ea
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,14 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua
{
/// <summary>
/// 滚动视图
/// </summary>
public class UIScrollView
{
}
}
@@ -1,15 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua
{
/// <summary>
/// 滚动视图(水平)
/// </summary>
public class UIScrollViewHorizontal
{
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: cce3cdf2fe4ebe14e8afcf78f68c9469
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,14 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua
{
/// <summary>
/// 滚动视图(垂直)
/// </summary>
public class UIScrollViewVertical
{
}
}
@@ -1,76 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace MuHua
{
/// <summary>
/// 滑块(水平)
/// </summary>
public class UIScrollerHorizontal
{
/// <summary> 绑定的元素 </summary>
public readonly VisualElement element;
/// <summary> 绑定的画布 </summary>
public readonly VisualElement canvas;
/// <summary> 值改变时 </summary>
public event Action<float> ValueChanged;
public float value;
public bool isDragger;
public float originalPosition;
public float pointerPosition;
public VisualElement dragger => element.Q<VisualElement>("Dragger");
public UIScrollerHorizontal(VisualElement element, VisualElement canvas)
{
this.element = element;
this.canvas = canvas;
//设置事件
dragger.RegisterCallback<PointerDownEvent>(DraggerDown);
element.RegisterCallback<PointerDownEvent>(ElementDown);
canvas.RegisterCallback<PointerUpEvent>((evt) => isDragger = false);
canvas.RegisterCallback<PointerLeaveEvent>((evt) => isDragger = false);
}
public void DraggerDown(PointerDownEvent evt)
{
isDragger = true;
originalPosition = dragger.transform.position.x;
pointerPosition = UITool.GetMousePosition().x;
}
public void ElementDown(PointerDownEvent evt)
{
float offset = evt.localPosition.x - dragger.resolvedStyle.width * 0.5f;
float max = element.resolvedStyle.width - dragger.resolvedStyle.width;
float value = Mathf.InverseLerp(0, max, offset);
UpdateValue(value);
}
/// <summary> 更新状态 </summary>
public virtual void Update()
{
if (!isDragger) { return; }
float differ = UITool.GetMousePosition().x - pointerPosition;
float offset = differ + originalPosition;
float max = element.resolvedStyle.width - dragger.resolvedStyle.width;
float value = Mathf.InverseLerp(0, max, offset);
UpdateValue(value);
}
/// <summary> 更新值(0-1) </summary>
public void UpdateValue(float value)
{
this.value = value;
ValueChanged?.Invoke(value);
float max = element.resolvedStyle.width - dragger.resolvedStyle.width;
float x = Mathf.Lerp(0, max, value);
dragger.transform.position = new Vector3(x, 0);
}
}
}
@@ -1,14 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua
{
/// <summary>
/// 滑块(垂直)
/// </summary>
public class UIScrollerVertical
{
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: d1f12d534a6e94f4d93a1a91f85e0519
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: