LightsPane.cs 11 KB

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