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

View File

@@ -7,7 +7,16 @@ namespace Cryville.Crtr.Config {
if (Range != null && Range.Length == 2) { if (Range != null && Range.Length == 2) {
var min = (double)Range[0]; var min = (double)Range[0];
var max = (double)Range[1]; 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) { protected override double GetValue(double ratio, float deltaTime, double min, double max) {

View File

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

View File

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

View File

@@ -2,6 +2,7 @@ using Cryville.Common;
using Cryville.Common.Collections.Specialized; using Cryville.Common.Collections.Specialized;
using Cryville.Common.Math; using Cryville.Common.Math;
using Cryville.Common.Pdt; using Cryville.Common.Pdt;
using Cryville.Crtr.Config;
using Cryville.Crtr.Event; using Cryville.Crtr.Event;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -48,6 +49,7 @@ namespace Cryville.Crtr {
_vec = ContextState.GetComputedValue(name); _vec = ContextState.GetComputedValue(name);
_vecsrc.Invalidate(); _vecsrc.Invalidate();
_vecsrc.Get(out type, out value); _vecsrc.Get(out type, out value);
RevokePotentialConstant();
} }
else if (ContextState != null && ContextState.Handler.PropSrcs.TryGetValue(name, out prop)) { else if (ContextState != null && ContextState.Handler.PropSrcs.TryGetValue(name, out prop)) {
prop.Get(out type, out value); prop.Get(out type, out value);
@@ -56,6 +58,9 @@ namespace Cryville.Crtr {
else if (ContextSkinContainer != null && ContextSkinContainer.Variables.TryGetValue(name, out variable)) { else if (ContextSkinContainer != null && ContextSkinContainer.Variables.TryGetValue(name, out variable)) {
variable.Source.Get(out type, out value); variable.Source.Get(out type, out value);
} }
else if (ContextRulesetConfig != null && ContextRulesetConfig.TryGetMappedSource(name, out prop)) {
prop.Get(out type, out value);
}
else if (ContextJudge != null && ContextJudge.TryGetScoreSrc(name, out prop)) { else if (ContextJudge != null && ContextJudge.TryGetScoreSrc(name, out prop)) {
prop.Get(out type, out value); prop.Get(out type, out value);
RevokePotentialConstant(); RevokePotentialConstant();
@@ -124,6 +129,7 @@ namespace Cryville.Crtr {
public ContainerState ContextState { get; set; } public ContainerState ContextState { get; set; }
public SkinContainer ContextSkinContainer { get; set; } public SkinContainer ContextSkinContainer { get; set; }
public Transform ContextTransform { get; set; } public Transform ContextTransform { get; set; }
public RulesetConfigStore ContextRulesetConfig { get; set; }
public Judge ContextJudge { get; set; } public Judge ContextJudge { get; set; }
public PropSrc ContextSelfValue { get; set; } public PropSrc ContextSelfValue { get; set; }
@@ -166,6 +172,7 @@ namespace Cryville.Crtr {
ContextCascadeBlocks.Push(0); ContextCascadeBlocks.Push(0);
ContextEvent = null; ContextEvent = null;
ContextJudge = null; ContextJudge = null;
ContextRulesetConfig = null;
ContextSelfValue = null; ContextSelfValue = null;
ContextSkinContainer = null; ContextSkinContainer = null;
ContextState = null; ContextState = null;
@@ -204,7 +211,6 @@ namespace Cryville.Crtr {
_ctxops.Add(IdentifierManager.Shared.Request("min"), new func_min(() => ContextSelfValue)); _ctxops.Add(IdentifierManager.Shared.Request("min"), new func_min(() => ContextSelfValue));
_ctxops.Add(IdentifierManager.Shared.Request("max"), new func_max(() => ContextSelfValue)); _ctxops.Add(IdentifierManager.Shared.Request("max"), new func_max(() => ContextSelfValue));
_ctxops.Add(IdentifierManager.Shared.Request("abs"), new func_abs(() => ContextSelfValue)); _ctxops.Add(IdentifierManager.Shared.Request("abs"), new func_abs(() => ContextSelfValue));
_ctxops.Add(IdentifierManager.Shared.Request("range"), new func_anim(() => ContextSelfValue));
_ctxops.Add(IdentifierManager.Shared.Request("anim"), new func_anim(() => ContextSelfValue)); _ctxops.Add(IdentifierManager.Shared.Request("anim"), new func_anim(() => ContextSelfValue));
_ctxops.Add(IdentifierManager.Shared.Request("cubic_bezier"), new func_cubic_bezier(() => ContextSelfValue)); _ctxops.Add(IdentifierManager.Shared.Request("cubic_bezier"), new func_cubic_bezier(() => ContextSelfValue));
_ctxops.Add(IdentifierManager.Shared.Request("ease"), new func_cubic_bezier_fixed(0.25f, 0.1f, 0.25f, 1f, () => ContextSelfValue)); _ctxops.Add(IdentifierManager.Shared.Request("ease"), new func_cubic_bezier_fixed(0.25f, 0.1f, 0.25f, 1f, () => ContextSelfValue));