Browse Source

Rework EnvironmentManager

Fixes an issue where flipping the background upside down distorts
skirts.

Needed to attach the drag point to the actual bg object but it changes
every time the bg changes so a patcher was made to listen for bg
changes. This allows for bg changes to be detected even from other
sources like the bg panel in studio mode.
habeebweeb 4 years ago
parent
commit
05a4fe56ce

+ 52 - 17
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/Managers/EnvironmentManager.cs

@@ -7,6 +7,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
     using Input = InputManager;
     using Input = InputManager;
     public class EnvironmentManager : IManager, ISerializable
     public class EnvironmentManager : IManager, ISerializable
     {
     {
+        private static readonly BgMgr bgMgr = GameMain.Instance.BgMgr;
         public const string header = "ENVIRONMENT";
         public const string header = "ENVIRONMENT";
         private static bool cubeActive;
         private static bool cubeActive;
         public static bool CubeActive
         public static bool CubeActive
@@ -134,15 +135,10 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
 
 
         public void Activate()
         public void Activate()
         {
         {
-            bgObject = GameObject.Find("__GameMain__/BG");
-            bg = bgObject.transform;
+            BgMgrPatcher.ChangeBgBegin += OnChangeBegin;
+            BgMgrPatcher.ChangeBgEnd += OnChangeEnd;
 
 
-            bgDragPoint = DragPoint.Make<DragPointBG>(PrimitiveType.Cube, Vector3.one * 0.12f);
-            bgDragPoint.Initialize(() => bg.position, () => Vector3.zero);
-            bgDragPoint.Set(bg);
-            bgDragPoint.AddGizmo();
-            bgDragPoint.ConstantScale = true;
-            bgDragPoint.gameObject.SetActive(CubeActive);
+            bgObject = bgMgr.Parent;
 
 
             cameraObject = new GameObject("subCamera");
             cameraObject = new GameObject("subCamera");
             subCamera = cameraObject.AddComponent<Camera>();
             subCamera = cameraObject.AddComponent<Camera>();
@@ -168,6 +164,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
                 ResetCamera();
                 ResetCamera();
                 ChangeBackground("Theater");
                 ChangeBackground("Theater");
             }
             }
+            else UpdateBG();
 
 
             SaveCameraInfo();
             SaveCameraInfo();
 
 
@@ -177,24 +174,27 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
 
 
         public void Deactivate()
         public void Deactivate()
         {
         {
-            if (bgDragPoint != null) GameObject.Destroy(bgDragPoint.gameObject);
+            BgMgrPatcher.ChangeBgBegin -= OnChangeBegin;
+            BgMgrPatcher.ChangeBgEnd -= OnChangeEnd;
+            DestroyDragPoint();
             GameObject.Destroy(cameraObject);
             GameObject.Destroy(cameraObject);
             GameObject.Destroy(subCamera);
             GameObject.Destroy(subCamera);
 
 
             BGVisible = true;
             BGVisible = true;
+
             Camera mainCamera = GameMain.Instance.MainCamera.camera;
             Camera mainCamera = GameMain.Instance.MainCamera.camera;
             mainCamera.backgroundColor = Color.black;
             mainCamera.backgroundColor = Color.black;
 
 
             ultimateOrbitCamera.moveSpeed = defaultCameraMoveSpeed;
             ultimateOrbitCamera.moveSpeed = defaultCameraMoveSpeed;
             ultimateOrbitCamera.zoomSpeed = defaultCameraZoomSpeed;
             ultimateOrbitCamera.zoomSpeed = defaultCameraZoomSpeed;
 
 
-            if (MeidoPhotoStudio.EditMode) ChangeBackground("Theater");
+            if (MeidoPhotoStudio.EditMode) bgMgr.ChangeBg("Theater");
             else
             else
             {
             {
                 bool isNight = GameMain.Instance.CharacterMgr.status.GetFlag("時間帯") == 3;
                 bool isNight = GameMain.Instance.CharacterMgr.status.GetFlag("時間帯") == 3;
 
 
-                if (isNight) ChangeBackground("ShinShitsumu_ChairRot_Night");
-                else ChangeBackground("ShinShitsumu_ChairRot");
+                if (isNight) bgMgr.ChangeBg("ShinShitsumu_ChairRot_Night");
+                else bgMgr.ChangeBg("ShinShitsumu_ChairRot");
 
 
                 GameMain.Instance.MainCamera.Reset(CameraMain.CameraType.Target, true);
                 GameMain.Instance.MainCamera.Reset(CameraMain.CameraType.Target, true);
                 GameMain.Instance.MainCamera.SetTargetPos(new Vector3(0.5609447f, 1.380762f, -1.382336f), true);
                 GameMain.Instance.MainCamera.SetTargetPos(new Vector3(0.5609447f, 1.380762f, -1.382336f), true);
@@ -202,7 +202,8 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
                 GameMain.Instance.MainCamera.SetAroundAngle(new Vector2(245.5691f, 6.273283f), true);
                 GameMain.Instance.MainCamera.SetAroundAngle(new Vector2(245.5691f, 6.273283f), true);
             }
             }
 
 
-            bg.localScale = Vector3.one;
+            if (bgMgr.BgObject) bgMgr.BgObject.transform.localScale = Vector3.one;
+
             CubeSmallChange -= OnCubeSmall;
             CubeSmallChange -= OnCubeSmall;
             CubeActiveChange -= OnCubeActive;
             CubeActiveChange -= OnCubeActive;
         }
         }
@@ -234,11 +235,10 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
 
 
         public void ChangeBackground(string assetName, bool creative = false)
         public void ChangeBackground(string assetName, bool creative = false)
         {
         {
-            currentBGAsset = assetName;
-            if (creative) GameMain.Instance.BgMgr.ChangeBgMyRoom(assetName);
+            if (creative) bgMgr.ChangeBgMyRoom(assetName);
             else
             else
             {
             {
-                GameMain.Instance.BgMgr.ChangeBg(assetName);
+                bgMgr.ChangeBg(assetName);
                 if (assetName == "KaraokeRoom")
                 if (assetName == "KaraokeRoom")
                 {
                 {
                     bg.transform.position = bgObject.transform.position;
                     bg.transform.position = bgObject.transform.position;
@@ -248,6 +248,38 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
             }
             }
         }
         }
 
 
+        private void AttachDragPoint(Transform bgObject)
+        {
+            bgDragPoint = DragPoint.Make<DragPointBG>(PrimitiveType.Cube, Vector3.one * 0.12f);
+            bgDragPoint.Initialize(() => bgObject.position, () => Vector3.zero);
+            bgDragPoint.Set(bgObject);
+            bgDragPoint.AddGizmo();
+            bgDragPoint.ConstantScale = true;
+            bgDragPoint.gameObject.SetActive(CubeActive);
+        }
+
+        private void OnChangeBegin(object sender, EventArgs args) => DestroyDragPoint();
+
+        private void OnChangeEnd(object sender, EventArgs args) => UpdateBG();
+
+        private void UpdateBG()
+        {
+            if (!bgMgr.BgObject) return;
+
+            currentBGAsset = bgMgr.GetBGName();
+            if (currentBGAsset.StartsWith("マイルーム:"))
+            {
+                currentBGAsset = currentBGAsset.Replace("マイルーム:", string.Empty);
+            }
+            bg = bgMgr.BgObject.transform;
+            AttachDragPoint(bg);
+        }
+
+        private void DestroyDragPoint()
+        {
+            if (bgDragPoint) GameObject.Destroy(bgDragPoint.gameObject);
+        }
+
         private void SaveCameraInfo()
         private void SaveCameraInfo()
         {
         {
             cameraInfo = new CameraInfo(GameMain.Instance.MainCamera);
             cameraInfo = new CameraInfo(GameMain.Instance.MainCamera);
@@ -286,7 +318,10 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
             bgDragPoint.DragPointScale = CubeSmall ? DragPointGeneral.smallCube : 1f;
             bgDragPoint.DragPointScale = CubeSmall ? DragPointGeneral.smallCube : 1f;
         }
         }
 
 
-        private void OnCubeActive(object sender, EventArgs args) => bgDragPoint.gameObject.SetActive(CubeActive);
+        private void OnCubeActive(object sender, EventArgs args)
+        {
+            bgDragPoint.gameObject.SetActive(CubeActive);
+        }
     }
     }
 
 
     public struct CameraInfo
     public struct CameraInfo

+ 1 - 0
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/MeidoPhotoStudio.cs

@@ -44,6 +44,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
         private void Awake()
         private void Awake()
         {
         {
             HarmonyLib.Harmony.CreateAndPatchAll(typeof(AllProcPropSeqStartPatcher));
             HarmonyLib.Harmony.CreateAndPatchAll(typeof(AllProcPropSeqStartPatcher));
+            HarmonyLib.Harmony.CreateAndPatchAll(typeof(BgMgrPatcher));
             ScreenshotEvent += OnScreenshotEvent;
             ScreenshotEvent += OnScreenshotEvent;
             DontDestroyOnLoad(this);
             DontDestroyOnLoad(this);
             UnityEngine.SceneManagement.SceneManager.sceneLoaded += OnSceneLoaded;
             UnityEngine.SceneManagement.SceneManager.sceneLoaded += OnSceneLoaded;

+ 21 - 0
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/Patchers/BgMgrPatcher.cs

@@ -0,0 +1,21 @@
+using System;
+using HarmonyLib;
+
+namespace COM3D2.MeidoPhotoStudio.Plugin
+{
+    public static class BgMgrPatcher
+    {
+        public static event EventHandler ChangeBgBegin;
+        public static event EventHandler ChangeBgEnd;
+
+        [HarmonyPatch(typeof(BgMgr), nameof(BgMgr.ChangeBg))]
+        [HarmonyPatch(typeof(BgMgr), nameof(BgMgr.ChangeBgMyRoom))]
+        [HarmonyPrefix]
+        private static void NotifyBeginChangeBg() => ChangeBgBegin?.Invoke(null, EventArgs.Empty);
+
+        [HarmonyPatch(typeof(BgMgr), nameof(BgMgr.ChangeBg))]
+        [HarmonyPatch(typeof(BgMgr), nameof(BgMgr.ChangeBgMyRoom))]
+        [HarmonyPostfix]
+        private static void NotifyEndChangeBg() => ChangeBgEnd?.Invoke(null, EventArgs.Empty);
+    }
+}