Logic cleanup for file dialog.

This commit is contained in:
2022-12-16 17:48:43 +08:00
parent b0c70bc62e
commit e6d94f248c
3 changed files with 35 additions and 47 deletions

View File

@@ -11,13 +11,13 @@ namespace Cryville.Common.Unity {
Transform dirs; Transform dirs;
Transform files; Transform files;
public Action Callback { private get; set; } public event Action OnClose;
#if UNITY_ANDROID && !UNITY_EDITOR_WIN #if UNITY_ANDROID && !UNITY_EDITOR_WIN
string androidStorage = ""; string androidStorage = "";
#endif #endif
string fileName = ""; string fileName = null;
public string FileName { public string FileName {
get { return fileName; } get { return fileName; }
} }
@@ -27,8 +27,10 @@ namespace Cryville.Common.Unity {
set { m_filter = value; } set { m_filter = value; }
} }
#pragma warning disable IDE0051 GameObject prefabButton;
void Start() { void Start() {
prefabButton = Resources.Load<GameObject>("Common/Button");
panel = gameObject.transform.Find("Panel"); panel = gameObject.transform.Find("Panel");
title = panel.Find("Title/Text"); title = panel.Find("Title/Text");
drives = panel.Find("Drives/DrivesInner"); drives = panel.Find("Drives/DrivesInner");
@@ -38,8 +40,8 @@ namespace Cryville.Common.Unity {
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
CurrentDirectory = new DirectoryInfo(Directory.GetCurrentDirectory()); CurrentDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
#elif UNITY_ANDROID #elif UNITY_ANDROID
using (AndroidJavaClass ajc=new AndroidJavaClass("android.os.Environment")) using (AndroidJavaClass ajc = new AndroidJavaClass("android.os.Environment"))
using (AndroidJavaObject file=ajc.CallStatic<AndroidJavaObject>("getExternalStorageDirectory")) { using (AndroidJavaObject file = ajc.CallStatic<AndroidJavaObject>("getExternalStorageDirectory")) {
androidStorage = file.Call<string>("getAbsolutePath"); androidStorage = file.Call<string>("getAbsolutePath");
CurrentDirectory = new DirectoryInfo(androidStorage); CurrentDirectory = new DirectoryInfo(androidStorage);
} }
@@ -47,9 +49,8 @@ namespace Cryville.Common.Unity {
#error No default directory #error No default directory
#endif #endif
} }
UpdateGUI(); UpdateGUI(0);
} }
#pragma warning restore IDE0051
public void Show() { public void Show() {
fileName = null; fileName = null;
@@ -57,54 +58,40 @@ namespace Cryville.Common.Unity {
} }
public void Close() { public void Close() {
if (Callback != null) Callback.Invoke(); var ev = OnClose;
if (ev != null) ev.Invoke();
gameObject.SetActive(false); gameObject.SetActive(false);
} }
public DirectoryInfo CurrentDirectory; public DirectoryInfo CurrentDirectory;
void OnDriveChanged(string s) { void ChangeDirectory(DirectoryInfo s) {
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN CurrentDirectory = s;
CurrentDirectory = new DirectoryInfo(s); UpdateGUI(1);
#elif UNITY_ANDROID
switch (s) {
case "?storage":
CurrentDirectory = new DirectoryInfo(androidStorage);
break;
}
#else
#error No change drive logic
#endif
UpdateGUI();
} }
void OnDirectoryChanged(string s) { void SelectFile(string s) {
CurrentDirectory = new DirectoryInfo(CurrentDirectory.FullName + "/" + s);
UpdateGUI();
}
void OnFileChanged(string s) {
fileName = s; fileName = s;
Close(); Close();
} }
void UpdateGUI() { void UpdateGUI(int depth) {
title.GetComponent<Text>().text = CurrentDirectory.FullName; title.GetComponent<Text>().text = CurrentDirectory.FullName;
if (depth <= 0) {
CallHelper.Purge(drives); CallHelper.Purge(drives);
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
var dl = Directory.GetLogicalDrives(); var dl = Directory.GetLogicalDrives();
foreach (string d in dl) { foreach (string d in dl) {
GameObject btn = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Common/Button")); GameObject btn = Instantiate(prefabButton);
btn.GetComponentInChildren<Text>().text = d; btn.GetComponentInChildren<Text>().text = d;
var ts = d; btn.GetComponentInChildren<Button>().onClick.AddListener(() => ChangeDirectory(new DirectoryInfo(d)));
btn.GetComponentInChildren<Button>().onClick.AddListener(() => OnDriveChanged(ts));
btn.transform.SetParent(drives, false); btn.transform.SetParent(drives, false);
} }
#elif UNITY_ANDROID #elif UNITY_ANDROID
GameObject sbtn = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Common/Button")); GameObject sbtn = GameObject.Instantiate<GameObject>(prefabButton);
sbtn.GetComponentInChildren<Text>().text = "Storage"; sbtn.GetComponentInChildren<Text>().text = "Storage";
sbtn.GetComponentInChildren<Button>().onClick.AddListener(() => OnDriveChanged("?storage")); sbtn.GetComponentInChildren<Button>().onClick.AddListener(() => OnDriveChanged(new DirectoryInfo(androidStorage)));
sbtn.transform.SetParent(drives, false); sbtn.transform.SetParent(drives, false);
#else #else
#error No update GUI logic #error No update GUI logic
@@ -112,30 +99,31 @@ namespace Cryville.Common.Unity {
CallHelper.Purge(dirs); CallHelper.Purge(dirs);
DirectoryInfo[] subdirs = CurrentDirectory.GetDirectories(); DirectoryInfo[] subdirs = CurrentDirectory.GetDirectories();
GameObject pbtn = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Common/Button")); GameObject pbtn = Instantiate(prefabButton);
pbtn.GetComponentInChildren<Text>().text = ".."; pbtn.GetComponentInChildren<Text>().text = "..";
pbtn.GetComponentInChildren<Button>().onClick.AddListener(() => OnDirectoryChanged("..")); pbtn.GetComponentInChildren<Button>().onClick.AddListener(() => ChangeDirectory(new DirectoryInfo(Path.Combine(CurrentDirectory.FullName, ".."))));
pbtn.transform.SetParent(dirs, false); pbtn.transform.SetParent(dirs, false);
foreach (DirectoryInfo d in subdirs) { foreach (DirectoryInfo d in subdirs) {
GameObject btn = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Common/Button")); GameObject btn = Instantiate(prefabButton);
btn.GetComponentInChildren<Text>().text = d.Name; btn.GetComponentInChildren<Text>().text = d.Name;
var ts = d.Name; var ts = d;
btn.GetComponentInChildren<Button>().onClick.AddListener(() => OnDirectoryChanged(ts)); btn.GetComponentInChildren<Button>().onClick.AddListener(() => ChangeDirectory(ts));
btn.transform.SetParent(dirs, false); btn.transform.SetParent(dirs, false);
} }
CallHelper.Purge(files); CallHelper.Purge(files);
FileInfo[] fl = CurrentDirectory.GetFiles(); FileInfo[] fl = CurrentDirectory.GetFiles();
foreach (FileInfo d in fl) { foreach (FileInfo d in fl) {
foreach (string ext in m_filter) foreach (string ext in m_filter) {
if (d.Extension == ext) goto ext_matched; if (d.Extension == ext) {
continue; GameObject btn = Instantiate(prefabButton);
ext_matched:
GameObject btn = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Common/Button"));
btn.GetComponentInChildren<Text>().text = d.Name + " / " + (d.Length / 1024.0).ToString("0.0 KiB"); btn.GetComponentInChildren<Text>().text = d.Name + " / " + (d.Length / 1024.0).ToString("0.0 KiB");
var ts = d.FullName; var ts = d.FullName;
btn.GetComponentInChildren<Button>().onClick.AddListener(() => OnFileChanged(ts)); btn.GetComponentInChildren<Button>().onClick.AddListener(() => SelectFile(ts));
btn.transform.SetParent(files, false); btn.transform.SetParent(files, false);
break;
}
}
} }
} }
} }

View File

@@ -118,7 +118,7 @@ namespace Cryville.Common.Unity {
fdialog = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Common/FileDialog")).GetComponent<FileDialog>(); fdialog = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Common/FileDialog")).GetComponent<FileDialog>();
fdialog.Filter = filter; fdialog.Filter = filter;
fdialog.CurrentDirectory = ContextPath; fdialog.CurrentDirectory = ContextPath;
fdialog.Callback = () => OnFileDialogClosed(); fdialog.OnClose += OnFileDialogClosed;
} }
editor.SetDescription(PropertyName, desc); editor.SetDescription(PropertyName, desc);
UpdateValue(); UpdateValue();

View File

@@ -20,6 +20,8 @@ namespace Cryville.Crtr.Browsing {
_dialog = GameObject.Instantiate(Resources.Load<GameObject>("Common/FileDialog")).GetComponent<FileDialog>(); _dialog = GameObject.Instantiate(Resources.Load<GameObject>("Common/FileDialog")).GetComponent<FileDialog>();
_dialog.gameObject.SetActive(false); _dialog.gameObject.SetActive(false);
_dialog.Filter = ResourceManager.GetSupportedFormats();
_dialog.OnClose += OnAddDialogClosed;
} }
private bool LoadPathPart(int id, GameObject obj) { private bool LoadPathPart(int id, GameObject obj) {
@@ -57,8 +59,6 @@ namespace Cryville.Crtr.Browsing {
} }
public void OnAddButtonClicked() { public void OnAddButtonClicked() {
_dialog.Callback = OnAddDialogClosed;
_dialog.Filter = ResourceManager.GetSupportedFormats();
_dialog.Show(); _dialog.Show();
} }