LightManager.cs 5.7 KB

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