Add project files.
This commit is contained in:
163
Assets/Cryville/Crtr/SkinManager.cs
Normal file
163
Assets/Cryville/Crtr/SkinManager.cs
Normal file
@@ -0,0 +1,163 @@
|
||||
using Cryville.Common.Pdt;
|
||||
using Cryville.Crtr.Components;
|
||||
using Cryville.Crtr.Event;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cryville.Crtr {
|
||||
#if false
|
||||
[Obsolete]
|
||||
public class SkinManager {
|
||||
readonly CompiledSkin skin;
|
||||
readonly Dictionary<Element, Transform> matchedStatic
|
||||
= new Dictionary<Element, Transform>();
|
||||
readonly Dictionary<Element, Transform> matchedDynamic
|
||||
= new Dictionary<Element, Transform>();
|
||||
public SkinManager(CompiledSkin _skin) {
|
||||
skin = _skin;
|
||||
}
|
||||
|
||||
public void MatchStatic(Evaluator etor, ContainerState context) {
|
||||
matchedStatic.Clear();
|
||||
MatchStatic(skin, context, context.Handler.gogroup);
|
||||
|
||||
foreach (var m in matchedStatic) {
|
||||
var el = m.Key;
|
||||
var obj = m.Value;
|
||||
foreach (var c in el.components) {
|
||||
obj.gameObject.AddComponent(GetComponentByName(c));
|
||||
}
|
||||
foreach (var p in el.properties) {
|
||||
ParseProperty(obj, p.Key, p.Value, context, etor);
|
||||
}
|
||||
}
|
||||
}
|
||||
void MatchStatic(Element rel, ContainerState context, Transform anchor = null) {
|
||||
matchedStatic.Add(rel, anchor);
|
||||
foreach (var r in rel.elements) {
|
||||
var new_anchor = r.Key.MatchStatic(context, anchor);
|
||||
if (new_anchor != null) {
|
||||
MatchStatic(r.Value, context, new_anchor);
|
||||
}
|
||||
}
|
||||
}
|
||||
public void MatchDynamic(Evaluator etor, ContainerState context) {
|
||||
UnityEngine.Profiling.Profiler.BeginSample("SkinManager.MatchDynamic()");
|
||||
matchedDynamic.Clear();
|
||||
MatchDynamic(skin, context, context.Handler.gogroup);
|
||||
|
||||
foreach (var m in matchedDynamic) {
|
||||
var el = m.Key;
|
||||
var obj = m.Value;
|
||||
/*foreach (var c in el.components) {
|
||||
if (obj.gameObject.GetComponent(GetComponentByName(c)) == null)
|
||||
obj.gameObject.AddComponent(GetComponentByName(c));
|
||||
}*/
|
||||
foreach (var p in el.properties) {
|
||||
if (!p.Value.IsDynamic) continue;
|
||||
ParseProperty(obj, p.Key, p.Value, context, etor);
|
||||
}
|
||||
}
|
||||
UnityEngine.Profiling.Profiler.EndSample();
|
||||
}
|
||||
void MatchDynamic(Element rel, ContainerState context, Transform anchor = null) {
|
||||
matchedDynamic.Add(rel, anchor);
|
||||
foreach (var r in rel.elements) {
|
||||
Transform new_anchor;
|
||||
/*if (r.Value.IsDynamic) {
|
||||
new_anchor = r.Key.MatchDynamic(
|
||||
context, r.Value, null, anchor
|
||||
);
|
||||
}
|
||||
else {*/
|
||||
if (!matchedStatic.ContainsKey(r.Value)) continue;
|
||||
if (!r.Key.IsUpdatable(context)) continue;
|
||||
new_anchor = r.Key.MatchDynamic(
|
||||
context, r.Value, matchedStatic[r.Value], anchor
|
||||
);
|
||||
//}
|
||||
if (new_anchor != null) {
|
||||
MatchDynamic(r.Value, context, new_anchor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ParseProperty(Transform obj, string key, Expression value, ContainerState context, Evaluator etor) {
|
||||
etor.Context = new EvaluatorContext() {
|
||||
// Extra = context.judge.GetScoreStrings(),
|
||||
State = context,
|
||||
Transform = obj,
|
||||
};
|
||||
/*var keytuple = key.Split('#');
|
||||
int matid = 0;*/
|
||||
switch (key) {
|
||||
/*case "tex":
|
||||
if (int.TryParse(keytuple[1], out matid) || keytuple.Length == 1) {
|
||||
var mat = obj.GetComponent<Renderer>().materials[matid];
|
||||
var frame = ChartPlayer.frames[value.Eval<string>(etor)];
|
||||
var tex = frame.Texture;
|
||||
if (keytuple.Length == 2) mat.mainTexture = tex;
|
||||
else mat.SetTexture(keytuple[2], tex);
|
||||
}
|
||||
else throw new ArgumentException(); // TODO
|
||||
break;*/
|
||||
/*case "shader":
|
||||
if (int.TryParse(keytuple[1], out matid) || keytuple.Length == 1) {
|
||||
var mat = obj.GetComponent<Renderer>().materials[matid];
|
||||
// TODO mat.shader = value.Eval<string>(etor);
|
||||
}
|
||||
else throw new ArgumentException(); // TODO
|
||||
break;*/
|
||||
case "pos":
|
||||
obj.localPosition = value.Eval<Vector3>(etor);
|
||||
break;
|
||||
case "rot":
|
||||
obj.localRotation = Quaternion.Euler(value.Eval<Vector3>(etor));
|
||||
break;
|
||||
case "scale":
|
||||
var s = value.Eval<Vector3>(etor);
|
||||
if (s.z == 0) s.z = 1;
|
||||
obj.localScale = s;
|
||||
break;
|
||||
default:
|
||||
var cp = key.Split('.');
|
||||
if (cp.Length == 2) {
|
||||
var ctype = GetComponentByName(cp[0]);
|
||||
var comp = (SkinComponent)obj.GetComponent(ctype);
|
||||
if (!comp.Properties.ContainsKey(cp[1]))
|
||||
throw new ArgumentException(string.Format("Property {0}.{1} not found", cp[0], cp[1]));
|
||||
var prop = comp.Properties[cp[1]];
|
||||
/*MemberInfo[] mi = ctype.GetMember(cp[1]);
|
||||
for (var i = 2; i < cp.Length; i++) {
|
||||
prop = ReflectionHelper.GetValue(mi[0], prop);
|
||||
ctype = prop.GetType();
|
||||
mi = ctype.GetMember(cp[i]);
|
||||
if (mi.Length != 1)
|
||||
throw new ArgumentException(); // TODO
|
||||
}
|
||||
ReflectionHelper.SetValue(mi[0], prop, etor.Cast(
|
||||
ReflectionHelper.GetMemberType(mi[0]),
|
||||
value.Eval(etor)
|
||||
));*/
|
||||
var r = value.Eval(etor);
|
||||
prop.Set(etor.Cast(prop.Type, r));
|
||||
}
|
||||
else
|
||||
throw new NotImplementedException("Unknown property " + key);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static readonly char[] nssep = new char[]{':'};
|
||||
Type GetComponentByName(string name) {
|
||||
var nstuple = name.Split(nssep, 2);
|
||||
var ns = nssep.Length == 2 ? nstuple[0] : "generic";
|
||||
name = nssep.Length == 2 ? nstuple[1] : nstuple[0];
|
||||
if (ns == "generic")
|
||||
return GenericResources.Components[name];
|
||||
throw new ArgumentException(); // TODO
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
Reference in New Issue
Block a user