Bladeren bron

Add dependency to BSML (using some pieces now).
Partially Fix button creation code (does not react to click events).
Clang 7.0.
Removed unused functions.

Halsafar 4 jaren geleden
bovenliggende
commit
fd49b7a301

+ 36 - 64
SongBrowserPlugin/Internals/BeatSaberExtensions.cs

@@ -1,10 +1,12 @@
 using BS_Utils.Utilities;
 using BS_Utils.Utilities;
+using HMUI;
 using System.Linq;
 using System.Linq;
 using TMPro;
 using TMPro;
 using UnityEngine;
 using UnityEngine;
 using UnityEngine.Events;
 using UnityEngine.Events;
 using UnityEngine.UI;
 using UnityEngine.UI;
 using Image = UnityEngine.UI.Image;
 using Image = UnityEngine.UI.Image;
+using Logger = SongBrowser.Logging.Logger;
 
 
 namespace SongBrowser.Internals
 namespace SongBrowser.Internals
 {
 {
@@ -13,12 +15,11 @@ namespace SongBrowser.Internals
         #region Button Extensions
         #region Button Extensions
         public static void SetButtonText(this Button _button, string _text)
         public static void SetButtonText(this Button _button, string _text)
         {
         {
-            Polyglot.LocalizedTextMeshProUGUI localizer = _button.GetComponentInChildren<Polyglot.LocalizedTextMeshProUGUI>();
-            if (localizer != null)
-                GameObject.Destroy(localizer);
-            TextMeshProUGUI tmpUgui = _button.GetComponentInChildren<TextMeshProUGUI>();
-            if (tmpUgui != null)
-                tmpUgui.text = _text;
+            HMUI.CurvedTextMeshPro textMesh = _button.GetComponentInChildren<HMUI.CurvedTextMeshPro>();
+            if (textMesh != null)
+            {
+                textMesh.SetText(_text);
+            }
         }
         }
 
 
         public static void SetButtonTextSize(this Button _button, float _fontSize)
         public static void SetButtonTextSize(this Button _button, float _fontSize)
@@ -39,81 +40,52 @@ namespace SongBrowser.Internals
 
 
         public static void SetButtonIcon(this Button _button, Sprite _icon)
         public static void SetButtonIcon(this Button _button, Sprite _icon)
         {
         {
-            if (_button.GetComponentsInChildren<Image>().Count() > 1)
-                _button.GetComponentsInChildren<Image>().First(x => x.name == "Icon").sprite = _icon;
+            _button.GetComponentsInChildren<ImageView>().First().sprite = _icon;
         }
         }
 
 
-        public static void SetButtonBackground(this Button _button, Sprite _background)
+        public static void SetButtonBackgroundActive(this Button parent, bool active)
         {
         {
-            if (_button.GetComponentsInChildren<Image>().Count() > 0)
-                _button.GetComponentsInChildren<Image>()[0].sprite = _background;
+            HMUI.ImageView img = parent.GetComponentsInChildren<HMUI.ImageView>().Last(x => x.name == "BG");
+            if (img != null)
+            {
+                img.gameObject.SetActive(active);
+            }
         }
         }
-        #endregion
 
 
-        #region ViewController Extensions
-
-        public static Button CreateUIButton(this HMUI.ViewController parent, string buttonTemplate)
+        public static void SetButtonBorderActive(this Button parent, bool active)
         {
         {
-            Button btn = BeatSaberUI.CreateUIButton(parent.rectTransform, buttonTemplate);
-            return btn;
+            HMUI.ImageView img = parent.GetComponentsInChildren<HMUI.ImageView>().FirstOrDefault(x => x.name == "Border");
+            if (img != null)
+            {
+                img.gameObject.SetActive(active);
+            }
         }
         }
 
 
-        public static Button CreateUIButton(this HMUI.ViewController parent, string buttonTemplate, Vector2 anchoredPosition, Vector2 sizeDelta, UnityAction onClick = null, string buttonText = "BUTTON", Sprite icon = null)
+        public static void SetButtonBorder(this Button button, Color color)
         {
         {
-            Button btn = BeatSaberUI.CreateUIButton(parent.rectTransform, buttonTemplate, anchoredPosition, sizeDelta, onClick, buttonText, icon);
-            return btn;
+            HMUI.ImageView img = button.GetComponentsInChildren<HMUI.ImageView>().FirstOrDefault(x => x.name == "Border");
+            if (img != null)
+            {
+                img.color0 = color;
+                img.color1 = color;
+                img.color = color;
+                img.fillMethod = Image.FillMethod.Horizontal;
+                img.SetAllDirty();
+            }
         }
         }
+        #endregion
 
 
-        public static Button CreateUIButton(this HMUI.ViewController parent, string buttonTemplate, Vector2 anchoredPosition, UnityAction onClick = null, string buttonText = "BUTTON", Sprite icon = null)
+        #region ViewController Extensions
+        public static Button CreateUIButton(this HMUI.ViewController parent, string name, string buttonTemplate, Vector2 anchoredPosition, Vector2 sizeDelta, UnityAction onClick = null, string buttonText = "BUTTON")
         {
         {
-            Button btn = BeatSaberUI.CreateUIButton(parent.rectTransform, buttonTemplate, anchoredPosition, onClick, buttonText, icon);
+            Button btn = BeatSaberUI.CreateUIButton(name, parent.rectTransform, buttonTemplate, anchoredPosition, sizeDelta, onClick, buttonText);
             return btn;
             return btn;
         }
         }
-
-        public static Button CreateUIButton(this HMUI.ViewController parent, string buttonTemplate, UnityAction onClick = null, string buttonText = "BUTTON", Sprite icon = null)
+        public static Button CreateIconButton(this HMUI.ViewController parent, string name, string buttonTemplate, Vector2 anchoredPosition, Vector2 sizeDelta, UnityAction onClick, Sprite icon)
         {
         {
-            Button btn = BeatSaberUI.CreateUIButton(parent.rectTransform, buttonTemplate, onClick, buttonText, icon);
+            Button btn = BeatSaberUI.CreateIconButton(name, parent.rectTransform, buttonTemplate, anchoredPosition, sizeDelta, onClick, icon);
             return btn;
             return btn;
         }
         }
-
-        public static Button CreateBackButton(this HMUI.ViewController parent)
-        {
-            Button btn = BeatSaberUI.CreateBackButton(parent.rectTransform);
-            return btn;
-        }
-
-        /*public static GameObject CreateLoadingSpinner(this HMUI.ViewController parent)
-        {
-            GameObject loadingSpinner = BeatSaberUI.CreateLoadingSpinner(parent.rectTransform);
-            return loadingSpinner;
-        }*/
-
-        public static TextMeshProUGUI CreateText(this HMUI.ViewController parent, string text, Vector2 anchoredPosition, Vector2 sizeDelta)
-        {
-            TextMeshProUGUI textMesh = BeatSaberUI.CreateText(parent.rectTransform, text, anchoredPosition, sizeDelta);
-            return textMesh;
-        }
-
-        public static TextMeshProUGUI CreateText(this HMUI.ViewController parent, string text, Vector2 anchoredPosition)
-        {
-            TextMeshProUGUI textMesh = BeatSaberUI.CreateText(parent.rectTransform, text, anchoredPosition);
-            return textMesh;
-        }
-
-        public static void SetText(this LevelListTableCell cell, string text)
-        {
-            cell.GetPrivateField<TextMeshProUGUI>("_songNameText").text = text;
-        }
-
-        public static void SetSubText(this LevelListTableCell cell, string subtext)
-        {
-            cell.GetPrivateField<TextMeshProUGUI>("_authorText").text = subtext;
-        }
-
-        public static void SetIcon(this LevelListTableCell cell, Sprite icon)
-        {
-            cell.GetPrivateField<UnityEngine.UI.Image>("_coverImage").sprite = icon;
-        }
         #endregion
         #endregion
     }
     }
 }
 }

+ 62 - 135
SongBrowserPlugin/Internals/BeatSaberUI.cs

@@ -35,74 +35,49 @@ namespace SongBrowser.Internals
         }
         }
 
 
         /// <summary>
         /// <summary>
-        /// Clone a Unity Button into a Button we control.
-        /// </summary>
-        /// <param name="parent"></param>
-        /// <param name="buttonTemplate"></param>
-        /// <param name="buttonInstance"></param>
-        /// <returns></returns>
-        static public Button CreateUIButton(RectTransform parent, Button buttonTemplate)
-        {
-            Button btn = UnityEngine.Object.Instantiate(buttonTemplate, parent, false);
-            UnityEngine.Object.DestroyImmediate(btn.GetComponent<SignalOnUIButtonClick>());
-            btn.onClick = new Button.ButtonClickedEvent();
-            btn.name = "CustomUIButton";
-
-            return btn;
-        }
-
-        /// <summary>
         /// Create an icon button, simple.
         /// Create an icon button, simple.
         /// </summary>
         /// </summary>
         /// <param name="parent"></param>
         /// <param name="parent"></param>
         /// <param name="buttonTemplate"></param>
         /// <param name="buttonTemplate"></param>
         /// <param name="iconSprite"></param>
         /// <param name="iconSprite"></param>
         /// <returns></returns>
         /// <returns></returns>
-        public static Button CreateIconButton(RectTransform parent, Button buttonTemplate, Sprite iconSprite)
+        public static Button CreateIconButton(String name, RectTransform parent, String buttonTemplate, Vector2 anchoredPosition, Vector2 sizeDelta, UnityAction onClick, Sprite icon)
         {
         {
-            Button newButton = BeatSaberUI.CreateUIButton(parent, buttonTemplate);
-            newButton.interactable = true;
-
-            RectTransform textRect = newButton.GetComponentsInChildren<RectTransform>(true).FirstOrDefault(c => c.name == "Text");
-            if (textRect != null)
+            Logger.Debug("CreateIconButton({0}, {1}, {2}, {3}, {4}", name, parent, buttonTemplate, anchoredPosition, sizeDelta);
+            Button btn = UnityEngine.Object.Instantiate(Resources.FindObjectsOfTypeAll<Button>().Last(x => (x.name == buttonTemplate)), parent, false);
+            btn.name = name;
+            btn.interactable = true;
+
+            UnityEngine.Object.Destroy(btn.GetComponent<HoverHint>());
+            GameObject.Destroy(btn.GetComponent<LocalizedHoverHint>());
+            btn.gameObject.AddComponent<BeatSaberMarkupLanguage.Components.ExternalComponents>().components.Add(btn.GetComponentsInChildren<LayoutGroup>().First(x => x.name == "Content"));
+
+            Transform contentTransform = btn.transform.Find("Content");
+            GameObject.Destroy(contentTransform.Find("Text").gameObject);
+            Image iconImage = new GameObject("Icon").AddComponent<ImageView>();
+            iconImage.material = BeatSaberMarkupLanguage.Utilities.ImageResources.NoGlowMat;
+            iconImage.rectTransform.SetParent(contentTransform, false);
+            iconImage.rectTransform.sizeDelta = new Vector2(10f, 10f);
+            iconImage.sprite = icon;
+            iconImage.preserveAspect = true;
+            if (iconImage != null)
             {
             {
-                UnityEngine.Object.Destroy(textRect.gameObject);
+                BeatSaberMarkupLanguage.Components.ButtonIconImage btnIcon = btn.gameObject.AddComponent<BeatSaberMarkupLanguage.Components.ButtonIconImage>();
+                btnIcon.image = iconImage;
             }
             }
 
 
-            newButton.SetButtonIcon(iconSprite);
-            newButton.onClick.RemoveAllListeners();
+            GameObject.Destroy(btn.transform.Find("Content").GetComponent<LayoutElement>());
+            btn.GetComponentsInChildren<RectTransform>().First(x => x.name == "Underline").gameObject.SetActive(false);
 
 
-            return newButton;
-        }
-
-        /// <summary>
-        /// Creates a copy of a template button and returns it.
-        /// </summary>
-        /// <param name="parent">The transform to parent the button to.</param>
-        /// <param name="buttonTemplate">The name of the button to make a copy of. Example: "QuitButton", "PlayButton", etc.</param>
-        /// <param name="anchoredPosition">The position the button should be anchored to.</param>
-        /// <param name="sizeDelta">The size of the buttons RectTransform.</param>
-        /// <param name="onClick">Callback for when the button is pressed.</param>
-        /// <param name="buttonText">The text that should be shown on the button.</param>
-        /// <param name="icon">The icon that should be shown on the button.</param>
-        /// <returns>The newly created button.</returns>
-        public static Button CreateUIButton(RectTransform parent, string buttonTemplate, Vector2 anchoredPosition, Vector2 sizeDelta, UnityAction onClick = null, string buttonText = "BUTTON", Sprite icon = null)
-        {
-            Button btn = UnityEngine.Object.Instantiate(Resources.FindObjectsOfTypeAll<Button>().Last(x => (x.name == buttonTemplate)), parent, false);
-            btn.onClick = new Button.ButtonClickedEvent();
-            if (onClick != null)
-                btn.onClick.AddListener(onClick);
-            btn.name = "CustomUIButton";
+            ContentSizeFitter buttonSizeFitter = btn.gameObject.AddComponent<ContentSizeFitter>();
+            buttonSizeFitter.verticalFit = ContentSizeFitter.FitMode.Unconstrained;
+            buttonSizeFitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
 
 
             (btn.transform as RectTransform).anchorMin = new Vector2(0.5f, 0.5f);
             (btn.transform as RectTransform).anchorMin = new Vector2(0.5f, 0.5f);
             (btn.transform as RectTransform).anchorMax = new Vector2(0.5f, 0.5f);
             (btn.transform as RectTransform).anchorMax = new Vector2(0.5f, 0.5f);
             (btn.transform as RectTransform).anchoredPosition = anchoredPosition;
             (btn.transform as RectTransform).anchoredPosition = anchoredPosition;
             (btn.transform as RectTransform).sizeDelta = sizeDelta;
             (btn.transform as RectTransform).sizeDelta = sizeDelta;
 
 
-            btn.SetButtonText(buttonText);
-            if (icon != null)
-                btn.SetButtonIcon(icon);
-
             return btn;
             return btn;
         }
         }
 
 
@@ -112,81 +87,61 @@ namespace SongBrowser.Internals
         /// <param name="parent">The transform to parent the button to.</param>
         /// <param name="parent">The transform to parent the button to.</param>
         /// <param name="buttonTemplate">The name of the button to make a copy of. Example: "QuitButton", "PlayButton", etc.</param>
         /// <param name="buttonTemplate">The name of the button to make a copy of. Example: "QuitButton", "PlayButton", etc.</param>
         /// <param name="anchoredPosition">The position the button should be anchored to.</param>
         /// <param name="anchoredPosition">The position the button should be anchored to.</param>
+        /// <param name="sizeDelta">The size of the buttons RectTransform.</param>
         /// <param name="onClick">Callback for when the button is pressed.</param>
         /// <param name="onClick">Callback for when the button is pressed.</param>
         /// <param name="buttonText">The text that should be shown on the button.</param>
         /// <param name="buttonText">The text that should be shown on the button.</param>
         /// <param name="icon">The icon that should be shown on the button.</param>
         /// <param name="icon">The icon that should be shown on the button.</param>
         /// <returns>The newly created button.</returns>
         /// <returns>The newly created button.</returns>
-        public static Button CreateUIButton(RectTransform parent, string buttonTemplate, Vector2 anchoredPosition, UnityAction onClick = null, string buttonText = "BUTTON", Sprite icon = null)
+        public static Button CreateUIButton(String name, RectTransform parent, string buttonTemplate, Vector2 anchoredPosition, Vector2 sizeDelta, UnityAction onClick = null, string buttonText = "BUTTON", Sprite icon = null)
         {
         {
+            Logger.Debug("CreateUIButton({0}, {1}, {2}, {3}, {4}", name, parent, buttonTemplate, anchoredPosition, sizeDelta);
             Button btn = UnityEngine.Object.Instantiate(Resources.FindObjectsOfTypeAll<Button>().Last(x => (x.name == buttonTemplate)), parent, false);
             Button btn = UnityEngine.Object.Instantiate(Resources.FindObjectsOfTypeAll<Button>().Last(x => (x.name == buttonTemplate)), parent, false);
-            btn.onClick = new Button.ButtonClickedEvent();
-            if (onClick != null)
-                btn.onClick.AddListener(onClick);
-            btn.name = "CustomUIButton";
+            btn.gameObject.SetActive(true);
+            btn.name = name;
+            btn.interactable = true;
 
 
-            (btn.transform as RectTransform).anchorMin = new Vector2(0.5f, 0.5f);
-            (btn.transform as RectTransform).anchorMax = new Vector2(0.5f, 0.5f);
-            (btn.transform as RectTransform).anchoredPosition = anchoredPosition;
+            Polyglot.LocalizedTextMeshProUGUI localizer = btn.GetComponentInChildren<Polyglot.LocalizedTextMeshProUGUI>();
+            if (localizer != null)
+            {
+                GameObject.Destroy(localizer);
+            }
+            BeatSaberMarkupLanguage.Components.ExternalComponents externalComponents = btn.gameObject.AddComponent<BeatSaberMarkupLanguage.Components.ExternalComponents>();
+            TextMeshProUGUI textMesh = btn.GetComponentInChildren<TextMeshProUGUI>();
+            textMesh.richText = true;
+            externalComponents.components.Add(textMesh);
 
 
-            btn.SetButtonText(buttonText);
-            if (icon != null)
-                btn.SetButtonIcon(icon);
+            var f = btn.transform.Find("Content").GetComponent<LayoutElement>();
+            if (f != null)
+            {
+                GameObject.Destroy(f);
+            }
 
 
-            return btn;
-        }
+            ContentSizeFitter buttonSizeFitter = btn.gameObject.AddComponent<ContentSizeFitter>();
+            buttonSizeFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
+            buttonSizeFitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
 
 
+            LayoutGroup stackLayoutGroup = btn.GetComponentInChildren<LayoutGroup>();
+            if (stackLayoutGroup != null)
+            {
+                externalComponents.components.Add(stackLayoutGroup);
+            }
 
 
-        /// <summary>
-        /// Creates a copy of a template button and returns it.
-        /// </summary>
-        /// <param name="parent">The transform to parent the button to.</param>
-        /// <param name="buttonTemplate">The name of the button to make a copy of. Example: "QuitButton", "PlayButton", etc.</param>
-        /// <param name="onClick">Callback for when the button is pressed.</param>
-        /// <param name="buttonText">The text that should be shown on the button.</param>
-        /// <param name="icon">The icon that should be shown on the button.</param>
-        /// <returns>The newly created button.</returns>
-        public static Button CreateUIButton(RectTransform parent, string buttonTemplate, UnityAction onClick = null, string buttonText = "BUTTON", Sprite icon = null)
-        {
-            Button btn = UnityEngine.Object.Instantiate(Resources.FindObjectsOfTypeAll<Button>().Last(x => (x.name == buttonTemplate)), parent, false);
-            btn.onClick = new Button.ButtonClickedEvent();
+            //btn.onClick = new Button.ButtonClickedEvent();
+            btn.onClick.RemoveAllListeners();
             if (onClick != null)
             if (onClick != null)
                 btn.onClick.AddListener(onClick);
                 btn.onClick.AddListener(onClick);
-            btn.name = "CustomUIButton";
+            
 
 
             (btn.transform as RectTransform).anchorMin = new Vector2(0.5f, 0.5f);
             (btn.transform as RectTransform).anchorMin = new Vector2(0.5f, 0.5f);
             (btn.transform as RectTransform).anchorMax = new Vector2(0.5f, 0.5f);
             (btn.transform as RectTransform).anchorMax = new Vector2(0.5f, 0.5f);
+            (btn.transform as RectTransform).anchoredPosition = anchoredPosition;
+            (btn.transform as RectTransform).sizeDelta = sizeDelta;
+
             btn.SetButtonText(buttonText);
             btn.SetButtonText(buttonText);
+
             if (icon != null)
             if (icon != null)
                 btn.SetButtonIcon(icon);
                 btn.SetButtonIcon(icon);
-            return btn;
-        }
-
-        /// <summary>
-        /// Creates a copy of a back button.
-        /// </summary>
-        /// <param name="parent">The transform to parent the new button to.</param>
-        /// <param name="onClick">Callback for when the button is pressed.</param>
-        /// <returns>The newly created back button.</returns>
-        public static Button CreateBackButton(RectTransform parent, UnityAction onClick = null)
-        {
-            if (_backButtonInstance == null)
-            {
-                try
-                {
-                    _backButtonInstance = Resources.FindObjectsOfTypeAll<Button>().First(x => (x.name == "BackArrowButton"));
-                }
-                catch
-                {
-                    return null;
-                }
-            }
-
-            Button btn = UnityEngine.GameObject.Instantiate(_backButtonInstance, parent, false);
-            btn.onClick = new Button.ButtonClickedEvent();
-            if (onClick != null)
-                btn.onClick.AddListener(onClick);
-            btn.name = "CustomUIButton";
-
+            
             return btn;
             return btn;
         }
         }
 
 
@@ -275,34 +230,6 @@ namespace SongBrowser.Internals
         }
         }
 
 
         /// <summary>
         /// <summary>
-        /// Adjust button border.
-        /// </summary>
-        /// <param name="button"></param>
-        /// <param name="color"></param>
-        static public void SetButtonBorder(Button button, Color color)
-        {
-            Image img = button.GetComponentsInChildren<Image>().FirstOrDefault(x => x.name == "Stroke");
-            if (img != null)
-            {
-                img.color = color;
-            }
-        }
-
-        /// <summary>
-        /// Adjust button border.
-        /// </summary>
-        /// <param name="button"></param>
-        /// <param name="color"></param>
-        static public void SetButtonBorderActive(Button button, bool active)
-        {
-            Image img = button.GetComponentsInChildren<Image>().FirstOrDefault(x => x.name == "Stroke");
-            if (img != null)
-            {
-                img.gameObject.SetActive(active);
-            }
-        }
-
-        /// <summary>
         /// Find and adjust a stat panel item text fields.
         /// Find and adjust a stat panel item text fields.
         /// </summary>
         /// </summary>
         /// <param name="rect"></param>
         /// <param name="rect"></param>

+ 6 - 0
SongBrowserPlugin/SongBrowser.csproj

@@ -25,6 +25,7 @@
     <ErrorReport>prompt</ErrorReport>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
     <WarningLevel>4</WarningLevel>
     <Prefer32Bit>false</Prefer32Bit>
     <Prefer32Bit>false</Prefer32Bit>
+    <LangVersion>7.0</LangVersion>
   </PropertyGroup>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
     <PlatformTarget>AnyCPU</PlatformTarget>
     <PlatformTarget>AnyCPU</PlatformTarget>
@@ -35,11 +36,16 @@
     <ErrorReport>prompt</ErrorReport>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
     <WarningLevel>4</WarningLevel>
     <Prefer32Bit>false</Prefer32Bit>
     <Prefer32Bit>false</Prefer32Bit>
+    <LangVersion>7.0</LangVersion>
   </PropertyGroup>
   </PropertyGroup>
   <ItemGroup>
   <ItemGroup>
     <Reference Include="BeatmapCore">
     <Reference Include="BeatmapCore">
       <HintPath>D:\Games\Steam\SteamApps\common\Beat Saber\Beat Saber_Data\Managed\BeatmapCore.dll</HintPath>
       <HintPath>D:\Games\Steam\SteamApps\common\Beat Saber\Beat Saber_Data\Managed\BeatmapCore.dll</HintPath>
     </Reference>
     </Reference>
+    <Reference Include="BSML, Version=1.3.5.0, Culture=neutral, processorArchitecture=MSIL">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>D:\Games\Steam\SteamApps\common\Beat Saber\Plugins\BSML.dll</HintPath>
+    </Reference>
     <Reference Include="BS_Utils">
     <Reference Include="BS_Utils">
       <HintPath>D:\Games\Steam\SteamApps\common\Beat Saber\Plugins\BS_Utils.dll</HintPath>
       <HintPath>D:\Games\Steam\SteamApps\common\Beat Saber\Plugins\BS_Utils.dll</HintPath>
     </Reference>
     </Reference>