123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using Object = UnityEngine.Object;
- namespace COM3D2.MeidoPhotoStudio.Plugin
- {
- using Input = InputManager;
- public class EnvironmentManager : IManager, ISerializable
- {
- private static readonly BgMgr bgMgr = GameMain.Instance.BgMgr;
- public const string header = "ENVIRONMENT";
- public const string defaultBg = "Theater";
- private const string myRoomPrefix = "マイルーム:";
- private static bool cubeActive;
- public static bool CubeActive
- {
- get => cubeActive;
- set
- {
- if (value == cubeActive) return;
- cubeActive = value;
- CubeActiveChange?.Invoke(null, EventArgs.Empty);
- }
- }
- private static bool cubeSmall;
- public static bool CubeSmall
- {
- get => cubeSmall;
- set
- {
- if (value == cubeSmall) return;
- cubeSmall = value;
- CubeSmallChange?.Invoke(null, EventArgs.Empty);
- }
- }
- private static event EventHandler CubeActiveChange;
- private static event EventHandler CubeSmallChange;
- private Transform bg;
- private GameObject bgObject;
- private DragPointBG bgDragPoint;
- public string CurrentBgAsset { get; private set; } = defaultBg;
- private bool bgVisible = true;
- public bool BGVisible
- {
- get => bgVisible;
- set
- {
- bgVisible = value;
- bgObject.SetActive(bgVisible);
- }
- }
- public EnvironmentManager()
- {
- DragPointLight.EnvironmentManager = this;
- Activate();
- }
- public void Update() { }
- public void Activate()
- {
- BgMgrPatcher.ChangeBgBegin += OnChangeBegin;
- BgMgrPatcher.ChangeBgEnd += OnChangeEnd;
- bgObject = bgMgr.Parent;
- bgObject.SetActive(true);
-
- if (MeidoPhotoStudio.EditMode) UpdateBG();
- else ChangeBackground(defaultBg);
- CubeSmallChange += OnCubeSmall;
- CubeActiveChange += OnCubeActive;
- }
- public void Deactivate()
- {
- BgMgrPatcher.ChangeBgBegin -= OnChangeBegin;
- BgMgrPatcher.ChangeBgEnd -= OnChangeEnd;
- DestroyDragPoint();
- BGVisible = true;
- if (MeidoPhotoStudio.EditMode) bgMgr.ChangeBg(defaultBg);
- else
- {
- var isNight = GameMain.Instance.CharacterMgr.status.GetFlag("時間帯") == 3;
- bgMgr.ChangeBg(isNight ? "ShinShitsumu_ChairRot_Night" : "ShinShitsumu_ChairRot");
- }
- if (bgMgr.BgObject) bgMgr.BgObject.transform.localScale = Vector3.one;
- CubeSmallChange -= OnCubeSmall;
- CubeActiveChange -= OnCubeActive;
- }
- public void Serialize(BinaryWriter binaryWriter)
- {
- binaryWriter.Write(header);
- binaryWriter.Write(CurrentBgAsset);
- binaryWriter.WriteVector3(bg.position);
- binaryWriter.WriteQuaternion(bg.rotation);
- binaryWriter.WriteVector3(bg.localScale);
- }
-
- public void Deserialize(BinaryReader binaryReader)
- {
- var bgAsset = binaryReader.ReadString();
- var isCreative = Utility.IsGuidString(bgAsset);
- List<string> bgList = isCreative
- ? Constants.MyRoomCustomBGList.ConvertAll(kvp => kvp.Key)
- : Constants.BGList;
- var assetIndex = bgList.FindIndex(
- asset => asset.Equals(bgAsset, StringComparison.InvariantCultureIgnoreCase)
- );
- if (assetIndex < 0)
- {
- Utility.LogWarning($"Could not load BG '{bgAsset}'");
- isCreative = false;
- bgAsset = defaultBg;
- }
- else bgAsset = bgList[assetIndex];
- ChangeBackground(bgAsset, isCreative);
- bg.position = binaryReader.ReadVector3();
- bg.rotation = binaryReader.ReadQuaternion();
- bg.localScale = binaryReader.ReadVector3();
- }
- public void ChangeBackground(string assetName, bool creative = false)
- {
- if (creative) bgMgr.ChangeBgMyRoom(assetName);
- else bgMgr.ChangeBg(assetName);
- }
- private void AttachDragPoint(Transform bgTransform)
- {
- bgDragPoint = DragPoint.Make<DragPointBG>(PrimitiveType.Cube, Vector3.one * 0.12f);
- bgDragPoint.Initialize(() => bgTransform.position, () => Vector3.zero);
- bgDragPoint.Set(bgTransform);
- 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(myRoomPrefix))
- CurrentBgAsset = CurrentBgAsset.Replace(myRoomPrefix, string.Empty);
- bg = bgMgr.BgObject.transform;
- AttachDragPoint(bg);
- }
- private void DestroyDragPoint()
- {
- if (bgDragPoint) Object.Destroy(bgDragPoint.gameObject);
- }
- private void OnCubeSmall(object sender, EventArgs args)
- {
- bgDragPoint.DragPointScale = CubeSmall ? DragPointGeneral.smallCube : 1f;
- }
- private void OnCubeActive(object sender, EventArgs args)
- {
- bgDragPoint.gameObject.SetActive(CubeActive);
- }
- }
- }
|