1
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
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 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 readonly VisualElement Viewport;
|
||||
public readonly VisualElement Container;
|
||||
public readonly VisualElement ScrollerHorizontal;
|
||||
public readonly 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;
|
||||
|
||||
Viewport = element.Q<VisualElement>("Viewport");
|
||||
Container = element.Q<VisualElement>("Container");
|
||||
ScrollerHorizontal = element.Q<VisualElement>("ScrollerHorizontal");
|
||||
ScrollerVertical = element.Q<VisualElement>("ScrollerVertical");
|
||||
|
||||
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,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9fa4135858d72ac4e8a90212625ec7db
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,186 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// 滚动条
|
||||
/// </summary>
|
||||
public class UIScroller : ModuleUIPanel {
|
||||
/// <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;
|
||||
|
||||
public readonly UIScrollerFunc scrollerFunc;
|
||||
|
||||
public readonly VisualElement Dragger;
|
||||
|
||||
public UIScroller(VisualElement element, VisualElement canvas, UIDirection direction = UIDirection.FromLeftToRight) : base(element) {
|
||||
this.canvas = canvas;
|
||||
this.direction = direction;
|
||||
|
||||
Dragger = element.Q<VisualElement>("Dragger");
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private void DraggerDown(PointerDownEvent evt) => scrollerFunc.DraggerDown(evt);
|
||||
private void ElementDown(PointerDownEvent evt) => scrollerFunc.ElementDown(evt);
|
||||
/// <summary> 更新状态 </summary>
|
||||
public void Update() => scrollerFunc.Update();
|
||||
/// <summary> 更新值(0-1) </summary>
|
||||
public 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,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b0bf333809175ae4ebc4469feab46cc7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,182 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// 滑块
|
||||
/// </summary>
|
||||
public class UISlider : ModuleUIPanel {
|
||||
/// <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;
|
||||
|
||||
public readonly UISliderFunc sliderFunc;
|
||||
|
||||
public readonly VisualElement Container;
|
||||
public readonly VisualElement Tracker;
|
||||
public readonly VisualElement Dragger;
|
||||
|
||||
public UISlider(VisualElement element, VisualElement canvas, UIDirection direction = UIDirection.FromLeftToRight) : base(element) {
|
||||
this.canvas = canvas;
|
||||
this.direction = direction;
|
||||
|
||||
Container = element.Q<VisualElement>("Container");
|
||||
Tracker = element.Q<VisualElement>("Tracker");
|
||||
Dragger = element.Q<VisualElement>("Dragger");
|
||||
|
||||
if (direction == UIDirection.FromLeftToRight) { sliderFunc = new FromLeftToRight(this); }
|
||||
if (direction == UIDirection.FromRightToLeft) { sliderFunc = new FromRightToLeft(this); }
|
||||
if (direction == UIDirection.FromTopToBottom) { sliderFunc = new FromTopToBottom(this); }
|
||||
if (direction == UIDirection.FromBottomToTop) { sliderFunc = new FromBottomToTop(this); }
|
||||
|
||||
//设置事件
|
||||
Dragger.RegisterCallback<PointerDownEvent>(DraggerDown);
|
||||
Container.RegisterCallback<PointerDownEvent>(ElementDown);
|
||||
|
||||
canvas.RegisterCallback<PointerUpEvent>((evt) => isDragger = false);
|
||||
canvas.RegisterCallback<PointerLeaveEvent>((evt) => isDragger = false);
|
||||
}
|
||||
|
||||
private void DraggerDown(PointerDownEvent evt) => sliderFunc.DraggerDown(evt);
|
||||
private void ElementDown(PointerDownEvent evt) => sliderFunc.ElementDown(evt);
|
||||
/// <summary> 更新状态 </summary>
|
||||
public void Update() => sliderFunc.Update();
|
||||
/// <summary> 更新值(0-1) </summary>
|
||||
public void UpdateValue(float value, bool send = true) => sliderFunc.UpdateValue(value, send);
|
||||
|
||||
public abstract class UISliderFunc {
|
||||
public readonly UISlider slider;
|
||||
public UISliderFunc(UISlider slider) => this.slider = slider;
|
||||
|
||||
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 : UISliderFunc {
|
||||
public FromLeftToRight(UISlider slider) : base(slider) { }
|
||||
public override void DraggerDown(PointerDownEvent evt) {
|
||||
slider.isDragger = true;
|
||||
slider.originalPosition = slider.Tracker.resolvedStyle.width;
|
||||
slider.pointerPosition = UITool.GetMousePosition().x;
|
||||
}
|
||||
public override void ElementDown(PointerDownEvent evt) {
|
||||
float offset = evt.localPosition.x;
|
||||
float max = slider.Container.resolvedStyle.width;
|
||||
float value = Mathf.InverseLerp(0, max, offset);
|
||||
UpdateValue(value);
|
||||
}
|
||||
public override void Update() {
|
||||
if (!slider.isDragger) { return; }
|
||||
float differ = UITool.GetMousePosition().x - slider.pointerPosition;
|
||||
float offset = differ + slider.originalPosition;
|
||||
float max = slider.Container.resolvedStyle.width;
|
||||
float value = Mathf.InverseLerp(0, max, offset);
|
||||
UpdateValue(value);
|
||||
}
|
||||
public override void UpdateValue(float value, bool send = true) {
|
||||
slider.value = value;
|
||||
if (send) { slider.ValueChanged?.Invoke(value); }
|
||||
slider.Tracker.style.width = Length.Percent(value * 100);
|
||||
}
|
||||
}
|
||||
|
||||
public class FromRightToLeft : UISliderFunc {
|
||||
public FromRightToLeft(UISlider slider) : base(slider) { }
|
||||
public override void DraggerDown(PointerDownEvent evt) {
|
||||
slider.isDragger = true;
|
||||
slider.originalPosition = slider.Container.resolvedStyle.width - slider.Tracker.resolvedStyle.width;
|
||||
slider.pointerPosition = UITool.GetMousePosition().x;
|
||||
}
|
||||
public override void ElementDown(PointerDownEvent evt) {
|
||||
float offset = evt.localPosition.x;
|
||||
float max = slider.Container.resolvedStyle.width;
|
||||
float value = Mathf.InverseLerp(max, 0, offset);
|
||||
UpdateValue(value);
|
||||
}
|
||||
public override void Update() {
|
||||
if (!slider.isDragger) { return; }
|
||||
float differ = UITool.GetMousePosition().x - slider.pointerPosition;
|
||||
float offset = differ + slider.originalPosition;
|
||||
float max = slider.Container.resolvedStyle.width;
|
||||
float value = Mathf.InverseLerp(max, 0, offset);
|
||||
UpdateValue(value);
|
||||
}
|
||||
public override void UpdateValue(float value, bool send = true) {
|
||||
slider.value = value;
|
||||
if (send) { slider.ValueChanged?.Invoke(value); }
|
||||
slider.Tracker.style.width = Length.Percent(value * 100);
|
||||
}
|
||||
}
|
||||
|
||||
public class FromTopToBottom : UISliderFunc {
|
||||
public FromTopToBottom(UISlider slider) : base(slider) { }
|
||||
public override void DraggerDown(PointerDownEvent evt) {
|
||||
slider.isDragger = true;
|
||||
slider.originalPosition = slider.Tracker.resolvedStyle.height;
|
||||
slider.pointerPosition = Screen.height - UITool.GetMousePosition().y;
|
||||
}
|
||||
public override void ElementDown(PointerDownEvent evt) {
|
||||
float offset = evt.localPosition.y;
|
||||
float max = slider.Container.resolvedStyle.height;
|
||||
float value = Mathf.InverseLerp(0, max, offset);
|
||||
UpdateValue(value);
|
||||
}
|
||||
public override void Update() {
|
||||
if (!slider.isDragger) { return; }
|
||||
float differ = Screen.height - UITool.GetMousePosition().y - slider.pointerPosition;
|
||||
float offset = differ + slider.originalPosition;
|
||||
float max = slider.Container.resolvedStyle.height;
|
||||
float value = Mathf.InverseLerp(0, max, offset);
|
||||
UpdateValue(value);
|
||||
}
|
||||
public override void UpdateValue(float value, bool send = true) {
|
||||
slider.value = value;
|
||||
if (send) { slider.ValueChanged?.Invoke(value); }
|
||||
slider.Tracker.style.height = Length.Percent(value * 100);
|
||||
}
|
||||
}
|
||||
|
||||
public class FromBottomToTop : UISliderFunc {
|
||||
public FromBottomToTop(UISlider slider) : base(slider) { }
|
||||
public override void DraggerDown(PointerDownEvent evt) {
|
||||
slider.isDragger = true;
|
||||
slider.originalPosition = slider.Container.resolvedStyle.height - slider.Tracker.resolvedStyle.height;
|
||||
slider.pointerPosition = Screen.height - UITool.GetMousePosition().y;
|
||||
}
|
||||
public override void ElementDown(PointerDownEvent evt) {
|
||||
float offset = evt.localPosition.y;
|
||||
float max = slider.Container.resolvedStyle.height;
|
||||
float value = Mathf.InverseLerp(max, 0, offset);
|
||||
UpdateValue(value);
|
||||
}
|
||||
public override void Update() {
|
||||
if (!slider.isDragger) { return; }
|
||||
float differ = Screen.height - UITool.GetMousePosition().y - slider.pointerPosition;
|
||||
float offset = differ + slider.originalPosition;
|
||||
float max = slider.Container.resolvedStyle.height;
|
||||
float value = Mathf.InverseLerp(max, 0, offset);
|
||||
UpdateValue(value);
|
||||
}
|
||||
public override void UpdateValue(float value, bool send = true) {
|
||||
slider.value = value;
|
||||
if (send) { slider.ValueChanged?.Invoke(value); }
|
||||
slider.Tracker.style.height = Length.Percent(value * 100);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0721892edd97c594d80af856cbf293c8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,68 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
#if ENABLE_INPUT_SYSTEM && UNITY_INPUT_SYSTEM_PACKAGE
|
||||
using UnityEngine.InputSystem;
|
||||
#endif
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// 悬浮窗口
|
||||
/// </summary>
|
||||
public abstract class UIWindow : ModuleUIPanel {
|
||||
/// <summary> 绑定的画布 </summary>
|
||||
public readonly VisualElement canvas;
|
||||
|
||||
private bool isDownMove;
|
||||
private Vector3 pointerPosition;
|
||||
private Vector3 originalPosition;
|
||||
|
||||
public VisualElement Window => element.Q<VisualElement>("Window");
|
||||
public VisualElement Top => element.Q<VisualElement>("Top");
|
||||
public VisualElement Container => element.Q<VisualElement>("Container");
|
||||
|
||||
public Label Title => element.Q<Label>("Title");
|
||||
public Button Close => element.Q<Button>("Close");
|
||||
|
||||
public UIWindow(VisualElement element, VisualElement canvas) : base(element) {
|
||||
this.canvas = canvas;
|
||||
|
||||
Top.RegisterCallback<PointerDownEvent>(TopDown);
|
||||
canvas.RegisterCallback<PointerUpEvent>((evt) => isDownMove = false);
|
||||
canvas.RegisterCallback<PointerLeaveEvent>((evt) => isDownMove = false);
|
||||
|
||||
Close.clicked += () => { SetActive(false); };
|
||||
}
|
||||
|
||||
/// <summary> 按下Top </summary>
|
||||
private void TopDown(PointerDownEvent evt) {
|
||||
isDownMove = true;
|
||||
pointerPosition = UITool.GetMousePosition();
|
||||
originalPosition = Window.transform.position;
|
||||
}
|
||||
|
||||
/// <summary> 设置活动状态 </summary>
|
||||
public virtual void SetActive(bool active) {
|
||||
Window.EnableInClassList("window-hidden", !active);
|
||||
}
|
||||
|
||||
/// <summary> 更新状态 </summary>
|
||||
public virtual void Update() {
|
||||
if (!isDownMove) { return; }
|
||||
Vector3 mousePosition = UITool.GetMousePosition();
|
||||
Vector3 offset = mousePosition - pointerPosition;
|
||||
Vector3 position = originalPosition + new Vector3(offset.x, -offset.y);
|
||||
|
||||
float width = canvas.resolvedStyle.width - Window.resolvedStyle.width;
|
||||
float height = canvas.resolvedStyle.height - Window.resolvedStyle.height;
|
||||
position.x = Mathf.Clamp(position.x, 0, width);
|
||||
position.y = Mathf.Clamp(position.y, 0, height);
|
||||
|
||||
Window.transform.position = position;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8db6302b07dcf8649b966f15c6fad581
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user