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

@@ -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);