encompass-cs/encompass-cs/DrawLayerManager.cs

113 lines
3.8 KiB
C#
Raw Normal View History

using System;
2019-07-29 03:25:52 +00:00
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
namespace Encompass
{
internal class DrawLayerManager
{
2019-07-29 03:25:52 +00:00
private readonly object layerOrderLock = new object();
2019-06-20 17:46:15 +00:00
private readonly SortedList<int, int> layerOrder = new SortedList<int, int>();
2019-07-29 03:25:52 +00:00
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>>();
2019-06-20 17:46:15 +00:00
2019-07-29 03:25:52 +00:00
private readonly ConcurrentDictionary<Guid, int> componentIDToLayerIndex = new ConcurrentDictionary<Guid, int>();
2019-07-29 03:25:52 +00:00
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];
2019-07-29 03:25:52 +00:00
set[renderer] = 0;
}
else
{
2019-07-29 03:25:52 +00:00
var set = new ConcurrentDictionary<GeneralRenderer, byte>();
layerIndexToGeneralRenderers[layer] = set;
set[renderer] = 0;
}
2019-07-29 03:25:52 +00:00
lock (layerOrderLock)
{
2019-07-29 03:25:52 +00:00
if (!layerOrder.ContainsKey(layer))
{
layerOrder.Add(layer, layer);
}
}
}
public void UnregisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer)
{
if (layerIndexToGeneralRenderers.ContainsKey(layer))
{
2019-07-29 03:25:52 +00:00
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];
2019-07-29 03:25:52 +00:00
set[id] = 0;
}
else
{
2019-07-29 03:25:52 +00:00
var set = new ConcurrentDictionary<Guid, byte>();
layerIndexToComponentIDs[layer] = set;
set[id] = 0;
}
componentIDToLayerIndex[id] = layer;
2019-07-29 03:25:52 +00:00
lock (layerOrderLock)
{
2019-07-29 03:25:52 +00:00
if (!layerOrder.ContainsKey(layer))
{
layerOrder.Add(layer, layer);
}
}
}
public void UnRegisterComponentWithLayer(Guid id)
{
if (componentIDToLayerIndex.ContainsKey(id))
{
var layer = componentIDToLayerIndex[id];
2019-07-29 03:25:52 +00:00
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) ?
2019-07-29 03:25:52 +00:00
layerIndexToComponentIDs[layer].Keys :
Enumerable.Empty<Guid>();
}
public IEnumerable<GeneralRenderer> GeneralRenderersByLayer(int layer)
{
return layerIndexToGeneralRenderers.ContainsKey(layer) ?
2019-07-29 03:25:52 +00:00
layerIndexToGeneralRenderers[layer].Keys :
Enumerable.Empty<GeneralRenderer>();
}
}
}