Files
crtr/Assets/Cryville/Crtr/Browsing/Actions/ActionManager.cs
2023-11-23 00:03:15 +08:00

48 lines
1.5 KiB
C#

using System;
using System.Collections.Generic;
namespace Cryville.Crtr.Browsing.Actions {
public class ActionManager {
readonly Dictionary<Type, List<IResourceAction>> _actions = new Dictionary<Type, List<IResourceAction>>();
class ActionPriorityComparer : IComparer<IResourceAction> {
public static readonly ActionPriorityComparer Instance = new ActionPriorityComparer();
public int Compare(IResourceAction x, IResourceAction y) {
return x.Priority.CompareTo(y.Priority);
}
}
void Register(Type type, IResourceAction action) {
List<IResourceAction> actions;
if (!_actions.TryGetValue(type, out actions)) {
_actions.Add(type, actions = new List<IResourceAction>());
}
int index = actions.BinarySearch(action, ActionPriorityComparer.Instance);
if (index < 0) index = ~index;
actions.Insert(index, action);
}
public void Register(IResourceAction action) {
Register(typeof(object), action);
}
public void Register<T>(IResourceAction<T> action) {
Register(typeof(T), action);
}
public void Unregister(Type type, IResourceAction action) {
List<IResourceAction> actions;
if (!_actions.TryGetValue(type, out actions)) return;
actions.Remove(action);
}
public void Unregister(IResourceAction action) {
Unregister(typeof(object), action);
}
public void Unregister<T>(IResourceAction<T> action) {
Unregister(typeof(T), action);
}
public IReadOnlyCollection<IResourceAction> GetActions(Type type) {
return _actions[type];
}
}
}