LightManager.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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
  8. {
  9. public const string header = "LIGHT";
  10. private List<DragPointLight> lightList = new List<DragPointLight>();
  11. private int selectedLightIndex = 0;
  12. public int SelectedLightIndex
  13. {
  14. get => selectedLightIndex;
  15. set
  16. {
  17. selectedLightIndex = Mathf.Clamp(value, 0, lightList.Count - 1);
  18. lightList[SelectedLightIndex].IsActiveLight = true;
  19. }
  20. }
  21. public string[] LightNameList => lightList.Select(light => LightName(light.Name)).ToArray();
  22. public string ActiveLightName => LightName(lightList[SelectedLightIndex].Name);
  23. public DragPointLight CurrentLight
  24. {
  25. get
  26. {
  27. return lightList[SelectedLightIndex];
  28. }
  29. }
  30. public event EventHandler Rotate;
  31. public event EventHandler Scale;
  32. public event EventHandler ListModified;
  33. public event EventHandler Select;
  34. // TODO: enabling and disabling gizmos for a variety of dragpoints
  35. public void Serialize(System.IO.BinaryWriter binaryWriter)
  36. {
  37. binaryWriter.Write(header);
  38. binaryWriter.Write(lightList.Count);
  39. foreach (DragPointLight light in lightList)
  40. {
  41. light.Serialize(binaryWriter);
  42. }
  43. }
  44. public void Deserialize(System.IO.BinaryReader binaryReader)
  45. {
  46. ClearLights();
  47. int numberOfLights = binaryReader.ReadInt32();
  48. lightList[0].Deserialize(binaryReader);
  49. for (int i = 1; i < numberOfLights; i++)
  50. {
  51. AddLight();
  52. lightList[i].Deserialize(binaryReader);
  53. }
  54. }
  55. public void Activate()
  56. {
  57. GameMain.Instance.MainCamera.GetComponent<Camera>().backgroundColor = Color.black;
  58. AddLight(GameMain.Instance.MainLight.gameObject, true);
  59. }
  60. public void Deactivate()
  61. {
  62. for (int i = 0; i < lightList.Count; i++)
  63. {
  64. DestroyLight(lightList[i]);
  65. }
  66. selectedLightIndex = 0;
  67. lightList.Clear();
  68. GameMain.Instance.MainLight.Reset();
  69. Light mainLight = GameMain.Instance.MainLight.GetComponent<Light>();
  70. mainLight.type = LightType.Directional;
  71. DragPointLight.SetLightProperties(mainLight, new LightProperty());
  72. }
  73. public void Update() { }
  74. public void AddLight(GameObject lightGo = null, bool isMain = false)
  75. {
  76. GameObject go = lightGo ?? new GameObject();
  77. DragPointLight light = DragPoint.Make<DragPointLight>(
  78. PrimitiveType.Cube, Vector3.one * 0.12f, DragPoint.LightBlue
  79. );
  80. light.Initialize(() => go.transform.position, () => go.transform.eulerAngles);
  81. light.Set(go.transform);
  82. light.IsMain = isMain;
  83. light.Rotate += OnRotate;
  84. light.Scale += OnScale;
  85. light.Delete += OnDelete;
  86. light.Select += OnSelect;
  87. lightList.Add(light);
  88. CurrentLight.IsActiveLight = false;
  89. SelectedLightIndex = lightList.Count;
  90. OnListModified();
  91. }
  92. public void DeleteActiveLight()
  93. {
  94. if (selectedLightIndex == 0) return;
  95. DeleteLight(SelectedLightIndex);
  96. }
  97. public void DeleteLight(int lightIndex, bool noUpdate = false)
  98. {
  99. if (lightIndex == 0) return;
  100. DestroyLight(lightList[lightIndex]);
  101. lightList.RemoveAt(lightIndex);
  102. if (lightIndex <= SelectedLightIndex) SelectedLightIndex -= 1;
  103. if (noUpdate) return;
  104. OnListModified();
  105. }
  106. public void SetColourModeActive(bool isColourMode)
  107. {
  108. lightList[0].IsColourMode = isColourMode;
  109. }
  110. public void ClearLights()
  111. {
  112. for (int i = lightList.Count - 1; i > 0; i--)
  113. {
  114. DeleteLight(i);
  115. }
  116. selectedLightIndex = 0;
  117. }
  118. private void DestroyLight(DragPointLight light)
  119. {
  120. if (light == null) return;
  121. light.Rotate -= OnRotate;
  122. light.Scale -= OnScale;
  123. light.Delete -= OnDelete;
  124. light.Select -= OnSelect;
  125. GameObject.Destroy(light.gameObject);
  126. }
  127. private string LightName(string name)
  128. {
  129. return Translation.Get("lightType", name);
  130. }
  131. private void OnDelete(object sender, EventArgs args)
  132. {
  133. DragPointLight theLight = (DragPointLight)sender;
  134. for (int i = 1; i < lightList.Count; i++)
  135. {
  136. DragPointLight light = lightList[i];
  137. if (light == theLight)
  138. {
  139. DeleteLight(i);
  140. return;
  141. }
  142. }
  143. }
  144. private void OnRotate(object sender, EventArgs args)
  145. {
  146. OnTransformEvent((DragPointLight)sender, Rotate);
  147. }
  148. private void OnScale(object sender, EventArgs args)
  149. {
  150. OnTransformEvent((DragPointLight)sender, Scale);
  151. }
  152. private void OnTransformEvent(DragPointLight light, EventHandler handler)
  153. {
  154. if (light.IsActiveLight)
  155. {
  156. handler?.Invoke(this, EventArgs.Empty);
  157. }
  158. }
  159. private void OnSelect(object sender, EventArgs args)
  160. {
  161. DragPointLight theLight = (DragPointLight)sender;
  162. int select = lightList.FindIndex(light => light == theLight);
  163. if (select >= 0)
  164. {
  165. this.SelectedLightIndex = select;
  166. this.Select?.Invoke(this, EventArgs.Empty);
  167. }
  168. }
  169. private void OnListModified()
  170. {
  171. ListModified?.Invoke(this, EventArgs.Empty);
  172. }
  173. }
  174. }