138 lines
5.0 KiB
C#
138 lines
5.0 KiB
C#
using Cryville.Common.Pdt;
|
|
using Cryville.Crtr.Event;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Profiling;
|
|
|
|
namespace Cryville.Crtr {
|
|
public class SkinContainer {
|
|
readonly PdtSkin skin;
|
|
readonly List<DynamicProperty> dynprops = new List<DynamicProperty>();
|
|
struct DynamicProperty {
|
|
public RuntimeSkinContext Context { get; set; }
|
|
public SkinPropertyKey Key { get; set; }
|
|
public PdtExpression Value { get; set; }
|
|
}
|
|
readonly List<DynamicElement> dynelems = new List<DynamicElement>();
|
|
struct DynamicElement {
|
|
public RuntimeSkinContext Context { get; set; }
|
|
public SkinSelectors Selectors { get; set; }
|
|
public SkinElement Element { get; set; }
|
|
}
|
|
public SkinContainer(PdtSkin _skin) {
|
|
skin = _skin;
|
|
}
|
|
public void MatchStatic(ContainerState state) {
|
|
dynprops.Clear(); dynelems.Clear();
|
|
ChartPlayer.etor.ContextState = state;
|
|
ChartPlayer.etor.ContextEvent = state.Container;
|
|
MatchStatic(skin, state, new RuntimeSkinContext(state.Handler.SkinContext));
|
|
ChartPlayer.etor.ContextEvent = null;
|
|
ChartPlayer.etor.ContextState = null;
|
|
}
|
|
void MatchStatic(SkinElement rel, ContainerState state, RuntimeSkinContext ctx) {
|
|
var rc = ctx.ReadContext;
|
|
ChartPlayer.etor.ContextTransform = rc.Transform;
|
|
if (rc.PropSrcs != null) ChartPlayer.etor.ContextCascadeInsert(rc.PropSrcs);
|
|
foreach (var p in rel.properties) {
|
|
p.Key.ExecuteStatic(state, ctx, p.Value);
|
|
if (p.Key.IsValueRequired && !p.Value.IsConstant) dynprops.Add(
|
|
new DynamicProperty { Context = ctx, Key = p.Key, Value = p.Value }
|
|
);
|
|
}
|
|
ChartPlayer.etor.ContextTransform = null;
|
|
foreach (var r in rel.elements) {
|
|
try {
|
|
var nctxs = r.Key.MatchStatic(state, rc);
|
|
var roflag = r.Key.annotations.Contains("if");
|
|
var woflag = r.Key.annotations.Contains("then");
|
|
foreach (var nctx in nctxs) {
|
|
var nrctx = new RuntimeSkinContext(nctx, ctx, roflag, woflag);
|
|
MatchStatic(r.Value, state, nrctx);
|
|
}
|
|
}
|
|
catch (SelectorNotAvailableException) {
|
|
dynelems.Add(
|
|
new DynamicElement { Context = ctx, Selectors = r.Key, Element = r.Value }
|
|
);
|
|
}
|
|
}
|
|
if (rc.PropSrcs != null) ChartPlayer.etor.ContextCascadeDiscard();
|
|
}
|
|
public void MatchDynamic(ContainerState state) {
|
|
if (dynprops.Count == 0 && dynelems.Count == 0) return;
|
|
Profiler.BeginSample("SkinContainer.MatchDynamic");
|
|
ChartPlayer.etor.ContextState = state;
|
|
ChartPlayer.etor.ContextEvent = state.Container;
|
|
for (int i = 0; i < dynprops.Count; i++) {
|
|
DynamicProperty p = dynprops[i];
|
|
p.Key.ExecuteDynamic(state, p.Context, p.Value);
|
|
}
|
|
for (int i = 0; i < dynelems.Count; i++) {
|
|
DynamicElement e = dynelems[i];
|
|
var psrcs = e.Context.ReadContext.PropSrcs;
|
|
if (psrcs != null) ChartPlayer.etor.ContextCascadeInsert(psrcs);
|
|
var nctx = e.Selectors.MatchDynamic(state, e.Context.ReadContext);
|
|
if (nctx != null) MatchDynamic(e.Element, state, new RuntimeSkinContext(
|
|
nctx, e.Context, e.Selectors.annotations.Contains("if"), e.Selectors.annotations.Contains("then")
|
|
));
|
|
if (psrcs != null) ChartPlayer.etor.ContextCascadeDiscard();
|
|
}
|
|
ChartPlayer.etor.ContextEvent = null;
|
|
ChartPlayer.etor.ContextState = null;
|
|
Profiler.EndSample();
|
|
}
|
|
void MatchDynamic(SkinElement rel, ContainerState state, RuntimeSkinContext ctx) {
|
|
var rc = ctx.ReadContext;
|
|
ChartPlayer.etor.ContextTransform = rc.Transform;
|
|
if (rc.PropSrcs != null) ChartPlayer.etor.ContextCascadeInsert(rc.PropSrcs);
|
|
foreach (var p in rel.properties) {
|
|
p.Key.ExecuteDynamic(state, ctx, p.Value);
|
|
}
|
|
ChartPlayer.etor.ContextTransform = null;
|
|
foreach (var r in rel.elements) {
|
|
if (!r.Key.IsUpdatable(state)) continue;
|
|
var nctx = r.Key.MatchDynamic(state, rc);
|
|
if (nctx != null) MatchDynamic(r.Value, state, new RuntimeSkinContext(
|
|
nctx, ctx, r.Key.annotations.Contains("if"), r.Key.annotations.Contains("then")
|
|
));
|
|
}
|
|
if (rc.PropSrcs != null) ChartPlayer.etor.ContextCascadeDiscard();
|
|
}
|
|
}
|
|
public class SkinContext {
|
|
public Transform Transform { get; private set; }
|
|
public Dictionary<int, PropSrc> PropSrcs { get; private set; }
|
|
public SkinContext(Transform transform, Dictionary<int, PropSrc> propSrcs = null) {
|
|
Transform = transform;
|
|
PropSrcs = propSrcs;
|
|
}
|
|
}
|
|
public struct RuntimeSkinContext {
|
|
public SkinContext ReadContext { get; private set; }
|
|
public Transform WriteTransform { get; private set; }
|
|
public RuntimeSkinContext(SkinContext rw) {
|
|
ReadContext = rw;
|
|
WriteTransform = rw.Transform;
|
|
}
|
|
public RuntimeSkinContext(SkinContext r, Transform w) {
|
|
ReadContext = r;
|
|
WriteTransform = w;
|
|
}
|
|
public RuntimeSkinContext(SkinContext newctx, RuntimeSkinContext oldctx, bool roflag, bool woflag) {
|
|
if (roflag) {
|
|
ReadContext = newctx;
|
|
WriteTransform = oldctx.WriteTransform;
|
|
}
|
|
else if (woflag) {
|
|
ReadContext = oldctx.ReadContext;
|
|
WriteTransform = newctx.Transform;
|
|
}
|
|
else {
|
|
ReadContext = newctx;
|
|
WriteTransform = newctx.Transform;
|
|
}
|
|
}
|
|
}
|
|
}
|