Browse Source

Add sorting options

sorting options available are "name," "date created" and "date modified"
habeebweeb 3 years ago
parent
commit
d7305af379

+ 3 - 0
MultipleMaids/.vscode/settings.json

@@ -0,0 +1,3 @@
+{
+    "cSpell.enabled": false
+}

+ 25 - 1
MultipleMaids/CM3D2/MultipleMaids/Plugin/MultipleMaids.Config.cs

@@ -1,4 +1,6 @@
-using ExIni;
+using System;
+using ExIni;
+using UnityEngine;
 
 namespace CM3D2.MultipleMaids.Plugin
 {
@@ -125,6 +127,28 @@ namespace CM3D2.MultipleMaids.Plugin
             }
 
             maxPage /= 10;
+
+            string configSortMode = Preferences["scenemanager"]["scene_sort_mode"].Value?.ToLowerInvariant();
+            int sortIndex = Array.FindIndex(ConfigSortMode, sort => sort == configSortMode);
+            if (sortIndex > -1)
+            {
+                selectedSceneSortMode = sceneSortModeOld = sortIndex;
+            }
+            else
+            {
+                Preferences["scenemanager"]["scene_sort_mode"].Value = ConfigSortMode[0];
+                SaveConfig();
+            }
+
+            if (Preferences["scenemanager"]["scene_sort_ascending"].Value == "false")
+            {
+                sceneSortAscending = sceneSortAscendingOld = false;
+            }
+            else if (Preferences["scenemanager"]["scene_sort_ascending"].Value != "true")
+            {
+                Preferences["scenemanager"]["scene_sort_ascending"].Value = "true";
+                SaveConfig();
+            }
         }
     }
 }

+ 56 - 15
MultipleMaids/CM3D2/MultipleMaids/Plugin/MultipleMaids.Gui.cs

@@ -116,6 +116,7 @@ namespace CM3D2.MultipleMaids.Plugin
                 doguCombo.height = rectWin.height;
                 parCombo.height = rectWin.height;
                 lightCombo.height = rectWin.height;
+                sceneSortCombo.height = sceneManagerRect.height;
 
                 if (sceneFlg)
                 {
@@ -8527,6 +8528,8 @@ namespace CM3D2.MultipleMaids.Plugin
             resizeHandleStyle.fontSize = GetPix(25);
             resizeHandleStyle.alignment = TextAnchor.MiddleCenter;
 
+            sceneSortComboStyle.fontSize = GetPix(13);
+
             // Window variables
             int windowPadding = GetPix(10);
             int windowPaddingY = GetPix(20);
@@ -8551,12 +8554,14 @@ namespace CM3D2.MultipleMaids.Plugin
 
                 bGui = true;
                 copyIndex = 0;
+                SaveSceneSortPreferences();
             }
 
+            int refreshButtonWidth = GetPix(70);
             if (GUI.Button(new Rect(
-                saveGridOffset - GetPix(70) + windowPadding,
+                saveGridOffset - refreshButtonWidth + windowPadding,
                 windowPadding,
-                GetPix(70),
+                refreshButtonWidth,
                 buttonSize
             ), "Refresh", saveControlStyle))
             {
@@ -8569,18 +8574,18 @@ namespace CM3D2.MultipleMaids.Plugin
             kankyoToggle = GUI.Toggle(new Rect(
                 windowPadding,
                 windowPadding,
-                GetPix(75),
+                GetPix(85),
                 buttonSize
             ), kankyoToggle, "Backgrounds", saveControlStyle);
 
             GUI.backgroundColor = originalColor;
 
-            GUI.Label(new Rect(
-                windowPadding,
-                windowPadding,
-                sceneManagerRect.width - windowPadding,
-                buttonSize
-            ), directoryList[selectedDirectory], labelStyle);
+            // GUI.Label(new Rect(
+            //     windowPadding,
+            //     windowPadding,
+            //     sceneManagerRect.width - windowPadding,
+            //     buttonSize
+            // ), directoryList[selectedDirectory], labelStyle);
 
             #endregion
 
@@ -8691,7 +8696,7 @@ namespace CM3D2.MultipleMaids.Plugin
             }
             #endregion
 
-            #region SAVE_GRID
+            #region Save Grid
 
             float scale = 0.55f;
             float imageWidth = 480f * scale;
@@ -8716,11 +8721,14 @@ namespace CM3D2.MultipleMaids.Plugin
             );
 
             GUI.Box(new Rect(
-            saveGridX
-            , saveGridY
-            , saveGridWidth
-            , saveGridHeight - windowPadding
-            ), "");
+                    saveGridX
+                    , saveGridY
+                    , saveGridWidth
+                    , saveGridHeight - windowPadding
+                ), ""
+            );
+
+            GUI.enabled = !sceneSortCombo.isClickedComboButton;
 
             sceneManagerScrollPos = GUI.BeginScrollView(saveGridScrollRect, sceneManagerScrollPos, saveGridScrollView);
             if (GUI.Button(new Rect(
@@ -8774,6 +8782,39 @@ namespace CM3D2.MultipleMaids.Plugin
             }
 
             GUI.EndScrollView();
+
+            GUI.enabled = true;
+
+            #endregion
+
+            #region Sort Options
+            int sceneSortComboWidth = GetPix(85);
+
+            bool ascending = GUI.Toggle(
+                new Rect(saveGridX + sceneSortComboWidth + windowPadding, windowPadding, sceneSortComboWidth, GetPix(20)),
+                sceneSortAscending,
+                "Ascending"
+            );
+
+            if (ascending != sceneSortAscending)
+            {
+                SetSceneSortAscending(ascending);
+            }
+
+            int sceneSortMode = sceneSortCombo.List(
+                new Rect(saveGridX, windowPadding, sceneSortComboWidth, buttonSize),
+                sceneSortComboList[selectedSceneSortMode].text,
+                sceneSortComboList,
+                saveControlStyle,
+                "box",
+                sceneSortComboStyle
+            );
+
+            if (sceneSortMode != selectedSceneSortMode)
+            {
+                SetSceneSortMode(sceneSortMode);
+            }
+
             #endregion
 
             GUI.Label(new Rect(

+ 1 - 0
MultipleMaids/CM3D2/MultipleMaids/Plugin/MultipleMaids.Init.cs

@@ -469,6 +469,7 @@ namespace CM3D2.MultipleMaids.Plugin
 
         public void init()
         {
+            SaveSceneSortPreferences();
             if (bg2Busy)
             {
                 bg2Busy = false;

+ 1 - 0
MultipleMaids/CM3D2/MultipleMaids/Plugin/MultipleMaids.MaidUpdate.cs

@@ -7123,6 +7123,7 @@ namespace CM3D2.MultipleMaids.Plugin
                 if (!isVR && Input.GetKeyDown(KeyCode.F8) && sceneFlg && bGui)
                 {
                     bGui = false;
+                    SaveSceneSortPreferences();
                 }
                 else if (!isVR && Input.GetKeyDown(KeyCode.F8))
                 {

+ 113 - 9
MultipleMaids/SaveManager.cs

@@ -19,6 +19,7 @@ namespace CM3D2.MultipleMaids.Plugin
         private static string sceneData;
         private static List<ScenePng> scenes = new List<ScenePng>(50);
         private GUIStyle selectedButtonStyle;
+        private GUIStyle sceneSortComboStyle;
         private GUIContent infoString;
         private string[] directoryList;
         private Texture2D frame;
@@ -45,17 +46,41 @@ namespace CM3D2.MultipleMaids.Plugin
         private int selectedDirectory = 0;
         private string currentDirectory = sceneDirectoryName;
         private string textFieldValue = "";
+        private readonly ComboBox2 sceneSortCombo = new ComboBox2();
+        private static GUIContent[] sceneSortComboList;
+        private static int sceneSortModeOld = 0;
+        private static bool sceneSortAscendingOld = true;
+        private static int selectedSceneSortMode = 0;
+        private static bool sceneSortAscending = true;
+        private static readonly string[] ConfigSortMode = new[] { "name", "date_created", "date_modified" };
+        private enum SortMode
+        {
+            Name, DateCreated, DateModified
+        }
 
         public void InitializeSceneManager()
         {
             frame = MakeTex(2, 2, Color.white);
             infoHighlight = MakeTex(2, 2, new Color(0f, 0f, 0f, 0.8f));
 
+            sceneSortCombo.selectedItemIndex = selectedSceneSortMode;
+            sceneSortComboList = new GUIContent[] {
+                new GUIContent("Name"),
+                new GUIContent("Date Created"),
+                new GUIContent("Date Modified")
+            };
 
             selectedButtonStyle = new GUIStyle("button");
             selectedButtonStyle.normal.background = MakeTex(1, 1, new Color(0.5f, 0.5f, 0.5f, 0.4f));
             selectedButtonStyle.normal.textColor = Color.white;
 
+            sceneSortComboStyle = new GUIStyle();
+            sceneSortComboStyle.normal.textColor = Color.white;
+            sceneSortComboStyle.normal.background = MakeTex(2, 2, new Color(0.0f, 0.0f, 0.0f, 0.5f));
+            sceneSortComboStyle.onHover.background = sceneSortComboStyle.hover.background = new Texture2D(2, 2);
+            sceneSortComboStyle.padding.left = sceneSortComboStyle.padding.right = sceneSortComboStyle.padding.top = sceneSortComboStyle.padding.bottom = GetPix(0);
+            sceneSortComboStyle.fontSize = GetPix(13);
+
             currentDirectory = kankyoModeFlag ? kankyoDirectoryName : sceneDirectoryName;
 
             GetSceneDirectories();
@@ -71,6 +96,18 @@ namespace CM3D2.MultipleMaids.Plugin
             sceneManagerInitialize = true;
         }
 
+        private void SaveSceneSortPreferences()
+        {
+            if (sceneSortAscending != sceneSortAscendingOld || sceneSortModeOld != selectedSceneSortMode)
+            {
+                Preferences["scenemanager"]["scene_sort_ascending"].Value = sceneSortAscending ? "true" : "false";
+                Preferences["scenemanager"]["scene_sort_mode"].Value = ConfigSortMode[selectedSceneSortMode];
+                SaveConfig();
+                sceneSortAscendingOld = sceneSortAscending;
+                sceneSortModeOld = selectedSceneSortMode;
+            }
+        }
+
         private void RefreshSceneManager()
         {
             SwitchDirectory(currentDirectory);
@@ -150,10 +187,61 @@ namespace CM3D2.MultipleMaids.Plugin
             }
 
             selectedScene = scenes.Count == 0 ? 0 : scenes.Count - 1;
-            scenes.Sort((a, b) => a.info.LastWriteTime.CompareTo(b.info.LastWriteTime));
+
+            SortScenes();
+
             sceneManagerScrollPos.y = 0;
         }
 
+        private void SortScenes()
+        {
+            Comparison<ScenePng> sortMode = null;
+            switch ((SortMode)selectedSceneSortMode)
+            {
+                case SortMode.Name:
+                    sortMode = SortByName;
+                    break;
+                case SortMode.DateCreated:
+                    sortMode = SortByDateCreated;
+                    break;
+                case SortMode.DateModified:
+                    sortMode = SortByDateModified;
+                    break;
+            }
+
+            scenes.Sort(sortMode);
+        }
+
+        private void SetSceneSortAscending(bool ascending)
+        {
+            sceneSortAscending = ascending;
+            SortScenes();
+        }
+
+        private void SetSceneSortMode(int sortMode)
+        {
+            selectedSceneSortMode = Mathf.Clamp(sortMode, 0, 2);
+            SortScenes();
+        }
+
+        private static int SortByName(ScenePng a, ScenePng b)
+        {
+            int direction = sceneSortAscending ? -1 : 1;
+            return direction * a.info.Name.CompareTo(b.info.Name);
+        }
+
+        private static int SortByDateCreated(ScenePng a, ScenePng b)
+        {
+            int direction = sceneSortAscending ? -1 : 1;
+            return direction * a.info.CreationTime.CompareTo(b.info.CreationTime);
+        }
+
+        private static int SortByDateModified(ScenePng a, ScenePng b)
+        {
+            int direction = sceneSortAscending ? -1 : 1;
+            return direction * a.info.LastWriteTime.CompareTo(b.info.LastWriteTime);
+        }
+
         private void CreateDirectory(string directoryName)
         {
             string root = kankyoModeFlag ? kankyoScenePath : saveScenePath;
@@ -204,12 +292,6 @@ namespace CM3D2.MultipleMaids.Plugin
             {
                 RefreshSceneManager();
             }
-            else
-            {
-                File.Delete(file);
-                scenes.RemoveAt(selectedScene);
-                sceneManagerScrollPos.y = 0;
-            }
         }
 
         private void SaveScene()
@@ -276,7 +358,18 @@ namespace CM3D2.MultipleMaids.Plugin
 
             string filePath;
 
-            filePath = Path.Combine(saveDirectory, $"{sceneType}{DateTime.Now:yyyyMMddHHmmss}.png");
+            string fileName = $"{sceneType}{DateTime.Now:yyyyMMddHHmmss}.png";
+
+            FileInfo oldFileInfo = null;
+
+            if (overwriteFlag)
+            {
+                oldFileInfo = scenes[selectedScene].info;
+                fileName = oldFileInfo.Name;
+                scenes.RemoveAt(selectedScene);
+            }
+
+            filePath = Path.Combine(saveDirectory, fileName);
 
             using (FileStream fileStream = File.Create(filePath))
             using (MemoryStream sceneStream = new MemoryStream(Encoding.Unicode.GetBytes(sceneString)))
@@ -287,8 +380,19 @@ namespace CM3D2.MultipleMaids.Plugin
                 if (kankyoModeFlag) fileStream.Write(kankyoHeader, 0, kankyoHeader.Length);
                 fileStream.Write(sceneBuffer, 0, sceneBuffer.Length);
             }
-            scenes.Add(new ScenePng(new FileInfo(filePath), screenshot));
+
+            ScenePng newScenePng = new ScenePng(new FileInfo(filePath), screenshot);
+
+            if (oldFileInfo != null)
+            {
+                File.SetCreationTime(filePath, oldFileInfo.CreationTime);
+                newScenePng.info.CreationTime = oldFileInfo.CreationTime;
+            }
+
+            scenes.Add(newScenePng);
             selectedScene = scenes.Count - 1;
+
+            SortScenes();
         }
 
         private void ReadScene()