LightsPane.cs 13 KB

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