Add type constraints for resource managers. Generalize detail panel.

This commit is contained in:
2023-11-29 13:52:34 +08:00
parent a471272c52
commit 111c500f3b
7 changed files with 88 additions and 45 deletions

View File

@@ -5,7 +5,7 @@ namespace Cryville.Crtr.Browsing {
public interface IResourceManager {
int Count { get; }
object this[int index] { get; }
IResourceMeta this[int index] { get; }
Uri GetItemUri(int index);
event Action ItemChanged;
@@ -16,10 +16,10 @@ namespace Cryville.Crtr.Browsing {
void Activate();
void Deactivate();
}
public interface IResourceManager<T> : IResourceManager {
public interface IResourceManager<out T> : IResourceManager where T : IResourceMeta {
new T this[int index] { get; }
}
public interface IPathedResourceManager<T> : IResourceManager<T> {
public interface IPathedResourceManager<out T> : IResourceManager<T> where T : IResourceMeta {
event Action DirectoryChanged;
IList<string> CurrentDirectory { get; }
void ChangeDirectory(IEnumerable<string> dir);

View File

@@ -106,7 +106,7 @@ namespace Cryville.Crtr.Browsing {
};
}
}
object IResourceManager.this[int index] { get { return this[index]; } }
IResourceMeta IResourceManager.this[int index] { get { return this[index]; } }
public Uri GetItemUri(int id) {
var item = _items[id];

View File

@@ -1,7 +1,5 @@
using Cryville.Common;
using Cryville.Common.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
@@ -45,40 +43,22 @@ namespace Cryville.Crtr.Browsing.UI {
m_icon.sprite = m_iconPlaceholder;
m_desc.text = string.Empty;
var basicProps = EnumerateBasicProperties().GetEnumerator();
var basicProps = meta.EnumerateBasicProperties(true).GetEnumerator();
if (basicProps.MoveNext()) {
m_title.text = basicProps.Current.Value;
m_title.text = basicProps.Current.Value.ToString();
if (basicProps.MoveNext()) {
m_desc.text = basicProps.Current.Value;
m_desc.text = basicProps.Current.Value.ToString();
}
}
else {
m_title.text = "<color=#ff0000>Invalid resource</color>";
}
_cover = (AsyncDelivery<Texture2D>)EnumerateProperties().Where(prop => prop.Value is AsyncDelivery<Texture2D>).FirstOrDefault().Value;
_cover = meta.EnumerateProperties<AsyncDelivery<Texture2D>>().FirstOrDefault().Value;
if (_cover != null) _cover.Destination = DisplayCover;
if (selected) _tweener.EnterState("Selected");
}
IEnumerable<KeyValuePair<string, string>> EnumerateBasicProperties() {
foreach (var prop in EnumerateProperties()) {
if (prop.Value is string || prop.Value is IFormattable) {
yield return new KeyValuePair<string, string>(prop.Key, prop.Value.ToString());
}
}
}
IEnumerable<KeyValuePair<string, object>> EnumerateProperties() {
foreach (var prop in meta.Properties) {
if (prop.Values.ContainsKey("distinct-primary")) {
yield return new KeyValuePair<string, object>(prop.Name, prop.Values["distinct-primary"]);
if (prop.Values.ContainsKey("distinct-secondary")) {
yield return new KeyValuePair<string, object>(prop.Name, prop.Values["distinct-secondary"]);
}
}
else yield return new KeyValuePair<string, object>(prop.Name, prop.MainValue);
}
}
void DisplayCover(bool succeeded, Texture2D tex) {
if (succeeded) {
m_icon.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero, 160, 0, SpriteMeshType.FullRect);

View File

@@ -1,5 +1,5 @@
using Cryville.Common;
using System;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
@@ -19,27 +19,42 @@ namespace Cryville.Crtr.Browsing.UI {
[SerializeField]
TMP_Text m_desc;
ChartDetail _data;
AsyncDelivery<Texture2D> _image;
void OnDestroy() {
DestroyDynamicResources();
}
public void Load(ChartDetail data) {
public void Load(IResourceMeta meta) {
m_placeholder.SetActive(false);
m_content.SetActive(true);
DestroyDynamicResources();
_data = data;
m_cover.sprite = m_coverPlaceholder;
if (data.Cover != null) data.Cover.Destination = DisplayCover;
var meta = data.Meta;
if (meta != null) {
m_title.text = string.Format("{0}\n{1}", meta.song.name, meta.name);
m_desc.text = string.Format(
"Music artist: {0}\nCharter: {1}\nLength: {2}\nNote Count: {3}",
meta.song.author, meta.author,
TimeSpan.FromSeconds(meta.length).ToString(3), meta.note_count
);
_image = meta.EnumerateProperties<AsyncDelivery<Texture2D>>().FirstOrDefault().Value;
if (_image != null) _image.Destination = DisplayCover;
var nameProp = meta.Properties.FirstOrDefault(prop => prop.Name == "Name");
if (nameProp != null) {
var names = nameProp.EnumerateProperty(true).GetEnumerator();
if (names.MoveNext()) {
m_title.text = names.Current.Value.ToString();
if (names.MoveNext()) {
m_title.text += "\n" + names.Current.Value.ToString();
}
}
else {
m_title.text = "";
}
m_desc.text = "";
var basicProps = meta.EnumerateBasicProperties().Where(prop => prop.Key != "Name").GetEnumerator();
for (int i = 0; i < 4 && basicProps.MoveNext(); i++) {
var cur = basicProps.Current;
var str = string.Format("{0}: {1}", cur.Key, cur.Value);
if (i == 0) m_desc.text = str;
else m_desc.text += "\n" + str;
}
}
else {
m_title.text = "<color=#ff0000>Invalid resource</color>";
@@ -58,7 +73,7 @@ namespace Cryville.Crtr.Browsing.UI {
}
void DestroyDynamicResources() {
if (_data.Cover != null) _data.Cover.Cancel();
if (_image != null) _image.Cancel();
if (m_cover.sprite != null && m_cover.sprite != m_coverPlaceholder) {
Destroy(m_cover.sprite.texture);
Destroy(m_cover.sprite);

View File

@@ -0,0 +1,37 @@
using System.Collections.Generic;
using System;
using System.Linq;
namespace Cryville.Crtr.Browsing.UI {
internal static class MetaPropertyExtensions {
public static IEnumerable<KeyValuePair<string, object>> EnumerateProperty(this MetaProperty prop, bool shortDistinct = false) {
if (prop.Values.ContainsKey("distinct-primary") && shortDistinct) {
yield return new KeyValuePair<string, object>(prop.Name, prop.Values["distinct-primary"]);
if (prop.Values.ContainsKey("distinct-secondary")) {
yield return new KeyValuePair<string, object>(prop.Name, prop.Values["distinct-secondary"]);
if (prop.Values.ContainsKey("distinct-tertiary")) {
yield return new KeyValuePair<string, object>(prop.Name, prop.Values["distinct-tertiary"]);
}
}
}
else yield return new KeyValuePair<string, object>(prop.Name, prop.MainValue);
}
public static IEnumerable<KeyValuePair<string, object>> EnumerateProperties(this IResourceMeta meta, bool shortDistinct = false) {
foreach (var prop in meta.Properties) {
foreach (var item in EnumerateProperty(prop, shortDistinct)) yield return item;
}
}
public static IEnumerable<KeyValuePair<string, object>> EnumerateBasicProperties(this IResourceMeta meta, bool shortDistinct = false) {
foreach (var prop in EnumerateProperties(meta, shortDistinct)) {
if (prop.Value is string || prop.Value is IFormattable) {
yield return new KeyValuePair<string, object>(prop.Key, prop.Value);
}
}
}
public static IEnumerable<KeyValuePair<string, T>> EnumerateProperties<T>(this IResourceMeta meta, bool shortDistinct = false) {
return EnumerateProperties(meta, shortDistinct)
.Where(prop => prop.Value is T)
.Select(prop => new KeyValuePair<string, T>(prop.Key, (T)prop.Value));
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5285db71ff9d63b41b0f1c18614b14ae
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -22,7 +22,7 @@ namespace Cryville.Crtr.Browsing.UI {
[SerializeField]
ActionBar m_actionBar;
IPathedResourceManager<ChartDetail> _manager;
IPathedResourceManager<IResourceMeta> _manager;
IResourceAction _importAction;
readonly HashSet<int> _selectedItems = new HashSet<int>();
@@ -47,7 +47,7 @@ namespace Cryville.Crtr.Browsing.UI {
_manager.Deactivate();
}
public void Init(ResourceBrowserMaster master, IPathedResourceManager<ChartDetail> manager) {
public void Init(ResourceBrowserMaster master, IPathedResourceManager<IResourceMeta> manager) {
if (_manager != null) {
UnregisterManager();
}
@@ -103,7 +103,7 @@ namespace Cryville.Crtr.Browsing.UI {
_selectedItems.Clear();
_items[id].OnSelect();
_selectedItems.Add(id);
ChartDetail res = _manager[id];
var res = _manager[id];
m_detailPanel.Load(res);
m_actionBar.Load(this, Master.Actions.GetActions(res.GetType()));
}