80 lines
1.8 KiB
C#
80 lines
1.8 KiB
C#
using System.Collections.Generic;
|
|
|
|
namespace SepCore.InputModule
|
|
{
|
|
public sealed class InputContextStack
|
|
{
|
|
private readonly List<InputContextId> _contexts = new List<InputContextId>();
|
|
|
|
public int Count => _contexts.Count;
|
|
|
|
public InputContextId Current => Count > 0 ? _contexts[Count - 1] : InputContextId.None;
|
|
|
|
public IReadOnlyList<InputContextId> Items => _contexts;
|
|
|
|
public bool Push(InputContextId context)
|
|
{
|
|
if (!IsStackable(context) || Current == context)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (_contexts.Contains(context))
|
|
{
|
|
_contexts.Remove(context);
|
|
}
|
|
|
|
_contexts.Add(context);
|
|
return true;
|
|
}
|
|
|
|
public bool Pop(out InputContextId context)
|
|
{
|
|
if (Count == 0)
|
|
{
|
|
context = InputContextId.None;
|
|
return false;
|
|
}
|
|
|
|
int index = Count - 1;
|
|
context = _contexts[index];
|
|
_contexts.RemoveAt(index);
|
|
return true;
|
|
}
|
|
|
|
public bool Set(InputContextId context)
|
|
{
|
|
if (!IsStackable(context))
|
|
{
|
|
return Clear();
|
|
}
|
|
|
|
if (Count == 1 && Current == context)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
_contexts.Clear();
|
|
_contexts.Add(context);
|
|
return true;
|
|
}
|
|
|
|
public bool Clear()
|
|
{
|
|
if (Count == 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
_contexts.Clear();
|
|
return true;
|
|
}
|
|
|
|
private static bool IsStackable(InputContextId context)
|
|
{
|
|
return context != InputContextId.None && context != InputContextId.Global;
|
|
}
|
|
}
|
|
}
|
|
|