Advertisement
FlipelyFlip

Controller-Test

Oct 21st, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 16.46 KB | None | 0 0
  1. #==============================================================================
  2. # Gamepad Extender v1.1 (2/20/15)
  3. # by Lone Wolf
  4. #------------------------------------------------------------------------------
  5. # This allows scripters to utilize the extra buttons on modern
  6. # XInput-compatible gamepads. It requires DirectX 9.0 or later and
  7. # an XInput compatible gamepad. Incompatible pads will default to
  8. # using the standard Input module functionality.
  9. #
  10. # This is not a plug-and-play script.
  11. # Some knowledge of RGSS is required for proper use.
  12. #
  13. # Instructions:
  14. #     1) Paste in the Materials section
  15. #  2) Replace button calls in (other) scripts to use gamepad buttons as needed
  16. #        (see Command Reference below)
  17. # Optional:
  18. #     2) Use the PadConfig section to specify button differences between
  19. #          Gamepad and Keyboard (for players without gamepads)
  20. #     3) Replace direct button calls in your scripts (and the defaults) with
  21. #          PadConfig calls or these will do nothing
  22. #        (see Command Reference below)
  23. #------------------------------------------------------------------------------
  24. # Command Reference:
  25. #------------------------------------------------------------------------------
  26. # All calls to extended buttons on Pad 1 can be made through the Input module.
  27. # When using multiple pads, send calls to WolfPad with pad number (0...3)
  28. #    as the final parameter. (i.e.  WolfPad.press?(:A, 3)  )
  29. #
  30. # The current list of mappable buttons is as follows:
  31. #
  32. #     :A, :B, :X, :Y     - XBOX 360 standard face buttons.
  33. #     :L1, :L2, :R1, :R2 - Four triggers (LB, LT, RB, RT)
  34. #     :SELECT, :START    - Back and Start buttons
  35. #     :L3, :R3                    - Clickable Analog buttons
  36. #
  37. #     :UP, :DOWN,
  38. #     :LEFT, :RIGHT        - D-Pad buttons
  39. #
  40. #  :L_UP, :L_DOWN
  41. #  :L_LEFT, :L_RIGHT  - Left stick directions
  42. #
  43. #  :R_UP, :R_DOWN
  44. #  :R_LEFT, :R_RIGHT  - Right stick directions
  45. #
  46. # NON-STANDARD MAPPINGS WILL DO NOTHING without a compatible gamepad.
  47. # To that end, use calls to PadConfig to remap non-standard keys into
  48. # the standard domain where possible.
  49. #
  50. #         for example:    Input.trigger?(PadConfig.page_down)
  51. #         will return :R1 if a gamepad is plugged in, but :R otherwise
  52. #
  53. # Analog values can be referenced with the following commands:
  54. #     left_stick
  55. #     right_stick
  56. #     left_trigger
  57. #     right_trigger
  58. #
  59. # Directional values of analog sticks can be referenced with these:
  60. #     lstick4
  61. #     lstick8
  62. #     rstick4
  63. #     rstick8
  64. #
  65. # Controller vibration can be accessed with these:
  66. #     vibrate(left_motor, right_motor, frame_duration)
  67. #     set_motors(left_motor, right_motor)
  68. #
  69. # All functions take an optional gamepad id (0-3) as their final parameter.
  70. #
  71. #------------------------------------------------------------------------------
  72. # Terms of Use:
  73. #------------------------------------------------------------------------------
  74. #     If you use it, give credit. With a few caveats:
  75. #
  76. #     Can't call it alpha nay more, but I consider this script in-development.
  77. #     I make no guarantees of compatibility with other scripts.
  78. #
  79. #  Contact me via PM is you plan on using this in a commercial game.
  80. #  I probably won't charge anything, but I will want to know it's out there.
  81. #
  82. #     This script was posted at the official RPG Maker forums.
  83. #     Do modify or redistribute this script by itself, though you may
  84. #     include a configured version with your own script demos provided
  85. #     you inclide this header in its entirety.
  86. #------------------------------------------------------------------------------
  87. # Contact Info:
  88. #------------------------------------------------------------------------------
  89. # I can be reached via PM only at the RPG Maker Web forums.
  90. #                                                                                     (http://forums.rpgmakerweb.com)
  91. #
  92. # PM'ing the user Lone Wolf at other RPG Maker sites may have...
  93. # unpredicatable results. I made someone really happy the day I registered...
  94. #------------------------------------------------------------------------------
  95. # Credits:
  96. # Lone Wolf (99% of the code)
  97. # raulf17 (directional movement bug fix; now obsolete)
  98. #------------------------------------------------------------------------------
  99. # 翻訳したいんですか?いいんだが、まだ未完成です。
  100. #==============================================================================
  101. module PadConfig
  102.   # Basic configuration settings:
  103.   # static:
  104.   CONTROLLERS = 1 # can be any number from 1-4, higher values may cause errors
  105.  
  106.   # can be redefined by other scripts and changed in-game:
  107.   def self.deadzone # Deadzone for axis input (%), may require adjusting
  108.     0.1
  109.   end
  110.   def self.move_with_stick  # Use left-analog stick with dir4 and dir8
  111.     true
  112.   end
  113.   def self.enable_vibration # Enable force-feedback
  114.     true
  115.   end
  116.  
  117.   # Use this section to write flexible button-mappings for your scripts.
  118.   # Add additional methods as needed.
  119.   # Format:  
  120.   #      WolfPad.plugged_in? ? (gamepad binding) : (fallback binding)
  121.   def self.confirm
  122.     WolfPad.plugged_in? ? :A : :C
  123.   end
  124.   def self.cancel
  125.     WolfPad.plugged_in? ? :B : :B
  126.   end
  127.   def self.dash
  128.     WolfPad.plugged_in? ? :X : :A
  129.   end
  130.   def self.menu
  131.     WolfPad.plugged_in? ? :Y : :B
  132.   end
  133.   def self.page_up
  134.     WolfPad.plugged_in? ? :L1 : :L
  135.   end
  136.   def self.page_down
  137.     WolfPad.plugged_in? ? :R1 : :R
  138.   end
  139.   def self.debug
  140.     WolfPad.plugged_in? ? :L2 : :CTRL
  141.   end
  142.   def self.debug2
  143.     WolfPad.plugged_in? ? :R3 : :F9
  144.   end
  145. end
  146. # Main module:
  147. module WolfPad
  148.   def self.update
  149.     for pad_index in 0...PadConfig::CONTROLLERS
  150.       input = get_state(pad_index)
  151.       if @packet[pad_index] == input[0]
  152.       set_holds(pad_index)
  153.       next
  154.       end
  155.       @packet[pad_index] = input[0]
  156.       @buttons[pad_index] = (input[1] | 0x10000).to_s(2)
  157.       @triggers[pad_index] = [input[2], input[3]]
  158.       @lstick[pad_index] = [constrain_axis(input[4]), -constrain_axis(input[5])]
  159.       @rstick[pad_index] = [constrain_axis(input[6]), -constrain_axis(input[7])]
  160.       set_holds(pad_index)
  161.     end
  162.     update_vibration
  163.     # Extra readout functions go here.
  164.     #dircheck
  165.   end
  166.   def self.test # Prints no. of detected controllers (debugging use only)
  167.     detected = 0
  168.     for i in 0...PadConfig::CONTROLLERS
  169.       self.update
  170.       detected += plugged_in?(i) ? 1 : 0
  171.     end
  172.     puts sprintf("%d XInput controller(s) in use.", detected)
  173.   end
  174.   def self.readout # prints a range from the holds table (debugging use only)
  175.     for i in 00...16
  176.       print sprintf(" %d", @holds[0, i])
  177.       print sprintf(" %d", @holds[1, i])
  178.     end
  179.     puts ";"
  180.   end
  181.   def self.dircheck
  182.     for i in 0...PadConfig::CONTROLLERS
  183.       print sprintf(" %d", key_holds(:UP, i))
  184.       print sprintf(" %d", key_holds(:LEFT, i))
  185.       print sprintf(" %d", key_holds(:DOWN, i))
  186.       print sprintf(" %d", key_holds(:RIGHT, i))
  187.       print " : "
  188.     end
  189.     puts ";"
  190.   end
  191.   # Basic vibration call.
  192.   # For simplicity, motor values should be floats from 0.0 to 1.0
  193.   def self.vibrate(left, right, duration, pad_index = 0)
  194.     return unless PadConfig.enable_vibration
  195.     set_motors(left, right, pad_index)
  196.     @vibrations[pad_index] = duration
  197.   end
  198.   # Counts down vibration event timers
  199.   def self.update_vibration
  200.     for pad in 0...PadConfig::CONTROLLERS
  201.        next if @vibrations[pad] == -1
  202.        @vibrations[pad] -= 1
  203.        if @vibrations[pad] == 0
  204.           @vibrations[pad] = -1
  205.           set_motors(0, 0, pad)
  206.        end
  207.     end
  208.   end
  209.   # Set left and right motor speeds. Vibration continues until stopped.
  210.   # Repeated calls with different values can create vibration effects.
  211.   # For simplicity, input values should be floats from 0.0 to 1.0
  212.   def self.set_motors(left, right, pad_index = 0)
  213.     return unless (PadConfig.enable_vibration) || (left == 0 && right == 0)
  214.     left_v = [left * 65535, 65535].min
  215.     right_v = [right * 65535, 65535].min
  216.     vibration = [left_v, right_v].pack("SS")
  217.     @set_state.call(pad_index, vibration)
  218.   end
  219.   def self.press?(button, pad_index = 0)
  220.     key_holds(button, pad_index) > 0
  221.   end
  222.   def self.trigger?(button, pad_index = 0)
  223.     key_holds(button, pad_index) == 1
  224.   end
  225.   def self.repeat?(button, p_i = 0)
  226.     return true if key_holds(button, p_i) == 1
  227.     return true if key_holds(button, p_i) > 30 && key_holds(button, p_i) % 5 == 0
  228.   end
  229.   # Returns left stick as a pair of floats [x, y] between -1.0 and 1.0
  230.   def self.left_stick(pad_index = 0)
  231.     @lstick[pad_index]
  232.   end
  233.   # Returns right stick as a pair of floats [x, y] between -1.0 and 1.0
  234.   def self.right_stick(pad_index = 0)
  235.     @rstick[pad_index]
  236.   end
  237.   # Returns left trigger as float between 0.0 to 1.0
  238.   def self.left_trigger(pad_index = 0)
  239.     @triggers[pad_index][0] / 255.0
  240.   end
  241.   # Returns right trigger as float between 0.0 to 1.0
  242.   def self.right_trigger(pad_index = 0)
  243.     @triggers[pad_index][1] / 255.0
  244.   end
  245.   def self.dir4(p_i = 0)
  246.     return lstick4(p_i) if PadConfig.move_with_stick
  247.     if press?(:UP, p_i)
  248.      8
  249.     elsif press?(:RIGHT, p_i)
  250.      6
  251.     elsif press?(:LEFT, p_i)
  252.      4
  253.     elsif press?(:DOWN, p_i)
  254.      2
  255.     else
  256.      0
  257.     end
  258.   end
  259.   def self.dir8(p_i = 0)
  260.     return lstick8(p_i) if PadConfig.move_with_stick
  261.     if press?(:UP, p_i) and press?(:LEFT, p_i)
  262.      7
  263.     elsif press?(:UP, p_i) and press?(:RIGHT, p_i)
  264.      9
  265.     elsif press?(:DOWN, p_i) and press?(:LEFT, p_i)
  266.      1
  267.     elsif press?(:DOWN, p_i) and press?(:RIGHT, p_i)
  268.      3
  269.     else
  270.      dir4(p_i)
  271.     end
  272.   end
  273.   # Left-stick direction
  274.   def self.lstick8(p_i = 0)
  275.       flags_to_dir8(key_holds(:L_RIGHT, p_i),key_holds(:L_LEFT, p_i),
  276.                     key_holds(:L_DOWN, p_i),key_holds(:L_UP, p_i))
  277.   end
  278.   def self.lstick4(p_i = 0)
  279.       flags_to_dir4(key_holds(:L_RIGHT, p_i),key_holds(:L_LEFT, p_i),
  280.                     key_holds(:L_DOWN, p_i),key_holds(:L_UP, p_i))
  281.   end
  282.   # Right-stick direction
  283.   def self.rstick8(p_i = 0)
  284.       flags_to_dir8(key_holds(:R_RIGHT, p_i),key_holds(:R_LEFT, p_i),
  285.                     key_holds(:R_DOWN, p_i),key_holds(:R_UP, p_i))
  286.   end
  287.   def self.rstick4(p_i = 0)
  288.       flags_to_dir4(key_holds(:R_RIGHT, p_i),key_holds(:R_LEFT, p_i),
  289.                     key_holds(:R_DOWN, p_i),key_holds(:R_UP, p_i))
  290.   end
  291.   def self.plugged_in?(pad_index = 0)
  292.     @packet[pad_index] && @packet[pad_index] > 0
  293.   end
  294.   def self.keyboard_key?(button)
  295.     !@keys.has_key?(button)
  296.   end
  297.  
  298.   #Helper functions:
  299.   # convert signed half-word axis state to float
  300.   def self.constrain_axis(axis)
  301.     val = axis.to_f / 2**15
  302.     val.abs > PadConfig.deadzone ? val : 0
  303.   end
  304.  
  305.   # derives a dir8 value from directional hold values
  306.   def self.flags_to_dir8(right, left, down, up)
  307.     x = right == left ? 0 : (right > left ? 1 : 2)
  308.     y = down == up ? 0 : (down > up ? 1 : 2)
  309.     table = [[0, 2, 8],
  310.             [ 6, 3, 9],
  311.             [ 4, 1, 7]]
  312.     return table[x][y]
  313.   end
  314.   # derives a dir4 value from directional hold values
  315.   def self.flags_to_dir4(right, left, down, up)
  316.     selection = [right, left, down, up].max
  317.     table = [0,0,down,0,left,0,right,0,up]
  318.     return table.index(selection)
  319.   end
  320.  
  321.   # calculates the precise geometric angle from stick axis values [x,y]
  322.   def self.axis_to_angle(axis)
  323.     cy = -axis[1]
  324.     cx = -axis[0]
  325.     return -1 if cy == 0 && cx == 0
  326.     angle = Math.atan2(cx, cy) * 180 / Math::PI
  327.     angle = angle < 0 ? angle + 360 : angle
  328.     return angle
  329.   end
  330.  
  331.   # Original angle-conversion handlers for analog sticks
  332.   # OBSOLETE: preserved for reference only
  333.   def self.axis_to_dir8(axis)
  334.     angle_to_dir8(axis_to_angle(axis))
  335.   end
  336.   def self.axis_to_dir4(axis)
  337.     angle_to_dir4(axis_to_angle(axis))
  338.   end
  339.  
  340.   def self.angle_to_dir8(angle)
  341.     return 0 if angle == -1
  342.     d = 0
  343.     if angle < 22.5 || angle >= 337.5
  344.      d = 8
  345.     elsif angle < 67.5
  346.      d = 7
  347.     elsif angle < 112.5
  348.      d = 4
  349.     elsif angle < 157.5
  350.      d = 1
  351.     elsif angle < 202.5
  352.      d = 2
  353.     elsif angle < 247.5
  354.      d = 3
  355.     elsif angle < 292.5
  356.      d = 6
  357.     elsif angle < 337.5
  358.      d = 9
  359.     end
  360.     return d
  361.   end
  362.   def self.angle_to_dir4(angle)
  363.     return 0 if angle == -1
  364.     d = 0
  365.     if angle < 45 || angle >= 315
  366.      d = 8
  367.     elsif angle < 135
  368.      d = 4
  369.     elsif angle < 225
  370.      d = 2
  371.     elsif angle < 315
  372.      d = 6
  373.     end
  374.     return d
  375.   end
  376.  
  377.   private # methods past here can't be called from outside
  378.   # This hash correlates RM's Input to XInput keys. Experienced Users only!
  379.   # The Input module will handle any keys not listed here (i.e. :CTRL) itself.
  380.   # Integers refer to specific gamepad button IDs.
  381.   @keys = {
  382.     :UP    => 15, #d-pad up
  383.     :DOWN => 14, #d-pad left
  384.     :LEFT => 13, #d-pad down
  385.     :RIGHT => 12, #d-pad right
  386.     :A     => 3, #lower face button
  387.     :B     => 2, #right face button
  388.     :X     => 1, #left face button
  389.     :Y     => 0, #upper face button
  390.     :L1    => 7, #upper left trigger
  391.     :R1    => 6, #upper right trigger
  392.     :START => 11,
  393.     :SELECT => 10,
  394.     :L3    => 9, #left thubstick button
  395.     :R3    => 8, #right thubstick button
  396.     :L2    => 16, #lower left trigger (press only)
  397.     :R2    => 17, #lower right trigger (press only)
  398.     :L_UP    => 21,
  399.     :L_DOWN    => 20,
  400.     :L_LEFT    => 19,
  401.     :L_RIGHT    =>18,
  402.     :R_UP    => 25,
  403.     :R_DOWN    => 24,
  404.     :R_LEFT    => 23,
  405.     :R_RIGHT    => 22
  406.     }
  407.   #Win32API calls. Leave these alone.
  408.   # Calls to XInput9_1_0.dll now only occur if DirectX is missing
  409.   @set_state = Win32API.new("XINPUT1_3", "XInputSetState", "IP", "V") rescue
  410.                 Win32API.new("XINPUT9_1_0", "XInputSetState", "IP", "V")
  411.   @get_state = Win32API.new("XINPUT1_3", "XInputGetState", "IP", "L") rescue
  412.                 Win32API.new("XINPUT9_1_0", "XInputGetState", "IP", "L")
  413.   #Initializers
  414.   # Will store data for number of gamepads in use.
  415.   @packet = Array.new(PadConfig::CONTROLLERS)
  416.   @buttons = Array.new(PadConfig::CONTROLLERS)
  417.   @triggers = Array.new(PadConfig::CONTROLLERS)
  418.   @lstick = Array.new(PadConfig::CONTROLLERS)
  419.   @rstick = Array.new(PadConfig::CONTROLLERS)
  420.   # tracks how long buttons have been pressed
  421.   @holds = Table.new(PadConfig::CONTROLLERS, 26)
  422.   # stores vibration event timers
  423.   @vibrations = Array.new(PadConfig::CONTROLLERS, -1)
  424.   def self.key_holds(symbol, pad_index)
  425.     return 0 if keyboard_key?(symbol)
  426.     @holds[pad_index, @keys[symbol]]
  427.   end
  428.   def self.get_state(pad_index)
  429.     state = "\0" * 16
  430.     @get_state.call(pad_index, state)
  431.     return state.unpack("LSCCssss")
  432.   end
  433.   def self.set_holds(p_i)
  434.     for i in 1...17
  435.      @holds[p_i, i-1] = @buttons[p_i][i].to_i > 0 ? @holds[p_i, i-1] + 1 : 0
  436.     end
  437.     @holds[p_i, 16] = left_trigger(p_i) >= 0.5 ? @holds[p_i, 16]+1 : 0
  438.     @holds[p_i, 17] = right_trigger(p_i) >= 0.5 ? @holds[p_i, 17]+1 : 0
  439.     @holds[p_i, 18] = left_stick(p_i)[0] >= 0.5 ? @holds[p_i, 18]+1 : 0
  440.     @holds[p_i, 19] = left_stick(p_i)[0] <= -0.5 ? @holds[p_i, 19]+1 : 0
  441.     @holds[p_i, 20] = left_stick(p_i)[1] >= 0.5 ? @holds[p_i, 20]+1 : 0
  442.     @holds[p_i, 21] = left_stick(p_i)[1] <= -0.5 ? @holds[p_i, 21]+1 : 0
  443.     @holds[p_i, 22] = right_stick(p_i)[0] >= 0.5 ? @holds[p_i, 22]+1 : 0
  444.     @holds[p_i, 23] = right_stick(p_i)[0] <= -0.5 ? @holds[p_i, 23]+1 : 0
  445.     @holds[p_i, 24] = right_stick(p_i)[1] >= 0.5 ? @holds[p_i, 24]+1 : 0
  446.     @holds[p_i, 25] = right_stick(p_i)[1] <= -0.5 ? @holds[p_i, 25]+1 : 0
  447.   end
  448. end
  449. # Aliases to tie the above into VXAce's Input module
  450. module Input
  451.   class <<self
  452.     alias :vxa_update :update
  453.     alias :vxa_press? :press?
  454.     alias :vxa_trigger? :trigger?
  455.     alias :vxa_repeat? :repeat?
  456.     alias :vxa_dir4 :dir4
  457.     alias :vxa_dir8 :dir8
  458.   end
  459.   def self.update
  460.     WolfPad.update
  461.     vxa_update
  462.   end
  463.   def self.press?(button)
  464.     return vxa_press?(button) if WolfPad.keyboard_key?(button)
  465.     return WolfPad.press?(button) if WolfPad.plugged_in?
  466.     return vxa_press?(button)
  467.   end
  468.   def self.trigger?(button)
  469.     return vxa_trigger?(button) if WolfPad.keyboard_key?(button)
  470.     return WolfPad.trigger?(button) if WolfPad.plugged_in?
  471.     return vxa_trigger?(button)
  472.   end
  473.   def self.repeat?(button)
  474.     return vxa_repeat?(button) if WolfPad.keyboard_key?(button)
  475.     return WolfPad.repeat?(button) if WolfPad.plugged_in?
  476.     return vxa_repeat?(button)
  477.   end
  478.   def self.dir4
  479.     WolfPad.plugged_in? ? WolfPad.dir4 : vxa_dir4
  480.   end
  481.   def self.dir8
  482.     WolfPad.plugged_in? ? WolfPad.dir8 : vxa_dir8
  483.   end
  484. end
  485.  
  486. msgbox(WolfPad.plugged_in?)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement