KeyRebindButton.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. internal class KeyRebindButton : BaseControl
  6. {
  7. private Button button;
  8. private bool listening = false;
  9. private KeyCode keyCode;
  10. public KeyCode KeyCode
  11. {
  12. get => keyCode;
  13. set
  14. {
  15. keyCode = value;
  16. button.Label = keyCode.ToString();
  17. }
  18. }
  19. public KeyRebindButton(KeyCode code)
  20. {
  21. button = new Button(code.ToString());
  22. button.ControlEvent += (s, a) => StartListening();
  23. }
  24. public void Draw(GUIStyle buttonStyle, params GUILayoutOption[] layoutOptions)
  25. {
  26. GUI.enabled = !listening && !InputManager.Listening;
  27. button.Draw(buttonStyle, layoutOptions);
  28. GUI.enabled = true;
  29. }
  30. public override void Draw(params GUILayoutOption[] layoutOptions)
  31. {
  32. GUIStyle buttonStyle = new GUIStyle(GUI.skin.button);
  33. Draw(buttonStyle, layoutOptions);
  34. }
  35. private void StartListening()
  36. {
  37. listening = true;
  38. InputManager.StartListening();
  39. InputManager.KeyChange += KeyChange;
  40. }
  41. private void KeyChange(object sender, EventArgs args)
  42. {
  43. listening = false;
  44. KeyCode = InputManager.CurrentKeyCode;
  45. InputManager.KeyChange -= KeyChange;
  46. OnControlEvent(EventArgs.Empty);
  47. }
  48. }
  49. }