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 ResourceManager; public ScrollableItemGrid ItemContainer; readonly HashSet _selectedItems = new HashSet(); readonly Dictionary _items = new Dictionary(); FileDialog _dialog; protected virtual void Start() { ItemContainer.LoadItem = LoadItem; ResourceManager.ItemChanged += OnItemChanged; ResourceManager.ChangeDirectory(new string[] { "" }); InitDialog(); } protected void InitDialog() { _dialog = Instantiate(Resources.Load("Common/FileDialog")).GetComponent(); _dialog.gameObject.SetActive(false); _dialog.Filter = ResourceManager.GetSupportedFormats(); _dialog.PresetPaths = ResourceManager.GetPresetPaths(); _dialog.OnClose += OnAddDialogClosed; } void OnItemChanged() { ItemContainer.ItemCount = ResourceManager.Count; _selectedItems.Clear(); _items.Clear(); } private bool LoadItem(int id, GameObject obj) { var bi = obj.GetComponent(); _items[id] = bi; try { var item = ResourceManager[id]; bi.Load(id, item, _selectedItems.Contains(id)); } catch (Exception) { bi.Load(id, default(ChartDetail), _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[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(new Uri(_dialog.FileName))) { Popup.Create("Import succeeded"); OnPathClicked(ResourceManager.CurrentDirectory.Length - 1); } else { Popup.Create("Import failed"); } } } }