HandPresetPane.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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("Left");
  43. this.leftHandButton.ControlEvent += (s, a) => SetHandPreset(right: false);
  44. this.rightHandButton = new Button("Right");
  45. this.rightHandButton.ControlEvent += (s, a) => SetHandPreset(right: true);
  46. this.previousCategory = SelectedCategory;
  47. this.presetListEnabled = CurrentPresetList.Count > 0;
  48. }
  49. public override void Draw()
  50. {
  51. GUILayoutOption dropdownWidth = GUILayout.Width(156f);
  52. GUILayoutOption noExpandWidth = GUILayout.ExpandWidth(false);
  53. GUI.enabled = this.meidoManager.HasActiveMeido;
  54. GUILayout.BeginHorizontal();
  55. this.presetCategoryDropdown.Draw(dropdownWidth);
  56. this.previousCategoryButton.Draw(noExpandWidth);
  57. this.nextCategoryButton.Draw(noExpandWidth);
  58. GUILayout.EndHorizontal();
  59. GUI.enabled = GUI.enabled && presetListEnabled;
  60. GUILayout.BeginHorizontal();
  61. this.presetDropdown.Draw(dropdownWidth);
  62. this.previousPresetButton.Draw(noExpandWidth);
  63. this.nextPresetButton.Draw(noExpandWidth);
  64. GUILayout.EndHorizontal();
  65. GUILayout.BeginHorizontal();
  66. this.rightHandButton.Draw();
  67. this.leftHandButton.Draw();
  68. GUILayout.EndHorizontal();
  69. GUI.enabled = true;
  70. }
  71. private void ChangePresetCategory()
  72. {
  73. presetListEnabled = CurrentPresetList.Count > 0;
  74. if (previousCategory == SelectedCategory)
  75. {
  76. this.presetDropdown.SelectedItemIndex = 0;
  77. }
  78. else
  79. {
  80. previousCategory = SelectedCategory;
  81. this.presetDropdown.SetDropdownItems(UIPresetList(), 0);
  82. }
  83. }
  84. private void SetHandPreset(bool right = false)
  85. {
  86. if (!meidoManager.HasActiveMeido) return;
  87. this.meidoManager.ActiveMeido.SetHandPreset(CurrentPreset, right);
  88. }
  89. private void SaveHandEnd(object sender, CustomPoseEventArgs args)
  90. {
  91. this.presetCategoryDropdown.SetDropdownItems(
  92. Constants.CustomHandGroupList.ToArray(), Constants.CustomHandGroupList.IndexOf(args.Category)
  93. );
  94. this.presetDropdown.SetDropdownItems(UIPresetList(), CurrentPresetList.IndexOf(args.Path));
  95. }
  96. private string[] UIPresetList()
  97. {
  98. if (CurrentPresetList.Count == 0) return new[] { "No Hand Presets" };
  99. else return CurrentPresetList.Select(file => Path.GetFileNameWithoutExtension(file)).ToArray();
  100. }
  101. }
  102. }