UIKeyBinding.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using UnityEngine;
  3. [AddComponentMenu("NGUI/Interaction/Key Binding")]
  4. public class UIKeyBinding : MonoBehaviour
  5. {
  6. protected virtual void Start()
  7. {
  8. UIInput component = base.GetComponent<UIInput>();
  9. this.mIsInput = (component != null);
  10. if (component != null)
  11. {
  12. EventDelegate.Add(component.onSubmit, new EventDelegate.Callback(this.OnSubmit));
  13. }
  14. }
  15. protected virtual void OnSubmit()
  16. {
  17. if (UICamera.currentKey == this.keyCode && this.IsModifierActive())
  18. {
  19. this.mIgnoreUp = true;
  20. }
  21. }
  22. protected virtual bool IsModifierActive()
  23. {
  24. if (this.modifier == UIKeyBinding.Modifier.None)
  25. {
  26. return true;
  27. }
  28. if (this.modifier == UIKeyBinding.Modifier.Alt)
  29. {
  30. if (Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt))
  31. {
  32. return true;
  33. }
  34. }
  35. else if (this.modifier == UIKeyBinding.Modifier.Control)
  36. {
  37. if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
  38. {
  39. return true;
  40. }
  41. }
  42. else if (this.modifier == UIKeyBinding.Modifier.Shift && (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)))
  43. {
  44. return true;
  45. }
  46. return false;
  47. }
  48. protected virtual void Update()
  49. {
  50. if (this.keyCode == KeyCode.None || !this.IsModifierActive())
  51. {
  52. return;
  53. }
  54. if (this.action == UIKeyBinding.Action.PressAndClick || this.action == UIKeyBinding.Action.All)
  55. {
  56. if (UICamera.inputHasFocus)
  57. {
  58. return;
  59. }
  60. UICamera.currentTouch = UICamera.controller;
  61. UICamera.currentScheme = UICamera.ControlScheme.Mouse;
  62. UICamera.currentTouch.current = base.gameObject;
  63. if (Input.GetKeyDown(this.keyCode))
  64. {
  65. this.mPress = true;
  66. this.OnBindingPress(true);
  67. }
  68. if (Input.GetKeyUp(this.keyCode))
  69. {
  70. this.OnBindingPress(false);
  71. if (this.mPress)
  72. {
  73. this.OnBindingClick();
  74. this.mPress = false;
  75. }
  76. }
  77. UICamera.currentTouch.current = null;
  78. }
  79. if ((this.action == UIKeyBinding.Action.Select || this.action == UIKeyBinding.Action.All) && Input.GetKeyUp(this.keyCode))
  80. {
  81. if (this.mIsInput)
  82. {
  83. if (!this.mIgnoreUp && !UICamera.inputHasFocus)
  84. {
  85. UICamera.selectedObject = base.gameObject;
  86. }
  87. this.mIgnoreUp = false;
  88. }
  89. else
  90. {
  91. UICamera.selectedObject = base.gameObject;
  92. }
  93. }
  94. }
  95. protected virtual void OnBindingPress(bool pressed)
  96. {
  97. UICamera.Notify(base.gameObject, "OnPress", pressed);
  98. }
  99. protected virtual void OnBindingClick()
  100. {
  101. UICamera.Notify(base.gameObject, "OnClick", null);
  102. }
  103. public KeyCode keyCode;
  104. public UIKeyBinding.Modifier modifier;
  105. public UIKeyBinding.Action action;
  106. private bool mIgnoreUp;
  107. private bool mIsInput;
  108. private bool mPress;
  109. public enum Action
  110. {
  111. PressAndClick,
  112. Select,
  113. All
  114. }
  115. public enum Modifier
  116. {
  117. None,
  118. Shift,
  119. Control,
  120. Alt
  121. }
  122. }