key_prompt.gd 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # Copyright (C) 2022-2023 John Pennycook
  2. # SPDX-License-Identifier: MIT
  3. @tool
  4. @icon("res://addons/input_prompts/key_prompt/icon.svg")
  5. class_name KeyPrompt
  6. extends "res://addons/input_prompts/input_prompt.gd"
  7. ## Displays a prompt based on a keyboard scancode.
  8. ##
  9. ## Displays a prompt based on a keyboard scancode.
  10. ## The texture used for the prompt is determined automatically.
  11. ## [br][br]
  12. ## [b]Note[/b]: A [KeyPrompt] will never show joypad or mouse
  13. ## prompts. To automatically reflect the most recent input device, use
  14. ## [ActionPrompt] instead.
  15. # TODO: Find a way to replace this with standard functionality.
  16. # The Key enum is not accessible directly and can't be iterated over.
  17. const _KEYS = [
  18. KEY_ESCAPE,
  19. KEY_F1,
  20. KEY_F2,
  21. KEY_F3,
  22. KEY_F4,
  23. KEY_F5,
  24. KEY_F6,
  25. KEY_F7,
  26. KEY_F8,
  27. KEY_F9,
  28. KEY_F10,
  29. KEY_F11,
  30. KEY_F12,
  31. KEY_ASCIITILDE,
  32. KEY_EXCLAM,
  33. KEY_AT,
  34. KEY_NUMBERSIGN,
  35. KEY_1,
  36. KEY_2,
  37. KEY_3,
  38. KEY_4,
  39. KEY_5,
  40. KEY_6,
  41. KEY_7,
  42. KEY_8,
  43. KEY_9,
  44. KEY_0,
  45. KEY_MINUS,
  46. KEY_PLUS,
  47. KEY_EQUAL,
  48. KEY_UNDERSCORE,
  49. KEY_BAR,
  50. KEY_BACKSPACE,
  51. KEY_Q,
  52. KEY_W,
  53. KEY_E,
  54. KEY_R,
  55. KEY_T,
  56. KEY_Y,
  57. KEY_U,
  58. KEY_I,
  59. KEY_O,
  60. KEY_P,
  61. KEY_BRACKETLEFT,
  62. KEY_BRACKETRIGHT,
  63. KEY_BRACELEFT,
  64. KEY_BRACERIGHT,
  65. KEY_BACKSLASH,
  66. KEY_ENTER,
  67. KEY_A,
  68. KEY_S,
  69. KEY_D,
  70. KEY_F,
  71. KEY_G,
  72. KEY_H,
  73. KEY_J,
  74. KEY_K,
  75. KEY_L,
  76. KEY_APOSTROPHE,
  77. KEY_QUOTEDBL,
  78. KEY_COLON,
  79. KEY_SEMICOLON,
  80. KEY_ASTERISK,
  81. KEY_META,
  82. KEY_Z,
  83. KEY_X,
  84. KEY_C,
  85. KEY_V,
  86. KEY_B,
  87. KEY_N,
  88. KEY_M,
  89. KEY_LESS,
  90. KEY_GREATER,
  91. KEY_QUESTION,
  92. KEY_SLASH,
  93. KEY_UP,
  94. KEY_RIGHT,
  95. KEY_DOWN,
  96. KEY_LEFT,
  97. KEY_ALT,
  98. KEY_TAB,
  99. KEY_DELETE,
  100. KEY_END,
  101. KEY_NUMLOCK,
  102. KEY_PERIOD,
  103. KEY_DOLLAR,
  104. KEY_PERCENT,
  105. KEY_ASCIICIRCUM,
  106. KEY_PARENLEFT,
  107. KEY_PARENRIGHT,
  108. KEY_CTRL,
  109. KEY_CAPSLOCK,
  110. KEY_HOME,
  111. KEY_PAGEUP,
  112. KEY_PAGEDOWN,
  113. KEY_COMMA,
  114. KEY_MEDIARECORD,
  115. KEY_SPACE,
  116. KEY_SHIFT,
  117. KEY_INSERT,
  118. KEY_PRINT,
  119. KEY_SCROLLLOCK,
  120. KEY_PAUSE,
  121. KEY_MEDIAPLAY,
  122. KEY_MEDIASTOP,
  123. KEY_BACK,
  124. KEY_FORWARD,
  125. KEY_MEDIAPREVIOUS,
  126. KEY_MEDIANEXT,
  127. ]
  128. ## A keyboard scancode, such as [constant @GlobalScope.KEY_ESCAPE].
  129. var key := KEY_EXCLAM:
  130. set = _set_key
  131. func _ready():
  132. _update_icon()
  133. func _set_key(scancode: int):
  134. key = scancode
  135. var event := InputEventKey.new()
  136. event.keycode = scancode
  137. events = [event]
  138. _update_icon()
  139. func _update_icon():
  140. var textures := PromptManager.get_keyboard_textures()
  141. texture = textures.get_texture(events[0])
  142. queue_redraw()
  143. func _get_property_list():
  144. var properties = []
  145. properties.append(
  146. {
  147. name = "KeyPrompt",
  148. type = TYPE_NIL,
  149. usage = PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_SCRIPT_VARIABLE
  150. }
  151. )
  152. var keys: String = ""
  153. for k in _KEYS:
  154. if keys != "":
  155. keys += ","
  156. keys += "{0}:{1}".format([OS.get_keycode_string(k), k])
  157. properties.append(
  158. {name = "key", type = TYPE_INT, hint = PROPERTY_HINT_ENUM, hint_string = keys}
  159. )
  160. return properties