LightManager.cs 5.3 KB

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