Pull down IPathedResourceManager
. Reconstruct resource browser.
This commit is contained in:
83
Assets/Cryville/Crtr/Browsing/UI/PathedResourceBrowser.cs
Normal file
83
Assets/Cryville/Crtr/Browsing/UI/PathedResourceBrowser.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using Cryville.Common.Unity;
|
||||
using Cryville.Common.Unity.UI;
|
||||
using Cryville.Crtr.UI;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cryville.Crtr.Browsing.UI {
|
||||
public class PathedResourceBrowser : ResourceBrowser {
|
||||
public IPathedResourceManager<ChartDetail> ResourceManager;
|
||||
public ScrollableItemGrid ItemContainer;
|
||||
|
||||
readonly HashSet<int> _selectedItems = new HashSet<int>();
|
||||
readonly Dictionary<int, BrowserItem> _items = new Dictionary<int, BrowserItem>();
|
||||
|
||||
FileDialog _dialog;
|
||||
|
||||
protected virtual void Start() {
|
||||
ItemContainer.LoadItem = LoadItem;
|
||||
ResourceManager.ItemChanged += OnItemChanged;
|
||||
ResourceManager.ChangeDirectory(new string[] { "" });
|
||||
InitDialog();
|
||||
}
|
||||
|
||||
protected void InitDialog() {
|
||||
_dialog = Instantiate(Resources.Load<GameObject>("Common/FileDialog")).GetComponent<FileDialog>();
|
||||
_dialog.gameObject.SetActive(false);
|
||||
_dialog.Filter = ResourceManager.GetSupportedFormats();
|
||||
_dialog.PresetPaths = ResourceManager.GetPresetPaths();
|
||||
_dialog.OnClose += OnAddDialogClosed;
|
||||
}
|
||||
|
||||
void OnItemChanged() {
|
||||
ItemContainer.ItemCount = ResourceManager.ItemCount;
|
||||
_selectedItems.Clear();
|
||||
_items.Clear();
|
||||
}
|
||||
|
||||
private bool LoadItem(int id, GameObject obj) {
|
||||
var bi = obj.GetComponent<BrowserItem>();
|
||||
_items[id] = bi;
|
||||
try {
|
||||
var item = ResourceManager.GetItemMeta(id);
|
||||
bi.Load(id, item, _selectedItems.Contains(id));
|
||||
}
|
||||
catch (Exception) {
|
||||
bi.Load(id, new ResourceItemMeta { Name = "<color=#ff0000>Invalid resource</color>" }, _selectedItems.Contains(id));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual void OnDirectoryItemClicked(int id) {
|
||||
ResourceManager.OpenDirectory(id);
|
||||
}
|
||||
|
||||
public void OnObjectItemClicked(int id) {
|
||||
foreach (var item in _selectedItems) _items[item].OnDeselect();
|
||||
_selectedItems.Clear();
|
||||
_items[id].OnSelect();
|
||||
_selectedItems.Add(id);
|
||||
Master.ShowDetail(id, ResourceManager.GetItemDetail(id));
|
||||
}
|
||||
|
||||
public void OnPathClicked(int id) {
|
||||
ResourceManager.ReturnToDirectory(id);
|
||||
}
|
||||
|
||||
public void OnAddButtonClicked() {
|
||||
_dialog.Show();
|
||||
}
|
||||
|
||||
private void OnAddDialogClosed() {
|
||||
if (_dialog.FileName == null) return;
|
||||
if (ResourceManager.ImportItemFrom(_dialog.FileName)) {
|
||||
Popup.Create("Import succeeded");
|
||||
OnPathClicked(ResourceManager.CurrentDirectory.Length - 1);
|
||||
}
|
||||
else {
|
||||
Popup.Create("Import failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user