BackgroundSelectorPane.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using UnityEngine;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. namespace COM3D2.MeidoPhotoStudio.Plugin
  5. {
  6. public class BackgroundSelectorPane : BasePane
  7. {
  8. private readonly EnvironmentManager environmentManager;
  9. private readonly Dropdown bgDropdown;
  10. private readonly Button prevBGButton;
  11. private readonly Button nextBGButton;
  12. public BackgroundSelectorPane(EnvironmentManager environmentManager)
  13. {
  14. this.environmentManager = environmentManager;
  15. int theaterIndex = Constants.BGList.FindIndex(bg => bg == EnvironmentManager.defaultBg);
  16. List<string> bgList = new List<string>(Translation.GetList("bgNames", Constants.BGList));
  17. if (Constants.MyRoomCustomBGIndex >= 0)
  18. {
  19. bgList.AddRange(Constants.MyRoomCustomBGList.Select(kvp => kvp.Value));
  20. }
  21. bgDropdown = new Dropdown(bgList.ToArray(), theaterIndex);
  22. bgDropdown.SelectionChange += (s, a) => ChangeBackground();
  23. prevBGButton = new Button("<");
  24. prevBGButton.ControlEvent += (s, a) => bgDropdown.Step(-1);
  25. nextBGButton = new Button(">");
  26. nextBGButton.ControlEvent += (s, a) => bgDropdown.Step(1);
  27. }
  28. protected override void ReloadTranslation()
  29. {
  30. List<string> bgList = new List<string>(Translation.GetList("bgNames", Constants.BGList));
  31. if (Constants.MyRoomCustomBGIndex >= 0)
  32. {
  33. bgList.AddRange(Constants.MyRoomCustomBGList.Select(kvp => kvp.Value));
  34. }
  35. updating = true;
  36. bgDropdown.SetDropdownItems(bgList.ToArray());
  37. updating = false;
  38. }
  39. public override void Draw()
  40. {
  41. const float buttonHeight = 30;
  42. GUILayoutOption[] arrowLayoutOptions = {
  43. GUILayout.Width(buttonHeight),
  44. GUILayout.Height(buttonHeight)
  45. };
  46. const float dropdownButtonWidth = 153f;
  47. GUILayoutOption[] dropdownLayoutOptions = new GUILayoutOption[] {
  48. GUILayout.Height(buttonHeight),
  49. GUILayout.Width(dropdownButtonWidth)
  50. };
  51. GUILayout.BeginHorizontal();
  52. prevBGButton.Draw(arrowLayoutOptions);
  53. bgDropdown.Draw(dropdownLayoutOptions);
  54. nextBGButton.Draw(arrowLayoutOptions);
  55. GUILayout.EndHorizontal();
  56. }
  57. private void ChangeBackground()
  58. {
  59. if (updating) return;
  60. int selectedIndex = bgDropdown.SelectedItemIndex;
  61. bool isCreative = bgDropdown.SelectedItemIndex >= Constants.MyRoomCustomBGIndex;
  62. string bg = isCreative
  63. ? Constants.MyRoomCustomBGList[selectedIndex - Constants.MyRoomCustomBGIndex].Key
  64. : Constants.BGList[selectedIndex];
  65. environmentManager.ChangeBackground(bg, isCreative);
  66. }
  67. }
  68. }