keyboard_textures.gd 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright (C) 2022-2023 John Pennycook
  2. # SPDX-License-Identifier: MIT
  3. @tool
  4. class_name KeyboardTextures
  5. extends Resource
  6. ## Textures used by a [KeyPrompt] or [ActionPrompt].
  7. ##
  8. ## Textures used by a [KeyPrompt] or [ActionPrompt]. Stores a texture for each
  9. ## keyboard scancode.
  10. ## A mapping from keyboard strings (as returned by
  11. ## [method OS.get_keycode_string]) to textures.
  12. var textures: Dictionary = {}
  13. func _init():
  14. for k in KeyPrompt._KEYS:
  15. textures[OS.get_keycode_string(k)] = null
  16. ## Return the [Texture2D] associated with the specified [InputEvent], or null.
  17. func get_texture(event: InputEvent) -> Texture2D:
  18. if not event is InputEventKey:
  19. return null
  20. var key_event := event as InputEventKey
  21. var scancode := key_event.keycode
  22. if scancode == 0:
  23. scancode = key_event.physical_keycode
  24. return textures[OS.get_keycode_string(scancode)]
  25. func _get(property):
  26. if property in textures.keys():
  27. return textures[property]
  28. return null
  29. func _set(property, value):
  30. if property in textures.keys():
  31. textures[property] = value
  32. return true
  33. return false
  34. func _get_property_list():
  35. var properties = []
  36. for k in KeyPrompt._KEYS:
  37. properties.append(
  38. {
  39. name = OS.get_keycode_string(k),
  40. type = TYPE_OBJECT,
  41. hint = PROPERTY_HINT_RESOURCE_TYPE,
  42. hint_string = "Texture2D"
  43. }
  44. )
  45. return properties