LightManager.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. light.Rotate -= OnRotate;
  99. light.Scale -= OnScale;
  100. light.Delete -= OnDelete;
  101. light.Select -= OnSelect;
  102. GameObject.Destroy(light.gameObject);
  103. }
  104. private string LightName(string name)
  105. {
  106. return Translation.Get("lightType", name);
  107. }
  108. private void OnDelete(object sender, EventArgs args)
  109. {
  110. DragPointLight theLight = (DragPointLight)sender;
  111. for (int i = 1; i < lightList.Count; i++)
  112. {
  113. DragPointLight light = lightList[i];
  114. if (light == theLight)
  115. {
  116. DeleteLight(i);
  117. return;
  118. }
  119. }
  120. }
  121. private void OnRotate(object sender, EventArgs args)
  122. {
  123. OnTransformEvent((DragPointLight)sender, Rotate);
  124. }
  125. private void OnScale(object sender, EventArgs args)
  126. {
  127. OnTransformEvent((DragPointLight)sender, Scale);
  128. }
  129. private void OnTransformEvent(DragPointLight light, EventHandler handler)
  130. {
  131. if (light.IsActiveLight)
  132. {
  133. handler?.Invoke(this, EventArgs.Empty);
  134. }
  135. }
  136. private void OnSelect(object sender, EventArgs args)
  137. {
  138. DragPointLight theLight = (DragPointLight)sender;
  139. int select = lightList.FindIndex(light => light == theLight);
  140. if (select >= 0)
  141. {
  142. this.SelectedLightIndex = select;
  143. this.Select?.Invoke(this, EventArgs.Empty);
  144. }
  145. }
  146. private void OnListModified()
  147. {
  148. ListModified?.Invoke(this, EventArgs.Empty);
  149. }
  150. }
  151. }