137 lines
3.2 KiB
C#
137 lines
3.2 KiB
C#
using Unity.Properties;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace KitsuneCafe.UI
|
|
{
|
|
[UxmlElement]
|
|
public partial class MenuView : VisualElement
|
|
{
|
|
public const string BaseClass = "kitsunecafe__menu-view";
|
|
public const string HeaderClass = "kitsunecafe__menu-view_header";
|
|
public const string TitleClass = "kitsunecafe__menu-view_title";
|
|
public const string OptionClass = "kitsunecafe__menu-view_option";
|
|
|
|
private VisualElement headerElement;
|
|
private Label titleLabel;
|
|
|
|
private string title;
|
|
|
|
[UxmlAttribute, CreateProperty, Delayed]
|
|
public string Title
|
|
{
|
|
get => title;
|
|
set
|
|
{
|
|
if (title == value)
|
|
{
|
|
title = value;
|
|
UpdateTitle();
|
|
}
|
|
}
|
|
}
|
|
|
|
private string[] options = new string[0];
|
|
|
|
[UxmlAttribute, CreateProperty, Delayed]
|
|
public string[] Options
|
|
{
|
|
get => options;
|
|
set
|
|
{
|
|
if (options != value)
|
|
{
|
|
options = value;
|
|
UpdateOptions();
|
|
}
|
|
}
|
|
}
|
|
|
|
public MenuView()
|
|
{
|
|
AddToClassList(BaseClass);
|
|
CreateTitle();
|
|
UpdateTitle();
|
|
UpdateOptions();
|
|
}
|
|
|
|
private void CreateTitle()
|
|
{
|
|
titleLabel = new Label();
|
|
titleLabel.AddToClassList(TitleClass);
|
|
|
|
headerElement = new VisualElement();
|
|
headerElement.AddToClassList(HeaderClass);
|
|
|
|
headerElement.Add(titleLabel);
|
|
hierarchy.Add(headerElement);
|
|
}
|
|
|
|
private void UpdateTitle()
|
|
{
|
|
if (title == null)
|
|
{
|
|
headerElement.visible = false;
|
|
}
|
|
else
|
|
{
|
|
headerElement.visible = false;
|
|
titleLabel.text = title;
|
|
}
|
|
}
|
|
|
|
private VisualElement CreateOption(string option)
|
|
{
|
|
var label = new Label();
|
|
label.AddToClassList(OptionClass);
|
|
label.text = option;
|
|
return label;
|
|
}
|
|
|
|
private void UpdateOptions()
|
|
{
|
|
var count = options.Length - childCount;
|
|
|
|
if (count == 0)
|
|
{
|
|
return;
|
|
}
|
|
else if (count > 0)
|
|
{
|
|
CreateOptions(count);
|
|
}
|
|
else if (count < 0)
|
|
{
|
|
RemoveOptions(-count);
|
|
}
|
|
|
|
Rebind();
|
|
}
|
|
|
|
private void CreateOptions(int count)
|
|
{
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
var option = CreateOption(options[i]);
|
|
Add(option);
|
|
}
|
|
}
|
|
|
|
private void RemoveOptions(int count)
|
|
{
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
RemoveAt(i);
|
|
}
|
|
}
|
|
|
|
private void Rebind()
|
|
{
|
|
for (int i = 0; i < childCount; i++)
|
|
{
|
|
Label child = this[i] as Label;
|
|
child.text = options[i];
|
|
}
|
|
}
|
|
}
|
|
}
|