Implement action manager.

This commit is contained in:
2023-11-23 00:03:15 +08:00
parent ed496859cb
commit d7a38416aa
3 changed files with 66 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 8da681baa9e0364429f4ff80b3cdcf79
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,47 @@
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];
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 47b543be94fb27542a312561b5cf2b71
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: