LightManager.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 List<DragPointLight> lightList = new List<DragPointLight>();
  25. private int selectedLightIndex = 0;
  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
  38. {
  39. get
  40. {
  41. return lightList[SelectedLightIndex];
  42. }
  43. }
  44. public event EventHandler Rotate;
  45. public event EventHandler Scale;
  46. public event EventHandler ListModified;
  47. public event EventHandler Select;
  48. public void Serialize(System.IO.BinaryWriter binaryWriter)
  49. {
  50. binaryWriter.Write(header);
  51. binaryWriter.Write(lightList.Count);
  52. foreach (DragPointLight light in lightList)
  53. {
  54. light.Serialize(binaryWriter);
  55. }
  56. }
  57. public void Deserialize(System.IO.BinaryReader binaryReader)
  58. {
  59. ClearLights();
  60. int numberOfLights = binaryReader.ReadInt32();
  61. lightList[0].Deserialize(binaryReader);
  62. for (int i = 1; i < numberOfLights; i++)
  63. {
  64. AddLight();
  65. lightList[i].Deserialize(binaryReader);
  66. }
  67. }
  68. public void Activate()
  69. {
  70. GameMain.Instance.MainCamera.GetComponent<Camera>().backgroundColor = Color.black;
  71. AddLight(GameMain.Instance.MainLight.gameObject, true);
  72. CubeActiveChange += OnCubeActive;
  73. }
  74. public void Deactivate()
  75. {
  76. for (int i = 0; i < lightList.Count; i++)
  77. {
  78. DestroyLight(lightList[i]);
  79. }
  80. selectedLightIndex = 0;
  81. lightList.Clear();
  82. GameMain.Instance.MainLight.Reset();
  83. Light mainLight = GameMain.Instance.MainLight.GetComponent<Light>();
  84. mainLight.type = LightType.Directional;
  85. DragPointLight.SetLightProperties(mainLight, new LightProperty());
  86. CubeActiveChange -= OnCubeActive;
  87. }
  88. public void Update() { }
  89. public void AddLight(GameObject lightGo = null, bool isMain = false)
  90. {
  91. GameObject go = lightGo ?? new GameObject("MPS Light");
  92. DragPointLight light = DragPoint.Make<DragPointLight>(
  93. PrimitiveType.Cube, Vector3.one * 0.12f, DragPoint.LightBlue
  94. );
  95. light.Initialize(() => go.transform.position, () => go.transform.eulerAngles);
  96. light.Set(go.transform);
  97. light.IsMain = isMain;
  98. light.Rotate += OnRotate;
  99. light.Scale += OnScale;
  100. light.Delete += OnDelete;
  101. light.Select += OnSelect;
  102. lightList.Add(light);
  103. CurrentLight.IsActiveLight = false;
  104. SelectedLightIndex = lightList.Count;
  105. OnListModified();
  106. }
  107. public void DeleteActiveLight()
  108. {
  109. if (selectedLightIndex == 0) return;
  110. DeleteLight(SelectedLightIndex);
  111. }
  112. public void DeleteLight(int lightIndex, bool noUpdate = false)
  113. {
  114. if (lightIndex == 0) return;
  115. DestroyLight(lightList[lightIndex]);
  116. lightList.RemoveAt(lightIndex);
  117. if (lightIndex <= SelectedLightIndex) SelectedLightIndex -= 1;
  118. if (noUpdate) return;
  119. OnListModified();
  120. }
  121. public void SetColourModeActive(bool isColourMode)
  122. {
  123. lightList[0].IsColourMode = isColourMode;
  124. }
  125. public void ClearLights()
  126. {
  127. for (int i = lightList.Count - 1; i > 0; i--)
  128. {
  129. DeleteLight(i);
  130. }
  131. selectedLightIndex = 0;
  132. }
  133. private void DestroyLight(DragPointLight light)
  134. {
  135. if (light == null) return;
  136. light.Rotate -= OnRotate;
  137. light.Scale -= OnScale;
  138. light.Delete -= OnDelete;
  139. light.Select -= OnSelect;
  140. GameObject.Destroy(light.gameObject);
  141. }
  142. private string LightName(string name)
  143. {
  144. return Translation.Get("lightType", name);
  145. }
  146. private void OnDelete(object sender, EventArgs args)
  147. {
  148. DragPointLight theLight = (DragPointLight)sender;
  149. for (int i = 1; i < lightList.Count; i++)
  150. {
  151. DragPointLight light = lightList[i];
  152. if (light == theLight)
  153. {
  154. DeleteLight(i);
  155. return;
  156. }
  157. }
  158. }
  159. private void OnRotate(object sender, EventArgs args)
  160. {
  161. OnTransformEvent((DragPointLight)sender, Rotate);
  162. }
  163. private void OnScale(object sender, EventArgs args)
  164. {
  165. OnTransformEvent((DragPointLight)sender, Scale);
  166. }
  167. private void OnTransformEvent(DragPointLight light, EventHandler handler)
  168. {
  169. if (light.IsActiveLight)
  170. {
  171. handler?.Invoke(this, EventArgs.Empty);
  172. }
  173. }
  174. private void OnSelect(object sender, EventArgs args)
  175. {
  176. DragPointLight theLight = (DragPointLight)sender;
  177. int select = lightList.FindIndex(light => light == theLight);
  178. if (select >= 0)
  179. {
  180. this.SelectedLightIndex = select;
  181. this.Select?.Invoke(this, EventArgs.Empty);
  182. }
  183. }
  184. private void OnListModified()
  185. {
  186. ListModified?.Invoke(this, EventArgs.Empty);
  187. }
  188. private void OnCubeActive(object sender, EventArgs args)
  189. {
  190. foreach (DragPointLight dragPoint in lightList)
  191. {
  192. dragPoint.gameObject.SetActive(CubeActive);
  193. }
  194. }
  195. }
  196. }