合并代码

This commit is contained in:
MuHua-123
2024-11-15 18:28:21 +08:00
parent 497b43a446
commit 72d1f89b54
274 changed files with 4939 additions and 1968 deletions
@@ -0,0 +1,33 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace MuHua {
public class MUScrollView : VisualElement {
public new class UxmlFactory : UxmlFactory<MUScrollView, UxmlTraits> { }
public new class UxmlTraits : VisualElement.UxmlTraits {
}
public VisualElement viewport = new VisualElement();
public VisualElement container = new VisualElement();
public MUScrollView() {
//设置名称
viewport.name = "viewport";
container.name = "container";
//设置USS类名
AddToClassList("scroll-view");
viewport.AddToClassList("scroll-view-viewport");
container.AddToClassList("scroll-view-container");
//设置层级结构
hierarchy.Add(viewport);
viewport.Add(container);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1f44619e724604b47834e3639ffe2947
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,138 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace MuHua {
public class MUScrollViewHorizontal : VisualElement {
public new class UxmlFactory : UxmlFactory<MUScrollViewHorizontal, UxmlTraits> { }
public new class UxmlTraits : VisualElement.UxmlTraits {
private UxmlIntAttributeDescription MouseWheelScrollSize = new UxmlIntAttributeDescription {
name = "mouse-wheel-scroll-size",
defaultValue = 18
};
private UxmlFloatAttributeDescription SlidingValue = new UxmlFloatAttributeDescription {
name = "sliding-value",
defaultValue = 0
};
public UxmlIntAttributeDescription Count = new UxmlIntAttributeDescription {
name = "count"
};
public UxmlStringAttributeDescription AssetPath = new UxmlStringAttributeDescription {
name = "asset-path"
};
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) {
base.Init(ve, bag, cc);
MUScrollViewHorizontal scrollView = (MUScrollViewHorizontal)ve;
scrollView.MouseWheelScrollSize = MouseWheelScrollSize.GetValueFromBag(bag, cc);
scrollView.SlidingValue = SlidingValue.GetValueFromBag(bag, cc);
scrollView.AssetPath = AssetPath.GetValueFromBag(bag, cc);
#if UNITY_EDITOR
VisualTreeAsset asset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(scrollView.AssetPath);
if (asset == null) { return; }
scrollView.ClearContainer();
int count = Count.GetValueFromBag(bag, cc);
for (int i = 0; i < count; i++) { scrollView.AddContainer(asset); }
#endif
scrollView.scroller.MouseWheelScrollSize = scrollView.MouseWheelScrollSize;
scrollView.ContainerRelease();
}
}
public MUScrollerHorizontal scroller = new MUScrollerHorizontal();
public VisualElement viewport = new VisualElement();
public VisualElement container = new VisualElement();
public string AssetPath { get; set; }
public float MouseWheelScrollSize { get; set; }
public float SlidingValue { get; set; }
public void AddContainer(VisualTreeAsset asset) {
AddContainer(asset.Instantiate());
}
public void AddContainer(VisualElement element) {
container.Add(element);
}
public void ClearContainer() {
container.Clear();
}
internal bool isDragger;
internal float mousePosition;
internal float ViewportWidth { get => viewport.resolvedStyle.width; }
internal float ContainerWidth { get => container.resolvedStyle.width; }
internal float MaxPosition { get => ComputeMaxPosition(); }
internal float ComputeMaxPosition() {
float value = ViewportWidth - ContainerWidth;
return value <= 0 ? value : -1;
}
internal void UpdateVisualElement(float value) {
SlidingValue = value;
container.transform.position = new Vector3(MaxPosition * SlidingValue, 0);
scroller.UpdateVisualElement(SlidingValue);
}
public MUScrollViewHorizontal() {
//设置名称
viewport.name = "viewport";
container.name = "container";
//设置USS类名
AddToClassList("horizontal-scroll-view");
viewport.AddToClassList("horizontal-scroll-view-viewport");
container.AddToClassList("horizontal-scroll-view-container");
scroller.ClearClassList();
scroller.AddToClassList("horizontal-scroll-view-scroller");
VisualElement dragger = scroller.dragger;
dragger.ClearClassList();
dragger.AddToClassList("horizontal-scroll-view-dragger");
//设置层级结构
hierarchy.Add(viewport);
hierarchy.Add(scroller);
viewport.Add(container);
//设置事件
scroller.SlidingValueChanged += UpdateVisualElement;
container.RegisterCallback<PointerDownEvent>(ContainerDown);
container.RegisterCallback<PointerMoveEvent>(ContainerDrag);
container.RegisterCallback<PointerUpEvent>((evt) => ContainerRelease());
container.RegisterCallback<PointerLeaveEvent>((evt) => ContainerRelease());
container.RegisterCallback<WheelEvent>(ContainerWheel);
container.generateVisualContent += ContainerGenerateVisualContent;
}
private void ContainerDown(PointerDownEvent evt) {
isDragger = true;
mousePosition = evt.position.x - SlidingValue * MaxPosition;
}
private void ContainerDrag(PointerMoveEvent evt) {
if (!isDragger) { return; }
float offset = evt.position.x - mousePosition;
float value = offset / MaxPosition;
UpdateVisualElement(value);
mousePosition = evt.position.x - SlidingValue * MaxPosition;
}
private void ContainerRelease() {
isDragger = false;
float value = Mathf.Clamp01(SlidingValue);
container.schedule.Execute(() => { UpdateVisualElement(value); }).StartingIn(1);
}
private void ContainerWheel(WheelEvent evt) {
float wheel = Mathf.Clamp(evt.delta.y, -1, 1);
float current = SlidingValue * MaxPosition;
float offset = current - wheel * MouseWheelScrollSize;
float value = offset / MaxPosition;
UpdateVisualElement(value);
ContainerRelease();
}
private void ContainerGenerateVisualContent(MeshGenerationContext mgc) {
float ratio = Mathf.Clamp01(ViewportWidth / ContainerWidth);
scroller.UpdateDragger(ratio);
ContainerRelease();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6d083933c2f0cfa448e7c5a5f22c253c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,139 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace MuHua {
public class MUScrollViewVertical : VisualElement {
public new class UxmlFactory : UxmlFactory<MUScrollViewVertical, UxmlTraits> { }
public new class UxmlTraits : VisualElement.UxmlTraits {
private UxmlIntAttributeDescription MouseWheelScrollSize = new UxmlIntAttributeDescription {
name = "mouse-wheel-scroll-size",
defaultValue = 18
};
private UxmlFloatAttributeDescription SlidingValue = new UxmlFloatAttributeDescription {
name = "sliding-value",
defaultValue = 0
};
public UxmlIntAttributeDescription Count = new UxmlIntAttributeDescription {
name = "count"
};
public UxmlStringAttributeDescription AssetPath = new UxmlStringAttributeDescription {
name = "asset-path"
};
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) {
base.Init(ve, bag, cc);
MUScrollViewVertical scrollView = (MUScrollViewVertical)ve;
scrollView.MouseWheelScrollSize = MouseWheelScrollSize.GetValueFromBag(bag, cc);
scrollView.SlidingValue = SlidingValue.GetValueFromBag(bag, cc);
scrollView.AssetPath = AssetPath.GetValueFromBag(bag, cc);
#if UNITY_EDITOR
VisualTreeAsset asset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(scrollView.AssetPath);
if (asset == null) { return; }
scrollView.ClearContainer();
int count = Count.GetValueFromBag(bag, cc);
for (int i = 0; i < count; i++) { scrollView.AddContainer(asset); }
#endif
scrollView.scroller.MouseWheelScrollSize = scrollView.MouseWheelScrollSize;
scrollView.ContainerRelease();
}
}
public MUScrollerVertical scroller = new MUScrollerVertical();
public VisualElement viewport = new VisualElement();
public VisualElement container = new VisualElement();
public string AssetPath { get; set; }
public float MouseWheelScrollSize { get; set; }
public float SlidingValue { get; set; }
public void AddContainer(VisualTreeAsset asset) {
AddContainer(asset.Instantiate());
}
public void AddContainer(VisualElement element) {
container.Add(element);
}
public void ClearContainer() {
container.Clear();
}
internal bool isDragger;
internal float mousePosition;
internal float ViewportHeight { get => viewport.resolvedStyle.height; }
internal float ContainerHeight { get => container.resolvedStyle.height; }
internal float MaxPosition { get => ComputeMaxPosition(); }
internal float ComputeMaxPosition() {
float value = ViewportHeight - ContainerHeight;
return value <= 0 ? value : -1;
}
internal void UpdateVisualElement(float value) {
SlidingValue = value;
container.transform.position = new Vector3(0, MaxPosition * SlidingValue);
scroller.UpdateVisualElement(SlidingValue);
}
public MUScrollViewVertical() {
//设置名称
viewport.name = "viewport";
container.name = "container";
//设置USS类名
AddToClassList("vertical-scroll-view");
viewport.AddToClassList("vertical-scroll-view-viewport");
container.AddToClassList("vertical-scroll-view-container");
scroller.ClearClassList();
scroller.AddToClassList("vertical-scroll-view-scroller");
VisualElement dragger = scroller.dragger;
dragger.ClearClassList();
dragger.AddToClassList("vertical-scroll-view-dragger");
//设置层级结构
hierarchy.Add(viewport);
hierarchy.Add(scroller);
viewport.Add(container);
//设置事件
scroller.SlidingValueChanged += UpdateVisualElement;
container.RegisterCallback<PointerDownEvent>(ContainerDown);
container.RegisterCallback<PointerMoveEvent>(ContainerDrag);
container.RegisterCallback<PointerUpEvent>((evt) => ContainerRelease());
container.RegisterCallback<PointerLeaveEvent>((evt) => ContainerRelease());
container.RegisterCallback<WheelEvent>(ContainerWheel);
container.generateVisualContent += ContainerGenerateVisualContent;
}
private void ContainerDown(PointerDownEvent evt) {
isDragger = true;
mousePosition = evt.position.y - SlidingValue * MaxPosition;
}
private void ContainerDrag(PointerMoveEvent evt) {
if (!isDragger) { return; }
float offset = evt.position.y - mousePosition;
float value = offset / MaxPosition;
UpdateVisualElement(value);
mousePosition = evt.position.y - SlidingValue * MaxPosition;
}
private void ContainerRelease() {
isDragger = false;
float value = Mathf.Clamp01(SlidingValue);
container.schedule.Execute(() => { UpdateVisualElement(value); }).StartingIn(1);
}
private void ContainerWheel(WheelEvent evt) {
float wheel = Mathf.Clamp(evt.delta.y, -1, 1);
float current = SlidingValue * MaxPosition;
float offset = current - wheel * MouseWheelScrollSize;
float value = offset / MaxPosition;
UpdateVisualElement(value);
ContainerRelease();
}
private void ContainerGenerateVisualContent(MeshGenerationContext mgc) {
float ratio = Mathf.Clamp01(ViewportHeight / ContainerHeight);
scroller.UpdateDragger(ratio);
ContainerRelease();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e8d4926c5a75b394b843a5630d5005f7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,105 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace MuHua {
public class MUScrollerHorizontal : VisualElement {
public new class UxmlFactory : UxmlFactory<MUScrollerHorizontal, UxmlTraits> { }
public new class UxmlTraits : VisualElement.UxmlTraits {
private UxmlIntAttributeDescription MouseWheelScrollSize = new UxmlIntAttributeDescription {
name = "mouse-wheel-scroll-size",
defaultValue = 18
};
private UxmlFloatAttributeDescription SlidingValue = new UxmlFloatAttributeDescription {
name = "sliding-value",
defaultValue = 0
};
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) {
base.Init(ve, bag, cc);
MUScrollerHorizontal scroller = (MUScrollerHorizontal)ve;
scroller.MouseWheelScrollSize = MouseWheelScrollSize.GetValueFromBag(bag, cc);
scroller.SlidingValue = SlidingValue.GetValueFromBag(bag, cc);
scroller.ElasticRestoration();
}
}
public event Action<float> SlidingValueChanged;
public VisualElement dragger = new VisualElement();
public float MouseWheelScrollSize { get; set; }
public float SlidingValue { get; set; }
internal bool isDragger;
internal float mousePosition;
internal float ViewportWidth { get => resolvedStyle.width; }
internal float ContainerWidth { get => dragger.resolvedStyle.width; }
internal float MaxPosition { get => ComputeMaxPosition(); }
internal float ComputeMaxPosition() {
float value = ViewportWidth - ContainerWidth;
return value >= 0 ? value : -1;
}
internal void UpdateDragger(float ratio) {
dragger.style.width = ratio * resolvedStyle.width;
}
internal void UpdateVisualElement(float value, bool callback = false) {
SlidingValue = Mathf.Clamp(value, 0, 1);
dragger.transform.position = new Vector3(MaxPosition * SlidingValue, 0);
if (callback) { SlidingValueChanged?.Invoke(SlidingValue); }
}
internal void ElasticRestoration() {
dragger.schedule.Execute(() => { UpdateVisualElement(SlidingValue); }).StartingIn(1);
}
public MUScrollerHorizontal() {
//设置名称
dragger.name = "dragger";
//设置USS类名
AddToClassList("horizontal-scroller");
dragger.AddToClassList("horizontal-scroller-dragger");
//设置层级结构
hierarchy.Add(dragger);
//设置事件
dragger.RegisterCallback<PointerDownEvent>(DraggerDown);
dragger.RegisterCallback<PointerMoveEvent>(DraggerDrag);
dragger.generateVisualContent += DraggerGenerateVisualContent;
RegisterCallback<PointerDownEvent>(ScrollerDown);
RegisterCallback<WheelEvent>(ScrollerWheel);
RegisterCallback<PointerUpEvent>((evt) => ScrollerRelease());
RegisterCallback<PointerLeaveEvent>((evt) => ScrollerRelease());
}
private void DraggerDown(PointerDownEvent evt) {
isDragger = true;
mousePosition = evt.position.x - SlidingValue * MaxPosition;
}
private void DraggerDrag(PointerMoveEvent evt) {
if (!isDragger) { return; }
float offset = evt.position.x - mousePosition;
float value = offset / MaxPosition;
UpdateVisualElement(value, true);
mousePosition = evt.position.x - SlidingValue * MaxPosition;
}
private void DraggerGenerateVisualContent(MeshGenerationContext mgc) {
ElasticRestoration();
}
private void ScrollerDown(PointerDownEvent evt) {
float offset = evt.localPosition.x - dragger.resolvedStyle.width * 0.5f;
float value = offset / MaxPosition;
UpdateVisualElement(value, true);
}
private void ScrollerWheel(WheelEvent evt) {
float wheel = Mathf.Clamp(evt.delta.y, -1, 1);
float current = SlidingValue * MaxPosition;
float offset = current + wheel * MouseWheelScrollSize;
float value = offset / MaxPosition;
UpdateVisualElement(value, true);
}
private void ScrollerRelease() {
isDragger = false;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e9c31f78aa7665041a4858f2db3b5c07
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,105 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace MuHua {
public class MUScrollerVertical : VisualElement {
public new class UxmlFactory : UxmlFactory<MUScrollerVertical, UxmlTraits> { }
public new class UxmlTraits : VisualElement.UxmlTraits {
private UxmlIntAttributeDescription MouseWheelScrollSize = new UxmlIntAttributeDescription {
name = "mouse-wheel-scroll-size",
defaultValue = 18
};
private UxmlFloatAttributeDescription SlidingValue = new UxmlFloatAttributeDescription {
name = "sliding-value",
defaultValue = 0
};
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) {
base.Init(ve, bag, cc);
MUScrollerVertical scroller = (MUScrollerVertical)ve;
scroller.MouseWheelScrollSize = MouseWheelScrollSize.GetValueFromBag(bag, cc);
scroller.SlidingValue = SlidingValue.GetValueFromBag(bag, cc);
scroller.ElasticRestoration();
}
}
public event Action<float> SlidingValueChanged;
public VisualElement dragger = new VisualElement();
public float MouseWheelScrollSize { get; set; }
public float SlidingValue { get; set; }
internal bool isDragger;
internal float mousePosition;
internal float ViewportHeight { get => resolvedStyle.height; }
internal float ContainerHeight { get => dragger.resolvedStyle.height; }
internal float MaxPosition { get => ComputeMaxPosition(); }
internal float ComputeMaxPosition() {
float value = ViewportHeight - ContainerHeight;
return value >= 0 ? value : -1;
}
internal void UpdateDragger(float ratio) {
dragger.style.height = ratio * resolvedStyle.height;
}
internal void UpdateVisualElement(float value, bool callback = false) {
SlidingValue = Mathf.Clamp(value, 0, 1);
dragger.transform.position = new Vector3(0, MaxPosition * SlidingValue);
if (callback) { SlidingValueChanged?.Invoke(SlidingValue); }
}
internal void ElasticRestoration() {
dragger.schedule.Execute(() => { UpdateVisualElement(SlidingValue); }).StartingIn(1);
}
public MUScrollerVertical() {
//设置名称
dragger.name = "dragger";
//设置USS类名
AddToClassList("vertical-scroller");
dragger.AddToClassList("vertical-scroller-dragger");
//设置层级结构
hierarchy.Add(dragger);
//设置事件
dragger.RegisterCallback<PointerDownEvent>(DraggerDown);
dragger.RegisterCallback<PointerMoveEvent>(DraggerDrag);
dragger.generateVisualContent += DraggerGenerateVisualContent;
RegisterCallback<PointerDownEvent>(ScrollerDown);
RegisterCallback<WheelEvent>(ScrollerWheel);
RegisterCallback<PointerUpEvent>((evt) => ScrollerRelease());
RegisterCallback<PointerLeaveEvent>((evt) => ScrollerRelease());
}
private void DraggerDown(PointerDownEvent evt) {
isDragger = true;
mousePosition = evt.position.y - SlidingValue * MaxPosition;
}
private void DraggerDrag(PointerMoveEvent evt) {
if (!isDragger) { return; }
float offset = evt.position.y - mousePosition;
float value = offset / MaxPosition;
UpdateVisualElement(value, true);
mousePosition = evt.position.y - SlidingValue * MaxPosition;
}
private void DraggerGenerateVisualContent(MeshGenerationContext mgc) {
ElasticRestoration();
}
private void ScrollerDown(PointerDownEvent evt) {
float offset = evt.localPosition.y - dragger.resolvedStyle.height * 0.5f;
float value = offset / MaxPosition;
UpdateVisualElement(value, true);
}
private void ScrollerWheel(WheelEvent evt) {
float wheel = Mathf.Clamp(evt.delta.y, -1, 1);
float current = SlidingValue * MaxPosition;
float offset = current + wheel * MouseWheelScrollSize;
float value = offset / MaxPosition;
UpdateVisualElement(value, true);
}
private void ScrollerRelease() {
isDragger = false;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 170e690428c828a4bbfad60ded902e45
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: