Glitchfinder's Gamepad Input Module 1.00

Gamepad Input Module Version: 1.00
By: Glitchfinder


Introduction

Glitchfinder's Gamepad Input Module is an advanced scripting tool that provides full scripted access to to check any and all buttons and axes on an XInput-compatible gamepad. It is useful in the sense that it will not break the default Input module, and because it allows far more use of modern gamepads than you would get from default RMXP or default RMVX. It also does not need to be updated, aside from the default call to Input.update that is used in most cases. It also does not break default scripts or events that use input.

Features
  • Check any button on the gamepad
  • Doesn't break the default Input module
  • Does not break events or scripts that call the default Input module
  • Automatically updated by the default Input module
  • Allows you to check to see if a button has been released
  • Works with all XInput-compatible controllers
  • Doesn't crash on F12

Screenshots

Since this script has no visual elements, a screenshot cannot be taken.

Script

  1. #≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
  2. # ** Glitchfinder's Gamepad Input Module          [RPG Maker XP] [RPG Maker VX]
  3. #    Version 1.00
  4. #------------------------------------------------------------------------------
  5. #  This script helps scripters to use an XInput-compatible gamepad with their
  6. #  games.
  7. #==============================================================================
  8. # * Version History
  9. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  10. #   Version 1.00 ------------------------------------------------- (2010-08-20)
  11. #     - Initial version
  12. #     - Author: Glitchfinder
  13. #==============================================================================
  14. # * Instructions
  15. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  16. #  Place this script above Main, and below the default scripts. (I realize this
  17. #  is obvious to most, but some people don't get it.)
  18. #
  19. #  You must also place Input.dll into the game's main project folder, since
  20. #  this script needs it to access the XInput functions it uses.
  21. #
  22. #  This module is automatically updated by the default Input module, which
  23. #  means that the only time you need to call the update method is in a scene
  24. #  that does not update the default Input module.
  25. #
  26. #  This module does not break the functionality of the default Input module.
  27. #
  28. #  To use this module to check a gamepad button, simply use one of the four
  29. #  methods (press?(button[, index]), trigger?(button[, index]),
  30. #  repeat?(button[, index]), or release?(button[, index])), where button is the
  31. #  constant of the button you want to check. Button may also be used as
  32. #  Gamepad::BUTTONNAME. For a list of acceptable button names, look below the
  33. #  header, in the section that declares button constants. The methods use index
  34. #  to determine which gamepad to check. If no index is given, they will check
  35. #  gamepad 0. Index is a value from 0 to 3.
  36. #
  37. #  To use this module to check a gamepad trigger or joystick, simply use one of
  38. #  the four methods (axis_press?(axis[, index]), axis_trigger?(axis[, index]),
  39. #  axis_repeat?(axis[, index]), or axis_release?(axis[, index])), where axis is
  40. #  the constant of the axis you want to check. Axis may also be used as
  41. #  Gamepad::AXISNAME. For a list of acceptable axis names, look below the
  42. #  header, in the section that declares axis constants. The methods use index
  43. #  to determine which gamepad to check. If no index is given, they will check
  44. #  gamepad 0. Index is a value from 0 to 3.
  45. #
  46. #  To use this module to check the value on a gamepad triger or joystick axis,
  47. #  use one of the two methods (axis_value(axis[, index]),
  48. #  real_axis_value(axis[, index])), where axis is the constant of the axis you
  49. #  want to check. Axis may also be used as Gamepad::AXISNAME. For a list of
  50. #  acceptable axis names, look below the header, in the section that declares
  51. #  axis constants. The methods use index to determine which gamepad to check.
  52. #  If no index is given, they will check gamepad 0. Index is a value from 0 to
  53. #  3. The difference between these two methods is that real_axis_value() will
  54. #  always return the current value, while axis_value will return 0 if the
  55. #  axis falls within its dead zone.
  56. #
  57. #  To use this module to cause a gamepad to "rumble", or vibrate, use the
  58. #  vibrate() method. Details for that are below, in the method list.
  59. #==============================================================================
  60. # * Method List
  61. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  62. #  Gamepad.update
  63. #    Updates gamepad input. Calls to this method are not necessary unless the
  64. #    default Input module is not being updated.
  65. #
  66. #  Gamepad.press?(button[, index])
  67. #    Determines whether the gamepad button determined by button is currently
  68. #    being pressed. If the button is being pressed, returns true. If not,
  69. #    returns false. Uses index to determine the gamepad to check, from 0 to 3.
  70. #    The index defaults to 0.
  71. #
  72. #  Gamepad.trigger?(button[, index])
  73. #    Determines whether the gamepad button determined by button is being
  74. #    pressed again. "Pressed again" is seen as time having passed between the
  75. #    button being not pressed and being pressed. If the button is being
  76. #    pressed, returns true. If not, returns false. Uses index to determine the
  77. #    gamepad to check, from 0 to 3. The index defaults to 0.
  78. #
  79. #  Gamepad.repeat?(button[, index])
  80. #    Determines whether the gamepad button determined by button is being
  81. #    pressed again. Unlike trigger?(), this takes into account the repeat input
  82. #    of a button being held down continuously. If the button is being pressed,
  83. #    returns true. If not, returns false. Uses index to determine the gamepad
  84. #    to check, from 0 to 3. The index defaults to 0.
  85. #
  86. #  Gamepad.release?(button[, index])
  87. #    Determines whether the gamepad button determined by button has just been
  88. #    released. If the button has been released, returns true. If not, returns
  89. #    false. Uses index to determine the gamepad to check, from 0 to 3. The
  90. #    index defaults to 0.
  91. #
  92. #  Gamepad.axis_press?(axis[, index])
  93. #    Determines whether the gamepad axis determined by axis is currently being
  94. #    pressed. If the button is being pressed, returns true. If not, returns
  95. #    false. Uses index to determine the gamepad to check, from 0 to 3. The
  96. #    index defaults to 0.
  97. #
  98. #  Gamepad.axis_trigger?(axis[, index])
  99. #    Determines whether the gamepad axis determined by axis is being pressed
  100. #    again. "Pressed again" is seen as time having passed between the button
  101. #    being not pressed and being pressed. If the button is being pressed,
  102. #    returns true. If not, returns false. Uses index to determine the gamepad
  103. #    to check, from 0 to 3. The index defaults to 0.
  104. #
  105. #  Gamepad.axis_repeat?(axis[, index])
  106. #    Determines whether the gamepad axis determined by axis is being pressed
  107. #    again. Unlike trigger?(), this takes into account the repeat input of a
  108. #    button being held down continuously. If the button is being pressed,
  109. #    returns true. If not, returns false. Uses index to determine the gamepad
  110. #    to check, from 0 to 3. The index defaults to 0.
  111. #
  112. #  Gamepad.axis_release?(axis[, index])
  113. #    Determines whether the gamepad axis determined by axis has just been
  114. #    released. If the button has been released, returns true. If not, returns
  115. #    false. Uses index to determine the gamepad to check, from 0 to 3. The
  116. #    index defaults to 0.
  117. #
  118. #  Gamepad.axis_value(axis[, index])
  119. #    Returns the current value of the gamepad axis determined by axis. The
  120. #    gamepad checked is determined by index, from 0 to 3. The value for index
  121. #    is 0 if none is provided. If the value for the axis falls within the dead
  122. #    zone specified for that axis, then axis_value() will return 0.
  123. #
  124. #  Gamepad.real_axis_value(axis[, index])
  125. #    Returns the current value of the gamepad axis determined by axis. The
  126. #    gamepad checked is determined by index, from 0 to 3. The value for index
  127. #    is 0 if none is provided. This method ignores the dead zone for the axis,
  128. #    and will always return the current value.
  129. #
  130. #  Gamepad.vibrate(left_motor, right_motor, frames[, index])
  131. #    Causes the motors to vibrate for the number of frames specified. The
  132. #    values for left_motor and right_motor range from 0 to 65535, though the
  133. #    motors themselves may differ in function, as they do in an Xbox 360
  134. #    controller, resulting in a different "feel" depending on the motor you
  135. #    select. Uses index to determine the gamepad to check, from 0 to 3. The
  136. #    index defaults to 0.
  137. #==============================================================================
  138. # * Known Issues
  139. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  140. #  This script currently has trouble with the vibrate method, which does not
  141. #  allow you to set one motor independently of the other. This part of a
  142. #  planned update sometime in the future.
  143. #==============================================================================
  144. # * Glitchfinder's Advice
  145. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  146. #  This script is meant for people with a medium or advanced level of scripting
  147. #  knowledge and ability, or for those using scripts that require this module.
  148. #==============================================================================
  149. # * Contact
  150. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  151. #  Glitchfinder, the author of this script, may be contacted through his
  152. #  website, found at http://www.glitchkey.com
  153. #
  154. #  You may also find Glitchfinder at http://www.hbgames.org
  155. #==============================================================================
  156. # * Usage
  157. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  158. #  This script may be used with the following terms and conditions:
  159. #
  160. #    1. This script is free to use in any noncommercial project. If you wish to
  161. #       use this script in a commercial (paid) project, please contact
  162. #       Glitchfinder at his website.
  163. #    2. This script may only be hosted at the following domains:
  164. #         http://www.glitchkey.com
  165. #         http://www.hbgames.org
  166. #    3. If you wish to host this script elsewhere, please contact Glitchfinder.
  167. #    4. If you wish to translate this script, please contact Glitchfinder. He
  168. #       will need the web address that you plan to host the script at, as well
  169. #       as the language this script is being translated to.
  170. #    5. This header must remain intact at all times.
  171. #    6. Glitchfinder remains the sole owner of this code. He may modify or
  172. #       revoke this license at any time, for any reason.
  173. #    7. Any code derived from code within this script is owned by Glitchfinder,
  174. #       and you must have his permission to publish, host, or distribute his
  175. #       code.
  176. #    8. This license applies to all code derived from the code within this
  177. #       script.
  178. #    9. If you use this script within your project, you must include visible
  179. #       credit to Glitchfinder, within reason.
  180. #≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
  181.  
  182. #==============================================================================
  183. # ** Gamepad
  184. #------------------------------------------------------------------------------
  185. #  This module performs gamepad input processing
  186. #==============================================================================
  187.  
  188. module Gamepad
  189.   #--------------------------------------------------------------------------
  190.   # * Declare Button Constants
  191.   #--------------------------------------------------------------------------
  192.   DPAD_UP        = 0  # Directional pad up button
  193.   DPAD_DOWN      = 1  # Directional pad down button
  194.   DPAD_LEFT      = 2  # Directional pad left button
  195.   DPAD_RIGHT     = 3  # Directional pad right button
  196.   START          = 4  # Start button
  197.   BACK           = 5  # Back button
  198.   LEFT_THUMB     = 6  # Left thumb button
  199.   RIGHT_THUMB    = 7  # Right thumb button
  200.   LEFT_SHOULDER  = 8  # Left shoulder button
  201.   RIGHT_SHOULDER = 9  # Right shoulder button
  202.   A              = 10 # A button
  203.   B              = 11 # B button
  204.   X              = 12 # X Button
  205.   Y              = 13 # Y Button
  206.   #--------------------------------------------------------------------------
  207.   # * Declare Axis Constants
  208.   #--------------------------------------------------------------------------
  209.   LEFT_TRIGGER   = 0  # Left trigger axis       (0 to 255)
  210.   RIGHT_TRIGGER  = 1  # Right trigger axis      (0 to 255)
  211.   LX             = 2  # Left thumbstick x axis  (-32768 to 32767)
  212.   LY             = 3  # Left thumbstick y axis  (-32768 to 32767)
  213.   RX             = 4  # Right thumbstick x axis (-32768 to 32767)
  214.   RY             = 5  # Right thumbstick y axis (-32768 to 32767)
  215.   #--------------------------------------------------------------------------
  216.   # * Declare Module Variables
  217.   #--------------------------------------------------------------------------
  218.   begin
  219.     # Declare API calls
  220.     @gamepadGetCapabilities = Win32API.new('Input.dll',
  221.       'GamepadGetCapabilities', 'IIP', 'I')
  222.     @gamepadGetDSoundAudioDeviceGuids = Win32API.new('Input.dll',
  223.       'GamepadGetDSoundAudioDeviceGuids', 'IPP', 'I')
  224.     @gamepadGetState = Win32API.new('Input.dll', 'GamepadGetState', 'IP', 'I')
  225.     @gamepadSetState = Win32API.new('Input.dll', 'GamepadSetState', 'IP', 'I')
  226.     # Declare blank structures
  227.     @capabilities = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0].pack('c2S2c2s4S2')
  228.     @gamepad = [0, 0, 0, 0, 0, 0, 0].pack('Sc2s4')
  229.     @state = [0, 0, 0, 0, 0, 0, 0, 0].pack('ISc2s4')
  230.     @vibrate = [0, 0].pack('n2')
  231.     @gamepadGetCapabilities.call(0, 0, @capabilities)
  232.     @capabilities.unpack('C2S2C2n6')
  233.     # Create blank button array
  234.     @buttons = Array.new(14, false)
  235.     # Create a blank axis array
  236.     @axis = Array.new(6, false)
  237.     values = Array.new(6, 0)
  238.     zones = [30, 30, 7849, 7849, 8689, 8689]
  239.     # Declare controller information storage
  240.     @controllers = {}
  241.     # Declare default controller information
  242.     @controllers.default = {
  243.       'Active' => false,                   # Controller active flag
  244.       'Change ID' => 0,                    # Current state change ID
  245.       'Press' => @buttons.clone,           # Flags for button press state
  246.       'Trigger' => @buttons.clone,         # Flags for button trigger state
  247.       'Repeat' => @buttons.clone,          # Flags for button repeat state
  248.       'Release' => @buttons.clone,         # Flags for button release state
  249.       'Repeat Timer' => Array.new(14, 0),  # Timer for button repeat state
  250.       'Base Repeat' => 15,                 # Base time for repeat flag (frames)
  251.       'Axes' => Array.new(6, 0),           # Axis values
  252.       'Axis Dead Zones' => zones.clone,    # Dead zone for the axes
  253.       'Axis Press' => @axis.clone,         # Flags for axis press state
  254.       'Axis Trigger' => @axis.clone,       # Flags for axis trigger state
  255.       'Axis Repeat' => @axis.clone,        # Flags for axis repeat state
  256.       'Axis Release' => @axis.clone,       # Flags for axis release state
  257.       'Axis Repeat Timer' => values.clone, # Timer for axis repeat state
  258.       'Vibrate State' => [0, 0],           # Current vibrate state
  259.       'Vibrate Timer' => 0,                # Current vibrate timer
  260.       'Vibrate Repeat Timer' => 0,         # Current rumbe repeat timer
  261.       'Vibrate Minimum Timer' => 0         # Min. time before changing vibrate
  262.     }
  263.     # Iterate through the total number of controllers allowed
  264.     for i in 0..3
  265.       # Add a new controller to the controller array
  266.       @controllers[i] = @controllers.default.clone
  267.     end
  268.   end
  269.   #--------------------------------------------------------------------------
  270.   # * Frame Update
  271.   #--------------------------------------------------------------------------
  272.   def self.update
  273.     # Iterate through all controllers
  274.     for i in 0..3
  275.       # Set up a blank state structure
  276.       state = @state
  277.       # Detect the controller state
  278.       success = @gamepadGetState.call(i, state)
  279.       # If the state was successfully obtained
  280.       if success == 1
  281.         # Set the controller to active
  282.         @controllers[i]['Active'] = true
  283.         # Backup the previous button states
  284.         controller = @controllers[i].clone
  285.         # Reset the controller button states
  286.         @controllers[i]['Press']        = @buttons.clone
  287.         @controllers[i]['Trigger']      = @buttons.clone
  288.         @controllers[i]['Repeat']       = @buttons.clone
  289.         @controllers[i]['Release']      = @buttons.clone
  290.         @controllers[i]['Axis Press']   = @axis.clone
  291.         @controllers[i]['Axis Trigger'] = @axis.clone
  292.         @controllers[i]['Axis Repeat']  = @axis.clone
  293.         @controllers[i]['Axis Release'] = @axis.clone
  294.         # Unpack the controller state
  295.         state = state.unpack('ISC2s4')
  296.         # If the controller state has changed
  297.         if state[0] != @controllers[i]['Change ID']
  298.           # Add the controller button states to the controller state
  299.           @controllers[i]['Press'][0]  = (state[1] & 0x00000001 == 0x00000001)
  300.           @controllers[i]['Press'][1]  = (state[1] & 0x00000002 == 0x00000002)
  301.           @controllers[i]['Press'][2]  = (state[1] & 0x00000004 == 0x00000004)
  302.           @controllers[i]['Press'][3]  = (state[1] & 0x00000008 == 0x00000008)
  303.           @controllers[i]['Press'][4]  = (state[1] & 0x00000010 == 0x00000010)
  304.           @controllers[i]['Press'][5]  = (state[1] & 0x00000020 == 0x00000020)
  305.           @controllers[i]['Press'][6]  = (state[1] & 0x00000040 == 0x00000040)
  306.           @controllers[i]['Press'][7]  = (state[1] & 0x00000080 == 0x00000080)
  307.           @controllers[i]['Press'][8]  = (state[1] & 0x0100 == 0x0100)
  308.           @controllers[i]['Press'][9]  = (state[1] & 0x0200 == 0x0200)
  309.           @controllers[i]['Press'][10] = (state[1] & 0x1000 == 0x1000)
  310.           @controllers[i]['Press'][11] = (state[1] & 0x2000 == 0x2000)
  311.           @controllers[i]['Press'][12] = (state[1] & 0x4000 == 0x4000)
  312.           @controllers[i]['Press'][13] = (state[1] & 0x8000 == 0x8000)
  313.           # Add the controller trigger states to the current controller state
  314.           @controllers[i]['Axes'][0]   = state[2]
  315.           @controllers[i]['Axes'][1]   = state[3]
  316.           # Add the controller thumb coords to the current controller state
  317.           @controllers[i]['Axes'][2]   = state[4]
  318.           @controllers[i]['Axes'][3]   = state[5]
  319.           @controllers[i]['Axes'][4]   = state[6]
  320.           @controllers[i]['Axes'][5]   = state[7]
  321.         end
  322.         # Iterate through the buttons
  323.         for button in 0...@controllers[i]['Press'].size
  324.           # If the button is pressed, and the repeat count is at 0
  325.           if ((@controllers[i]['Press'][button] == true) &&
  326.             (@controllers[i]['Repeat Timer'][button] <= 0))
  327.             # Set the button to repeat
  328.             @controllers[i]['Repeat'][button] = true
  329.             # Get the base repeat time
  330.             repeat_timer = @controllers[i]['Base Repeat']
  331.             # Reset the repeat timer
  332.             @controllers[i]['Repeat Timer'][button] = repeat_timer
  333.           # If the repeat timer is above 0
  334.           elsif @controllers[i]['Repeat Timer'][button] > 0
  335.             # Cycle down the repeat timer
  336.             @controllers[i]['Repeat Timer'][button] -= 1
  337.           end
  338.           # If the button is pressed, and it wasn't pressed previously
  339.           if ((@controllers[i]['Press'][button] == true) &&
  340.             (controller['Press'][button] == false))
  341.             # Set the button to trigger
  342.             @controllers[i]['Trigger'][button] = true
  343.           # If the button is not pressed, and it was previously
  344.           elsif ((@controllers[i]['Press'][button] == false) &&
  345.             (controller['Press'][button] == true))
  346.             # Set the button to release
  347.             @controllers[i]['Release'][button] = true
  348.           end
  349.         end
  350.         # Iterate through the buttons
  351.         for axis in 0...@controllers[i]['Axes'].size
  352.           # If the trigger is greater than the dead zone, set it to pressed
  353.           if (@controllers[i]['Axes'][axis].abs >
  354.             @controllers[i]['Axis Dead Zones'][axis])
  355.             # Set the axis to pressed
  356.             @controllers[i]['Axis Press'][axis] = true
  357.           end
  358.           # If the axis is pressed, and the repeat count is at 0
  359.           if ((@controllers[i]['Axis Press'][axis] == true) &&
  360.             (@controllers[i]['Axis Repeat Timer'][axis] <= 0))
  361.             # Set the axis to repeat
  362.             @controllers[i]['Axis Repeat'][axis] = true
  363.             # Get the base repeat time
  364.             repeat_timer = @controllers[i]['Base Repeat']
  365.             # Reset the repeat timer
  366.             @controllers[i]['Axis Repeat Timer'][axis] = repeat_timer
  367.           # If the repeat timer is above 0
  368.           elsif @controllers[i]['Axis Repeat Timer'][axis] > 0
  369.             # Cycle down the repeat timer
  370.             @controllers[i]['Axis Repeat Timer'][axis] -= 1
  371.           end
  372.           # If the axis is pressed, and it wasn't pressed previously
  373.           if ((@controllers[i]['Axis Press'][axis] == true) &&
  374.             (controller['Axis Press'][axis] == false))
  375.             # Set the axis to trigger
  376.             @controllers[i]['Axis Trigger'][axis] = true
  377.           # If the axis is not pressed, and it was previously
  378.           elsif ((@controllers[i]['Press'][axis] == false) &&
  379.             (controller['Axis Press'][axis] == true))
  380.             # Set the axis to release
  381.             @controllers[i]['Axis Release'][axis] = true
  382.           end
  383.         end
  384.         # If the controller is set to vibrate
  385.         if @controllers[i]['Vibrate Timer'] > 0
  386.           # If the vibrate repeat timer has reached 0
  387.           if @controllers[i]['Vibrate Repeat Timer'] <= 0
  388.             # Get the current vibration values
  389.             left_motor = @controllers[i]['Vibrate State'][0] - 1
  390.             right_motor = @controllers[i]['Vibrate State'][1] - 1
  391.             # Correct the motor values
  392.             left_motor = 0 if left_motor < 0
  393.             right_motor = 0 if right_motor < 0
  394.             # Create the vibration state structure
  395.             vibrate = [left_motor, right_motor].pack('n2')
  396.             # Reset the controller vibrate state
  397.             @gamepadSetState.call(i, vibrate)
  398.             # Pack the current vibrate state
  399.             vibrate_state = @controllers[i]['Vibrate State'].pack('n2')
  400.             # Set the vibrate state to the current controller vibrate state
  401.             @gamepadSetState.call(i, vibrate_state)
  402.             # Reset the vibrate repeat timer
  403.             @controllers[i]['Vibrate Repeat Timer'] = Graphics.frame_rate
  404.           # If the vibrate repeat timer is greater than 0
  405.           else
  406.             # Decrement the vibrate repeat timer
  407.             @controllers[i]['Vibrate Repeat Timer'] -= 1
  408.           end
  409.           # Decrement the vibrate timer
  410.           @controllers[i]['Vibrate Timer'] -= 1
  411.         # If the vibrate timer is 0, and the vibrate state is still set
  412.         elsif ((@controllers[i]['Vibrate Timer'] <= 0) &&
  413.           (@controllers[i]['Vibrate State'] != [0, 0]))
  414.           # Reset the vibrate state
  415.           @controllers[i]['Vibrate State'] = [0, 0]
  416.           # Set the vibrate repeat timer to 1/3 of a second
  417.           @controllers[i]['Vibrate Repeat Timer'] = Graphics.frame_rate / 3
  418.         # If the vibrate timer is 0 and the vibrate state is cleared
  419.         elsif ((@controllers[i]['Vibrate Timer'] <= 0) &&
  420.           (@controllers[i]['Vibrate State'] == [0, 0]))
  421.           # If the vibrate repeat timer is running
  422.           if @controllers[i]['Vibrate Repeat Timer'] >= 0
  423.             # Decrement the vibrate repeat timer
  424.             @controllers[i]['Vibrate Repeat Timer'] -= 1
  425.           # If the timer has run out
  426.           else
  427.             # Reset the vibrate repeat timer
  428.             @controllers[i]['Vibrate Repeat Timer'] = 0
  429.             # Reset the controller vibrate state
  430.             @gamepadSetState.call(i, @vibrate)
  431.           end
  432.         end
  433.         # If the controller's minimum vibrate update timer is running
  434.         if @controllers[i]['Vibrate Minimum Timer'] > 0
  435.           # Decrement the timer
  436.           @controllers[i]['Vibrate Minimum Timer'] -= 1
  437.         end
  438.       # If the controller is not connected, or the state could not be read
  439.       elsif success == 2 || success == -1
  440.         # Set the controller to inactive
  441.         @controllers[i]['Active'] = false
  442.       end
  443.     end
  444.   end
  445.   #--------------------------------------------------------------------------
  446.   # * Get Button Pressed State
  447.   #     button : button number
  448.   #     index  : controller number (0 to 3)
  449.   #--------------------------------------------------------------------------
  450.   def self.press?(button, index = 0)
  451.     # Return false if the controller is inactive
  452.     return false if @controllers[index]['Active'] == false
  453.     # Return button pressed state
  454.     return @controllers[index]['Press'][button]
  455.   end
  456.   #--------------------------------------------------------------------------
  457.   # * Get Button Triggered State
  458.   #     button : button number
  459.   #     index  : controller number (0 to 3)
  460.   #--------------------------------------------------------------------------
  461.   def self.trigger?(button, index = 0)
  462.     # Return false if the controller is inactive
  463.     return false if @controllers[index]['Active'] == false
  464.     # Return button triggered state
  465.     return @controllers[index]['Trigger'][button]
  466.   end
  467.   #--------------------------------------------------------------------------
  468.   # * Get Button Repeated State
  469.   #     button : button number
  470.   #     index  : controller number (0 to 3)
  471.   #--------------------------------------------------------------------------
  472.   def self.repeat?(button, index = 0)
  473.     # Return false if the controller is inactive
  474.     return false if @controllers[index]['Active'] == false
  475.     # Return button repeated state
  476.     return @controllers[index]['Repeat'][button]
  477.   end
  478.   #--------------------------------------------------------------------------
  479.   # * Get Button Released State
  480.   #     button : button number
  481.   #     index  : controller number (0 to 3)
  482.   #--------------------------------------------------------------------------
  483.   def self.release?(button, index = 0)
  484.     # Return false if the controller is inactive
  485.     return false if @controllers[index]['Active'] == false
  486.     # Return button released state
  487.     return @controllers[index]['Release'][button]
  488.   end
  489.   #--------------------------------------------------------------------------
  490.   # * Get Axis Pressed State
  491.   #     axis  : axis number
  492.   #     index : controller number (0 to 3)
  493.   #--------------------------------------------------------------------------
  494.   def self.axis_press?(axis, index = 0)
  495.     # Return false if the controller is inactive
  496.     return false if @controllers[index]['Active'] == false
  497.     # Return axis pressed state
  498.     return @controllers[index]['Axis Press'][axis]
  499.   end
  500.   #--------------------------------------------------------------------------
  501.   # * Get Axis Triggered State
  502.   #     axis  : axis number
  503.   #     index : controller number (0 to 3)
  504.   #--------------------------------------------------------------------------
  505.   def self.axis_trigger?(axis, index = 0)
  506.     # Return false if the controller is inactive
  507.     return false if @controllers[index]['Active'] == false
  508.     # Return axis triggered state
  509.     return @controllers[index]['Axis Trigger'][axis]
  510.   end
  511.   #--------------------------------------------------------------------------
  512.   # * Get Axis Repeated State
  513.   #     axis  : axis number
  514.   #     index : controller number (0 to 3)
  515.   #--------------------------------------------------------------------------
  516.   def self.axis_repeat?(axis, index = 0)
  517.     # Return false if the controller is inactive
  518.     return false if @controllers[index]['Active'] == false
  519.     # Return axis repeated state
  520.     return @controllers[index]['Axis Repeat'][axis]
  521.   end
  522.   #--------------------------------------------------------------------------
  523.   # * Get Axis Released State
  524.   #     axis  : axis number
  525.   #     index : controller number (0 to 3)
  526.   #--------------------------------------------------------------------------
  527.   def self.axis_release?(axis, index = 0)
  528.     # Return false if the controller is inactive
  529.     return false if @controllers[index]['Active'] == false
  530.     # Return axis released state
  531.     return @controllers[index]['Axis Release'][axis]
  532.   end
  533.   #--------------------------------------------------------------------------
  534.   # * Get Axis Value
  535.   #     axis  : axis number
  536.   #     index : controller number (0 to 3)
  537.   #--------------------------------------------------------------------------
  538.   def self.axis_value(axis, index = 0)
  539.     # Set a temporary variable to the axis value
  540.     value = self.real_axis_value(axis, index)
  541.     # Return axis value
  542.     return value unless value.abs < @controllers[index]['Axis Dead Zones'][axis]
  543.     # Return 0 if the axis is within the dead zone
  544.     return 0
  545.   end
  546.   #--------------------------------------------------------------------------
  547.   # * Get Real Axis Value
  548.   #     axis  : axis number
  549.   #     index : controller number (0 to 3)
  550.   #--------------------------------------------------------------------------
  551.   def self.real_axis_value(axis, index = 0)
  552.     # Return the axis value
  553.     return @controllers[index]['Axes'][axis]
  554.   end
  555.   #--------------------------------------------------------------------------
  556.   # * Set Controller to Vibrate
  557.   #     left_motor  : left motor value (0 to 65535)
  558.   #     right_motor : right motor value (0 to 65535)
  559.   #     frames      : number of frames to vibrate
  560.   #     index       : controller number (0 to 3)
  561.   #--------------------------------------------------------------------------
  562.   def self.vibrate(left_motor, right_motor, frames, index = 0)
  563.     # Return false if the vibrate has updated too recently
  564.     return false if @controllers[index]['Vibrate Minimum Timer'] > 0
  565.     # Set the controller vibrate timer to the number of frames
  566.     @controllers[index]['Vibrate Timer'] = frames
  567.     # Correct the motor values if out of range
  568.     left_motor = 0 if left_motor < 0
  569.     left_motor = 65535 if left_motor > 65535
  570.     right_motor = 0 if right_motor < 0
  571.     right_motor = 65535 if right_motor > 65535
  572.     # Set the controller motor values
  573.     @controllers[index]['Vibrate State'] = [left_motor.to_i, right_motor.to_i]
  574.     # Set the minimum update timer
  575.     @controllers[index]['Vibrate Minimum Timer'] = Graphics.frame_rate / 4
  576.     # Return true if the vibrate was successfully set
  577.     return true
  578.   end
  579.   #--------------------------------------------------------------------------
  580.   # * Initialize Module
  581.   #--------------------------------------------------------------------------
  582.   begin
  583.     # Force an initial update of the gamepad
  584.     self.update
  585.   end
  586. end
  587.  
  588. #==============================================================================
  589. # ** Input
  590. #------------------------------------------------------------------------------
  591. #  This module performs input processing
  592. #==============================================================================
  593.  
  594. module Input
  595.   # Add class data
  596.   class << self
  597.     #------------------------------------------------------------------------
  598.     # * Alias Methods
  599.     #------------------------------------------------------------------------
  600.     # If the update method has not been aliased
  601.     unless method_defined?(:glitch_gamepad_input_update)
  602.       # Alias the update method
  603.       alias glitch_gamepad_input_update update
  604.     end
  605.     #------------------------------------------------------------------------
  606.     # * Frame Update
  607.     #------------------------------------------------------------------------
  608.     def update
  609.       # Call original method
  610.       glitch_gamepad_input_update
  611.       # Update Gamepad module
  612.       Gamepad.update
  613.     end
  614.   end
  615. end
Expand to see the code.


Requirements

This script requires Glitchfinder's Input.dll file to be placed in the game directory.

Instructions

Place this script above Main, and below the default scripts. (I realize this is obvious to most, but some people don't get it.)

You must also place Input.dll into the game's main project folder, since this script needs it to access the XInput functions it uses.

This module is automatically updated by the default Input module, which means that the only time you need to call the update method is in a scene that does not update the default Input module.

This module does not break the functionality of the default Input module.

To use this module to check a gamepad button, simply use one of the four methods (press?(button[, index]), trigger?(button[, index]), repeat?(button[, index]), or release?(button[, index])), where button is the constant of the button you want to check. Button may also be used as Gamepad::BUTTONNAME. For a list of acceptable button names, look below the header, in the section that declares button constants. The methods use index to determine which gamepad to check. If no index is given, they will check gamepad 0. Index is a value from 0 to 3.

To use this module to check a gamepad trigger or joystick, simply use one of the four methods (axis_press?(axis[, index]), axis_trigger?(axis[, index]), axis_repeat?(axis[, index]), or axis_release?(axis[, index])), where axis is the constant of the axis you want to check. Axis may also be used as Gamepad::AXISNAME. For a list of acceptable axis names, look below the header, in the section that declares axis constants. The methods use index to determine which gamepad to check. If no index is given, they will check gamepad 0. Index is a value from 0 to 3.

To use this module to check the value on a gamepad triger or joystick axis, use one of the two methods (axis_value(axis[, index]), real_axis_value(axis[, index])), where axis is the constant of the axis you want to check. Axis may also be used as Gamepad::AXISNAME. For a list of acceptable axis names, look below the header, in the section that declares axis constants. The methods use index to determine which gamepad to check. If no index is given, they will check gamepad 0. Index is a value from 0 to 3. The difference between these two methods is that real_axis_value() will always return the current value, while axis_value will return 0 if the axis falls within its dead zone.

To use this module to cause a gamepad to "rumble", or vibrate, use the vibrate() method. Details for that are below, in the method list.

Method List

Gamepad.update

Updates gamepad input. Calls to this method are not necessary unless the default Input module is not being updated.

Gamepad.press?(button[, index])

Determines whether the gamepad button determined by button is currently being pressed. If the button is being pressed, returns true. If not, returns false. Uses index to determine the gamepad to check, from 0 to 3. The index defaults to 0.

Gamepad.trigger?(button[, index])

Determines whether the gamepad button determined by button is being pressed again. "Pressed again" is seen as time having passed between the button being not pressed and being pressed. If the button is being pressed, returns true. If not, returns false. Uses index to determine the gamepad to check, from 0 to 3. The index defaults to 0.

Gamepad.repeat?(button[, index])

Determines whether the gamepad button determined by button is being pressed again. Unlike trigger?(), this takes into account the repeat input of a button being held down continuously. If the button is being pressed, returns true. If not, returns false. Uses index to determine the gamepad to check, from 0 to 3. The index defaults to 0.

Gamepad.release?(button[, index])

Determines whether the gamepad button determined by button has just been released. If the button has been released, returns true. If not, returns false. Uses index to determine the gamepad to check, from 0 to 3. The index defaults to 0.

Gamepad.axis_press?(axis[, index])

Determines whether the gamepad axis determined by axis is currently being pressed. If the button is being pressed, returns true. If not, returns false. Uses index to determine the gamepad to check, from 0 to 3. The index defaults to 0.

Gamepad.axis_trigger?(axis[, index])

Determines whether the gamepad axis determined by axis is being pressed again. "Pressed again" is seen as time having passed between the button being not pressed and being pressed. If the button is being pressed, returns true. If not, returns false. Uses index to determine the gamepad to check, from 0 to 3. The index defaults to 0.

Gamepad.axis_repeat?(axis[, index])

Determines whether the gamepad axis determined by axis is being pressed again. Unlike trigger?(), this takes into account the repeat input of a button being held down continuously. If the button is being pressed, returns true. If not, returns false. Uses index to determine the gamepad to check, from 0 to 3. The index defaults to 0.

Gamepad.axis_release?(axis[, index])

Determines whether the gamepad axis determined by axis has just been released. If the button has been released, returns true. If not, returns false. Uses index to determine the gamepad to check, from 0 to 3. The index defaults to 0.

Gamepad.axis_value(axis[, index])

Returns the current value of the gamepad axis determined by axis. The gamepad checked is determined by index, from 0 to 3. The value for index is 0 if none is provided. If the value for the axis falls within the dead zone specified for that axis, then axis_value() will return 0.

Gamepad.real_axis_value(axis[, index])

Returns the current value of the gamepad axis determined by axis. The gamepad checked is determined by index, from 0 to 3. The value for index is 0 if none is provided. This method ignores the dead zone for the axis, and will always return the current value.

Gamepad.vibrate(left_motor, right_motor, frames[, index])
Causes the motors to vibrate for the number of frames specified. The values for left_motor and right_motor range from 0 to 65535, though the motors themselves may differ in function, as they do in an Xbox 360 controller, resulting in a different "feel" depending on the motor you select. Uses index to determine the gamepad to check, from 0 to 3. The index defaults to 0.

Known Issues

This script currently has trouble with the vibrate method, which does not allow you to set one motor independently of the other. This part of a planned update sometime in the future.

Terms and Conditions

  • This script is free to use in any noncommercial project. If you wish to use this script in a commercial (paid) project, please contact Glitchfinder at his website.
  • This script may only be hosted at the following domains:
    http://www.glitchkey.com
    http://www.hbgames.org
  • If you wish to host this script elsewhere, please contact Glitchfinder.
  • If you wish to translate this script, please contact Glitchfinder. He will need the web address that you plan to host the script at, as well as the language this script is being translated to.
  • The script header must remain intact at all times.
  • Glitchfinder remains the sole owner of this code. He may modify or revoke this license at any time, for any reason.
  • Any code derived from code within this script is owned by Glitchfinder, and you must have his permission to publish, host, or distribute his code.
  • This license applies to all code derived from the code within this script.
  • If you use this script within your project, you must include visible credit to Glitchfinder, within reason.
Alright, this is the straw(script) that broke the camel's(AInput) back. This will work pefectly for what I have in mind for my games: Fully customizable button configuration in game (not using f1 menu) including gamepad. I will now officially make the switch from Alework's to Glitchfinder's scripts. Congrats, and thanks.
Well, I've worked with the script for a while, and I can only say it's a pleasure to use... works like a charm with my Windows Live Controller (the X-Box 360 one), and i'm about to test it with my G25 whenever I got it back on the PC.

I'd like to drag special attention - even though that's kinda there as it's a feature list - to the bunhc of functions included, from analog recognition to Force Feedback. I was there from somewhat halfway through to it's current state to test it, and it's come a long way and is, well... ingeniously improving RMVX's functionality range ;)
Big suggestion from my end if you were looking for a Gamepad script - this is where you can stop and rejoice.
i have no idea how to use this 😢
wanted to use gamepad and use the START button for [cross pause] script ,
but dunno what to do :tongue:
mouse_std wrote:
i have no idea how to use this :cry:
wanted to use gamepad and use the START button for [cross pause] script ,
but dunno what to do :tongue:


If you're not a scripter, my recommendation is that you put in a script modification request, since this is a bit advanced. Anyway, I'll go into a basic explanation of how to check the start button, I suppose.

  1. if Gamepad.trigger?(Gamepad::START)
  2.   #do stuff here
  3. end
Expand to see the code.


  1. if Gamepad.trigger?(Gamepad::START, 0)
  2.   #do stuff here
  3. end
Expand to see the code.


These would allow any code placed where the comment is to activate if the start button on controller one (the first controller plugged in) was pressed. The only requirement for this code to work is that you add my gamepad script and place the Input.dll file into the game folder. That being said, I'm unfamiliar with the script you wanted to add it to, so I'm not certain where you would need to replace existing lines with calls to my script, which is why I would suggest putting in a request.
As a special honor to me to hopefully launch a successful system, as well as bring your script's success forward, lemme do my share of featuring you on the list of The Third Man's Scripting Quest, and award you the very first trophy... remember to dust it regularly! :thumb:
okay, i finally got around to testing this out, and I have to say.... I don't have a xbox360 controller. :sad:

I have a usb controller that is very similar to a Dualshock 2 controller. I don't like RMXP's default control setup as it seems more suited to a SNES style controller layout. I did find Trebor777's Joypad Module but it isn't complete. It does allow for more use of the controller, but not the analog sticks or vibration. If you could make a script using DirectInput or winmm to allow all of us with controllers other than XBox360 compatible a chance to have a fully functional joypad, I would greatly appreciate it.

Unfortunately I am actually confused by api implementation in rgss otherwise I would do it myself.

Thank you,
Draycos Goldaryn
DraycosGoldaryn wrote:
okay, i finally got around to testing this out, and I have to say.... I don't have a xbox360 controller. :sad:

I have a usb controller that is very similar to a Dualshock 2 controller. I don't like RMXP's default control setup as it seems more suited to a SNES style controller layout. I did find Trebor777's Joypad Module but it isn't complete. It does allow for more use of the controller, but not the analog sticks or vibration. If you could make a script using DirectInput or winmm to allow all of us with controllers other than XBox360 compatible a chance to have a fully functional joypad, I would greatly appreciate it.

Unfortunately I am actually confused by api implementation in rgss otherwise I would do it myself.

Thank you,
Draycos Goldaryn


Actually, in this case, there's a reason this script uses a .dll file. That because both XInput and DirectInput have no native Windows implementation. I have to actually program the access points into the .dll files. And last time I looked at DirectInput, I was left extremely confused. Official documentation on it is sorely lacking, unfortunately. That being said, I still have plans to look into it sometime. However, if I do, I'll be making sure to make it into a separate script, instead of trying to merge it into this one. (This will become the XInput Gamepad Module, and the other will become the DirectInput Gamepad Module)

That way, you'll be able to check both modules without them interfering with each other, thus giving you the best of both worlds. One thing to note is that I'm sort of on a scripting binge right now, as evidenced by the fact that I've updated all but one of my posted scripts in the last few days, and posted a couple of new ones to boot. I have several more oldies queued for release, and then I'll get around to either picking up old unfinished scripts or working on dropped ideas like DirectInput.
Great work Glitchfinder ^_^

P.s. remove the debug code

*hugs*
Zeriab wrote:
Great work Glitchfinder ^_^

P.s. remove the debug code

*hugs*


Oh, thanks for that one! I'll cut it out right now.
Glitchfinder wrote:

Actually, in this case, there's a reason this script uses a .dll file. That because both XInput and DirectInput have no native Windows implementation. I have to actually program the access points into the .dll files. And last time I looked at DirectInput, I was left extremely confused. Official documentation on it is sorely lacking, unfortunately. That being said, I still have plans to look into it sometime. However, if I do, I'll be making sure to make it into a separate script, instead of trying to merge it into this one. (This will become the XInput Gamepad Module, and the other will become the DirectInput Gamepad Module)

That way, you'll be able to check both modules without them interfering with each other, thus giving you the best of both worlds. One thing to note is that I'm sort of on a scripting binge right now, as evidenced by the fact that I've updated all but one of my posted scripts in the last few days, and posted a couple of new ones to boot. I have several more oldies queued for release, and then I'll get around to either picking up old unfinished scripts or working on dropped ideas like DirectInput.


Alright. Until then, I'm looking into Trebor777's, trying to edit it to improve it (I already have some ideas on how to achieve this. For example, removing the dependency on AInput. It isn't really needed anyway.). I'll use my edited version until if and when a better one is released. I'm learning a little about winmm. It seems to allow everything except force feedback. I'll look into that after I finish tweaking this one first. I may not post it though as it might still contain too much of Trebor's original code... we'll see.

EDIT Show
Don't change this script's name. Instead, call the other Glitchfinder's Joypad Input Module That way all your input modules will have the following initials to tell them apart:
GF::KIM #Keyboard
GF::MIM #Mouse
GF::GIM #Gamepad
GF::JIM #Joypad

This is actually how I have them labeled in my script editor. 😉
Line 220: RuntimeError occured. LoadLibrary: Input.dll

Tried it my current project, and in new project as well, got the error both times. I copied the .dll to the main project directory of both the project folders before testing. Am I just missing something?
Ace of Spades wrote:
Line 220: RuntimeError occured. LoadLibrary: Input.dll

Tried it my current project, and in new project as well, got the error both times. I copied the .dll to the main project directory of both the project folders before testing. Am I just missing something?


Generally speaking, no. RMXP has an issue where .dll files that should work and/or have worked in the past cease to function and give this error instead. I've talked to a few people about it in the past, and the only solution I've ever found is giving them a recompiled version of the exact same .dll file. I was actually working with theory on my transition.dll file, and the same thing happened to him. It stopped working, and gave that message, and wouldn't work at all until I sent him a newer version that I had been working on to tweak the visual effect it caused.

To be honest, I'm not entirely certain what causes the issue, as the error message is actually intended to be shown if a .dll file cannot be loaded due to a dependency of some sort. (Though you generally get an error regarding what's missing, in that case)

I'm still looking at my older .dll files, this one included, though. I just recently switched from Microsoft Visual Studio Express to Code::Blocks, so I might give a go at recompiling the .dll file for this in the next couple of days using mingw, to see if that helps.
Hello,

I am enjoying the script and so far its working for me. However when the project starts, before the title screen, a window pops up with various numbers like "[1,1,4,6292,1,256,256,256,256]" click the ok button and the game loads normally. I am rather new to scripting so I'm sorry for the hassle, I know its coming from your script - because it popped up before I made any changes. I am not sure where exactly in your script - any direction would be greatly appreciated.

Oh, I am running the project from inside rpgmakerxp - not independently, still learning the scripting.
If you want to get rid of that, commenting out line 232 should do the job if I'm not mistaken.
newcoda wrote:
Hello,

I am enjoying the script and so far its working for me. However when the project starts, before the title screen, a window pops up with various numbers like "[1,1,4,6292,1,256,256,256,256]" click the ok button and the game loads normally. I am rather new to scripting so I'm sorry for the hassle, I know its coming from your script - because it popped up before I made any changes. I am not sure where exactly in your script - any direction would be greatly appreciated.

Oh, I am running the project from inside rpgmakerxp - not independently, still learning the scripting.

I could have sworn I removed that. I just updated the script in the first post so that it no longer does that. Thanks for the quick response, BlueScope.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=73959