LightsPane.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace COM3D2.MeidoPhotoStudio.Plugin
  5. {
  6. using static DragPointLight;
  7. public class LightsPane : BasePane
  8. {
  9. private static readonly string[] lightTypes = { "normal", "spot", "point" };
  10. private readonly LightManager lightManager;
  11. private readonly Dictionary<LightProp, Slider> lightSlider;
  12. private readonly Dropdown lightDropdown;
  13. private readonly Button addLightButton;
  14. private readonly Button deleteLightButton;
  15. private readonly Button clearLightsButton;
  16. private readonly Button resetPropsButton;
  17. private readonly Button resetPositionButton;
  18. private readonly SelectionGrid lightTypeGrid;
  19. private readonly Toggle colorToggle;
  20. private readonly Toggle disableToggle;
  21. private MPSLightType currentLightType;
  22. private string lightHeader;
  23. private string resetLabel;
  24. private static readonly Dictionary<LightProp, SliderProp> lightSliderProp;
  25. private static readonly string[,] sliderNames = {
  26. { "lights", "x" }, { "lights", "y" }, { "lights", "intensity" }, { "lights", "shadow" },
  27. { "lights", "spot" }, { "lights", "range" }, { "backgroundWindow", "red" }, { "backgroundWindow", "green" },
  28. { "backgroundWindow", "blue" }
  29. };
  30. static LightsPane()
  31. {
  32. Vector3 rotation = LightProperty.DefaultRotation.eulerAngles;
  33. var range = GameMain.Instance.MainLight.GetComponent<Light>().range;
  34. lightSliderProp = new Dictionary<LightProp, SliderProp>
  35. {
  36. [LightProp.LightRotX] = new SliderProp(0f, 360f, rotation.x, rotation.x),
  37. [LightProp.LightRotY] = new SliderProp(0f, 360f, rotation.y, rotation.y),
  38. [LightProp.Intensity] = new SliderProp(0f, 2f, 0.95f, 0.95f),
  39. [LightProp.ShadowStrength] = new SliderProp(0f, 1f, 0.098f, 0.098f),
  40. [LightProp.Range] = new SliderProp(0f, 150f, range, range),
  41. [LightProp.SpotAngle] = new SliderProp(0f, 150f, 50f, 50f),
  42. [LightProp.Red] = new SliderProp(0f, 1f, 1f, 1f),
  43. [LightProp.Green] = new SliderProp(0f, 1f, 1f, 1f),
  44. [LightProp.Blue] = new SliderProp(0f, 1f, 1f, 1f),
  45. };
  46. }
  47. public LightsPane(LightManager lightManager)
  48. {
  49. this.lightManager = lightManager;
  50. this.lightManager.Rotate += (s, a) => UpdateRotation();
  51. this.lightManager.Scale += (s, a) => UpdateScale();
  52. this.lightManager.Select += (s, a) => UpdateCurrentLight();
  53. this.lightManager.ListModified += (s, a) => UpdateList();
  54. lightTypeGrid = new SelectionGrid(Translation.GetArray("lightType", lightTypes));
  55. lightTypeGrid.ControlEvent += (s, a) => SetCurrentLightType();
  56. lightDropdown = new Dropdown(new[] { "Main" });
  57. lightDropdown.SelectionChange += (s, a) => SetCurrentLight();
  58. addLightButton = new Button("+");
  59. addLightButton.ControlEvent += (s, a) => lightManager.AddLight();
  60. deleteLightButton = new Button(Translation.Get("lightsPane", "delete"));
  61. deleteLightButton.ControlEvent += (s, a) => lightManager.DeleteActiveLight();
  62. disableToggle = new Toggle(Translation.Get("lightsPane", "disable"));
  63. disableToggle.ControlEvent += (s, a) => lightManager.CurrentLight.IsDisabled = disableToggle.Value;
  64. clearLightsButton = new Button(Translation.Get("lightsPane", "clear"));
  65. clearLightsButton.ControlEvent += (s, a) => ClearLights();
  66. var numberOfLightProps = Enum.GetNames(typeof(LightProp)).Length;
  67. lightSlider = new Dictionary<LightProp, Slider>(numberOfLightProps);
  68. for (var i = 0; i < numberOfLightProps; i++)
  69. {
  70. var lightProp = (LightProp)i;
  71. SliderProp sliderProp = lightSliderProp[lightProp];
  72. var slider = new Slider(Translation.Get(sliderNames[i, 0], sliderNames[i, 1]), sliderProp)
  73. {
  74. HasTextField = true,
  75. HasReset = true
  76. };
  77. if (lightProp <= LightProp.LightRotY) slider.ControlEvent += (s, a) => SetLightRotation();
  78. else slider.ControlEvent += (s, a) => SetLightProp(lightProp, slider.Value);
  79. lightSlider[lightProp] = slider;
  80. }
  81. colorToggle = new Toggle(Translation.Get("lightsPane", "colour"));
  82. colorToggle.ControlEvent += (s, a) => SetColourMode();
  83. resetPropsButton = new Button(Translation.Get("lightsPane", "resetProperties"));
  84. resetPropsButton.ControlEvent += (s, a) => ResetLightProps();
  85. resetPositionButton = new Button(Translation.Get("lightsPane", "resetPosition"));
  86. resetPositionButton.ControlEvent += (s, a) => lightManager.CurrentLight.ResetLightPosition();
  87. lightHeader = Translation.Get("lightsPane", "header");
  88. resetLabel = Translation.Get("lightsPane", "resetLabel");
  89. }
  90. protected override void ReloadTranslation()
  91. {
  92. updating = true;
  93. lightTypeGrid.SetItems(Translation.GetArray("lightType", lightTypes));
  94. lightDropdown.SetDropdownItems(lightManager.LightNameList);
  95. deleteLightButton.Label = Translation.Get("lightsPane", "delete");
  96. disableToggle.Label = Translation.Get("lightsPane", "disable");
  97. clearLightsButton.Label = Translation.Get("lightsPane", "clear");
  98. for (var lightProp = LightProp.LightRotX; lightProp <= LightProp.Blue; lightProp++)
  99. {
  100. lightSlider[lightProp].Label =
  101. Translation.Get(sliderNames[(int)lightProp, 0], sliderNames[(int)lightProp, 1]);
  102. }
  103. colorToggle.Label = Translation.Get("lightsPane", "colour");
  104. resetPropsButton.Label = Translation.Get("lightsPane", "resetProperties");
  105. resetPositionButton.Label = Translation.Get("lightsPane", "resetPosition");
  106. lightHeader = Translation.Get("lightsPane", "header");
  107. resetLabel = Translation.Get("lightsPane", "resetLabel");
  108. updating = false;
  109. }
  110. private void SetColourMode()
  111. {
  112. lightManager.SetColourModeActive(colorToggle.Value);
  113. UpdatePane();
  114. }
  115. private void ClearLights()
  116. {
  117. lightManager.ClearLights();
  118. UpdatePane();
  119. }
  120. private void SetCurrentLight()
  121. {
  122. if (updating) return;
  123. lightManager.SelectedLightIndex = lightDropdown.SelectedItemIndex;
  124. UpdatePane();
  125. }
  126. private void ResetLightProps()
  127. {
  128. lightManager.CurrentLight.ResetLightProps();
  129. UpdatePane();
  130. }
  131. private void SetCurrentLightType()
  132. {
  133. if (updating) return;
  134. currentLightType = (MPSLightType)lightTypeGrid.SelectedItemIndex;
  135. DragPointLight currentLight = lightManager.CurrentLight;
  136. currentLight.SetLightType(currentLightType);
  137. lightDropdown.SetDropdownItem(lightManager.ActiveLightName);
  138. UpdatePane();
  139. }
  140. private void SetLightProp(LightProp prop, float value)
  141. {
  142. if (updating) return;
  143. lightManager.CurrentLight.SetProp(prop, value);
  144. }
  145. private void SetLightRotation()
  146. {
  147. if (updating) return;
  148. var lightRotX = lightSlider[LightProp.LightRotX].Value;
  149. var lightRotY = lightSlider[LightProp.LightRotY].Value;
  150. lightManager.CurrentLight.SetRotation(lightRotX, lightRotY);
  151. }
  152. private void UpdateList()
  153. {
  154. string[] newList = lightManager.LightNameList;
  155. lightDropdown.SetDropdownItems(newList, lightManager.SelectedLightIndex);
  156. UpdatePane();
  157. }
  158. private void UpdateRotation()
  159. {
  160. updating = true;
  161. LightProperty prop = lightManager.CurrentLight.CurrentLightProperty;
  162. lightSlider[LightProp.LightRotX].Value = prop.Rotation.eulerAngles.x;
  163. lightSlider[LightProp.LightRotY].Value = prop.Rotation.eulerAngles.y;
  164. updating = false;
  165. }
  166. private void UpdateScale()
  167. {
  168. updating = true;
  169. lightSlider[LightProp.SpotAngle].Value = lightManager.CurrentLight.CurrentLightProperty.SpotAngle;
  170. lightSlider[LightProp.Range].Value = lightManager.CurrentLight.CurrentLightProperty.Range;
  171. updating = false;
  172. }
  173. private void UpdateCurrentLight()
  174. {
  175. updating = true;
  176. lightDropdown.SelectedItemIndex = lightManager.SelectedLightIndex;
  177. updating = false;
  178. UpdatePane();
  179. }
  180. public override void UpdatePane()
  181. {
  182. updating = true;
  183. DragPointLight currentLight = lightManager.CurrentLight;
  184. currentLightType = currentLight.SelectedLightType;
  185. lightTypeGrid.SelectedItemIndex = (int)currentLightType;
  186. disableToggle.Value = currentLight.IsDisabled;
  187. lightSlider[LightProp.LightRotX].Value = currentLight.Rotation.eulerAngles.x;
  188. lightSlider[LightProp.LightRotY].Value = currentLight.Rotation.eulerAngles.y;
  189. lightSlider[LightProp.Intensity].Value = currentLight.Intensity;
  190. lightSlider[LightProp.ShadowStrength].Value = currentLight.ShadowStrength;
  191. lightSlider[LightProp.Range].Value = currentLight.Range;
  192. lightSlider[LightProp.SpotAngle].Value = currentLight.SpotAngle;
  193. lightSlider[LightProp.Red].Value = currentLight.LightColour.r;
  194. lightSlider[LightProp.Green].Value = currentLight.LightColour.g;
  195. lightSlider[LightProp.Blue].Value = currentLight.LightColour.b;
  196. updating = false;
  197. }
  198. public override void Draw()
  199. {
  200. var isMain = lightManager.SelectedLightIndex == 0;
  201. GUILayoutOption noExpandWidth = GUILayout.ExpandWidth(false);
  202. MpsGui.Header(lightHeader);
  203. MpsGui.WhiteLine();
  204. GUILayout.BeginHorizontal();
  205. lightDropdown.Draw(GUILayout.Width(84));
  206. addLightButton.Draw(noExpandWidth);
  207. GUILayout.FlexibleSpace();
  208. GUI.enabled = !isMain;
  209. deleteLightButton.Draw(noExpandWidth);
  210. GUI.enabled = true;
  211. clearLightsButton.Draw(noExpandWidth);
  212. GUILayout.EndHorizontal();
  213. bool isDisabled = !isMain && lightManager.CurrentLight.IsDisabled;
  214. GUILayout.BeginHorizontal();
  215. GUI.enabled = !isDisabled;
  216. lightTypeGrid.Draw(noExpandWidth);
  217. if (!isMain)
  218. {
  219. GUI.enabled = true;
  220. disableToggle.Draw();
  221. }
  222. GUILayout.EndHorizontal();
  223. GUI.enabled = !isDisabled;
  224. if (currentLightType != MPSLightType.Point)
  225. {
  226. lightSlider[LightProp.LightRotX].Draw();
  227. lightSlider[LightProp.LightRotY].Draw();
  228. }
  229. lightSlider[LightProp.Intensity].Draw();
  230. if (currentLightType == MPSLightType.Normal) lightSlider[LightProp.ShadowStrength].Draw();
  231. else lightSlider[LightProp.Range].Draw();
  232. if (currentLightType == MPSLightType.Spot) lightSlider[LightProp.SpotAngle].Draw();
  233. MpsGui.BlackLine();
  234. lightSlider[LightProp.Red].Draw();
  235. lightSlider[LightProp.Green].Draw();
  236. lightSlider[LightProp.Blue].Draw();
  237. if (lightManager.SelectedLightIndex == 0 && currentLightType == MPSLightType.Normal) colorToggle.Draw();
  238. GUILayout.BeginHorizontal();
  239. GUILayout.Label(resetLabel, noExpandWidth);
  240. resetPropsButton.Draw(noExpandWidth);
  241. resetPositionButton.Draw(noExpandWidth);
  242. GUILayout.EndHorizontal();
  243. GUI.enabled = true;
  244. }
  245. }
  246. }