Implement action manager.
This commit is contained in:
8
Assets/Cryville/Crtr/Browsing/Actions.meta
Normal file
8
Assets/Cryville/Crtr/Browsing/Actions.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8da681baa9e0364429f4ff80b3cdcf79
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
47
Assets/Cryville/Crtr/Browsing/Actions/ActionManager.cs
Normal file
47
Assets/Cryville/Crtr/Browsing/Actions/ActionManager.cs
Normal 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];
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Cryville/Crtr/Browsing/Actions/ActionManager.cs.meta
Normal file
11
Assets/Cryville/Crtr/Browsing/Actions/ActionManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 47b543be94fb27542a312561b5cf2b71
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user