Advertisement
Kakakadafi

[Overwrite] controller2

Oct 20th, 2017
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 18.07 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.     false
  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.attack # YIN - Added for XAS
  122.     WolfPad.plugged_in? ? $game_options.game_controls[:controller][:Attack] : $game_options.game_controls[:keyboard][:Attack]
  123.   end
  124.   def self.skill # YIN - Added for XAS
  125.     WolfPad.plugged_in? ? $game_options.game_controls[:controller][:Skill] : $game_options.game_controls[:keyboard][:Skill]
  126.   end
  127.   def self.skill2 # Kadafi - Added for XAS
  128.     WolfPad.plugged_in? ? $game_options.game_controls[:controller][:Skill2] : $game_options.game_controls[:keyboard][:Skill2]
  129.   end
  130.  
  131.   def self.move_up # Kadafi - Added for XAS
  132.     WolfPad.plugged_in? ? $game_options.game_controls[:controller][:Move_Up] : $game_options.game_controls[:keyboard][:Move_Up]
  133.   end
  134.   def self.move_down # Kadafi - Added for XAS
  135.     WolfPad.plugged_in? ? $game_options.game_controls[:controller][:Move_Down] : $game_options.game_controls[:keyboard][:Move_Down]
  136.   end
  137.   def self.move_left # Kadafi - Added for XAS
  138.     WolfPad.plugged_in? ? $game_options.game_controls[:controller][:Move_Left] : $game_options.game_controls[:keyboard][:Move_Left]
  139.   end
  140.   def self.move_right # Kadafi - Added for XAS
  141.     WolfPad.plugged_in? ? $game_options.game_controls[:controller][:Move_Right] : $game_options.game_controls[:keyboard][:Move_Right]
  142.   end
  143.  
  144.   def self.confirm
  145.     WolfPad.plugged_in? ? $game_options.game_controls[:controller][:Confirm] : $game_options.game_controls[:keyboard][:Confirm]
  146.   end
  147.   def self.cancel
  148.     WolfPad.plugged_in? ? $game_options.game_controls[:controller][:Cancel] : $game_options.game_controls[:keyboard][:Cancel]
  149.   end
  150.   def self.dash
  151.     WolfPad.plugged_in? ? $game_options.game_controls[:controller][:Dash] : $game_options.game_controls[:keyboard][:Dash]
  152.   end
  153.   def self.page_up
  154.     WolfPad.plugged_in? ? $game_options.game_controls[:controller][:Page_Up] : $game_options.game_controls[:keyboard][:Page_Up]
  155.   end
  156.   def self.page_down
  157.     WolfPad.plugged_in? ? $game_options.game_controls[:controller][:Page_Down] : $game_options.game_controls[:keyboard][:Page_Down]
  158.   end
  159.   def self.debug
  160.     WolfPad.plugged_in? ? :L2 : :CTRL
  161.   end
  162.   def self.debug2
  163.     WolfPad.plugged_in? ? :R3 : :F9
  164.   end
  165. end
  166. # Main module:
  167. module WolfPad
  168.   def self.update
  169.     for pad_index in 0...PadConfig::CONTROLLERS
  170.       input = get_state(pad_index)
  171.       if @packet[pad_index] == input[0]
  172.       set_holds(pad_index)
  173.       next
  174.       end
  175.       @packet[pad_index] = input[0]
  176.       @buttons[pad_index] = (input[1] | 0x10000).to_s(2)
  177.       @triggers[pad_index] = [input[2], input[3]]
  178.       @lstick[pad_index] = [constrain_axis(input[4]), -constrain_axis(input[5])]
  179.       @rstick[pad_index] = [constrain_axis(input[6]), -constrain_axis(input[7])]
  180.       set_holds(pad_index)
  181.     end
  182.     update_vibration
  183.     # Extra readout functions go here.
  184.     #dircheck
  185.   end
  186.   def self.test # Prints no. of detected controllers (debugging use only)
  187.     detected = 0
  188.     for i in 0...PadConfig::CONTROLLERS
  189.       self.update
  190.       detected += plugged_in?(i) ? 1 : 0
  191.     end
  192.     puts sprintf("%d XInput controller(s) in use.", detected)
  193.   end
  194.   def self.readout # prints a range from the holds table (debugging use only)
  195.     for i in 00...16
  196.       print sprintf(" %d", @holds[0, i])
  197.       print sprintf(" %d", @holds[1, i])
  198.     end
  199.     puts ";"
  200.   end
  201.   def self.dircheck
  202.     for i in 0...PadConfig::CONTROLLERS
  203.       print sprintf(" %d", key_holds(:UP, i))
  204.       print sprintf(" %d", key_holds(:LEFT, i))
  205.       print sprintf(" %d", key_holds(:DOWN, i))
  206.       print sprintf(" %d", key_holds(:RIGHT, i))
  207.       print " : "
  208.     end
  209.     puts ";"
  210.   end
  211.   # Basic vibration call.
  212.   # For simplicity, motor values should be floats from 0.0 to 1.0
  213.   def self.vibrate(left, right, duration, pad_index = 0)
  214.     return unless PadConfig.enable_vibration
  215.     set_motors(left, right, pad_index)
  216.     @vibrations[pad_index] = duration
  217.   end
  218.   # Counts down vibration event timers
  219.   def self.update_vibration
  220.     for pad in 0...PadConfig::CONTROLLERS
  221.        next if @vibrations[pad] == -1
  222.        @vibrations[pad] -= 1
  223.        if @vibrations[pad] == 0
  224.           @vibrations[pad] = -1
  225.           set_motors(0, 0, pad)
  226.        end
  227.     end
  228.   end
  229.   # Set left and right motor speeds. Vibration continues until stopped.
  230.   # Repeated calls with different values can create vibration effects.
  231.   # For simplicity, input values should be floats from 0.0 to 1.0
  232.   def self.set_motors(left, right, pad_index = 0)
  233.     return unless (PadConfig.enable_vibration) || (left == 0 && right == 0)
  234.     left_v = [left * 65535, 65535].min
  235.     right_v = [right * 65535, 65535].min
  236.     vibration = [left_v, right_v].pack("SS")
  237.     @set_state.call(pad_index, vibration)
  238.   end
  239.   def self.press?(button, pad_index = 0)
  240.     key_holds(button, pad_index) > 0
  241.   end
  242.   def self.trigger?(button, pad_index = 0)
  243.     key_holds(button, pad_index) == 1
  244.   end
  245.   def self.repeat?(button, p_i = 0)
  246.     return true if key_holds(button, p_i) == 1
  247.     return true if key_holds(button, p_i) > 30 && key_holds(button, p_i) % 5 == 0
  248.   end
  249.   # Returns left stick as a pair of floats [x, y] between -1.0 and 1.0
  250.   def self.left_stick(pad_index = 0)
  251.     @lstick[pad_index]
  252.   end
  253.   # Returns right stick as a pair of floats [x, y] between -1.0 and 1.0
  254.   def self.right_stick(pad_index = 0)
  255.     @rstick[pad_index]
  256.   end
  257.   # Returns left trigger as float between 0.0 to 1.0
  258.   def self.left_trigger(pad_index = 0)
  259.     @triggers[pad_index][0] / 255.0
  260.   end
  261.   # Returns right trigger as float between 0.0 to 1.0
  262.   def self.right_trigger(pad_index = 0)
  263.     @triggers[pad_index][1] / 255.0
  264.   end
  265.   def self.dir4(p_i = 0)
  266.     return lstick4(p_i) if PadConfig.move_with_stick
  267.     if press?(:UP, p_i)
  268.      8
  269.     elsif press?(:RIGHT, p_i)
  270.      6
  271.     elsif press?(:LEFT, p_i)
  272.      4
  273.     elsif press?(:DOWN, p_i)
  274.      2
  275.     else
  276.      0
  277.     end
  278.   end
  279.   def self.dir8(p_i = 0)
  280.     return lstick8(p_i) if PadConfig.move_with_stick
  281.     if press?(:UP, p_i) and press?(:LEFT, p_i)
  282.      7
  283.     elsif press?(:UP, p_i) and press?(:RIGHT, p_i)
  284.      9
  285.     elsif press?(:DOWN, p_i) and press?(:LEFT, p_i)
  286.      1
  287.     elsif press?(:DOWN, p_i) and press?(:RIGHT, p_i)
  288.      3
  289.     else
  290.      dir4(p_i)
  291.     end
  292.   end
  293.   # Left-stick direction
  294.   def self.lstick8(p_i = 0)
  295.       flags_to_dir8(key_holds(:L_RIGHT, p_i),key_holds(:L_LEFT, p_i),
  296.                     key_holds(:L_DOWN, p_i),key_holds(:L_UP, p_i))
  297.   end
  298.   def self.lstick4(p_i = 0)
  299.       flags_to_dir4(key_holds(:L_RIGHT, p_i),key_holds(:L_LEFT, p_i),
  300.                     key_holds(:L_DOWN, p_i),key_holds(:L_UP, p_i))
  301.   end
  302.   # Right-stick direction
  303.   def self.rstick8(p_i = 0)
  304.       flags_to_dir8(key_holds(:R_RIGHT, p_i),key_holds(:R_LEFT, p_i),
  305.                     key_holds(:R_DOWN, p_i),key_holds(:R_UP, p_i))
  306.   end
  307.   def self.rstick4(p_i = 0)
  308.       flags_to_dir4(key_holds(:R_RIGHT, p_i),key_holds(:R_LEFT, p_i),
  309.                     key_holds(:R_DOWN, p_i),key_holds(:R_UP, p_i))
  310.   end
  311.   def self.plugged_in?(pad_index = 0)
  312.     @packet[pad_index] && @packet[pad_index] > 0
  313.   end
  314.   def self.keyboard_key?(button)
  315.     !@keys.has_key?(button)
  316.   end
  317.  
  318.   #Helper functions:
  319.   # convert signed half-word axis state to float
  320.   def self.constrain_axis(axis)
  321.     val = axis.to_f / 2**15
  322.     val.abs > PadConfig.deadzone ? val : 0
  323.   end
  324.  
  325.   # derives a dir8 value from directional hold values
  326.   def self.flags_to_dir8(right, left, down, up)
  327.     x = right == left ? 0 : (right > left ? 1 : 2)
  328.     y = down == up ? 0 : (down > up ? 1 : 2)
  329.     table = [[0, 2, 8],
  330.             [ 6, 3, 9],
  331.             [ 4, 1, 7]]
  332.     return table[x][y]
  333.   end
  334.   # derives a dir4 value from directional hold values
  335.   def self.flags_to_dir4(right, left, down, up)
  336.     selection = [right, left, down, up].max
  337.     table = [0,0,down,0,left,0,right,0,up]
  338.     return table.index(selection)
  339.   end
  340.  
  341.   # calculates the precise geometric angle from stick axis values [x,y]
  342.   def self.axis_to_angle(axis)
  343.     cy = -axis[1]
  344.     cx = -axis[0]
  345.     return -1 if cy == 0 && cx == 0
  346.     angle = Math.atan2(cx, cy) * 180 / Math::PI
  347.     angle = angle < 0 ? angle + 360 : angle
  348.     return angle
  349.   end
  350.  
  351.   # Original angle-conversion handlers for analog sticks
  352.   # OBSOLETE: preserved for reference only
  353.   def self.axis_to_dir8(axis)
  354.     angle_to_dir8(axis_to_angle(axis))
  355.   end
  356.   def self.axis_to_dir4(axis)
  357.     angle_to_dir4(axis_to_angle(axis))
  358.   end
  359.  
  360.   def self.angle_to_dir8(angle)
  361.     return 0 if angle == -1
  362.     d = 0
  363.     if angle < 22.5 || angle >= 337.5
  364.      d = 8
  365.     elsif angle < 67.5
  366.      d = 7
  367.     elsif angle < 112.5
  368.      d = 4
  369.     elsif angle < 157.5
  370.      d = 1
  371.     elsif angle < 202.5
  372.      d = 2
  373.     elsif angle < 247.5
  374.      d = 3
  375.     elsif angle < 292.5
  376.      d = 6
  377.     elsif angle < 337.5
  378.      d = 9
  379.     end
  380.     return d
  381.   end
  382.   def self.angle_to_dir4(angle)
  383.     return 0 if angle == -1
  384.     d = 0
  385.     if angle < 45 || angle >= 315
  386.      d = 8
  387.     elsif angle < 135
  388.      d = 4
  389.     elsif angle < 225
  390.      d = 2
  391.     elsif angle < 315
  392.      d = 6
  393.     end
  394.     return d
  395.   end
  396.  
  397.   private # methods past here can't be called from outside
  398.   # This hash correlates RM's Input to XInput keys. Experienced Users only!
  399.   # The Input module will handle any keys not listed here (i.e. :CTRL) itself.
  400.   # Integers refer to specific gamepad button IDs.
  401.   @keys = {
  402.     :UP    => 15, #d-pad up
  403.     :DOWN => 14, #d-pad left
  404.     :LEFT => 13, #d-pad down
  405.     :RIGHT => 12, #d-pad right
  406.     :A     => 3, #lower face button
  407.     :B     => 2, #right face button
  408.     :X     => 1, #left face button
  409.     :Y     => 0, #upper face button
  410.     :L1    => 7, #upper left trigger
  411.     :R1    => 6, #upper right trigger
  412.     :START => 11,
  413.     :SELECT => 10,
  414.     :L3    => 9, #left thubstick button
  415.     :R3    => 8, #right thubstick button
  416.     :L2    => 16, #lower left trigger (press only)
  417.     :R2    => 17, #lower right trigger (press only)
  418.     :L_UP    => 21,
  419.     :L_DOWN    => 20,
  420.     :L_LEFT    => 19,
  421.     :L_RIGHT    =>18,
  422.     :R_UP    => 25,
  423.     :R_DOWN    => 24,
  424.     :R_LEFT    => 23,
  425.     :R_RIGHT    => 22
  426.     }
  427.   #Win32API calls. Leave these alone.
  428.   # Calls to XInput9_1_0.dll now only occur if DirectX is missing
  429.   @set_state = Win32API.new("XINPUT1_3", "XInputSetState", "IP", "V") rescue
  430.                 Win32API.new("XINPUT9_1_0", "XInputSetState", "IP", "V")
  431.   @get_state = Win32API.new("XINPUT1_3", "XInputGetState", "IP", "L") rescue
  432.                 Win32API.new("XINPUT9_1_0", "XInputGetState", "IP", "L")
  433.   #Initializers
  434.   # Will store data for number of gamepads in use.
  435.   @packet = Array.new(PadConfig::CONTROLLERS)
  436.   @buttons = Array.new(PadConfig::CONTROLLERS)
  437.   @triggers = Array.new(PadConfig::CONTROLLERS)
  438.   @lstick = Array.new(PadConfig::CONTROLLERS)
  439.   @rstick = Array.new(PadConfig::CONTROLLERS)
  440.   # tracks how long buttons have been pressed
  441.   @holds = Table.new(PadConfig::CONTROLLERS, 26)
  442.   # stores vibration event timers
  443.   @vibrations = Array.new(PadConfig::CONTROLLERS, -1)
  444.   def self.key_holds(symbol, pad_index)
  445.     return 0 if keyboard_key?(symbol)
  446.     @holds[pad_index, @keys[symbol]]
  447.   end
  448.   def self.get_state(pad_index)
  449.     state = "\0" * 16
  450.     @get_state.call(pad_index, state)
  451.     return state.unpack("LSCCssss")
  452.   end
  453.   def self.set_holds(p_i)
  454.     for i in 1...17
  455.      @holds[p_i, i-1] = @buttons[p_i][i].to_i > 0 ? @holds[p_i, i-1] + 1 : 0
  456.     end
  457.     @holds[p_i, 16] = left_trigger(p_i) >= 0.5 ? @holds[p_i, 16]+1 : 0
  458.     @holds[p_i, 17] = right_trigger(p_i) >= 0.5 ? @holds[p_i, 17]+1 : 0
  459.     @holds[p_i, 18] = left_stick(p_i)[0] >= 0.5 ? @holds[p_i, 18]+1 : 0
  460.     @holds[p_i, 19] = left_stick(p_i)[0] <= -0.5 ? @holds[p_i, 19]+1 : 0
  461.     @holds[p_i, 20] = left_stick(p_i)[1] >= 0.5 ? @holds[p_i, 20]+1 : 0
  462.     @holds[p_i, 21] = left_stick(p_i)[1] <= -0.5 ? @holds[p_i, 21]+1 : 0
  463.     @holds[p_i, 22] = right_stick(p_i)[0] >= 0.5 ? @holds[p_i, 22]+1 : 0
  464.     @holds[p_i, 23] = right_stick(p_i)[0] <= -0.5 ? @holds[p_i, 23]+1 : 0
  465.     @holds[p_i, 24] = right_stick(p_i)[1] >= 0.5 ? @holds[p_i, 24]+1 : 0
  466.     @holds[p_i, 25] = right_stick(p_i)[1] <= -0.5 ? @holds[p_i, 25]+1 : 0
  467.   end
  468. end
  469. # Aliases to tie the above into VXAce's Input module
  470. module Input
  471.   class <<self
  472.     alias :vxa_update :update
  473.     alias :vxa_press? :press?
  474.     alias :vxa_trigger? :trigger?
  475.     alias :vxa_repeat? :repeat?
  476.     alias :vxa_dir4 :dir4
  477.     alias :vxa_dir8 :dir8
  478.   end
  479.   def self.update
  480.     WolfPad.update
  481.     vxa_update
  482.   end
  483.   def self.press?(button)
  484.     return vxa_press?(button) if WolfPad.keyboard_key?(button)
  485.     return WolfPad.press?(button) if WolfPad.plugged_in?
  486.     return vxa_press?(button)
  487.   end
  488.   def self.trigger?(button)
  489.     return vxa_trigger?(button) if WolfPad.keyboard_key?(button)
  490.     return WolfPad.trigger?(button) if WolfPad.plugged_in?
  491.     return vxa_trigger?(button)
  492.   end
  493.   def self.repeat?(button)
  494.     return vxa_repeat?(button) if WolfPad.keyboard_key?(button)
  495.     return WolfPad.repeat?(button) if WolfPad.plugged_in?
  496.     return vxa_repeat?(button)
  497.   end
  498.   def self.dir4
  499.     WolfPad.plugged_in? ? WolfPad.dir4 : vxa_dir4
  500.   end
  501.   def self.dir8
  502.     WolfPad.plugged_in? ? WolfPad.dir8 : vxa_dir8
  503.   end
  504. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement