Jelajahi Sumber

Added Delete song button along with confirmation dialog. Fixed another
issue with BeatSaverDownloader.

Stephen Damm 5 tahun lalu
induk
melakukan
4ac9374884

+ 1 - 3
SongBrowserPlugin/SongBrowserApplication.cs

@@ -141,9 +141,7 @@ namespace SongBrowserPlugin
         {
             _log.Debug("Hi jacking solo mode buttons");
             try
-            {
-                
-
+            {                
                 ReflectionUtil.SetPrivateField(_menuMasterViewController, "_gameplayMode", gameplayMode);
                 ReflectionUtil.SetPrivateField(_menuMasterViewController, "_songSelectionMasterViewController", _songBrowserMasterViewController);
 

+ 89 - 10
SongBrowserPlugin/SongBrowserMasterViewController.cs

@@ -5,7 +5,7 @@ using System.Collections.Generic;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 using HMUI;
-
+using System.IO;
 
 namespace SongBrowserPlugin
 {
@@ -27,6 +27,10 @@ namespace SongBrowserPlugin
         private List<SongSortButton> _sortButtonGroup;        
         private Button _addFavoriteButton;
         private String _addFavoriteButtonText = null;
+        private SimpleDialogPromptViewController _simpleDialogPromptViewControllerPrefab;
+        private SimpleDialogPromptViewController _deleteDialog;
+
+        private Button _deleteButton;
 
         // Debug
         private int _sortButtonLastPushedIndex = 0;
@@ -59,7 +63,12 @@ namespace SongBrowserPlugin
             InitModel();
 
             _uiInitialized = false;
-            SceneManager.activeSceneChanged += SceneManagerOnActiveSceneChanged;            
+            SceneManager.activeSceneChanged += SceneManagerOnActiveSceneChanged;
+
+            _simpleDialogPromptViewControllerPrefab = Resources.FindObjectsOfTypeAll<SimpleDialogPromptViewController>().First();
+
+            this._deleteDialog = UnityEngine.Object.Instantiate<SimpleDialogPromptViewController>(this._simpleDialogPromptViewControllerPrefab);
+            this._deleteDialog.gameObject.SetActive(false);
         }
 
         /// <summary>
@@ -141,18 +150,20 @@ namespace SongBrowserPlugin
 
                 RectTransform transform = _songDetailViewController.transform as RectTransform;
                 _addFavoriteButton = UIBuilder.CreateUIButton(transform, "QuitButton", SongBrowserApplication.Instance.ButtonTemplate);
-                (_addFavoriteButton.transform as RectTransform).anchoredPosition = new Vector2(45f, 5f);
-                (_addFavoriteButton.transform as RectTransform).sizeDelta = new Vector2(10f, 10f);
+                (_addFavoriteButton.transform as RectTransform).anchoredPosition = new Vector2(45f, 9f);
+                (_addFavoriteButton.transform as RectTransform).sizeDelta = new Vector2(10f, 5.0f);
                 
                 if (_addFavoriteButtonText == null)
                 {
                     _log.Debug("Determinng if first selected song is a favorite.");
-                    LevelStaticData level = getSelectedSong();                    
-                    RefreshAddFavoriteButton(level.levelId);
+                    LevelStaticData level = getSelectedSong();
+                    if (level != null)
+                    {
+                        RefreshAddFavoriteButton(level.levelId);
+                    }                    
                 }
                 
-                UIBuilder.SetButtonText(ref _addFavoriteButton, _addFavoriteButtonText);
-                //UIBuilder.SetButtonIcon(ref _addFavoriteButton, SongBrowserApplication.Instance.CachedIcons["AllDirectionsIcon"]);
+                UIBuilder.SetButtonText(ref _addFavoriteButton, _addFavoriteButtonText);                
                 UIBuilder.SetButtonTextSize(ref _addFavoriteButton, 3);
                 UIBuilder.SetButtonIconEnabled(ref _addFavoriteButton, false);                
                 _addFavoriteButton.onClick.RemoveAllListeners();
@@ -160,12 +171,27 @@ namespace SongBrowserPlugin
                     ToggleSongInFavorites();
                 });
 
+                // Create delete button
+                _log.Debug("Creating delete button...");
+
+                transform = _songDetailViewController.transform as RectTransform;
+                _deleteButton = UIBuilder.CreateUIButton(transform, "QuitButton", SongBrowserApplication.Instance.ButtonTemplate);
+                (_deleteButton.transform as RectTransform).anchoredPosition = new Vector2(45f, 0f);
+                (_deleteButton.transform as RectTransform).sizeDelta = new Vector2(16f, 5f);
+                UIBuilder.SetButtonText(ref _deleteButton, "Delete");
+                UIBuilder.SetButtonTextSize(ref _deleteButton, 3);
+                UIBuilder.SetButtonIconEnabled(ref _deleteButton, false);
+                _deleteButton.onClick.RemoveAllListeners();
+                _deleteButton.onClick.AddListener(delegate () {
+                    HandleDeleteSelectedSong();
+                });
+
                 RefreshUI();
                 _uiInitialized = true;
             }
             catch (Exception e)
             {
-                _log.Exception("Exception CreateUI: " + e.Message);
+                _log.Exception("Exception CreateUI: {0}\n{1}", e.Message, e.StackTrace);
             }
         }
 
@@ -213,6 +239,11 @@ namespace SongBrowserPlugin
                 return null;
             }
 
+            /*if (this._levelsStaticData == null)
+            {
+                return null;
+            }*/
+
             int selectedIndex = this.GetSelectedSongIndex();
             if (selectedIndex < 0)
             {
@@ -220,11 +251,53 @@ namespace SongBrowserPlugin
             }
 
             LevelStaticData level = this.GetLevelStaticDataForSelectedSong();
-
             return level;
         }
 
         /// <summary>
+        /// Pop up a delete dialog.
+        /// </summary>
+        private void HandleDeleteSelectedSong()
+        {
+            int selectedIndex = this.GetSelectedSongIndex();
+            if (selectedIndex < 0)
+            {
+                return;
+            }
+
+            LevelStaticData level = this.GetLevelStaticDataForSelectedSong();
+            SongLoaderPlugin.CustomSongInfo songInfo = _model.LevelIdToCustomSongInfos[level.levelId];
+
+            this._deleteDialog.Init("Delete song warning!", String.Format("<color=#00AAFF>Permanently delete song: {0}</color>\n  Do you want to continue?", songInfo.songName), "YES", "NO");
+
+            this._deleteDialog.didFinishEvent += this.HandleSimpleDialogPromptViewControllerDidFinish;
+            this.PresentModalViewController(this._deleteDialog, null, false);
+        }
+
+        /// <summary>
+        /// Handle delete dialog resolution.
+        /// </summary>
+        /// <param name="viewController"></param>
+        /// <param name="ok"></param>
+        public virtual void HandleSimpleDialogPromptViewControllerDidFinish(SimpleDialogPromptViewController viewController, bool ok)
+        {
+            viewController.didFinishEvent -= this.HandleSimpleDialogPromptViewControllerDidFinish;
+            if (!ok)
+            {
+                viewController.DismissModalViewController(null, false);
+            }
+            else
+            {
+                LevelStaticData level = this.GetLevelStaticDataForSelectedSong();
+                SongLoaderPlugin.CustomSongInfo songInfo = _model.LevelIdToCustomSongInfos[level.levelId];
+
+                viewController.DismissModalViewController(null, false);
+                _log.Debug("DELETING: {0}", songInfo.path);
+                //Directory.Delete(songInfo.path);
+            }
+        }
+
+        /// <summary>
         /// Add/Remove song from favorites depending on if it already exists.
         /// </summary>
         private void ToggleSongInFavorites()
@@ -374,6 +447,12 @@ namespace SongBrowserPlugin
                 _sortButtonGroup[_sortButtonLastPushedIndex].Button.onClick.Invoke();                
             }
 
+            // delete
+            if (Input.GetKeyDown(KeyCode.D))
+            {
+                _deleteButton.onClick.Invoke();
+            }
+
             // z,x,c,v can be used to get into a song, b will hit continue button after song ends
             if (Input.GetKeyDown(KeyCode.C))
             {                

+ 21 - 3
SongBrowserPlugin/SongBrowserModel.cs

@@ -26,6 +26,7 @@ namespace SongBrowserPlugin
         private SongSortMode _cachedSortMode = default(SongSortMode);
 
         private DateTime _cachedCustomSongDirLastWriteTIme = DateTime.MinValue;
+        private int _customSongDirTotalCount = -1;
 
         public SongBrowserSettings Settings
         {
@@ -43,6 +44,14 @@ namespace SongBrowserPlugin
             }
         }
 
+        public Dictionary<String, SongLoaderPlugin.CustomSongInfo> LevelIdToCustomSongInfos
+        {
+            get
+            {
+                return _levelIdToCustomSongInfo;
+            }
+        }
+
         /// <summary>
         /// Constructor.
         /// </summary>
@@ -69,13 +78,22 @@ namespace SongBrowserPlugin
         {
             String customSongsPath = Path.Combine(Environment.CurrentDirectory, "CustomSongs");
             DateTime currentLastWriteTIme = File.GetLastWriteTimeUtc(customSongsPath);
-            if (_cachedCustomSongDirLastWriteTIme == null || DateTime.Compare(currentLastWriteTIme, _cachedCustomSongDirLastWriteTIme) != 0)
+            string[] directories = Directory.GetDirectories(customSongsPath);
+            int directoryCount = directories.Length;
+            int fileCount = Directory.GetFiles(customSongsPath, "*").Length;
+            int currentTotalCount = directoryCount + fileCount;
+
+            if (_cachedCustomSongDirLastWriteTIme == null || 
+                DateTime.Compare(currentLastWriteTIme, _cachedCustomSongDirLastWriteTIme) != 0 ||
+                currentTotalCount != this._customSongDirTotalCount)
             {
                 _log.Debug("Custom Song directory has changed. Fetching new songs. Sorting song list.");
-        
+
+                this._customSongDirTotalCount = directoryCount + fileCount;
+
                 // Get LastWriteTimes
                 var Epoch = new DateTime(1970, 1, 1);
-                string[] directories = Directory.GetDirectories(customSongsPath);
+
                 //_log.Debug("Directories: " + directories);
                 foreach (string dir in directories)
                 {