增加更新接口
This commit is contained in:
@@ -8,7 +8,7 @@ namespace MuHua {
|
||||
/// <summary>
|
||||
/// UI项容器
|
||||
/// </summary>
|
||||
public class ModuleUIItems<TItem, TData> : ModuleUIPanel, IDisposable where TItem : ModuleUIItem<TData> {
|
||||
public class ModuleUIItems<TItem, TData> : ModuleUIPanel where TItem : ModuleUIItem<TData> {
|
||||
/// <summary> 模板资源 </summary>
|
||||
public readonly VisualTreeAsset templateAsset;
|
||||
/// <summary> 生成UI项的函数 </summary>
|
||||
@@ -27,19 +27,19 @@ namespace MuHua {
|
||||
this.generate = generate;
|
||||
}
|
||||
/// <summary> 释放资源 </summary>
|
||||
public void Dispose() {
|
||||
public void Release() {
|
||||
element.Clear();
|
||||
Items.ForEach(obj => obj.Dispose());
|
||||
Items = new List<TItem>();
|
||||
}
|
||||
/// <summary> 创建UI项 </summary>
|
||||
public void Create(List<TData> datas, bool isClear = true) {
|
||||
if (isClear) { Dispose(); }
|
||||
if (isClear) { Release(); }
|
||||
datas.ForEach(Create);
|
||||
}
|
||||
/// <summary> 创建UI项 </summary>
|
||||
public void Create(TData data, bool isClear = false) {
|
||||
if (isClear) { Dispose(); }
|
||||
if (isClear) { Release(); }
|
||||
Create(data);
|
||||
}
|
||||
/// <summary> 创建UI项 </summary>
|
||||
|
||||
@@ -5,7 +5,7 @@ using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// UI控件
|
||||
/// UI面板
|
||||
/// </summary>
|
||||
public class ModuleUIPanel {
|
||||
/// <summary> 绑定的元素 </summary>
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// UI控制接口
|
||||
/// </summary>
|
||||
public interface UIControl {
|
||||
/// <summary> 更新UI状态 </summary>
|
||||
public void Update();
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b0bf333809175ae4ebc4469feab46cc7
|
||||
guid: b46dec8e6f5bddc49a0a37b501d06229
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
@@ -8,7 +8,7 @@ namespace MuHua {
|
||||
/// <summary>
|
||||
/// 下拉框
|
||||
/// </summary>
|
||||
public class UIDropdown<T> : ModuleUIPanel, IDisposable {
|
||||
public class UIDropdown<T> : ModuleUIPanel, IDisposable, UIControl {
|
||||
/// <summary> 绑定的画布 </summary>
|
||||
internal readonly VisualElement canvas;
|
||||
/// <summary> 下拉框容器 </summary>
|
||||
@@ -59,7 +59,7 @@ namespace MuHua {
|
||||
}
|
||||
public virtual void Dispose() {
|
||||
canvas.Remove(DropdownContainer);
|
||||
DropdownItems.Dispose();
|
||||
DropdownItems.Release();
|
||||
Input.UnregisterCallback<ClickEvent>(evt => OpenDropdown());
|
||||
DropdownContainer.UnregisterCallback<PointerDownEvent>(evt => CloseDropdown());
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace MuHua {
|
||||
/// <summary>
|
||||
/// 滚动视图
|
||||
/// </summary>
|
||||
public class UIScrollView : ModuleUIPanel, IDisposable {
|
||||
public class UIScrollView : ModuleUIPanel, IDisposable, UIControl {
|
||||
/// <summary> 绑定的画布 </summary>
|
||||
public readonly VisualElement canvas;
|
||||
/// <summary> 水平滑块 </summary>
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace MuHua {
|
||||
/// <summary>
|
||||
/// 滚动视图 - 水平
|
||||
/// </summary>
|
||||
public class UIScrollViewH : ModuleUIPanel, IDisposable {
|
||||
public class UIScrollViewH : ModuleUIPanel, IDisposable, UIControl {
|
||||
/// <summary> 绑定的画布 </summary>
|
||||
public readonly VisualElement canvas;
|
||||
/// <summary> 元素方向 </summary>
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace MuHua {
|
||||
/// <summary>
|
||||
/// 滚动视图 - 垂直
|
||||
/// </summary>
|
||||
public class UIScrollViewV : ModuleUIPanel, IDisposable {
|
||||
public class UIScrollViewV : ModuleUIPanel, IDisposable, UIControl {
|
||||
/// <summary> 绑定的画布 </summary>
|
||||
public readonly VisualElement canvas;
|
||||
/// <summary> 元素方向 </summary>
|
||||
|
||||
@@ -22,10 +22,10 @@ namespace MuHua {
|
||||
}
|
||||
public override void Dispose() {
|
||||
base.Dispose();
|
||||
Items.Dispose();
|
||||
Items.Release();
|
||||
}
|
||||
/// <summary> 释放资源 </summary>
|
||||
public virtual void Release() => Items.Dispose();
|
||||
public virtual void Release() => Items.Release();
|
||||
/// <summary> 创建UI项 </summary>
|
||||
public virtual void Create(List<Data> datas, bool isClear = true) => Items.Create(datas, isClear);
|
||||
/// <summary> 创建UI项 </summary>
|
||||
|
||||
@@ -23,10 +23,10 @@ namespace MuHua {
|
||||
}
|
||||
public override void Dispose() {
|
||||
base.Dispose();
|
||||
Items.Dispose();
|
||||
Items.Release();
|
||||
}
|
||||
/// <summary> 释放资源 </summary>
|
||||
public virtual void Release() => Items.Dispose();
|
||||
public virtual void Release() => Items.Release();
|
||||
/// <summary> 创建UI项 </summary>
|
||||
public virtual void Create(List<Data> datas, bool isClear = true) => Items.Create(datas, isClear);
|
||||
/// <summary> 创建UI项 </summary>
|
||||
|
||||
@@ -23,10 +23,10 @@ namespace MuHua {
|
||||
}
|
||||
public override void Dispose() {
|
||||
base.Dispose();
|
||||
Items.Dispose();
|
||||
Items.Release();
|
||||
}
|
||||
/// <summary> 释放资源 </summary>
|
||||
public virtual void Release() => Items.Dispose();
|
||||
public virtual void Release() => Items.Release();
|
||||
/// <summary> 创建UI项 </summary>
|
||||
public virtual void Create(List<Data> datas, bool isClear = true) => Items.Create(datas, isClear);
|
||||
/// <summary> 创建UI项 </summary>
|
||||
|
||||
@@ -1,203 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// 滚动条
|
||||
/// </summary>
|
||||
[Obsolete("使用 UIScrollerV 或者 UIScrollerH 替换")]
|
||||
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 VisualElement Dragger => Q<VisualElement>("Dragger");
|
||||
|
||||
public UIScroller(VisualElement element, VisualElement canvas, UIDirection direction = UIDirection.FromLeftToRight) : base(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);
|
||||
}
|
||||
/// <summary> 拖拽元素 </summary>
|
||||
private void DraggerDown(PointerDownEvent evt) => scrollerFunc.DraggerDown(evt);
|
||||
/// <summary> 按下元素 </summary>
|
||||
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;
|
||||
/// <summary> 拖拽元素 </summary>
|
||||
public abstract void DraggerDown(PointerDownEvent evt);
|
||||
/// <summary> 按下元素 </summary>
|
||||
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);
|
||||
}
|
||||
/// <summary>
|
||||
/// 滑块从左到右
|
||||
/// </summary>
|
||||
public class FromLeftToRight : UIScrollerFunc {
|
||||
public FromLeftToRight(UIScroller scroller) : base(scroller) {
|
||||
scroller.element.style.flexDirection = FlexDirection.Row;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 滑块从右到左
|
||||
/// </summary>
|
||||
public class FromRightToLeft : UIScrollerFunc {
|
||||
public FromRightToLeft(UIScroller scroller) : base(scroller) {
|
||||
scroller.element.style.flexDirection = FlexDirection.RowReverse;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 滑块从上到下
|
||||
/// </summary>
|
||||
public class FromTopToBottom : UIScrollerFunc {
|
||||
public FromTopToBottom(UIScroller scroller) : base(scroller) {
|
||||
scroller.element.style.flexDirection = FlexDirection.Column;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 滑块从下到上
|
||||
/// </summary>
|
||||
public class FromBottomToTop : UIScrollerFunc {
|
||||
public FromBottomToTop(UIScroller scroller) : base(scroller) {
|
||||
scroller.element.style.flexDirection = FlexDirection.ColumnReverse;
|
||||
}
|
||||
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.Dragger.resolvedStyle.height - scroller.element.resolvedStyle.height;
|
||||
float y = Mathf.Lerp(0, max, value);
|
||||
scroller.Dragger.transform.position = new Vector3(0, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ namespace MuHua {
|
||||
/// <summary>
|
||||
/// 滚动条 - 水平
|
||||
/// </summary>
|
||||
public class UIScrollerH : ModuleUIPanel, IDisposable {
|
||||
public class UIScrollerH : ModuleUIPanel, IDisposable, UIControl {
|
||||
/// <summary> 绑定的画布 </summary>
|
||||
public readonly VisualElement canvas;
|
||||
/// <summary> 元素方向 </summary>
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace MuHua {
|
||||
/// <summary>
|
||||
/// 滚动条 - 垂直
|
||||
/// </summary>
|
||||
public class UIScrollerV : ModuleUIPanel, IDisposable {
|
||||
public class UIScrollerV : ModuleUIPanel, IDisposable, UIControl {
|
||||
/// <summary> 绑定的画布 </summary>
|
||||
public readonly VisualElement canvas;
|
||||
/// <summary> 元素方向 </summary>
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace MuHua {
|
||||
/// <summary>
|
||||
/// 滑动按钮
|
||||
/// </summary>
|
||||
public class UISlideButton<TItem, Data> : ModuleUIPanel where TItem : ModuleUIItem<Data> where Data : DataSlideButton {
|
||||
public class UISlideButton<TItem, Data> : ModuleUIPanel, UIControl where TItem : ModuleUIItem<Data> where Data : DataSlideButton {
|
||||
/// <summary> 绑定的画布 </summary>
|
||||
public readonly VisualElement canvas;
|
||||
|
||||
@@ -44,7 +44,7 @@ namespace MuHua {
|
||||
ModuleUIItem<Data>.OnSelect += Settings;
|
||||
}
|
||||
public virtual void Dispose() {
|
||||
Items.Dispose();
|
||||
Items.Release();
|
||||
}
|
||||
public virtual void Update() {
|
||||
if (data == null || data.element == null) { return; }
|
||||
@@ -55,7 +55,7 @@ namespace MuHua {
|
||||
/// <summary> 设置UI项 </summary>
|
||||
public virtual void Settings(Data data) => this.data = data;
|
||||
/// <summary> 释放资源 </summary>
|
||||
public virtual void Release() => Items.Dispose();
|
||||
public virtual void Release() => Items.Release();
|
||||
/// <summary> 创建UI项 </summary>
|
||||
public virtual void Create(List<Data> datas, bool isClear = true) => Items.Create(datas, isClear);
|
||||
/// <summary> 创建UI项 </summary>
|
||||
|
||||
@@ -1,180 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// 滑块
|
||||
/// </summary>
|
||||
[Obsolete("使用 UISliderV 或者 UISliderH 替换")]
|
||||
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 Label Title => Q<Label>("Title");
|
||||
public VisualElement Container => Q<VisualElement>("Container");
|
||||
public VisualElement Tracker => Q<VisualElement>("Tracker");
|
||||
public VisualElement Dragger => Q<VisualElement>("Dragger");
|
||||
|
||||
public UISlider(VisualElement element, VisualElement canvas, UIDirection direction = UIDirection.FromLeftToRight) : base(element) {
|
||||
this.canvas = canvas;
|
||||
this.direction = direction;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0721892edd97c594d80af856cbf293c8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -8,7 +8,7 @@ namespace MuHua {
|
||||
/// <summary>
|
||||
/// 滑块 - 水平
|
||||
/// </summary>
|
||||
public class UISliderH : ModuleUIPanel, IDisposable {
|
||||
public class UISliderH : ModuleUIPanel, IDisposable, UIControl {
|
||||
/// <summary> 绑定的画布 </summary>
|
||||
public readonly VisualElement canvas;
|
||||
/// <summary> 元素方向 </summary>
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace MuHua {
|
||||
/// <summary>
|
||||
/// 滑块 - 垂直
|
||||
/// </summary>
|
||||
public class UISliderV : ModuleUIPanel, IDisposable {
|
||||
public class UISliderV : ModuleUIPanel, IDisposable, UIControl {
|
||||
/// <summary> 绑定的画布 </summary>
|
||||
public readonly VisualElement canvas;
|
||||
/// <summary> 元素方向 </summary>
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
@@ -7,9 +8,11 @@ namespace MuHua {
|
||||
/// <summary>
|
||||
/// 悬浮窗口
|
||||
/// </summary>
|
||||
public abstract class UIWindow : ModuleUIPanel {
|
||||
public abstract class UIWindow : ModuleUIPanel, IDisposable, UIControl {
|
||||
/// <summary> 绑定的画布 </summary>
|
||||
public readonly VisualElement canvas;
|
||||
/// <summary> 隐藏的USS类名 </summary>
|
||||
public string hideClassName = "document-page-hide";
|
||||
|
||||
private bool isDownMove;
|
||||
private Vector3 pointerPosition;
|
||||
@@ -26,10 +29,16 @@ namespace MuHua {
|
||||
this.canvas = canvas;
|
||||
|
||||
Top.RegisterCallback<PointerDownEvent>(TopDown);
|
||||
canvas.RegisterCallback<PointerUpEvent>((evt) => isDownMove = false);
|
||||
canvas.RegisterCallback<PointerLeaveEvent>((evt) => isDownMove = false);
|
||||
|
||||
Close.RegisterCallback<ClickEvent>((evt) => Settings(false));
|
||||
canvas.RegisterCallback<PointerUpEvent>(DraggerUpOrLeave);
|
||||
canvas.RegisterCallback<PointerLeaveEvent>(DraggerUpOrLeave);
|
||||
Close.RegisterCallback<ClickEvent>(CloseButton);
|
||||
}
|
||||
/// <summary> 解绑事件,防止内存泄漏 </summary>
|
||||
public virtual void Dispose() {
|
||||
Top.UnregisterCallback<PointerDownEvent>(TopDown);
|
||||
canvas.UnregisterCallback<PointerUpEvent>(DraggerUpOrLeave);
|
||||
canvas.UnregisterCallback<PointerLeaveEvent>(DraggerUpOrLeave);
|
||||
Close.UnregisterCallback<ClickEvent>(CloseButton);
|
||||
}
|
||||
|
||||
/// <summary> 按下Top </summary>
|
||||
@@ -38,10 +47,18 @@ namespace MuHua {
|
||||
pointerPosition = UITool.GetMousePosition();
|
||||
originalPosition = Window.transform.position;
|
||||
}
|
||||
/// <summary> 鼠标松开或离开 </summary>
|
||||
private void DraggerUpOrLeave(EventBase evt) {
|
||||
isDownMove = false;
|
||||
}
|
||||
/// <summary> 关闭按钮 </summary>
|
||||
private void CloseButton(EventBase evt) {
|
||||
Settings(false);
|
||||
}
|
||||
|
||||
/// <summary> 设置活动状态 </summary>
|
||||
public virtual void Settings(bool active) {
|
||||
element.style.display = active ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
element.EnableInClassList(hideClassName, !active);
|
||||
}
|
||||
|
||||
/// <summary> 更新状态 </summary>
|
||||
|
||||
Reference in New Issue
Block a user