Implement custom ruleset config. (2)

This commit is contained in:
2023-08-02 17:28:55 +08:00
parent 3b22d4fce3
commit 9044631fe7
7 changed files with 96 additions and 27 deletions

View File

@@ -12,6 +12,7 @@ namespace Cryville.Crtr.Config {
object[] Range { get; }
object GetValue();
void SetValue(object value);
event Action ValueChanged;
object MapValue(object value);
bool SetMapped { get; }
object MapValueInverse(object value);
@@ -35,6 +36,8 @@ namespace Cryville.Crtr.Config {
public object GetValue() { return _prop.GetValue(_target, null); }
public void SetValue(object value) { _prop.SetValue(_target, value, null); }
public event Action ValueChanged { add { } remove { } }
readonly double _precision;
readonly double _step;
readonly bool _logarithmic;

View File

@@ -7,7 +7,16 @@ namespace Cryville.Crtr.Config {
if (Range != null && Range.Length == 2) {
var min = (double)Range[0];
var max = (double)Range[1];
SetRatio((float)((Convert.ToDouble(RawValue) - min) / (max - min)));
var value = Convert.ToDouble(RawValue);
if (value < min) {
value = min;
RawValue = value;
}
else if (value > max) {
value = max;
RawValue = value;
}
SetRatio((float)((value - min) / (max - min)));
}
}
protected override double GetValue(double ratio, float deltaTime, double min, double max) {

View File

@@ -5,6 +5,7 @@ namespace Cryville.Crtr.Config {
IPropertyAdapter _property;
public void Init(IPropertyAdapter property) {
_property = property;
_property.ValueChanged += GetValue;
GetValue();
}
protected object[] Range { get { return _property.Range; } }

View File

@@ -5,21 +5,14 @@ using System.Collections.Generic;
namespace Cryville.Crtr.Config {
internal class RulesetConfigPropertyMasterAdapter : IPropertyMasterAdapter {
readonly List<IPropertyAdapter> _props;
readonly Dictionary<string, object> _values;
readonly List<RulesetConfigPropertyAdapter> _props = new List<RulesetConfigPropertyAdapter>();
readonly RulesetConfigStore _store;
public RulesetConfigPropertyMasterAdapter(Dictionary<Identifier, ConfigDefinition> defs, Dictionary<string, object> values) {
_values = values;
_props = new List<IPropertyAdapter>();
PdtEvaluator.Instance.ContextCascadeInsert();
_store = new RulesetConfigStore(defs, values);
PdtEvaluator.Instance.ContextRulesetConfig = _store;
if (defs == null) return;
foreach (var def in defs) {
var name = (string)def.Key.Name;
if (!_values.ContainsKey(name)) {
float value = 0;
PdtEvaluator.Instance.Evaluate(new PropOp.Float(v => value = v), def.Value.@default);
_values.Add(name, value);
}
_props.Add(new RulesetConfigPropertyAdapter(def.Key, def.Value, this));
}
}
@@ -29,11 +22,14 @@ namespace Cryville.Crtr.Config {
public IEnumerable<IPropertyAdapter> GetProperties() { return _props; }
public object GetValue(string key) {
return _values[key];
return _store[key];
}
public void SetValue(string key, object value) {
_values[key] = value;
_store[key] = value;
foreach (var prop in _props) {
prop.OnValueChanged();
}
}
}
@@ -54,9 +50,6 @@ namespace Cryville.Crtr.Config {
m_range[0] = (double)v.Behind;
m_range[1] = (double)v.Ahead;
});
_numsrc = new PropSrc.Float(() => _buf);
_numop = new PropOp.Float(v => _buf = v);
PdtEvaluator.Instance.ContextCascadeUpdate(key.Key, _numsrc);
}
public string Category { get { return _def.category; } }
@@ -83,19 +76,22 @@ namespace Cryville.Crtr.Config {
_master.SetValue(Name, value);
}
public event Action ValueChanged;
public void OnValueChanged() {
var ev = ValueChanged;
if (ev != null) ev();
}
public bool SetMapped { get { return false; } }
float _buf;
readonly PropSrc _numsrc;
readonly PropOp _numop;
readonly PropStores.Float _numst = new PropStores.Float();
public object MapValue(object value) {
_buf = (float)(double)value;
if (_def.value == null) return _buf;
_numsrc.Invalidate();
PdtEvaluator.Instance.ContextSelfValue = _numsrc;
PdtEvaluator.Instance.Evaluate(_numop, _def.value);
_numst.Value = (float)(double)value;
if (_def.value == null) return _numst.Value;
PdtEvaluator.Instance.ContextSelfValue = _numst.Source;
PdtEvaluator.Instance.Evaluate(_numst.Target, _def.value);
PdtEvaluator.Instance.ContextSelfValue = null;
return _buf;
return _numst.Value;
}
public object MapValueInverse(object value) { throw new NotSupportedException(); }

View File

@@ -0,0 +1,43 @@
using Cryville.Common;
using Cryville.Common.Collections.Specialized;
using System;
using System.Collections.Generic;
namespace Cryville.Crtr.Config {
public class RulesetConfigStore {
readonly IntKeyedDictionary<PropSrc> _srcs = new IntKeyedDictionary<PropSrc>();
readonly Dictionary<string, int> _revMap = new Dictionary<string, int>();
readonly Dictionary<string, object> _values;
public RulesetConfigStore(Dictionary<Identifier, ConfigDefinition> defs, Dictionary<string, object> values) {
_values = values;
if (defs == null) return;
foreach (var def in defs) {
var key = def.Key.Key;
var name = (string)def.Key.Name;
if (!_values.ContainsKey(name)) {
float value = 0;
PdtEvaluator.Instance.Evaluate(new PropOp.Float(v => value = v), def.Value.@default);
_values.Add(name, value);
}
_revMap.Add(name, key);
_srcs.Add(key, new PropSrc.Float(() => {
float result = 0;
PdtEvaluator.Instance.ContextSelfValue = new PropSrc.Float(() => Convert.ToSingle(_values[name]));
PdtEvaluator.Instance.Evaluate(new PropOp.Float(v => result = v), def.Value.value);
PdtEvaluator.Instance.ContextSelfValue = null;
return result;
}));
}
}
public object this[string key] {
get { return _values[key]; }
set {
_values[key] = value;
_srcs[_revMap[key]].Invalidate();
}
}
public bool TryGetMappedSource(int key, out PropSrc result) {
return _srcs.TryGetValue(key, out result);
}
}
}

View File

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