mcgluszak

[Author: Aleworks] Aleworks' Input Module v3.00

Aug 9th, 2011
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 9.16 KB | None | 0 0
  1. #=============================================================================
  2. # *** AWorks Input Module(AIM)
  3. #=============================================================================
  4. # Created by AWorks
  5. # Version: 3.00
  6. # Last Modification: 26/01/2009 (day/month/year)
  7. # Compatible with RMXP & RMVX
  8. #=============================================================================
  9. #==== Description ====
  10. # This script simulates the RGSS/2 module Input. It is not the original module,
  11. # but it has all the original module functions and some new.
  12. #=============================================================================
  13. #==== Requeriments ====
  14. # * ALibrary (AWorks Library)
  15. #   - Version: 1.03
  16. # * AKM (AWorks Keys Module)
  17. #   - Optional, but recommended
  18. #=============================================================================
  19. #==== Version History ====
  20. # * Version 3.00
  21. #   - Complete rewrite of the script, now it uses AWorks.dll for input updating
  22. #=============================================================================
  23. #==== Features ====
  24. # * All keyboard/mouse keys
  25. #   - When calling one of the keys states methods(press?, trigger?, and so on)
  26. #     you can use the AKM script for obtaining the keys identifiers.
  27. #     Insteand of: (Like in the original Input)
  28. #       Input.press?(Input::[KEY NAME HERE])
  29. #     Use: (If you want to detect "X" key from keyboard/mouse)
  30. #       Input.press?(Keys::[KEY NAME HERE])
  31. #     Example for F3 key:
  32. #       Input.press?(Keys::F3)
  33. #     Example for H key:
  34. #       Input.press?(Keys::H)
  35. #     Example for Y key:
  36. #       Input.press?(Keys::Y)
  37. #
  38. # * Input.release?(vk)
  39. #   - Determines whether the key vk is being released. It can be considered as
  40. #     the opposite function of Input.trigger?
  41. #
  42. # * Input.pressed
  43. #   - Returns all keys which are being pressed
  44. #
  45. # * Input.triggered
  46. #   - Returns all keys which are being triggered
  47. #
  48. # * Input.repeated
  49. #   - Returns all keys which are being repeated
  50. #
  51. # * Input.released
  52. #   - Returns all keys which are being released
  53. #=============================================================================
  54. #==== Notes ====
  55. # * For more information about the Input.press?, Input.trigger?, Input.repeat?,
  56. #   Input.dir4, Input.dir8 and Input.update methods see the RMXP/RMVX help file.
  57. #
  58. # * Don't confuse the Input module keys(A, B, C, X, Y, Z, L, R) with the
  59. #   keyboard letters, as the firsts recognize only the user's buttons
  60. #   configuration(Accessible in the F1 menu) and not the real keyboard keys.
  61. #
  62. # * This input system works with virtual keys(VK) for identifying all of the
  63. #   keyboard and mouse keys, and they consist on numbers between 0..255. Each
  64. #   key on keyboard has its own and unique VK and are listed on the
  65. #   AKM for easy use.
  66. #
  67. # * Note that not all computers have the same keyboard and keyboard
  68. #   configuration(Layout), specially for the special symbols and characters
  69. #   keys. Commonly, keyboards just vary in the OEM keys(See AKM), but also
  70. #   some keyboards have some extra keys others doesn't have. (The same applies
  71. #   for mouses)
  72. #
  73. # * This script doesn't detect changes in the buttons configuration from the
  74. #   F1 menu until the game closes and the configuration is saved to the
  75. #   registry. So, you have to restart the game for the configurations to have
  76. #   effect.
  77. #
  78. # * The Conditional Branch "Button being pressed" option and the "Button Input
  79. #   Processing" command are incompatible with this new Input system. However,
  80. #   you may use AIM:PXP patch for RMXP or AIM:PVX patch for RMVX for fixing this
  81. #   incompatibility.
  82. #=============================================================================
  83. #==== Advices ====
  84. # * You can check the state of a key in an event using the Script option of the
  85. #   Conditional Branch event command just writing the Input method and the key
  86. #   you want to check.
  87. #   Example for letter "O", just put:
  88. #     Input.press?(Keys::O)
  89. #=============================================================================
  90.  
  91. #=============================================================================
  92. # ** Module Input
  93. #=============================================================================
  94. module Input
  95.   #---------------------------------------------------------------------------
  96.   # * Variables declaration
  97.   #---------------------------------------------------------------------------
  98.   @time = Array.new(256, 0)
  99.   @press = Array.new(256, false)
  100.   @trigger = Array.new(256, false)
  101.   @repeat = Array.new(256, false)
  102.   @release = Array.new(256, false)
  103.   @dirs = [0, 0]
  104.   #---------------------------------------------------------------------------
  105.   # * convert_keys (internal method)
  106.   #---------------------------------------------------------------------------
  107.   REG_KVALUES = {0=>32,1=>13,2=>27,3=>96,4=>16,5=>90,6=>88,7=>67,8=>86,9=>66,
  108.                  10=>65,11=>83,12=>68,13=>81,14=>87}
  109.   DEFAULT_KEYS = {11=>[16,90],12=>[27,88,96],13=>[13,32,67],14=>[65],15=>[83],
  110.                   16=>[68],17=>[81],18=>[87]}
  111.   def convert_keys(key)
  112.     begin
  113.       keys = []
  114.       rkey = 'HKEY_CURRENT_USER\\Software\\Enterbrain\\RGSS'
  115.       data = AWorks::Registry.read_entry(rkey, 'ButtonAssign')[10, 25].scan(/./)
  116.       15.times {|i| keys.push(REG_KVALUES[i]) if key == data[i].unpack('C')[0]}
  117.       keys
  118.     rescue
  119.       DEFAULT_KEYS[key] rescue []
  120.     end
  121.   end  
  122.   module_function :convert_keys
  123.   #---------------------------------------------------------------------------
  124.   # * Keys Constants declaration
  125.   #---------------------------------------------------------------------------
  126.   LOWER_LEFT = 97
  127.   LOWER_RIGHT = 99
  128.   UPPER_LEFT = 103
  129.   UPPER_RIGHT = 105
  130.   DOWN = [98, 40]
  131.   LEFT = [100, 37]
  132.   RIGHT = [ 102, 39]
  133.   UP = [104, 38]
  134.   A = convert_keys(11)
  135.   B = [45] + convert_keys(12)
  136.   C = convert_keys(13)
  137.   X = convert_keys(14)
  138.   Y = convert_keys(15)
  139.   Z = convert_keys(16)
  140.   L = [33] + convert_keys(17)
  141.   R = [34] + convert_keys(18)
  142.   SHIFT = 16
  143.   CTRL = 17
  144.   ALT = 18
  145.   F5 = 116
  146.   F6 = 117
  147.   F7 = 118
  148.   F8 = 119
  149.   F9 = 120
  150.   #---------------------------------------------------------------------------
  151.   # * Initialize Input
  152.   #---------------------------------------------------------------------------
  153.   AWorks::API::InputIni.call(@time.object_id, @press.object_id,
  154.   @trigger.object_id, @repeat.object_id,  @release.object_id, @dirs.object_id)
  155.   #---------------------------------------------------------------------------
  156.   # * Keys input state update
  157.   #---------------------------------------------------------------------------
  158.   def Input.update
  159.     AWorks::API::InputUpdate.call
  160.   end
  161.   #---------------------------------------------------------------------------
  162.   # * Press?
  163.   #---------------------------------------------------------------------------
  164.   def Input.press?(vk)
  165.     @press.indexes(*vk).include?(true)
  166.   end
  167.   #---------------------------------------------------------------------------
  168.   # * Pressed
  169.   #---------------------------------------------------------------------------
  170.   def Input.pressed
  171.     array = Array.new
  172.     @press.each_index {|i| array.push(i) if @press[i]}
  173.     array
  174.   end
  175.   #---------------------------------------------------------------------------
  176.   # * Trigger?
  177.   #---------------------------------------------------------------------------
  178.   def Input.trigger?(vk)
  179.     @trigger.indexes(*vk).include?(true)
  180.   end
  181.   #---------------------------------------------------------------------------
  182.   # * Triggered
  183.   #---------------------------------------------------------------------------
  184.   def Input.triggered
  185.     array = Array.new
  186.     @trigger.each_index {|i| array.push(i) if @trigger[i]}
  187.     array
  188.   end
  189.   #---------------------------------------------------------------------------
  190.   # * Repeat?
  191.   #---------------------------------------------------------------------------
  192.   def Input.repeat?(vk)
  193.     @repeat.indexes(*vk).include?(true)
  194.   end
  195.   #---------------------------------------------------------------------------
  196.   # * Repeated
  197.   #---------------------------------------------------------------------------
  198.   def Input.repeated
  199.     array = Array.new
  200.     @repeat.each_index {|i| array.push(i) if @repeat[i]}
  201.     array
  202.   end
  203.   #---------------------------------------------------------------------------
  204.   # * Release?
  205.   #---------------------------------------------------------------------------
  206.   def Input.release?(vk)
  207.     @release.indexes(*vk).include?(true)
  208.   end
  209.   #---------------------------------------------------------------------------
  210.   # * Released
  211.   #---------------------------------------------------------------------------
  212.   def Input.released
  213.     array = Array.new
  214.     @release.each_index {|i| array.push(i) if @release[i]}
  215.     array
  216.   end
  217.   #---------------------------------------------------------------------------
  218.   # * 4 Directions
  219.   #---------------------------------------------------------------------------
  220.   def Input.dir4
  221.     @dirs[0]
  222.   end
  223.   #---------------------------------------------------------------------------
  224.   # * 8 Directions
  225.   #---------------------------------------------------------------------------
  226.   def Input.dir8
  227.     @dirs[1]
  228.   end
  229. end
Advertisement
Add Comment
Please, Sign In to add comment