joy_mapping.gd 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. extends RefCounted
  2. class_name JoyMapping
  3. enum TYPE {NONE, BTN, AXIS}
  4. enum AXIS {FULL, HALF_PLUS, HALF_MINUS}
  5. const PLATFORMS = {
  6. # From gamecontrollerdb
  7. "Windows": "Windows",
  8. "OSX": "Mac OS X",
  9. "X11": "Linux",
  10. "Android": "Android",
  11. "iOS": "iOS",
  12. # Godot customs
  13. "HTML5": "Javascript",
  14. "UWP": "UWP",
  15. # 4.x compat
  16. "Linux": "Linux",
  17. "FreeBSD": "Linux",
  18. "NetBSD": "Linux",
  19. "BSD": "Linux",
  20. "macOS": "Mac OS X",
  21. }
  22. const BASE = {
  23. # Buttons
  24. "a": JOY_BUTTON_A,
  25. "b": JOY_BUTTON_B,
  26. "y": JOY_BUTTON_Y,
  27. "x": JOY_BUTTON_X,
  28. "start": JOY_BUTTON_START,
  29. "back": JOY_BUTTON_BACK,
  30. "leftstick": JOY_BUTTON_LEFT_STICK,
  31. "rightstick": JOY_BUTTON_RIGHT_STICK,
  32. "leftshoulder": JOY_BUTTON_LEFT_SHOULDER,
  33. "rightshoulder": JOY_BUTTON_RIGHT_SHOULDER,
  34. "dpup": JOY_BUTTON_DPAD_UP,
  35. "dpleft": JOY_BUTTON_DPAD_LEFT,
  36. "dpdown": JOY_BUTTON_DPAD_DOWN,
  37. "dpright": JOY_BUTTON_DPAD_RIGHT,
  38. # Axis
  39. "leftx": JOY_AXIS_LEFT_X,
  40. "lefty": JOY_AXIS_LEFT_Y,
  41. "rightx": JOY_AXIS_RIGHT_X,
  42. "righty": JOY_AXIS_RIGHT_Y,
  43. "lefttrigger": JOY_AXIS_TRIGGER_LEFT,
  44. "righttrigger": JOY_AXIS_TRIGGER_RIGHT,
  45. }
  46. const XBOX = {
  47. "a": "b0",
  48. "b": "b1",
  49. "y": "b3",
  50. "x": "b2",
  51. "start": "b7",
  52. "guide": "b8",
  53. "back": "b6",
  54. "leftstick": "b9",
  55. "rightstick": "b10",
  56. "leftshoulder": "b4",
  57. "rightshoulder": "b5",
  58. "dpup": "-a7",
  59. "dpleft":"-a6",
  60. "dpdown": "+a7",
  61. "dpright": "+a6",
  62. "leftx": "a0",
  63. "lefty": "a1",
  64. "rightx": "a3",
  65. "righty": "a4",
  66. "lefttrigger": "a2",
  67. "righttrigger": "a5",
  68. }
  69. const XBOX_OSX = {
  70. "a": "b11",
  71. "b": "b12",
  72. "y": "b14",
  73. "x": "b13",
  74. "start": "b4",
  75. "back": "b5",
  76. "leftstick": "b6",
  77. "rightstick": "b7",
  78. "leftshoulder": "b8",
  79. "rightshoulder": "b9",
  80. "dpup": "b0",
  81. "dpleft": "b2",
  82. "dpdown": "b1",
  83. "dpright": "b3",
  84. "leftx": "a0",
  85. "lefty": "a1",
  86. "rightx": "a2",
  87. "righty": "a3",
  88. "lefttrigger": "a4",
  89. "righttrigger":"a5",
  90. }
  91. var type = TYPE.NONE
  92. var idx = -1
  93. var axis = AXIS.FULL
  94. var inverted = false
  95. func _init(p_type = TYPE.NONE, p_idx = -1, p_axis = AXIS.FULL):
  96. type = p_type
  97. idx = p_idx
  98. axis = p_axis
  99. func _to_string():
  100. if type == TYPE.NONE:
  101. return ""
  102. var ts = "b" if type == TYPE.BTN else "a"
  103. var prefix = ""
  104. var suffix = "~" if inverted else ""
  105. match axis:
  106. AXIS.HALF_PLUS:
  107. prefix = "+"
  108. AXIS.HALF_MINUS:
  109. prefix = "-"
  110. return "%s%s%d%s" % [prefix, ts, idx, suffix]
  111. func to_human_string():
  112. if type == TYPE.BTN:
  113. return "Button %d" % idx
  114. if type == TYPE.AXIS:
  115. var prefix = ""
  116. match axis:
  117. AXIS.HALF_PLUS:
  118. prefix = "(+) "
  119. AXIS.HALF_MINUS:
  120. prefix = "(-) "
  121. var suffix = " (inverted)" if inverted else ""
  122. return "Axis %s%d%s" % [prefix, idx, suffix]
  123. return ""