Initial commit
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// 滚动列表
|
||||
/// </summary>
|
||||
public class UIScrollList<T, Data> : UIScrollView where T : ModuleUIItem<Data> {
|
||||
|
||||
private ModuleUIItems<T, Data> Items;// UI项容器
|
||||
|
||||
public UIScrollList(VisualElement element, VisualElement canvas, VisualTreeAsset templateAsset, Func<Data, VisualElement, T> generate,
|
||||
UIDirection direction = UIDirection.HorizontalAndVertical,
|
||||
UIDirection sh = UIDirection.FromLeftToRight,
|
||||
UIDirection sv = UIDirection.FromTopToBottom) : base(element, canvas, direction, sh, sv) {
|
||||
Items = new ModuleUIItems<T, Data>(Container, templateAsset, generate);
|
||||
}
|
||||
/// <summary> 释放资源 </summary>
|
||||
public void Release() => Items.Release();
|
||||
/// <summary> 创建UI项 </summary>
|
||||
public void Create(List<Data> datas) => Items.Create(datas);
|
||||
/// <summary> 创建UI项 </summary>
|
||||
public void Create(Data data) => Items.Create(data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e2fb9b587b7ce5d4295f1bb0da1c8d70
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,144 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// 滚动视图
|
||||
/// </summary>
|
||||
public class UIScrollView : ModuleUIPanel {
|
||||
/// <summary> 绑定的画布 </summary>
|
||||
public readonly VisualElement canvas;
|
||||
/// <summary> 元素方向 </summary>
|
||||
public readonly UIDirection direction;
|
||||
/// <summary> 水平滑动方向 </summary>
|
||||
public readonly UIDirection sh;
|
||||
/// <summary> 垂直滑动方向 </summary>
|
||||
public readonly UIDirection sv;
|
||||
/// <summary> 值改变时 </summary>
|
||||
public event Action<Vector2> ValueChanged;
|
||||
|
||||
public Vector2 value;
|
||||
public bool isDrag;
|
||||
public Vector3 originalPosition;
|
||||
public Vector3 pointerPosition;
|
||||
|
||||
public readonly UIScroller horizontal;
|
||||
public readonly UIScroller vertical;
|
||||
|
||||
public VisualElement Viewport => Q<VisualElement>("Viewport");
|
||||
public VisualElement Container => Q<VisualElement>("Container");
|
||||
public VisualElement ScrollerHorizontal => Q<VisualElement>("ScrollerHorizontal");
|
||||
public VisualElement ScrollerVertical => Q<VisualElement>("ScrollerVertical");
|
||||
|
||||
public UIScrollView(VisualElement element, VisualElement canvas,
|
||||
UIDirection direction = UIDirection.HorizontalAndVertical,
|
||||
UIDirection sh = UIDirection.FromLeftToRight,
|
||||
UIDirection sv = UIDirection.FromTopToBottom) : base(element) {
|
||||
this.canvas = canvas;
|
||||
this.direction = direction;
|
||||
this.sh = sh;
|
||||
this.sv = sv;
|
||||
|
||||
element.generateVisualContent += ElementGenerateVisualContent;
|
||||
|
||||
if (sh == UIDirection.FromLeftToRight) {
|
||||
horizontal = new UIScroller(ScrollerHorizontal, canvas, sh);
|
||||
Viewport.style.flexDirection = FlexDirection.Row;
|
||||
}
|
||||
if (sh == UIDirection.FromRightToLeft) {
|
||||
horizontal = new UIScroller(ScrollerHorizontal, canvas, sh);
|
||||
Viewport.style.flexDirection = FlexDirection.RowReverse;
|
||||
}
|
||||
|
||||
if (sv == UIDirection.FromTopToBottom) {
|
||||
vertical = new UIScroller(ScrollerVertical, canvas, sv);
|
||||
Viewport.style.flexDirection = FlexDirection.Column;
|
||||
}
|
||||
if (sv == UIDirection.FromBottomToTop) {
|
||||
vertical = new UIScroller(ScrollerVertical, canvas, sv);
|
||||
Viewport.style.flexDirection = FlexDirection.ColumnReverse;
|
||||
}
|
||||
|
||||
// 设置事件
|
||||
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);
|
||||
Vector2 offset = new Vector2(0, wheel);
|
||||
if (direction == UIDirection.Horizontal) { offset = new Vector2(wheel, 0); }
|
||||
UpdateValue(new Vector2(value.x, value.y) - offset);
|
||||
}
|
||||
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 virtual 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;
|
||||
x *= sh == UIDirection.FromLeftToRight ? 1 : -1;
|
||||
y *= sv == UIDirection.FromTopToBottom ? 1 : -1;
|
||||
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;
|
||||
xPos *= sh == UIDirection.FromLeftToRight ? 1 : -1;
|
||||
yPos *= sv == UIDirection.FromTopToBottom ? 1 : -1;
|
||||
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,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9fa4135858d72ac4e8a90212625ec7db
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,117 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// 滚动视图 - 垂直
|
||||
/// </summary>
|
||||
public class UIScrollViewV : ModuleUIPanel {
|
||||
/// <summary> 绑定的画布 </summary>
|
||||
public readonly VisualElement canvas;
|
||||
/// <summary> 元素方向 </summary>
|
||||
public readonly UIDirection direction;
|
||||
/// <summary> 垂直滑块 </summary>
|
||||
public readonly UIScroller vertical;
|
||||
/// <summary> 值改变时 </summary>
|
||||
public event Action<float> ValueChanged;
|
||||
|
||||
/// <summary>
|
||||
/// 方向
|
||||
/// </summary>
|
||||
public enum UIDirection {
|
||||
FromTopToBottom = 0,
|
||||
FromBottomToTop = 1,
|
||||
}
|
||||
|
||||
public float value;
|
||||
public bool isDrag;
|
||||
public Vector3 originalPosition;
|
||||
public Vector3 pointerPosition;
|
||||
|
||||
public VisualElement Viewport => Q<VisualElement>("Viewport");
|
||||
public VisualElement Container => Q<VisualElement>("Container");
|
||||
public VisualElement ScrollerVertical => Q<VisualElement>("ScrollerVertical");
|
||||
|
||||
public UIScrollViewV(VisualElement element, VisualElement canvas, UIDirection direction) : base(element) {
|
||||
this.canvas = canvas;
|
||||
this.direction = direction;
|
||||
|
||||
if (direction == UIDirection.FromTopToBottom) {
|
||||
// vertical = new UIScroller(ScrollerVertical, canvas, direction);
|
||||
Viewport.style.flexDirection = FlexDirection.Column;
|
||||
}
|
||||
if (direction == UIDirection.FromBottomToTop) {
|
||||
// vertical = new UIScroller(ScrollerVertical, canvas, direction);
|
||||
Viewport.style.flexDirection = FlexDirection.ColumnReverse;
|
||||
}
|
||||
|
||||
// 设置事件
|
||||
vertical.ValueChanged += (y) => { UpdateValue(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);
|
||||
// 视图原始更新
|
||||
element.generateVisualContent += ElementGenerateVisualContent;
|
||||
}
|
||||
/// <summary> 原始更新 </summary>
|
||||
private void ElementGenerateVisualContent(MeshGenerationContext context) {
|
||||
float height = Mathf.Clamp01(Viewport.resolvedStyle.height / Container.resolvedStyle.height);
|
||||
vertical.Dragger.style.height = Length.Percent(height * 100);
|
||||
}
|
||||
/// <summary> 滚轮滑动 </summary>
|
||||
private void ViewportWheel(WheelEvent evt) {
|
||||
float wheel = Mathf.Clamp(evt.delta.y, -1, 1);
|
||||
UpdateValue(value - wheel);
|
||||
}
|
||||
/// <summary> 拖拽按下 </summary>
|
||||
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>
|
||||
private void Dragger() {
|
||||
Vector3 mousePosition = UITool.GetMousePosition();
|
||||
Vector3 differ = new Vector3(mousePosition.x, Screen.height - mousePosition.y) - pointerPosition;
|
||||
Vector3 offset = differ + originalPosition;
|
||||
|
||||
float maxHeight = Viewport.resolvedStyle.height - Container.resolvedStyle.height;
|
||||
float y = offset.y / maxHeight;
|
||||
y *= direction == UIDirection.FromTopToBottom ? 1 : -1;
|
||||
UpdateValue(y);
|
||||
}
|
||||
/// <summary> 滑动弹性 </summary>
|
||||
private void SlidingElasticity() {
|
||||
float original = value;
|
||||
float max = Viewport.resolvedStyle.height < Container.resolvedStyle.height ? 1 : 0;
|
||||
if (value < 0) { value = Mathf.Lerp(value, 0, Time.deltaTime * 10); }
|
||||
if (value > max) { value = Mathf.Lerp(value, max, Time.deltaTime * 10); }
|
||||
if (original != value) { UpdateValue(value); }
|
||||
}
|
||||
|
||||
/// <summary> 更新状态 </summary>
|
||||
public virtual void Update() {
|
||||
vertical.Update();
|
||||
SlidingElasticity();
|
||||
if (isDrag) { Dragger(); }
|
||||
}
|
||||
/// <summary> 更新值(0-1) </summary>
|
||||
public virtual void UpdateValue(float value, bool send = true) {
|
||||
this.value = value;
|
||||
if (send) { ValueChanged?.Invoke(value); }
|
||||
float maxHeight = Viewport.resolvedStyle.height - Container.resolvedStyle.height;
|
||||
float position = maxHeight * value;
|
||||
position *= direction == UIDirection.FromTopToBottom ? 1 : -1;
|
||||
Container.transform.position = new Vector3(0, position);
|
||||
if (vertical.value != value) { vertical.UpdateValue(value, false); }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ecfd61355ce764642865903953329f1e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user