LightManager.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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>(PrimitiveType.Cube, Vector3.one * 0.12f);
  93. light.Initialize(() => go.transform.position, () => go.transform.eulerAngles);
  94. light.Set(go.transform);
  95. light.IsMain = isMain;
  96. light.Rotate += OnRotate;
  97. light.Scale += OnScale;
  98. light.Delete += OnDelete;
  99. light.Select += OnSelect;
  100. lightList.Add(light);
  101. CurrentLight.IsActiveLight = false;
  102. SelectedLightIndex = lightList.Count;
  103. OnListModified();
  104. }
  105. public void DeleteActiveLight()
  106. {
  107. if (selectedLightIndex == 0) return;
  108. DeleteLight(SelectedLightIndex);
  109. }
  110. public void DeleteLight(int lightIndex, bool noUpdate = false)
  111. {
  112. if (lightIndex == 0) return;
  113. DestroyLight(lightList[lightIndex]);
  114. lightList.RemoveAt(lightIndex);
  115. if (lightIndex <= SelectedLightIndex) SelectedLightIndex -= 1;
  116. if (noUpdate) return;
  117. OnListModified();
  118. }
  119. public void SetColourModeActive(bool isColourMode)
  120. {
  121. lightList[0].IsColourMode = isColourMode;
  122. }
  123. public void ClearLights()
  124. {
  125. for (int i = lightList.Count - 1; i > 0; i--)
  126. {
  127. DeleteLight(i);
  128. }
  129. selectedLightIndex = 0;
  130. }
  131. private void DestroyLight(DragPointLight light)
  132. {
  133. if (light == null) return;
  134. light.Rotate -= OnRotate;
  135. light.Scale -= OnScale;
  136. light.Delete -= OnDelete;
  137. light.Select -= OnSelect;
  138. GameObject.Destroy(light.gameObject);
  139. }
  140. private string LightName(string name)
  141. {
  142. return Translation.Get("lightType", name);
  143. }
  144. private void OnDelete(object sender, EventArgs args)
  145. {
  146. DragPointLight theLight = (DragPointLight)sender;
  147. for (int i = 1; i < lightList.Count; i++)
  148. {
  149. DragPointLight light = lightList[i];
  150. if (light == theLight)
  151. {
  152. DeleteLight(i);
  153. return;
  154. }
  155. }
  156. }
  157. private void OnRotate(object sender, EventArgs args)
  158. {
  159. OnTransformEvent((DragPointLight)sender, Rotate);
  160. }
  161. private void OnScale(object sender, EventArgs args)
  162. {
  163. OnTransformEvent((DragPointLight)sender, Scale);
  164. }
  165. private void OnTransformEvent(DragPointLight light, EventHandler handler)
  166. {
  167. if (light.IsActiveLight)
  168. {
  169. handler?.Invoke(this, EventArgs.Empty);
  170. }
  171. }
  172. private void OnSelect(object sender, EventArgs args)
  173. {
  174. DragPointLight theLight = (DragPointLight)sender;
  175. int select = lightList.FindIndex(light => light == theLight);
  176. if (select >= 0)
  177. {
  178. this.SelectedLightIndex = select;
  179. this.Select?.Invoke(this, EventArgs.Empty);
  180. }
  181. }
  182. private void OnListModified()
  183. {
  184. ListModified?.Invoke(this, EventArgs.Empty);
  185. }
  186. private void OnCubeActive(object sender, EventArgs args)
  187. {
  188. foreach (DragPointLight dragPoint in lightList)
  189. {
  190. dragPoint.gameObject.SetActive(CubeActive);
  191. }
  192. }
  193. }
  194. }