Rewrite property panel to adapt to ruleset config.

This commit is contained in:
2023-07-27 22:07:20 +08:00
parent 9b091a0084
commit bc51a45df8
39 changed files with 825 additions and 298 deletions

View File

@@ -1,55 +0,0 @@
using UnityEngine;
using UnityEngine.EventSystems;
namespace Cryville.Crtr.Browsing {
public class PVPBool : PropertyValuePanel, IPointerClickHandler {
bool _value;
public override object Value {
get { return _value; }
set { _value = (bool)value; }
}
[SerializeField]
RectTransform m_on;
[SerializeField]
RectTransform m_handleArea;
[SerializeField]
RectTransform m_handle;
public void Toggle() {
_value = !_value;
Callback(Value);
}
const float SPEED = 8;
float _ratio;
#pragma warning disable IDE0051
void Update() {
if (_value && _ratio != 1) {
_ratio += SPEED * Time.deltaTime;
if (_ratio > 1) _ratio = 1;
UpdateGraphics();
}
else if (!_value && _ratio != 0) {
_ratio -= SPEED * Time.deltaTime;
if (_ratio < 0) _ratio = 0;
UpdateGraphics();
}
}
void OnRectTransformDimensionsChange() {
m_handleArea.sizeDelta = new Vector2(m_handle.rect.height - m_handle.rect.width, 0);
}
#pragma warning restore IDE0051
void UpdateGraphics() {
m_on.anchorMax = new Vector2(_ratio, m_on.anchorMax.y);
m_handle.anchorMin = new Vector2(_ratio, m_handle.anchorMin.y);
m_handle.anchorMax = new Vector2(_ratio, m_handle.anchorMax.y);
}
public void OnPointerClick(PointerEventData eventData) {
Toggle();
}
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: b281561aca5d19f43ba8af035de8ec98
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,157 +0,0 @@
using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace Cryville.Crtr.Browsing {
public class PVPNumber : PropertyValuePanel {
static Color _steppedTint = new Color(1.0f, 0.8f, 0.5f);
double m_value;
public override object Value {
get {
float s_value = GetDisplayValue();
return IntegerMode ? (int)s_value : (object)s_value;
}
set {
if (value is double) m_value = (double)value;
else m_value = IntegerMode ? (int)value : (double)(float)value;
float s_value = GetDisplayValue();
m_text.text = s_value.ToString();
if (Range != null && MaxStep == 0) {
SetRatio((float)(m_value - Range.Value.x) / (Range.Value.y - Range.Value.x));
}
}
}
float GetDisplayValue() {
double s_value = m_value;
if (Precision > 0)
s_value = Math.Round(s_value / Precision) * Precision;
if (IntegerMode)
s_value = Math.Round(s_value);
return (float)s_value;
}
public bool IntegerMode { get; set; }
public bool LogarithmicMode { get; set; }
public float MaxStep { get; set; }
public double Precision { get; set; }
public Vector2? Range { get; set; }
[SerializeField]
EventTrigger m_ctn;
[SerializeField]
RectTransform m_handleArea;
[SerializeField]
Image m_handle;
[SerializeField]
Text m_text;
#pragma warning disable IDE0051
void Start() {
var ev = new EventTrigger.Entry { eventID = EventTriggerType.InitializePotentialDrag };
ev.callback.AddListener(e => OnInitializePotentialDrag((PointerEventData)e));
m_ctn.triggers.Add(ev);
ev = new EventTrigger.Entry { eventID = EventTriggerType.Drag };
ev.callback.AddListener(e => OnDrag((PointerEventData)e));
m_ctn.triggers.Add(ev);
ev = new EventTrigger.Entry { eventID = EventTriggerType.EndDrag };
ev.callback.AddListener(e => OnEndDrag((PointerEventData)e));
m_ctn.triggers.Add(ev);
ev = new EventTrigger.Entry { eventID = EventTriggerType.PointerClick };
ev.callback.AddListener(e => OnPointerClick((PointerEventData)e));
m_ctn.triggers.Add(ev);
if (MaxStep != 0) {
m_handle.color = _steppedTint;
SetRatio(0.5f);
}
}
void Update() {
if (use && MaxStep != 0) {
SetRatio(GetRatioFromPos(pp));
SetValueFromPos(pp);
}
}
void OnRectTransformDimensionsChange() {
m_handleArea.sizeDelta = new Vector2(m_handle.rectTransform.rect.height - m_handle.rectTransform.rect.width, 0);
}
#pragma warning restore IDE0051
Camera cam;
Vector2 pp;
bool use, nouse;
public void OnInitializePotentialDrag(PointerEventData eventData) {
eventData.useDragThreshold = false;
pp = eventData.position;
}
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;
if (MaxStep == 0) SetValueFromPos(eventData.position);
eventData.Use();
}
}
public void OnEndDrag(PointerEventData eventData) {
if (!nouse) {
SetValueFromPos(eventData.position);
Callback(Value);
if (MaxStep != 0) SetRatio(0.5f);
eventData.Use();
use = false;
}
nouse = false;
}
public void OnPointerClick(PointerEventData eventData) {
SetValueFromPos(eventData.position);
Callback(Value);
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);
}
return float.NegativeInfinity;
}
void SetValueFromPos(Vector2 pos) {
double ratio = GetRatioFromPos(pos);
double result;
if (MaxStep == 0) {
if (LogarithmicMode) throw new NotImplementedException();
else result = (1 - ratio) * Range.Value.x + ratio * Range.Value.y;
}
else {
double delta = (ratio > 0.5 ? 1 : -1) * Math.Pow((ratio - 0.5f) * 2, 2) * MaxStep * Time.deltaTime;
if (LogarithmicMode) result = Math.Pow(Math.E, Math.Log(m_value) + delta);
else result = m_value + delta;
}
if (Range != null) {
if (result < Range.Value.x) result = Range.Value.x;
else if (result > Range.Value.y) result = Range.Value.y;
}
Value = result;
}
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);
}
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 1eb74ffd69f934d4a8eca80aa3077b6c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,28 +0,0 @@
using UnityEngine.UI;
namespace Cryville.Crtr.Browsing {
public class PVPString : PropertyValuePanel {
string m_value;
public override object Value {
get {
return m_value;
}
set {
m_value = (string)value;
_inputField.text = m_value;
}
}
InputField _inputField;
void Awake() {
_inputField = GetComponent<InputField>();
_inputField.onValueChanged.AddListener(OnValueChanged);
}
void OnValueChanged(string value) {
m_value = value;
Callback(Value);
}
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: aadf11739189bc94e9cb4f702eb7ccd3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,51 +0,0 @@
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.UI;
namespace Cryville.Crtr.Browsing {
public class PropertyCategoryPanel : MonoBehaviour {
[SerializeField]
private GameObject m_propertyPrefab;
Text _nameLabel = null;
string _name;
public string Name {
get { return _name; }
set { _name = value; UpdateName(); }
}
bool _collapsed = false;
public bool Collapsed {
get { return _collapsed; }
set { _collapsed = value; UpdateName(); }
}
#pragma warning disable IDE0051
void Awake() {
_nameLabel = transform.Find("Name/__text__").GetComponent<Text>();
transform.Find("Name").GetComponent<Button>().onClick.AddListener(ToggleCollapsed);
}
#pragma warning restore IDE0051
public void Load(string name, IEnumerable<PropertyInfo> props, object target) {
Name = name.ToUpper();
foreach (var prop in props) {
var obj = GameObject.Instantiate<GameObject>(m_propertyPrefab, transform, false);
obj.GetComponent<PropertyPanel>().Load(prop, target);
}
}
void ToggleCollapsed() {
Collapsed = !Collapsed;
for (int i = 1; i < transform.childCount; i++) {
transform.GetChild(i).gameObject.SetActive(!Collapsed);
}
}
private void UpdateName() {
_nameLabel.text = (Collapsed ? "+ " : "- ") + Name;
}
}
}

View File

@@ -1,12 +0,0 @@
fileFormatVersion: 2
guid: 3a13b7ea14b96e54ea8a7e6ba1275281
timeCreated: 1638435211
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,76 +0,0 @@
using Cryville.Common.ComponentModel;
using System.Reflection;
using UnityEngine;
using UnityEngine.UI;
using RangeAttribute = Cryville.Common.ComponentModel.RangeAttribute;
namespace Cryville.Crtr.Browsing {
public class PropertyPanel : MonoBehaviour {
[SerializeField]
GameObject m_bool;
[SerializeField]
GameObject m_number;
[SerializeField]
GameObject m_string;
PropertyInfo _property;
object _target;
Text _key;
Transform _valueContainer;
PropertyValuePanel _value;
#pragma warning disable IDE0051
void Awake() {
_key = transform.Find("Key").GetComponent<Text>();
_valueContainer = transform.Find("Value");
}
#pragma warning restore IDE0051
public void Load(PropertyInfo prop, object target) {
_target = target;
_property = prop;
_key.text = prop.Name;
GameObject vp;
if (prop.PropertyType == typeof(bool)) vp = m_bool;
else if (prop.PropertyType == typeof(float) || prop.PropertyType == typeof(int)) vp = m_number;
else if (prop.PropertyType == typeof(string)) vp = m_string;
else return;
_value = GameObject.Instantiate(vp, _valueContainer, false).GetComponent<PropertyValuePanel>();
if (_value is PVPNumber) {
var t = (PVPNumber)_value;
t.IntegerMode = prop.PropertyType == typeof(int);
var attr = prop.GetCustomAttributes(typeof(RangeAttribute), true);
if (attr.Length > 0) {
var u = (RangeAttribute)attr[0];
t.Range = new Vector2(u.Min, u.Max);
}
attr = prop.GetCustomAttributes(typeof(PrecisionAttribute), true);
if (attr.Length > 0) {
var u = (PrecisionAttribute)attr[0];
t.Precision = u.Precision;
}
attr = prop.GetCustomAttributes(typeof(StepAttribute), true);
if (attr.Length > 0) {
var u = (StepAttribute)attr[0];
t.MaxStep = u.Step;
}
attr = prop.GetCustomAttributes(typeof(LogarithmicScaleAttribute), true);
if (attr.Length > 0) {
t.LogarithmicMode = true;
}
}
_value.Callback = SetValueToObject;
GetValueFromObject();
}
void GetValueFromObject() {
_value.Value = _property.GetValue(_target, null);
}
void SetValueToObject(object value) {
_property.SetValue(_target, value, null);
GetValueFromObject();
}
}
}

View File

@@ -1,12 +0,0 @@
fileFormatVersion: 2
guid: bcca29fea992ac24698a213f0e2baedc
timeCreated: 1638435590
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,9 +0,0 @@
using System;
using UnityEngine;
namespace Cryville.Crtr.Browsing {
public abstract class PropertyValuePanel : MonoBehaviour {
public Action<object> Callback { protected get; set; }
public abstract object Value { get; set; }
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: fca3da10a8b0677439885f6732ac3b6e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: