Disables text field when it is not active in settings.

This commit is contained in:
2023-11-21 11:26:12 +08:00
parent 342e216372
commit ad625e3e35
4 changed files with 623 additions and 290 deletions

View File

@@ -5,7 +5,9 @@ using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace Cryville.Crtr.Config.UI {
public abstract class PVPNumberBase : PropertyValuePanel, IInitializePotentialDragHandler, IDragHandler, IEndDragHandler, IPointerClickHandler {
public abstract class PVPNumberBase : PropertyValuePanel {
[SerializeField]
GameObject m_slider;
[SerializeField]
RectTransform m_handleArea;
[SerializeField]
@@ -13,6 +15,8 @@ namespace Cryville.Crtr.Config.UI {
[SerializeField]
TMP_InputField m_text;
protected void Start() {
m_slider.AddComponent<SliderBehaviour>().parent = this;
m_text.interactable = SetMapped;
m_text.onValueChanged.AddListener(OnTextEdited);
m_text.onDeselect.AddListener(OnTextDeselected);
@@ -29,6 +33,7 @@ namespace Cryville.Crtr.Config.UI {
}
void OnTextDeselected(string value) {
OnValueUpdated();
m_text.enabled = false;
}
protected override void OnValueUpdated() {
@@ -52,79 +57,92 @@ namespace Cryville.Crtr.Config.UI {
protected virtual void OnIdle() { }
protected virtual void Update() {
if (use) {
SetRatio(GetRatioFromPos(pp));
SetValueFromPos(pp);
}
}
class SliderBehaviour : UIBehaviour, IInitializePotentialDragHandler, IDragHandler, IEndDragHandler, IPointerClickHandler {
public PVPNumberBase parent;
Camera cam;
Vector2 pp;
bool use, nouse;
public void OnInitializePotentialDrag(PointerEventData eventData) {
// eventData.useDragThreshold = false;
pp = eventData.position;
}
Camera cam;
Vector2 pp;
bool use, nouse;
public void OnDrag(PointerEventData eventData) {
if (nouse) return;
cam = eventData.pressEventCamera;
if (!use) {
var delta = eventData.position - pp;
float dx = Mathf.Abs(delta.x), dy = Mathf.Abs(delta.y);
if (dx > dy) use = true;
else if (dx < dy) nouse = true;
void Update() {
if (use) {
parent.SetRatio(GetRatioFromPos(pp));
SetValueFromPos(pp);
}
}
if (use) {
public void OnInitializePotentialDrag(PointerEventData eventData) {
// eventData.useDragThreshold = false;
pp = eventData.position;
eventData.Use();
}
}
public void OnEndDrag(PointerEventData eventData) {
if (!nouse) {
public void OnDrag(PointerEventData eventData) {
if (nouse) return;
cam = eventData.pressEventCamera;
if (!use) {
var delta = eventData.position - pp;
float dx = Mathf.Abs(delta.x), dy = Mathf.Abs(delta.y);
if (dx > dy) use = true;
else if (dx < dy) nouse = true;
}
if (use) {
pp = eventData.position;
eventData.Use();
}
}
public void OnEndDrag(PointerEventData eventData) {
if (!nouse) {
SetValueFromPos(eventData.position);
parent.OnIdle();
eventData.Use();
use = false;
}
nouse = false;
}
public void OnPointerClick(PointerEventData eventData) {
SetValueFromPos(eventData.position);
OnIdle();
eventData.Use();
use = false;
}
nouse = false;
}
public void OnPointerClick(PointerEventData eventData) {
SetValueFromPos(eventData.position);
eventData.Use();
}
float GetRatioFromPos(Vector2 pos) {
Vector2 lp;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(m_handleArea, pos, cam, out lp)) {
lp -= m_handleArea.rect.position;
return Mathf.Clamp01(lp.x / m_handleArea.rect.width);
float GetRatioFromPos(Vector2 pos) {
Vector2 lp;
RectTransform handleArea = parent.m_handleArea;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(handleArea, pos, cam, out lp)) {
lp -= handleArea.rect.position;
return Mathf.Clamp01(lp.x / handleArea.rect.width);
}
return float.NegativeInfinity;
}
void SetValueFromPos(Vector2 pos) {
double min, max;
parent.GetRange(out min, out max);
double ratio = GetRatioFromPos(pos);
double result = parent.GetValue(ratio, Time.deltaTime, min, max);
if (result < min) result = min;
else if (result > max) result = max;
parent.RawValue = result;
}
return float.NegativeInfinity;
}
void SetValueFromPos(Vector2 pos) {
double min = double.NegativeInfinity, max = double.PositiveInfinity;
void GetRange(out double min, out double max) {
min = double.NegativeInfinity;
max = double.PositiveInfinity;
if (Range != null && Range.Length == 2) {
min = (double)Range[0];
max = (double)Range[1];
}
double ratio = GetRatioFromPos(pos);
double result = GetValue(ratio, Time.deltaTime, min, max);
if (result < min) result = min;
else if (result > max) result = max;
RawValue = result;
}
protected void SetRatio(float ratio) {
RectTransform handle = m_handle.rectTransform;
handle.anchorMin = new Vector2(ratio, handle.anchorMin.y);
handle.anchorMax = new Vector2(ratio, handle.anchorMax.y);
handle.anchoredPosition = Vector2.zero;
}
protected abstract double GetValue(double ratio, float deltaTime, double min, double max);
protected void SetRatio(float ratio) {
m_handle.rectTransform.anchorMin = new Vector2(ratio, m_handle.rectTransform.anchorMin.y);
m_handle.rectTransform.anchorMax = new Vector2(ratio, m_handle.rectTransform.anchorMax.y);
m_handle.rectTransform.anchoredPosition = Vector2.zero;
}
}
}