LightsPane.cs 14 KB

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