Add browser item meta interface.
This commit is contained in:
@@ -1,10 +1,30 @@
|
||||
using Cryville.Common;
|
||||
using Cryville.Crtr.Browsing.UI;
|
||||
using Cryville.Crtr.Extension;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cryville.Crtr.Browsing {
|
||||
public struct ChartDetail {
|
||||
public struct ChartDetail : IBrowserItemMeta {
|
||||
public AsyncDelivery<Texture2D> Cover { get; set; }
|
||||
public ChartMeta Meta { get; set; }
|
||||
|
||||
public IEnumerable<MetaProperty> Properties {
|
||||
get {
|
||||
if (Meta == null) yield break;
|
||||
yield return new MetaProperty("Name", new Dictionary<string, object> {
|
||||
{ "", string.Format("{0} - {1}", Meta.song.name, Meta.name) },
|
||||
{ "short", Meta.name },
|
||||
{ "distinct-primary", Meta.song.name },
|
||||
{ "distinct-secondary", Meta.name },
|
||||
});
|
||||
yield return new MetaProperty("Image", Cover);
|
||||
yield return new MetaProperty("Song.Author", Meta.song.author);
|
||||
yield return new MetaProperty("Author", Meta.author);
|
||||
yield return new MetaProperty("Length", TimeSpan.FromSeconds(Meta.length));
|
||||
yield return new MetaProperty("NoteCount", Meta.note_count);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -3,8 +3,8 @@ using UnityEngine;
|
||||
namespace Cryville.Crtr.Browsing.UI {
|
||||
internal abstract class BrowserItem : MonoBehaviour {
|
||||
public int? Id { get; private set; }
|
||||
protected ChartDetail meta;
|
||||
internal void Load(int id, ChartDetail item, bool selected) {
|
||||
protected IBrowserItemMeta meta;
|
||||
internal void Load(int id, IBrowserItemMeta item, bool selected) {
|
||||
OnReset();
|
||||
Id = id;
|
||||
meta = item;
|
||||
|
@@ -1,4 +1,8 @@
|
||||
using Cryville.Common;
|
||||
using Cryville.Common.Unity;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
@@ -19,6 +23,7 @@ namespace Cryville.Crtr.Browsing.UI {
|
||||
TMP_Text m_desc;
|
||||
|
||||
StateTweener _tweener;
|
||||
AsyncDelivery<Texture2D> _cover;
|
||||
|
||||
void Awake() {
|
||||
_tweener = GetComponent<StateTweener>();
|
||||
@@ -28,7 +33,7 @@ namespace Cryville.Crtr.Browsing.UI {
|
||||
OnReset();
|
||||
}
|
||||
protected override void OnReset() {
|
||||
if (meta.Cover != null) meta.Cover.Cancel();
|
||||
if (_cover != null) _cover.Cancel();
|
||||
if (m_icon.sprite != null && m_icon.sprite != m_iconPlaceholder) {
|
||||
Destroy(m_icon.sprite.texture);
|
||||
Destroy(m_icon.sprite);
|
||||
@@ -38,18 +43,43 @@ namespace Cryville.Crtr.Browsing.UI {
|
||||
|
||||
protected override void OnLoad(bool selected) {
|
||||
m_icon.sprite = m_iconPlaceholder;
|
||||
if (meta.Cover != null) meta.Cover.Destination = DisplayCover;
|
||||
if (meta.Meta != null) {
|
||||
m_title.text = meta.Meta.song.name;
|
||||
m_desc.text = meta.Meta.name;
|
||||
m_desc.text = string.Empty;
|
||||
|
||||
var basicProps = EnumerateBasicProperties().GetEnumerator();
|
||||
if (basicProps.MoveNext()) {
|
||||
m_title.text = basicProps.Current.Value;
|
||||
if (basicProps.MoveNext()) {
|
||||
m_desc.text = basicProps.Current.Value;
|
||||
}
|
||||
}
|
||||
else {
|
||||
m_title.text = "<color=#ff0000>Invalid resource</color>";
|
||||
m_desc.text = string.Empty;
|
||||
}
|
||||
|
||||
_cover = (AsyncDelivery<Texture2D>)EnumerateProperties().Where(prop => prop.Value is AsyncDelivery<Texture2D>).FirstOrDefault().Value;
|
||||
|
||||
if (_cover != null) _cover.Destination = DisplayCover;
|
||||
if (selected) _tweener.EnterState("Selected");
|
||||
}
|
||||
private void DisplayCover(bool succeeded, Texture2D tex) {
|
||||
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);
|
||||
}
|
||||
|
7
Assets/Cryville/Crtr/Browsing/UI/IBrowserItemMeta.cs
Normal file
7
Assets/Cryville/Crtr/Browsing/UI/IBrowserItemMeta.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Cryville.Crtr.Browsing.UI {
|
||||
public interface IBrowserItemMeta {
|
||||
IEnumerable<MetaProperty> Properties { get; }
|
||||
}
|
||||
}
|
11
Assets/Cryville/Crtr/Browsing/UI/IBrowserItemMeta.cs.meta
Normal file
11
Assets/Cryville/Crtr/Browsing/UI/IBrowserItemMeta.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 026e696a02e3d674bad92bf80aa77278
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
20
Assets/Cryville/Crtr/Browsing/UI/MetaProperty.cs
Normal file
20
Assets/Cryville/Crtr/Browsing/UI/MetaProperty.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Cryville.Crtr.Browsing.UI {
|
||||
public class MetaProperty {
|
||||
public string Name { get; set; }
|
||||
public IReadOnlyDictionary<string, object> Values { get; set; }
|
||||
public object MainValue { get { return Values[""]; } }
|
||||
public MetaProperty(string name, IReadOnlyDictionary<string, object> values) {
|
||||
if (values.Count == 0) throw new ArgumentException("Value is empty.");
|
||||
if (!values.ContainsKey("")) throw new ArgumentException("Main value is missing.");
|
||||
Name = name;
|
||||
Values = values;
|
||||
}
|
||||
public MetaProperty(string name, object value) {
|
||||
Name = name;
|
||||
Values = new Dictionary<string, object> { { "", value } };
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Cryville/Crtr/Browsing/UI/MetaProperty.cs.meta
Normal file
11
Assets/Cryville/Crtr/Browsing/UI/MetaProperty.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c8ccfc6a81a7c774d8df59df0ddd4c1a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -88,7 +88,7 @@ namespace Cryville.Crtr.Browsing.UI {
|
||||
bi.Load(id, item, _selectedItems.Contains(id));
|
||||
}
|
||||
catch (Exception) {
|
||||
bi.Load(id, default(ChartDetail), _selectedItems.Contains(id));
|
||||
bi.Load(id, null, _selectedItems.Contains(id));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user