BackgroundSelectorPane.cs 2.6 KB

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