LightManager.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Rendering;
  6. namespace COM3D2.MeidoPhotoStudio.Plugin
  7. {
  8. using static MPSLight;
  9. internal class LightManager
  10. {
  11. private List<MPSLight> LightList { get; set; } = new List<MPSLight>();
  12. private int selectedLightIndex = 0;
  13. public int SelectedLightIndex
  14. {
  15. get => selectedLightIndex;
  16. set
  17. {
  18. selectedLightIndex = Mathf.Clamp(value, 0, LightList.Count - 1);
  19. LightList[SelectedLightIndex].isActiveLight = true;
  20. }
  21. }
  22. public string[] LightNameList => LightList.Select(light => LightName(light.Name)).ToArray();
  23. public string ActiveLightName => LightName(LightList[SelectedLightIndex].Name);
  24. public MPSLight CurrentLight
  25. {
  26. get
  27. {
  28. return LightList[SelectedLightIndex];
  29. }
  30. }
  31. public event EventHandler Rotate;
  32. public event EventHandler Scale;
  33. public event EventHandler ListModified;
  34. public event EventHandler Select;
  35. private DragType dragTypeOld = DragType.None;
  36. private DragType currentDragType = DragType.None;
  37. private bool gizmoActive = false;
  38. enum DragType
  39. {
  40. None, Move, Rotate, Scale, Delete, Select
  41. }
  42. public void Activate()
  43. {
  44. GameMain.Instance.MainCamera.GetComponent<Camera>().backgroundColor = Color.black;
  45. AddLight(GameMain.Instance.MainLight.gameObject, true);
  46. }
  47. public void Deactivate()
  48. {
  49. for (int i = 0; i < LightList.Count; i++)
  50. {
  51. DestroyLight(LightList[i]);
  52. }
  53. selectedLightIndex = 0;
  54. LightList.Clear();
  55. GameMain.Instance.MainLight.Reset();
  56. Light mainLight = GameMain.Instance.MainLight.GetComponent<Light>();
  57. mainLight.type = LightType.Directional;
  58. MPSLight.SetLightProperties(mainLight, new LightProperty());
  59. }
  60. public void Update()
  61. {
  62. if (Input.GetKey(KeyCode.Z))
  63. {
  64. currentDragType = DragType.Move;
  65. }
  66. else if (Input.GetKey(KeyCode.X))
  67. {
  68. currentDragType = DragType.Rotate;
  69. }
  70. else if (Input.GetKey(KeyCode.C))
  71. {
  72. currentDragType = DragType.Scale;
  73. }
  74. else if (Input.GetKey(KeyCode.D))
  75. {
  76. currentDragType = DragType.Delete;
  77. }
  78. else if (Input.GetKey(KeyCode.A))
  79. {
  80. currentDragType = DragType.Select;
  81. }
  82. else
  83. {
  84. currentDragType = DragType.None;
  85. }
  86. if (currentDragType != dragTypeOld) UpdateDragType();
  87. dragTypeOld = currentDragType;
  88. }
  89. private void UpdateDragType()
  90. {
  91. foreach (MPSLight light in LightList)
  92. {
  93. bool active;
  94. if (currentDragType >= DragType.Delete || currentDragType == DragType.None)
  95. {
  96. if (currentDragType == DragType.Delete)
  97. {
  98. active = !light.IsMain;
  99. }
  100. else
  101. {
  102. active = currentDragType == DragType.Select;
  103. }
  104. }
  105. else
  106. {
  107. if (light.SelectedLightType == MPSLightType.Normal)
  108. {
  109. active = false;
  110. }
  111. else if (light.SelectedLightType == MPSLightType.Point)
  112. {
  113. active = currentDragType != DragType.Rotate;
  114. }
  115. else
  116. {
  117. active = true;
  118. }
  119. }
  120. light.DragLight.SetDragProp(gizmoActive && active, active, active);
  121. }
  122. }
  123. public void AddLight(GameObject lightGo = null, bool isMain = false)
  124. {
  125. MPSLight light = new MPSLight(lightGo, isMain);
  126. light.Rotate += OnRotate;
  127. light.Scale += OnScale;
  128. light.Delete += OnDelete;
  129. light.Select += OnSelect;
  130. LightList.Add(light);
  131. LightList[SelectedLightIndex].isActiveLight = false;
  132. SelectedLightIndex = LightList.Count;
  133. OnListModified();
  134. }
  135. public void DeleteActiveLight()
  136. {
  137. if (selectedLightIndex == 0) return;
  138. DeleteLight(SelectedLightIndex);
  139. }
  140. public void DeleteLight(int lightIndex, bool noUpdate = false)
  141. {
  142. if (lightIndex == 0) return;
  143. DestroyLight(LightList[lightIndex]);
  144. LightList.RemoveAt(lightIndex);
  145. if (lightIndex <= SelectedLightIndex) SelectedLightIndex -= 1;
  146. if (noUpdate) return;
  147. OnListModified();
  148. }
  149. public void SetColourModeActive(bool isColourMode)
  150. {
  151. LightList[0].IsColourMode = isColourMode;
  152. }
  153. public void ClearLights()
  154. {
  155. for (int i = LightList.Count - 1; i > 0; i--)
  156. {
  157. DeleteLight(i);
  158. }
  159. selectedLightIndex = 0;
  160. }
  161. private void DestroyLight(MPSLight light)
  162. {
  163. light.Rotate -= OnRotate;
  164. light.Scale -= OnScale;
  165. light.Delete -= OnDelete;
  166. light.Select -= OnSelect;
  167. light.Destroy();
  168. }
  169. private string LightName(string name)
  170. {
  171. return Translation.Get("lightType", name);
  172. }
  173. private void OnDelete(object sender, EventArgs args)
  174. {
  175. MPSLight theLight = (MPSLight)sender;
  176. for (int i = 1; i < LightList.Count; i++)
  177. {
  178. MPSLight light = LightList[i];
  179. if (light == theLight)
  180. {
  181. DeleteLight(i);
  182. return;
  183. }
  184. }
  185. }
  186. private void OnRotate(object sender, EventArgs args)
  187. {
  188. OnTransformEvent((MPSLight)sender, Rotate);
  189. }
  190. private void OnScale(object sender, EventArgs args)
  191. {
  192. OnTransformEvent((MPSLight)sender, Scale);
  193. }
  194. private void OnTransformEvent(MPSLight light, EventHandler handler)
  195. {
  196. if (light.isActiveLight)
  197. {
  198. handler?.Invoke(this, EventArgs.Empty);
  199. }
  200. }
  201. private void OnSelect(object sender, EventArgs args)
  202. {
  203. MPSLight theLight = (MPSLight)sender;
  204. int select = LightList.FindIndex(light => light == theLight);
  205. if (select >= 0)
  206. {
  207. this.SelectedLightIndex = select;
  208. this.Select?.Invoke(this, EventArgs.Empty);
  209. }
  210. }
  211. private void OnListModified()
  212. {
  213. ListModified?.Invoke(this, EventArgs.Empty);
  214. }
  215. }
  216. }