LightsPane.cs 11 KB

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