DeviceManager.gd 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. extends Object
  2. class_name DeviceManager
  3. #
  4. enum DeviceType
  5. {
  6. KEYBOARD = 0,
  7. JOYSTICK
  8. }
  9. enum ActionInfo
  10. {
  11. NAME = 0,
  12. DEVICE_TYPE,
  13. COUNT
  14. }
  15. #
  16. static var currentDeviceId : int = -1
  17. static var currentDeviceType : DeviceType = DeviceType.JOYSTICK if LauncherCommons.isMobile else DeviceType.KEYBOARD
  18. static var joyButton : Array = ["A", "B", "X", "Y", "Back", "Guide", "Start", "LSB", "RSB", "L", "R", "UP", "DOWN", "LEFT", "RIGHT", "Misc", "Paddle1", "Paddle2", "Paddle3", "Paddle4", "Touch"]
  19. static var actionNames : Dictionary[String, String] = {
  20. "ui_select" : "Select",
  21. "ui_focus_next" : "Focus Next",
  22. "ui_focus_prev" : "Focus Previous",
  23. "ui_page_up" : "Page Up",
  24. "ui_page_down" : "Page Down",
  25. "ui_home" : "Home",
  26. "ui_end" : "End",
  27. "ui_menu" : "Menu",
  28. "gp_sit" : "Sit",
  29. "gp_move_up" : "Move Up",
  30. "gp_move_down" : "Move Down",
  31. "gp_move_left" : "Move Left",
  32. "gp_move_right" : "Move Right",
  33. "ui_inventory" : "Inventory",
  34. "ui_close" : "Close",
  35. "ui_minimap" : "Minimap",
  36. "ui_chat" : "Chat",
  37. "ui_validate" : "Validate",
  38. "gp_click_to" : "Click To",
  39. "ui_screenshot" : "Screenshot",
  40. "gp_interact" : "Interact",
  41. "ui_emote" : "Emote",
  42. "smile_1" : "Emote",
  43. "smile_2" : "Emote",
  44. "smile_3" : "Emote",
  45. "smile_4" : "Emote",
  46. "smile_5" : "Emote",
  47. "smile_6" : "Emote",
  48. "smile_7" : "Emote",
  49. "smile_8" : "Emote",
  50. "smile_9" : "Emote",
  51. "smile_10" : "Emote",
  52. "smile_11" : "Emote",
  53. "smile_12" : "Emote",
  54. "smile_13" : "Emote",
  55. "smile_14" : "Emote",
  56. "smile_15" : "Emote",
  57. "smile_16" : "Emote",
  58. "smile_17" : "Emote",
  59. "gp_morph" : "Morph",
  60. "ui_settings" : "Settings",
  61. "ui_stat": "Stat",
  62. "gp_shortcut_1" : "Shortcut 1",
  63. "gp_shortcut_2" : "Shortcut 2",
  64. "gp_shortcut_3" : "Shortcut 3",
  65. "gp_shortcut_4" : "Shortcut 4",
  66. "gp_shortcut_5" : "Shortcut 5",
  67. "gp_shortcut_6" : "Shortcut 6",
  68. "gp_shortcut_7" : "Shortcut 7",
  69. "gp_shortcut_8" : "Shortcut 8",
  70. "gp_shortcut_9" : "Shortcut 9",
  71. "gp_shortcut_10" : "Shortcut 10",
  72. "gp_target" : "Target",
  73. "gp_untarget" : "Clear Target",
  74. "gp_pickup" : "Pickup",
  75. "ui_skill": "Skill",
  76. "ui_accept": "Accept",
  77. "ui_cancel": "Cancel",
  78. "ui_context_validate": "Context Validate",
  79. "ui_context_cancel": "Context Cancel",
  80. "ui_context_secondary": "Context Secondary",
  81. "ui_context_tertiary": "Context Tertiary"
  82. }
  83. #
  84. static func Init():
  85. if DeviceManager.HasDeviceSupport():
  86. Input.set_custom_mouse_cursor(FileSystem.LoadGfx("gui/misc/mouse.png"))
  87. static func GetActionInfo(action : String) -> Array:
  88. var defaultValue : String = ""
  89. var defaultDeviceType : DeviceType = DeviceType.KEYBOARD
  90. if HasDeviceSupport():
  91. for event in GetEvents(action):
  92. if not LauncherCommons.isMobile and currentDeviceType == DeviceType.KEYBOARD and event is InputEventKey:
  93. if event.keycode:
  94. defaultValue = event.as_text_keycode()
  95. break
  96. elif event.physical_keycode:
  97. defaultValue = event.as_text_physical_keycode()
  98. break
  99. elif currentDeviceType == DeviceType.JOYSTICK and event is InputEventJoypadButton:
  100. var inScope : bool = event.button_index >= 0 and event.button_index < joyButton.size()
  101. defaultValue = joyButton[event.button_index] if inScope else str(event.button_index)
  102. break
  103. return [defaultValue, defaultDeviceType]
  104. static func HasActionName(action : String) -> bool:
  105. return action in actionNames
  106. static func GetActionName(action : String) -> String:
  107. return actionNames[action] if HasActionName(action) else ""
  108. static func GetEvents(action : String) -> Array:
  109. return ProjectSettings.get_setting("input/" + action).events if ProjectSettings.has_setting("input/" + action) else []
  110. static func HasEvent(action : String) -> bool:
  111. return GetEvents(action).size() > 0
  112. static func HasDeviceSupport() -> bool:
  113. return DisplayServer.get_name() != "headless"
  114. #
  115. static func SendEvent(action : String, state : bool = true):
  116. var event = InputEventJoypadButton.new()
  117. event.action = action
  118. event.pressed = state
  119. Input.parse_input_event(event)
  120. static func SendEventJoy(buttonID : int, state : bool = true):
  121. var event = InputEventJoypadButton.new()
  122. event.pressed = state
  123. event.button_index = buttonID
  124. event.pressure = 1.0 if state else 0.0
  125. Input.parse_input_event(event)
  126. #
  127. static func DeviceChanged(deviceType : DeviceType):
  128. currentDeviceType = deviceType
  129. if not LauncherCommons.isMobile:
  130. Launcher.Action.deviceChanged.emit()