HandPresetPane.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. namespace COM3D2.MeidoPhotoStudio.Plugin
  7. {
  8. internal class HandPresetPane : BasePane
  9. {
  10. private MeidoManager meidoManager;
  11. private Dropdown presetCategoryDropdown;
  12. private Button nextCategoryButton;
  13. private Button previousCategoryButton;
  14. private Dropdown presetDropdown;
  15. private Button nextPresetButton;
  16. private Button previousPresetButton;
  17. private Button leftHandButton;
  18. private Button rightHandButton;
  19. private string SelectedCategory => Constants.CustomHandGroupList[presetCategoryDropdown.SelectedItemIndex];
  20. private List<string> CurrentPresetList => Constants.CustomHandDict[SelectedCategory];
  21. private string CurrentPreset => CurrentPresetList[presetDropdown.SelectedItemIndex];
  22. private string previousCategory;
  23. private bool presetListEnabled = true;
  24. public HandPresetPane(MeidoManager meidoManager)
  25. {
  26. Constants.customHandChange += SaveHandEnd;
  27. this.meidoManager = meidoManager;
  28. this.presetCategoryDropdown = new Dropdown(Constants.CustomHandGroupList.ToArray());
  29. this.presetCategoryDropdown.SelectionChange += (s, a) => ChangePresetCategory();
  30. this.nextCategoryButton = new Button(">");
  31. this.nextCategoryButton.ControlEvent += (s, a) => this.presetCategoryDropdown.Step(1);
  32. this.previousCategoryButton = new Button("<");
  33. this.previousCategoryButton.ControlEvent += (s, a) =>
  34. {
  35. this.presetCategoryDropdown.Step(-1);
  36. };
  37. this.presetDropdown = new Dropdown(UIPresetList());
  38. this.nextPresetButton = new Button(">");
  39. this.nextPresetButton.ControlEvent += (s, a) => this.presetDropdown.Step(1);
  40. this.previousPresetButton = new Button("<");
  41. this.previousPresetButton.ControlEvent += (s, a) => this.presetDropdown.Step(-1);
  42. this.leftHandButton = new Button(Translation.Get("handPane", "leftHand"));
  43. this.leftHandButton.ControlEvent += (s, a) => SetHandPreset(right: false);
  44. this.rightHandButton = new Button(Translation.Get("handPane", "rightHand"));
  45. this.rightHandButton.ControlEvent += (s, a) => SetHandPreset(right: true);
  46. this.previousCategory = SelectedCategory;
  47. this.presetListEnabled = CurrentPresetList.Count > 0;
  48. }
  49. protected override void ReloadTranslation()
  50. {
  51. this.leftHandButton.Label = Translation.Get("handPane", "leftHand");
  52. this.rightHandButton.Label = Translation.Get("handPane", "rightHand");
  53. if (CurrentPresetList.Count == 0) this.presetDropdown.SetDropdownItems(UIPresetList());
  54. }
  55. public override void Draw()
  56. {
  57. GUILayoutOption dropdownWidth = GUILayout.Width(156f);
  58. GUILayoutOption noExpandWidth = GUILayout.ExpandWidth(false);
  59. GUI.enabled = this.meidoManager.HasActiveMeido;
  60. GUILayout.BeginHorizontal();
  61. this.presetCategoryDropdown.Draw(dropdownWidth);
  62. this.previousCategoryButton.Draw(noExpandWidth);
  63. this.nextCategoryButton.Draw(noExpandWidth);
  64. GUILayout.EndHorizontal();
  65. GUI.enabled = GUI.enabled && presetListEnabled;
  66. GUILayout.BeginHorizontal();
  67. this.presetDropdown.Draw(dropdownWidth);
  68. this.previousPresetButton.Draw(noExpandWidth);
  69. this.nextPresetButton.Draw(noExpandWidth);
  70. GUILayout.EndHorizontal();
  71. GUILayout.BeginHorizontal();
  72. this.rightHandButton.Draw();
  73. this.leftHandButton.Draw();
  74. GUILayout.EndHorizontal();
  75. GUI.enabled = true;
  76. }
  77. private void ChangePresetCategory()
  78. {
  79. presetListEnabled = CurrentPresetList.Count > 0;
  80. if (previousCategory == SelectedCategory)
  81. {
  82. this.presetDropdown.SelectedItemIndex = 0;
  83. }
  84. else
  85. {
  86. previousCategory = SelectedCategory;
  87. this.presetDropdown.SetDropdownItems(UIPresetList(), 0);
  88. }
  89. }
  90. private void SetHandPreset(bool right = false)
  91. {
  92. if (!meidoManager.HasActiveMeido) return;
  93. this.meidoManager.ActiveMeido.SetHandPreset(CurrentPreset, right);
  94. }
  95. private void SaveHandEnd(object sender, CustomPoseEventArgs args)
  96. {
  97. this.presetCategoryDropdown.SetDropdownItems(
  98. Constants.CustomHandGroupList.ToArray(), Constants.CustomHandGroupList.IndexOf(args.Category)
  99. );
  100. this.presetDropdown.SetDropdownItems(UIPresetList(), CurrentPresetList.IndexOf(args.Path));
  101. }
  102. private string[] UIPresetList()
  103. {
  104. if (CurrentPresetList.Count == 0) return new[] { Translation.Get("handPane", "noPresetsMessage") };
  105. else return CurrentPresetList.Select(file => Path.GetFileNameWithoutExtension(file)).ToArray();
  106. }
  107. }
  108. }