Reloads actions on actions changed.

This commit is contained in:
2023-11-30 15:08:00 +08:00
parent 59339d5bc6
commit 048a9f54fb
5 changed files with 25 additions and 2 deletions

View File

@@ -6,6 +6,8 @@ namespace Cryville.Crtr.Browsing.Actions {
public class ActionManager {
readonly Dictionary<Type, List<IResourceAction>> _actions = new Dictionary<Type, List<IResourceAction>>();
public event Action Changed;
class ActionPriorityComparer : IComparer<IResourceAction> {
public static readonly ActionPriorityComparer Instance = new ActionPriorityComparer();
public int Compare(IResourceAction x, IResourceAction y) {
@@ -21,6 +23,7 @@ namespace Cryville.Crtr.Browsing.Actions {
int index = actions.BinarySearch(action, ActionPriorityComparer.Instance);
if (index < 0) index = ~index;
actions.Insert(index, action);
Changed?.Invoke();
}
public void Register(IResourceAction action) {
Register(typeof(object), action);
@@ -33,6 +36,7 @@ namespace Cryville.Crtr.Browsing.Actions {
List<IResourceAction> actions;
if (!_actions.TryGetValue(type, out actions)) return;
actions.Remove(action);
Changed?.Invoke();
}
public void Unregister(IResourceAction action) {
Unregister(typeof(object), action);

View File

@@ -36,7 +36,7 @@ namespace Cryville.Crtr.Browsing.UI {
UnregisterManager();
}
void UnregisterManager() {
Master.Actions.Unregister(_importAction);
if (_importAction != null) Master.Actions.Unregister(_importAction);
_manager.ItemChanged -= OnItemChanged;
_manager.DirectoryChanged -= OnDirectoryChanged;
}
@@ -111,9 +111,19 @@ namespace Cryville.Crtr.Browsing.UI {
_selectedItems.Add(id);
var res = _manager[id];
m_detailPanel.Load(res);
m_actionBar.Load(this, Master.Actions.GetActions(res.GetType()).Except(_importActionArray));
if (_selectedItems.Count == 1) {
LoadActions(res);
}
}
}
internal override void OnActionsChanged() {
if (_selectedItems.Count == 1) {
LoadActions(_manager[_selectedItems.Single()]);
}
}
void LoadActions(IResourceMeta res) {
m_actionBar.Load(this, Master.Actions.GetActions(res.GetType()).Except(_importActionArray));
}
public void OnPathClicked(int index) {
_manager.ReturnToDirectory(index);

View File

@@ -15,5 +15,6 @@ namespace Cryville.Crtr.Browsing.UI {
public abstract void OnItemClicked(int index);
public abstract void InvokeAction(IResourceAction action);
internal abstract void OnActionsChanged();
}
}

View File

@@ -28,6 +28,7 @@ namespace Cryville.Crtr.Browsing.UI {
base.Awake();
Actions = new ActionManager();
Actions.Changed += OnActionsChanged;
Actions.Register(new PlayChartAction());
OnTabClicked(AddPathedBrowserTab("Local", new LegacyResourceManager(Settings.Default.GameDataPath)));
@@ -64,6 +65,12 @@ namespace Cryville.Crtr.Browsing.UI {
}
}
void OnActionsChanged() {
foreach (var tab in _tabs) {
tab.Value.OnActionsChanged();
}
}
public bool Back() {
return true; // TODO
}

View File

@@ -9,5 +9,6 @@ namespace Cryville.Crtr.Browsing.UI {
}
public override void OnItemClicked(int index) { }
public override void InvokeAction(IResourceAction action) { }
internal override void OnActionsChanged() { }
}
}