113 lines
3.8 KiB
C#
113 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace Encompass
|
|
{
|
|
internal class DrawLayerManager
|
|
{
|
|
private readonly object layerOrderLock = new object();
|
|
|
|
private readonly SortedList<int, int> layerOrder = new SortedList<int, int>();
|
|
|
|
private readonly ConcurrentDictionary<int, ConcurrentDictionary<Guid, byte>> layerIndexToComponentIDs = new ConcurrentDictionary<int, ConcurrentDictionary<Guid, byte>>();
|
|
private readonly ConcurrentDictionary<int, ConcurrentDictionary<GeneralRenderer, byte>> layerIndexToGeneralRenderers = new ConcurrentDictionary<int, ConcurrentDictionary<GeneralRenderer, byte>>();
|
|
|
|
private readonly ConcurrentDictionary<Guid, int> componentIDToLayerIndex = new ConcurrentDictionary<Guid, int>();
|
|
|
|
public IEnumerable<int> LayerOrder { get { lock(layerOrderLock) { return layerOrder.Values; } } }
|
|
|
|
public void RegisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer)
|
|
{
|
|
if (layerIndexToGeneralRenderers.ContainsKey(layer))
|
|
{
|
|
var set = layerIndexToGeneralRenderers[layer];
|
|
set[renderer] = 0;
|
|
}
|
|
else
|
|
{
|
|
var set = new ConcurrentDictionary<GeneralRenderer, byte>();
|
|
layerIndexToGeneralRenderers[layer] = set;
|
|
set[renderer] = 0;
|
|
}
|
|
|
|
lock (layerOrderLock)
|
|
{
|
|
if (!layerOrder.ContainsKey(layer))
|
|
{
|
|
layerOrder.Add(layer, layer);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void UnregisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer)
|
|
{
|
|
if (layerIndexToGeneralRenderers.ContainsKey(layer))
|
|
{
|
|
layerIndexToGeneralRenderers[layer].TryRemove(renderer, out _);
|
|
}
|
|
}
|
|
|
|
public void AdjustRendererLayer(GeneralRenderer renderer, int oldLayer, int newLayer)
|
|
{
|
|
UnregisterGeneralRendererWithLayer(renderer, oldLayer);
|
|
RegisterGeneralRendererWithLayer(renderer, newLayer);
|
|
}
|
|
|
|
public void RegisterComponentWithLayer(Guid id, int layer)
|
|
{
|
|
if (layerIndexToComponentIDs.ContainsKey(layer))
|
|
{
|
|
var set = layerIndexToComponentIDs[layer];
|
|
set[id] = 0;
|
|
}
|
|
else
|
|
{
|
|
var set = new ConcurrentDictionary<Guid, byte>();
|
|
layerIndexToComponentIDs[layer] = set;
|
|
set[id] = 0;
|
|
}
|
|
|
|
componentIDToLayerIndex[id] = layer;
|
|
|
|
lock (layerOrderLock)
|
|
{
|
|
if (!layerOrder.ContainsKey(layer))
|
|
{
|
|
layerOrder.Add(layer, layer);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void UnRegisterComponentWithLayer(Guid id)
|
|
{
|
|
if (componentIDToLayerIndex.ContainsKey(id))
|
|
{
|
|
var layer = componentIDToLayerIndex[id];
|
|
layerIndexToComponentIDs[layer].TryRemove(id, out _);
|
|
}
|
|
}
|
|
|
|
public void AdjustComponentLayer(Guid id, int layer)
|
|
{
|
|
UnRegisterComponentWithLayer(id);
|
|
RegisterComponentWithLayer(id, layer);
|
|
}
|
|
|
|
public IEnumerable<Guid> ComponentIDsByLayer(int layer)
|
|
{
|
|
return layerIndexToComponentIDs.ContainsKey(layer) ?
|
|
layerIndexToComponentIDs[layer].Keys :
|
|
Enumerable.Empty<Guid>();
|
|
}
|
|
|
|
public IEnumerable<GeneralRenderer> GeneralRenderersByLayer(int layer)
|
|
{
|
|
return layerIndexToGeneralRenderers.ContainsKey(layer) ?
|
|
layerIndexToGeneralRenderers[layer].Keys :
|
|
Enumerable.Empty<GeneralRenderer>();
|
|
}
|
|
}
|
|
}
|