LightManager.cs 5.3 KB

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