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

@@ -14,7 +14,7 @@ namespace Cryville.Crtr.Config {
Transform m_content;
[SerializeField]
SettingsPanel m_genericConfigPanel;
PropertyMasterPanel m_genericConfigPanel;
[SerializeField]
InputConfigPanel m_inputConfigPanel;
@@ -56,7 +56,7 @@ namespace Cryville.Crtr.Config {
}
}
m_genericConfigPanel.Target = _rscfg.generic;
m_genericConfigPanel.Adapter = new DefaultPropertyMasterAdapter(_rscfg.generic);
var proxy = new InputProxy(ruleset.Root, null, new Vector2(Screen.width, Screen.height));
proxy.LoadFrom(_rscfg.inputs);

View File

@@ -0,0 +1,102 @@
using Cryville.Common.ComponentModel;
using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
namespace Cryville.Crtr.Config {
public interface IPropertyAdapter {
string Category { get; }
string Name { get; }
PropertyType Type { get; }
object[] Range { get; }
object GetValue();
void SetValue(object value);
object MapValue(object value);
bool SetMapped { get; }
object MapValueInverse(object value);
}
public enum PropertyType {
Unknown,
Number,
SteppedNumber,
Boolean,
String,
}
public class DefaultPropertyAdapter : IPropertyAdapter {
readonly object _target;
readonly PropertyInfo _prop;
readonly string m_category;
public string Category { get { return m_category; } }
readonly string m_name;
public string Name { get { return m_name; } }
readonly PropertyType m_type = PropertyType.Unknown;
public PropertyType Type { get { return m_type; } }
readonly object[] m_range;
public object[] Range { get { return m_range; } }
public object GetValue() { return _prop.GetValue(_target, null); }
public void SetValue(object value) { _prop.SetValue(_target, value, null); }
double _precision;
double _step;
bool _logarithmic;
public object MapValue(object value) {
if (Type == PropertyType.Number || Type == PropertyType.SteppedNumber) {
var result = (double)value;
if (_step != 0) result *= _step;
if (_logarithmic) result = Math.Pow(Math.E, result);
if (_precision != 0) result = Math.Round(result / _precision) * _precision;
return Convert.ChangeType(result, _prop.PropertyType);
}
return value;
}
public bool SetMapped { get { return true; } }
public object MapValueInverse(object value) {
if (Type == PropertyType.Number || Type == PropertyType.SteppedNumber) {
var result = Convert.ToDouble(value);
if (_logarithmic) result = Math.Log(result);
if (_step != 0) result /= _step;
return result;
}
return value;
}
public DefaultPropertyAdapter(object target, PropertyInfo prop) {
_target = target;
_prop = prop;
var attrs = prop.GetCustomAttributes(typeof(CategoryAttribute), true);
if (attrs.Length > 0) m_category = ((CategoryAttribute)attrs.Single()).Category;
m_name = prop.Name;
if (prop.PropertyType == typeof(bool)) m_type = PropertyType.Boolean;
else if (prop.PropertyType == typeof(char)) throw new NotSupportedException();
else if (prop.PropertyType.IsPrimitive) {
m_type = prop.GetCustomAttributes(typeof(StepAttribute), true).Length > 0
? PropertyType.SteppedNumber
: PropertyType.Number;
var attr = prop.GetCustomAttributes(typeof(RangeAttribute), true);
if (attr.Length > 0) {
var u = (RangeAttribute)attr.Single();
m_range = new object[] { u.Min, u.Max };
}
attr = prop.GetCustomAttributes(typeof(PrecisionAttribute), true);
if (attr.Length > 0) {
var u = (PrecisionAttribute)attr.Single();
_precision = u.Precision;
}
attr = prop.GetCustomAttributes(typeof(StepAttribute), true);
if (attr.Length > 0) {
var u = (StepAttribute)attr.Single();
_step = u.Step;
}
attr = prop.GetCustomAttributes(typeof(LogarithmicScaleAttribute), true);
if (attr.Length > 0) {
_logarithmic = true;
}
}
else if (prop.PropertyType == typeof(string)) m_type = PropertyType.String;
else return;
}
}
}

View File

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

View File

@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace Cryville.Crtr.Config {
public interface IPropertyMasterAdapter {
string DefaultCategory { get; }
IEnumerable<IPropertyAdapter> GetProperties();
}
public class DefaultPropertyMasterAdapter : IPropertyMasterAdapter {
readonly object _target;
public DefaultPropertyMasterAdapter(object target) {
if (target == null) throw new ArgumentNullException("target");
_target = target;
}
public string DefaultCategory { get { return "miscellaneous"; } }
public IEnumerable<IPropertyAdapter> GetProperties() {
return _target.GetType().GetProperties().Where(p => {
var attrs = p.GetCustomAttributes(typeof(BrowsableAttribute), true);
if (attrs.Length == 0) return true;
else return ((BrowsableAttribute)attrs.Single()).Browsable;
}).Select(p => new DefaultPropertyAdapter(_target, p));
}
}
}

View File

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

View File

@@ -0,0 +1,50 @@
using UnityEngine;
using UnityEngine.EventSystems;
namespace Cryville.Crtr.Config {
public class PVPBool : PropertyValuePanel, IPointerClickHandler {
[SerializeField]
RectTransform m_on;
[SerializeField]
RectTransform m_handleArea;
[SerializeField]
RectTransform m_handle;
protected override void OnValueUpdated() { }
public void Toggle() {
RawValue = !(bool)RawValue;
}
const float SPEED = 8;
float _ratio;
#pragma warning disable IDE0051
void Update() {
if ((bool)RawValue && _ratio != 1) {
_ratio += SPEED * Time.deltaTime;
if (_ratio > 1) _ratio = 1;
UpdateGraphics();
}
else if (!(bool)RawValue && _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

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

View File

@@ -0,0 +1,18 @@
using System;
namespace Cryville.Crtr.Config {
public class PVPNumber : PVPNumberBase {
protected override void OnValueUpdated() {
base.OnValueUpdated();
if (Range != null && Range.Length == 2) {
var min = (double)Range[0];
var max = (double)Range[1];
SetRatio((float)((Convert.ToDouble(RawValue) - min) / (max - min)));
}
}
protected override double GetValue(double ratio, float deltaTime, double min, double max) {
// if (LogarithmicMode) throw new NotImplementedException();
return (1 - ratio) * min + ratio * max;
}
}
}

View File

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

View File

@@ -0,0 +1,117 @@
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace Cryville.Crtr.Config {
public abstract class PVPNumberBase : PropertyValuePanel {
[SerializeField]
EventTrigger m_ctn;
[SerializeField]
RectTransform m_handleArea;
[SerializeField]
Image m_handle;
[SerializeField]
Text m_text;
#pragma warning disable IDE0051
protected 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);
OnIdle();
}
protected override void OnValueUpdated() {
m_text.text = MappedValue.ToString();
}
protected virtual void OnIdle() { }
void Update() {
if (use) {
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;
eventData.Use();
}
}
public void OnEndDrag(PointerEventData eventData) {
if (!nouse) {
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);
}
return float.NegativeInfinity;
}
void SetValueFromPos(Vector2 pos) {
double 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 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);
}
}
}

View File

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

View File

@@ -0,0 +1,14 @@
using System;
namespace Cryville.Crtr.Config {
public class PVPNumberStepped : PVPNumberBase {
protected override void OnIdle() {
SetRatio(0.5f);
}
protected override double GetValue(double ratio, float deltaTime, double min, double max) {
double delta = (ratio > 0.5 ? 1 : -1) * Math.Pow((ratio - 0.5f) * 2, 2) * deltaTime;
// if (LogarithmicMode) return Math.Pow(Math.E, Math.Log(m_value) + delta);
return Convert.ToDouble(RawValue) + delta;
}
}
}

View File

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

View File

@@ -0,0 +1,18 @@
using UnityEngine.UI;
namespace Cryville.Crtr.Config {
public class PVPString : PropertyValuePanel {
protected override void OnValueUpdated() { _inputField.text = (string)MappedValue; }
InputField _inputField;
void Awake() {
_inputField = GetComponent<InputField>();
_inputField.onValueChanged.AddListener(OnValueChanged);
}
void OnValueChanged(string value) {
RawValue = value;
}
}
}

View File

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

View File

@@ -0,0 +1,50 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Cryville.Crtr.Config {
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<IPropertyAdapter> props) {
Name = name.ToUpper();
foreach (var prop in props) {
var obj = Instantiate(m_propertyPrefab, transform, false);
obj.GetComponent<PropertyPanel>().Load(prop);
}
}
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

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

View File

@@ -0,0 +1,51 @@
using System.Collections.Generic;
using UnityEngine;
namespace Cryville.Crtr.Config {
public class PropertyMasterPanel : MonoBehaviour {
[SerializeField]
GameObject m_categoryPrefab;
[SerializeField]
Transform m_container;
bool _invalidated = true;
public void Invalidate() {
_invalidated = true;
}
private IPropertyMasterAdapter m_adapter;
public IPropertyMasterAdapter Adapter {
get { return m_adapter; }
set {
m_adapter = value;
Invalidate();
}
}
public void Update() {
if (!_invalidated) return;
LoadProperties();
foreach (Transform c in m_container) Destroy(c.gameObject);
foreach (var c in _categories) {
var obj = Instantiate(m_categoryPrefab, m_container, false);
obj.GetComponent<PropertyCategoryPanel>().Load(c.Key, c.Value);
}
}
readonly Dictionary<string, List<IPropertyAdapter>> _categories = new Dictionary<string, List<IPropertyAdapter>>();
public void LoadProperties() {
_categories.Clear();
_invalidated = false;
if (Adapter == null) return;
foreach (var p in Adapter.GetProperties()) {
string category = p.Category;
if (category == null) category = Adapter.DefaultCategory;
if (!_categories.ContainsKey(category))
_categories.Add(category, new List<IPropertyAdapter>());
_categories[category].Add(p);
}
}
}
}

View File

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

View File

@@ -0,0 +1,40 @@
using UnityEngine;
using UnityEngine.UI;
namespace Cryville.Crtr.Config {
public class PropertyPanel : MonoBehaviour {
[SerializeField]
GameObject m_bool;
[SerializeField]
GameObject m_number;
[SerializeField]
GameObject m_steppedNumber;
[SerializeField]
GameObject m_string;
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(IPropertyAdapter prop) {
_key.text = prop.Name;
GameObject vp;
switch (prop.Type) {
case PropertyType.Number: vp = m_number; break;
case PropertyType.SteppedNumber: vp = m_steppedNumber; break;
case PropertyType.Boolean: vp = m_bool; break;
case PropertyType.String: vp = m_string; break;
default: return;
}
_value = Instantiate(vp, _valueContainer, false).GetComponent<PropertyValuePanel>();
_value.Init(prop);
}
}
}

View File

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

View File

@@ -0,0 +1,39 @@
using UnityEngine;
namespace Cryville.Crtr.Config {
public abstract class PropertyValuePanel : MonoBehaviour {
IPropertyAdapter _property;
public void Init(IPropertyAdapter property) {
_property = property;
GetValue();
}
protected object[] Range { get { return _property.Range; } }
public object MappedValue { get; private set; }
private object m_rawValue;
public object RawValue {
get { return m_rawValue; }
set {
m_rawValue = value;
SetValue();
}
}
protected abstract void OnValueUpdated();
void GetValue() {
if (_property.SetMapped) {
MappedValue = _property.GetValue();
m_rawValue = _property.MapValueInverse(MappedValue);
}
else {
m_rawValue = _property.GetValue();
MappedValue = _property.MapValue(m_rawValue);
}
OnValueUpdated();
}
void SetValue() {
var outRaw = RawValue;
MappedValue = _property.MapValue(outRaw);
_property.SetValue(_property.SetMapped ? MappedValue : outRaw);
OnValueUpdated();
}
}
}

View File

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

View File

@@ -14,13 +14,13 @@ namespace Cryville.Crtr.Config {
[Category("gameplay")]
[JsonProperty("sound_offset")]
[Step(0.04f)]
[Step(0.04)]
[Precision(1e-3)]
public float SoundOffset { get; set; }
[Category("deprecated")][Obsolete]
[JsonProperty("scroll_velocity")][DefaultValue(1)]
[LogarithmicScale][Step(0.5f)][Precision(1e-1)]
[LogarithmicScale][Step(0.5)][Precision(1e-1)]
public float ScrollVelocity { get; set; }
public Generic() {