LightManager.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace COM3D2.MeidoPhotoStudio.Plugin
  6. {
  7. internal class LightManager : IManager, ISerializable
  8. {
  9. public const string header = "LIGHT";
  10. private static bool cubeActive = true;
  11. public static bool CubeActive
  12. {
  13. get => cubeActive;
  14. set
  15. {
  16. if (value != cubeActive)
  17. {
  18. cubeActive = value;
  19. CubeActiveChange?.Invoke(null, EventArgs.Empty);
  20. }
  21. }
  22. }
  23. private static event EventHandler CubeActiveChange;
  24. private readonly List<DragPointLight> lightList = new List<DragPointLight>();
  25. private int selectedLightIndex;
  26. public int SelectedLightIndex
  27. {
  28. get => selectedLightIndex;
  29. set
  30. {
  31. selectedLightIndex = Mathf.Clamp(value, 0, lightList.Count - 1);
  32. lightList[SelectedLightIndex].IsActiveLight = true;
  33. }
  34. }
  35. public string[] LightNameList => lightList.Select(light => LightName(light.Name)).ToArray();
  36. public string ActiveLightName => LightName(lightList[SelectedLightIndex].Name);
  37. public DragPointLight CurrentLight => lightList[SelectedLightIndex];
  38. public event EventHandler Rotate;
  39. public event EventHandler Scale;
  40. public event EventHandler ListModified;
  41. public event EventHandler Select;
  42. public void Serialize(System.IO.BinaryWriter binaryWriter)
  43. {
  44. binaryWriter.Write(header);
  45. binaryWriter.Write(lightList.Count);
  46. foreach (DragPointLight light in lightList)
  47. {
  48. light.Serialize(binaryWriter);
  49. }
  50. }
  51. public void Deserialize(System.IO.BinaryReader binaryReader)
  52. {
  53. ClearLights();
  54. int numberOfLights = binaryReader.ReadInt32();
  55. lightList[0].Deserialize(binaryReader);
  56. for (int i = 1; i < numberOfLights; i++)
  57. {
  58. AddLight();
  59. lightList[i].Deserialize(binaryReader);
  60. }
  61. }
  62. public void Activate()
  63. {
  64. GameMain.Instance.MainCamera.GetComponent<Camera>().backgroundColor = Color.black;
  65. AddLight(GameMain.Instance.MainLight.gameObject, true);
  66. CubeActiveChange += OnCubeActive;
  67. }
  68. public void Deactivate()
  69. {
  70. for (int i = 0; i < lightList.Count; i++)
  71. {
  72. DestroyLight(lightList[i]);
  73. }
  74. selectedLightIndex = 0;
  75. lightList.Clear();
  76. GameMain.Instance.MainLight.Reset();
  77. Light mainLight = GameMain.Instance.MainLight.GetComponent<Light>();
  78. mainLight.type = LightType.Directional;
  79. DragPointLight.SetLightProperties(mainLight, new LightProperty());
  80. CubeActiveChange -= OnCubeActive;
  81. }
  82. public void Update() { }
  83. public void AddLight(GameObject lightGo = null, bool isMain = false)
  84. {
  85. GameObject go = lightGo ?? new GameObject("MPS Light");
  86. DragPointLight light = DragPoint.Make<DragPointLight>(PrimitiveType.Cube, Vector3.one * 0.12f);
  87. light.Initialize(() => go.transform.position, () => go.transform.eulerAngles);
  88. light.Set(go.transform);
  89. light.IsMain = isMain;
  90. light.Rotate += OnRotate;
  91. light.Scale += OnScale;
  92. light.Delete += OnDelete;
  93. light.Select += OnSelect;
  94. lightList.Add(light);
  95. CurrentLight.IsActiveLight = false;
  96. SelectedLightIndex = lightList.Count;
  97. OnListModified();
  98. }
  99. public void DeleteActiveLight()
  100. {
  101. if (selectedLightIndex == 0) return;
  102. DeleteLight(SelectedLightIndex);
  103. }
  104. public void DeleteLight(int lightIndex, bool noUpdate = false)
  105. {
  106. if (lightIndex == 0) return;
  107. DestroyLight(lightList[lightIndex]);
  108. lightList.RemoveAt(lightIndex);
  109. if (lightIndex <= SelectedLightIndex) SelectedLightIndex--;
  110. if (noUpdate) return;
  111. OnListModified();
  112. }
  113. public void SetColourModeActive(bool isColourMode) => lightList[0].IsColourMode = isColourMode;
  114. public void ClearLights()
  115. {
  116. for (int i = lightList.Count - 1; i > 0; i--) DeleteLight(i);
  117. selectedLightIndex = 0;
  118. }
  119. private void DestroyLight(DragPointLight light)
  120. {
  121. if (light == null) return;
  122. light.Rotate -= OnRotate;
  123. light.Scale -= OnScale;
  124. light.Delete -= OnDelete;
  125. light.Select -= OnSelect;
  126. GameObject.Destroy(light.gameObject);
  127. }
  128. private string LightName(string name) => Translation.Get("lightType", name);
  129. private void OnDelete(object sender, EventArgs args)
  130. {
  131. DragPointLight theLight = (DragPointLight)sender;
  132. for (int i = 1; i < lightList.Count; i++)
  133. {
  134. DragPointLight light = lightList[i];
  135. if (light == theLight)
  136. {
  137. DeleteLight(i);
  138. return;
  139. }
  140. }
  141. }
  142. private void OnRotate(object sender, EventArgs args) => OnTransformEvent((DragPointLight)sender, Rotate);
  143. private void OnScale(object sender, EventArgs args) => OnTransformEvent((DragPointLight)sender, Scale);
  144. private void OnTransformEvent(DragPointLight light, EventHandler handler)
  145. {
  146. if (light.IsActiveLight) handler?.Invoke(this, EventArgs.Empty);
  147. }
  148. private void OnSelect(object sender, EventArgs args)
  149. {
  150. DragPointLight theLight = (DragPointLight)sender;
  151. int select = lightList.FindIndex(light => light == theLight);
  152. if (select >= 0)
  153. {
  154. SelectedLightIndex = select;
  155. Select?.Invoke(this, EventArgs.Empty);
  156. }
  157. }
  158. private void OnListModified() => ListModified?.Invoke(this, EventArgs.Empty);
  159. private void OnCubeActive(object sender, EventArgs args)
  160. {
  161. foreach (DragPointLight dragPoint in lightList) dragPoint.gameObject.SetActive(CubeActive);
  162. }
  163. }
  164. }