using HMUI;
using SongBrowserPlugin.DataAccess;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using VRUI;
using Image = UnityEngine.UI.Image;
namespace SongBrowserPlugin.UI
{
public static class UIBuilder
{
///
/// Create an empty BeatSaber VRUI view controller.
///
///
///
///
public static T CreateViewController(string name) where T : VRUIViewController
{
T vc = new GameObject(name).AddComponent();
vc.rectTransform.anchorMin = new Vector2(0f, 0f);
vc.rectTransform.anchorMax = new Vector2(1f, 1f);
vc.rectTransform.sizeDelta = new Vector2(0f, 0f);
vc.rectTransform.anchoredPosition = new Vector2(0f, 0f);
return vc;
}
///
/// Create empty FlowCoordinator
///
///
///
///
public static T CreateFlowCoordinator(string name) where T : FlowCoordinator
{
T vc = new GameObject(name).AddComponent();
return vc;
}
///
/// Helper, create a UI button from template name.
///
///
///
///
static public Button CreateUIButton(RectTransform parent, String buttonTemplateName)
{
Button b = Resources.FindObjectsOfTypeAll().First(x => (x.name == buttonTemplateName));
return CreateUIButton(parent, b);
}
///
/// Clone a Unity Button into a Button we control.
///
///
///
///
///
static public Button CreateUIButton(RectTransform parent, Button buttonTemplate)
{
Button btn = UnityEngine.Object.Instantiate(buttonTemplate, parent, false);
UnityEngine.Object.DestroyImmediate(btn.GetComponent());
btn.onClick = new Button.ButtonClickedEvent();
btn.name = "CustomUIButton";
return btn;
}
///
/// Very generic helper create button method.
///
///
///
///
///
///
///
///
///
static public Button CreateButton(RectTransform parent, Button buttonTemplate, String buttonText, float fontSize, float x, float y, float w, float h)
{
Button newButton = UIBuilder.CreateUIButton(parent, buttonTemplate);
newButton.interactable = true;
(newButton.transform as RectTransform).anchoredPosition = new Vector2(x, y);
(newButton.transform as RectTransform).sizeDelta = new Vector2(w, h);
UIBuilder.SetButtonText(ref newButton, buttonText);
UIBuilder.SetButtonIconEnabled(ref newButton, false);
UIBuilder.SetButtonTextSize(ref newButton, fontSize);
return newButton;
}
///
/// Generic create sort button.
///
///
///
///
///
///
///
///
///
///
public static SongSortButton CreateSortButton(RectTransform parent, Button buttonTemplate, Sprite iconSprite, string buttonText, float fontSize, float x, float y, float w, float h, SongSortMode sortMode, System.Action onClickEvent)
{
SongSortButton sortButton = new SongSortButton();
Button newButton = UIBuilder.CreateUIButton(parent, buttonTemplate);
newButton.interactable = true;
(newButton.transform as RectTransform).anchoredPosition = new Vector2(x, y);
(newButton.transform as RectTransform).sizeDelta = new Vector2(w, h);
UIBuilder.SetButtonText(ref newButton, buttonText);
UIBuilder.SetButtonIconEnabled(ref newButton, false);
UIBuilder.SetButtonIcon(ref newButton, iconSprite);
UIBuilder.SetButtonTextSize(ref newButton, fontSize);
newButton.onClick.RemoveAllListeners();
newButton.onClick.AddListener(delegate ()
{
onClickEvent(sortMode);
});
sortButton.Button = newButton;
sortButton.SortMode = sortMode;
return sortButton;
}
///
/// Create a page up/down button.
///
///
///
///
///
///
///
///
///
///
///
///
public static Button CreateIconButton(RectTransform parent, Button buttonTemplate, Sprite iconSprite, Vector2 pos, Vector2 size, Vector2 iconPos, Vector2 iconSize, Vector2 iconScale, float iconRotation)
{
Button newButton = UIBuilder.CreateUIButton(parent, buttonTemplate);
newButton.interactable = true;
(newButton.transform as RectTransform).anchoredPosition = new Vector2(pos.x, pos.y);
(newButton.transform as RectTransform).sizeDelta = new Vector2(size.x, size.y);
RectTransform iconTransform = newButton.GetComponentsInChildren(true).First(c => c.name == "Icon");
iconTransform.gameObject.SetActive(true);
HorizontalLayoutGroup hgroup = iconTransform.parent.GetComponent();
UnityEngine.Object.Destroy(hgroup);
iconTransform.anchoredPosition = new Vector2(iconPos.x, iconPos.y);
iconTransform.sizeDelta = new Vector2(iconSize.x, iconSize.y);
iconTransform.localScale = new Vector2(iconScale.x, iconScale.y);
iconTransform.Rotate(0, 0, iconRotation);
UnityEngine.Object.Destroy(newButton.GetComponentsInChildren(true).First(c => c.name == "Text").gameObject);
UIBuilder.SetButtonBorder(ref newButton, Color.clear);
UIBuilder.SetButtonIcon(ref newButton, iconSprite);
return newButton;
}
///
/// Create a beat saber dismiss button.
///
///
///
public static Button CreateBackButton(RectTransform parent)
{
Button dismissButton = CreateUIButton(parent, "BackArrowButton");
UnityEngine.Object.DestroyImmediate(dismissButton.GetComponent());
dismissButton.onClick = new Button.ButtonClickedEvent();
dismissButton.name = "CustomUIButton";
return dismissButton;
}
///
/// Generate TextMesh.
///
///
///
///
///
static public TextMeshProUGUI CreateText(RectTransform parent, string text, Vector2 position, Vector2 width)
{
TextMeshProUGUI textMesh = new GameObject("TextMeshProUGUI_GO").AddComponent();
textMesh.rectTransform.SetParent(parent, false);
textMesh.text = text;
textMesh.fontSize = 4;
textMesh.color = Color.white;
textMesh.font = Resources.Load("Teko-Medium SDF No Glow");
textMesh.rectTransform.anchorMin = new Vector2(0.5f, 1f);
textMesh.rectTransform.anchorMax = new Vector2(0.5f, 1f);
//textMesh.rectTransform.sizeDelta = size;
textMesh.rectTransform.sizeDelta = width;
textMesh.rectTransform.anchoredPosition = position;
return textMesh;
}
///
/// Adjust a Button text.
///
///
///
static public void SetButtonText(ref Button button, string text)
{
if (button.GetComponentInChildren() != null)
{
button.GetComponentInChildren().text = text;
}
}
///
/// Adjust button text size.
///
///
///
static public void SetButtonTextSize(ref Button button, float fontSize)
{
if (button.GetComponentInChildren() != null)
{
button.GetComponentInChildren().fontSize = fontSize;
}
}
///
/// Set a button icon.
///
///
///
static public void SetButtonIcon(ref Button button, Sprite icon)
{
if (button.GetComponentsInChildren().Count() > 1)
{
button.GetComponentsInChildren().First(x => x.name == "Icon").sprite = icon;
}
}
///
/// Disable a button icon.
///
///
///
static public void SetButtonIconEnabled(ref Button button, bool enabled)
{
if (button.GetComponentsInChildren().Count() > 1)
{
button.GetComponentsInChildren()[1].enabled = enabled;
}
}
///
/// Adjust button background color.
///
///
///
static public void SetButtonBackground(ref Button button, Sprite background)
{
if (button.GetComponentsInChildren().Any())
{
button.GetComponentsInChildren()[0].sprite = background;
}
}
///
/// Adjust button border.
///
///
///
static public void SetButtonBorder(ref Button button, Color color)
{
if (button.GetComponentsInChildren().Any())
{
button.GetComponentsInChildren()[0].color = color;
}
}
}
}