Files
crtr/Assets/Cryville/Crtr/SkinSelectors.cs
2022-09-30 17:32:21 +08:00

355 lines
9.9 KiB
C#

using Cryville.Common.Pdt;
using Cryville.Crtr.Components;
using Cryville.Crtr.Event;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Cryville.Crtr {
#if false
[Obsolete]
public class SkinSelectors {
static readonly char[] sep = new char[]{
'[', ' ', '$', '#', '*', '@', '.'
};
readonly SkinSelector[] selectors;
readonly string[] annotations;
public readonly static SkinSelectors None = new SkinSelectors("");
public SkinSelectors(string exp) {
exp = exp.Trim();
var r = new List<SkinSelector>();
var annos = new List<string>();
int i = 0;
int eos = exp.Length - 1;
while (i <= eos) {
while (exp[i] == ' ') i++;
char sc = exp[i];
int ep;
switch (sc) {
case '[':
ep = exp.IndexOf(']', i + 1);
if (ep == -1) throw new ArgumentException(); // TODO
r.Add(new SkinSelector.ElementProperty(exp.Substring(i, ep - i + 1)));
i = ep + 1;
break;
case '.':
if (exp[i + 1] == '.') {
ep = exp.IndexOfAny(sep, i + 2);
if (ep == -1) ep = eos + 1;
r.Add(new SkinSelector.ElementState(exp.Substring(i, ep - i)));
}
else {
ep = exp.IndexOfAny(sep, i + 1);
if (ep == -1) ep = eos + 1;
r.Add(new SkinSelector.ElementAnchor(exp.Substring(i, ep - i)));
}
i = ep;
break;
case '$':
ep = exp.IndexOfAny(sep, i + 1);
if (ep == -1) ep = eos + 1;
r.Add(new SkinSelector.CreateObject(exp.Substring(i, ep - i)));
i = ep;
break;
case '@':
ep = exp.IndexOfAny(sep, i + 1);
if (ep == -1) ep = eos + 1;
annos.Add(exp.Substring(i, ep - i));
i = ep;
break;
default:
ep = exp.IndexOfAny(sep, i);
if (ep == -1) ep = eos + 1;
if (i == ep)
throw new ArgumentException(); // TODO
r.Add(new SkinSelector.ElementType(exp.Substring(i, ep - i)));
i = ep;
break;
}
// throw new ArgumentException();
}
selectors = r.ToArray();
annotations = annos.ToArray();
}
public override string ToString() {
if (selectors.Length == 0) return "";
bool flag = false;
string r = "";
foreach (var a in annotations) {
if (flag) r += " " + a;
else { r += a; flag = true; }
}
foreach (var s in selectors) {
if (flag) r += " " + s.ToString();
else { r += s.ToString(); flag = true; }
}
return r;
}
public Transform MatchStatic(ContainerState h, Transform anchor = null) {
foreach (var s in selectors) {
anchor = s.Match(h, anchor);
if (anchor == null) return null;
}
return anchor;
}
public bool IsUpdatable(ContainerState h) {
foreach (var s in selectors)
if (!s.IsUpdatable(h)) return false;
return true;
}
[Obsolete]
public Transform MatchDynamic(ContainerState h, Element e, Transform old_target, Transform anchor = null) {
if (!e.IsDynamic) return null;
foreach (var s in selectors) {
anchor = s.Match(h, anchor, old_target);
if (anchor == null) return null;
}
return anchor;
}
public Transform MatchDynamic(ContainerState h, SkinElement e, Transform old_target, Transform anchor = null) {
if (!e.IsDynamic) return null;
foreach (var s in selectors) {
anchor = s.Match(h, anchor, old_target);
if (anchor == null) return null;
}
return anchor;
}
}
#endif
public class SkinSelectors {
readonly SkinSelector[] selectors;
readonly string[] annotations;
public SkinSelectors(IEnumerable<SkinSelector> s, IEnumerable<string> a) {
selectors = s.ToArray();
annotations = a.ToArray();
}
public override string ToString() {
if (selectors.Length == 0) return "";
bool flag = false;
string r = "";
foreach (var a in annotations) {
if (flag) r += " " + a;
else { r += a; flag = true; }
}
foreach (var s in selectors) {
if (flag) r += " " + s.ToString();
else { r += s.ToString(); flag = true; }
}
return r;
}
public void Optimize(PdtEvaluatorBase etor) {
for (int i = 0; i < selectors.Length; i++) {
selectors[i].Optimize(etor);
}
}
public Transform MatchStatic(ContainerState h, Transform anchor = null) {
foreach (var s in selectors) {
anchor = s.Match(h, anchor);
if (anchor == null) return null;
}
return anchor;
}
public bool IsUpdatable(ContainerState h) {
foreach (var s in selectors)
if (!s.IsUpdatable(h)) return false;
return true;
}
public Transform MatchDynamic(ContainerState h, SkinElement e, Transform old_target, Transform anchor = null) {
if (!e.IsDynamic) return null;
foreach (var s in selectors) {
anchor = s.Match(h, anchor, old_target);
if (anchor == null) return null;
}
return anchor;
}
}
public abstract class SkinSelector {
#if false
[Obsolete]
public string Expression {
get;
private set;
}
[Obsolete]
protected SkinSelector(string exp) {
Expression = exp;
}
#endif
protected SkinSelector() { }
public abstract bool IsStatic {
get;
}
public virtual void Optimize(PdtEvaluatorBase etor) { }
public abstract Transform Match(ContainerState h, Transform a, Transform ot = null);
public virtual bool IsUpdatable(ContainerState h) {
return true;
}
public class CreateObject : SkinSelector {
public CreateObject() { }
//[Obsolete]
//public CreateObject(string exp) : base(exp) { }
public override bool IsStatic {
get { return true; }
}
public override Transform Match(ContainerState h, Transform a, Transform ot = null) {
if (ot != null)
return ot;
var obj = new GameObject {
name = "__obj__"
};
obj.transform.SetParent(a, false);
obj.AddComponent<TransformInterface>();
return obj.transform;
}
}
#if false
[Obsolete]
public class ElementAnchor : SkinSelector {
public ElementAnchor(string exp) : base(exp) {
AnchorName = Expression.Substring(1);
}
public override bool IsStatic {
get { return true; }
}
public string AnchorName { get; private set; }
public override Transform Match(ContainerState h, Transform a, Transform ot = null) {
return h.Handler.Anchors[AnchorName].Transform;
}
public override bool IsUpdatable(ContainerState h) {
return h.Handler.Anchors[AnchorName].Opened;
}
}
#endif
public class Anchor : SkinSelector {
public string Name { get; private set; }
public Anchor(string name) {
Name = name;
}
public override bool IsStatic {
get { return true; }
}
public override Transform Match(ContainerState h, Transform a, Transform ot = null) {
return h.Handler.Anchors[Name].Transform;
}
public override bool IsUpdatable(ContainerState h) {
return h.Handler.Anchors[Name].Opened;
}
}
#if false
[Obsolete]
public class ElementProperty : SkinSelector {
readonly bool notFlag;
readonly string prop;
readonly string value;
// Expression exp;
public ElementProperty(string exp) : base(exp) {
if (exp.Length <= 2) throw new ArgumentException(); // TODO
char sc = exp[0];
char ec = exp[exp.Length - 1];
if (sc != '[') throw new ArgumentException(); // TODO
if (ec != ']') throw new ArgumentException(); // TODO
var pexp = exp.Substring(1, exp.Length - 2);
var m = Regex.Match(pexp, @"^(\!?)(.*?)(?(=)=(.*))$");
if (!m.Success) throw new ArgumentException(); // TODO
notFlag = m.Groups[1].Length != 0;
prop = m.Groups[2].Value;
value = m.Groups[3].Value;
// TODO exp = new Expression(m.Groups[3]);
}
public override bool IsStatic {
get { throw new NotImplementedException(); }
}
public override Transform Match(ContainerState h, Transform a, Transform ot = null) {
return (MatchPropertyInternal(h, a) ^ notFlag) ? a : null;
}
bool MatchPropertyInternal(ContainerState h, Transform a) {
/*if (prop[0] == '$') {
MotionValue v = h.GetRawValue(prop.Substring(1));
return MatchValueInternal(v, value);
}
else {*/
return MatchValueInternal(h.Container.Properties[prop](), value);
//}
}
bool MatchValueInternal(object obj, string cond) {
if (obj is bool)
return bool.Equals(obj, true);
if (cond == "odd") {
if (obj is float)
return (float)obj % 2 == 1;
}
if (cond == "even") {
if (obj is float)
return (float)obj % 2 == 0;
}
if (cond == "mid") {
return false; // TODO
}
return false; // TODO
//throw new ArgumentException(); // TODO
}
}
#endif
public class Property : SkinSelector {
readonly PdtExpression _exp;
readonly PdtOperator _op;
bool _flag;
public Property(PdtExpression exp) {
_exp = exp;
_op = new PropOp.Boolean(v => _flag = v);
}
public override void Optimize(PdtEvaluatorBase etor) {
etor.Optimize(_exp);
}
public override bool IsStatic {
get { throw new NotImplementedException(); }
}
public override Transform Match(ContainerState h, Transform a, Transform ot = null) {
ChartPlayer.etor.ContextTransform = a;
ChartPlayer.etor.ContextEvent = h.Container;
ChartPlayer.etor.Evaluate(_op, _exp);
return _flag ? a : null;
}
}
#if false
[Obsolete]
public class ElementState : SkinSelector {
public ElementState(string exp) : base(exp) { }
public override bool IsStatic {
get { return false; }
}
public override Transform Match(ContainerState h, Transform a, Transform ot = null) {
return null; // TODO
}
}
#endif
public class State : SkinSelector {
public State(string state) { }
public override bool IsStatic {
get { return false; }
}
public override Transform Match(ContainerState h, Transform a, Transform ot = null) {
return null; // TODO
}
}
public class ElementType : SkinSelector {
readonly string _type;
public ElementType(string type) { _type = type; }
public override bool IsStatic {
get { return true; }
}
public override Transform Match(ContainerState h, Transform a, Transform ot = null) {
return h.Handler.TypeName == _type ? a : null;
}
}
}
}