LightsPane.cs 13 KB

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