encompass-cs/encompass-cs/DrawLayerManager.cs

104 lines
3.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
namespace Encompass
{
internal class DrawLayerManager
{
private SortedList<int, int> layerOrder = new SortedList<int, int>();
private Dictionary<int, HashSet<Guid>> layerIndexToComponentIDs = new Dictionary<int, HashSet<Guid>>();
private Dictionary<int, HashSet<GeneralRenderer>> layerIndexToGeneralRenderers = new Dictionary<int, HashSet<GeneralRenderer>>();
private Dictionary<Guid, int> componentIDToLayerIndex = new Dictionary<Guid, int>();
public IEnumerable<int> LayerOrder { get { return layerOrder.Values; } }
public void RegisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer)
{
if (layerIndexToGeneralRenderers.ContainsKey(layer))
{
var set = layerIndexToGeneralRenderers[layer];
set.Add(renderer);
}
else
{
var set = new HashSet<GeneralRenderer>();
layerIndexToGeneralRenderers.Add(layer, set);
set.Add(renderer);
}
if (!layerOrder.ContainsKey(layer))
{
layerOrder.Add(layer, layer);
}
}
public void UnregisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer)
{
if (layerIndexToGeneralRenderers.ContainsKey(layer))
{
layerIndexToGeneralRenderers[layer].Remove(renderer);
}
}
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.Add(id);
}
else
{
var set = new HashSet<Guid>();
layerIndexToComponentIDs.Add(layer, set);
set.Add(id);
}
componentIDToLayerIndex[id] = layer;
if (!layerOrder.ContainsKey(layer))
{
layerOrder.Add(layer, layer);
}
}
public void UnRegisterComponentWithLayer(Guid id)
{
if (componentIDToLayerIndex.ContainsKey(id))
{
var layer = componentIDToLayerIndex[id];
layerIndexToComponentIDs[layer].Remove(id);
}
}
public void AdjustComponentLayer(Guid id, int layer)
{
UnRegisterComponentWithLayer(id);
RegisterComponentWithLayer(id, layer);
}
public IEnumerable<Guid> ComponentIDsByLayer(int layer)
{
return layerIndexToComponentIDs.ContainsKey(layer) ?
layerIndexToComponentIDs[layer] :
Enumerable.Empty<Guid>();
}
public IEnumerable<GeneralRenderer> GeneralRenderersByLayer(int layer)
{
return layerIndexToGeneralRenderers.ContainsKey(layer) ?
layerIndexToGeneralRenderers[layer] :
Enumerable.Empty<GeneralRenderer>();
}
}
}