LightManager.cs 6.4 KB

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