LightsPane.cs 12 KB

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