Implement coevent. (2)

This commit is contained in:
2023-01-13 16:37:31 +08:00
parent 555c88855c
commit 6efe70d751
4 changed files with 35 additions and 15 deletions

View File

@@ -149,6 +149,7 @@ namespace Cryville.Crtr {
nbus.BroadcastEndUpdate();
nbus.Anchor();
tbus.StripTempEvents();
tbus.ForwardStepByTime(clippingDist, step);
tbus.ForwardStepByTime(renderDist, step);
tbus.BroadcastEndUpdate();

View File

@@ -9,6 +9,7 @@ namespace Cryville.Crtr.Event {
public class ContainerState {
public EventBus Bus;
public EventContainer Container;
public StampedEvent StampedContainer;
public ContainerState Parent = null;
public ushort Depth;
@@ -38,6 +39,7 @@ namespace Cryville.Crtr.Event {
InvalidatedChildren.Clear();
}
public bool Active { get; set; }
private bool m_Working;
public bool Working {
get { return m_Working; }

View File

@@ -12,6 +12,8 @@ namespace Cryville.Crtr.Event {
ContainerState RootState;
readonly Dictionary<ChartEvent, ContainerState> containerMap
= new Dictionary<ChartEvent, ContainerState>();
readonly Dictionary<EventContainer, ContainerState> stateMap
= new Dictionary<EventContainer, ContainerState>();
readonly Dictionary<ChartEvent, StampedEvent> map
= new Dictionary<ChartEvent, StampedEvent>();
public Dictionary<EventContainer, List<StampedEvent>> coeventMap
@@ -34,6 +36,7 @@ namespace Cryville.Crtr.Event {
void AddEventContainer(EventContainer c, ContainerState parent = null) {
var cs = new ContainerState(chart, c, parent);
stateMap.Add(c, cs);
if (parent == null) {
cs.Depth = 0;
RootState = cs;
@@ -78,6 +81,9 @@ namespace Cryville.Crtr.Event {
Unstamped = ev,
Container = con
};
if (ev is EventContainer) {
stateMap[(EventContainer)ev].StampedContainer = sev;
}
if (ev is InstantEvent) {
var tev = (InstantEvent)ev;
var pev = map[tev.Original];
@@ -146,8 +152,8 @@ namespace Cryville.Crtr.Event {
ocevs.Add(cev);
BatchCoevents(cev, ocevs);
}
ocevs.Add(ev);
if (rootFlag) ocevs.Sort(CompareStampedEvents);
else ocevs.Add(ev);
}
}

View File

@@ -86,8 +86,18 @@ namespace Cryville.Crtr.Event {
void EnsureActivity(EventContainer c) {
if (activeContainers.Contains(c)) return;
if (RootState.CloneType >= 2) prototype.states[c].CopyTo(RootState.CloneType, states[c]);
var state = states[c];
if (state.Parent != null) EnsureActivity(state.Parent.Container);
if (RootState.CloneType >= 2) prototype.states[c].CopyTo(RootState.CloneType, state);
state.Active = true;
activeContainers.Add(c);
if (state.StampedContainer.Coevents == null) return;
foreach (var cev in state.StampedContainer.Coevents) {
if (cev.Container == null) continue;
EnsureActivity(cev.Container);
states[cev.Container].Handle(cev);
}
}
void AttachBus() {
@@ -107,6 +117,14 @@ namespace Cryville.Crtr.Event {
tempEvents.Insert(index, ev);
}
readonly StampedEvent _dummyEvent = new StampedEvent();
public void StripTempEvents() {
_dummyEvent.Time = Time;
var index = tempEvents.BinarySearch(_dummyEvent);
if (index < 0) index = ~index;
tempEvents.RemoveRange(0, index);
}
public override void ForwardOnceToTime(double toTime) {
double time1 = EventId < events.Count ? events[EventId].Time : double.PositiveInfinity;
double time2 = tempEvents.Count > 0 ? tempEvents[0].Time : double.PositiveInfinity;
@@ -119,26 +137,19 @@ namespace Cryville.Crtr.Event {
var batch = events[EventId];
for (var i = 0; i < batch.Count; i++) {
var ev = batch[i];
if (ev.Coevents == null) {
if (ev.Container != null) {
EnsureActivity(ev.Container);
states[ev.Container].Handle(ev);
}
}
else foreach (var cev in ev.Coevents) {
if (cev.Container == null) continue;
EnsureActivity(cev.Container);
states[cev.Container].Handle(cev);
}
if (ev.Unstamped is EventContainer) {
if (ev.Container != null) EnsureActivity((EventContainer)ev.Unstamped);
if (ev.Container != null) {
if (ev.Unstamped is EventContainer) EnsureActivity((EventContainer)ev.Unstamped);
else EnsureActivity(ev.Container);
states[ev.Container].Handle(ev);
}
else if (ev.Coevents != null) EnsureActivity((EventContainer)ev.Unstamped);
}
EventId++;
}
if (time2 == time0) {
while (tempEvents.Count > 0) {
var ev = tempEvents[0];
if (ev.Time != time0) break;
if (ev.Container != null) {
EnsureActivity(ev.Container);
states[ev.Container].Handle(ev);