IceDragon200

WASD - Movement + YEM KeyboardInput

Aug 26th, 2011
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 16.36 KB | None | 0 0
  1. #===============================================================================
  2. #
  3. # OriginalWij and Yanfly Collaboration - Keyboard Input
  4. # Last Date Updated: 2010.06.12
  5. # Level: Normal
  6. #
  7. # This is a utility script that provides the functionality to return inputs
  8. # from the keyboard as well as free up more keys to be used in the Input module.
  9. # This script will also replace Scene_Name and allow for direct keyboard input
  10. # to type in an actor's name as well as fix the maximum characters shown from
  11. # the default base script.
  12. #
  13. #===============================================================================
  14. # Updates
  15. # -----------------------------------------------------------------------------
  16. # o 2010.06.12 - Started and Finished Script.
  17. #===============================================================================
  18. # Instructions
  19. # -----------------------------------------------------------------------------
  20. # To install this script, open up your script editor and copy/paste this script
  21. # to an open slot below ▼ Materials but above ▼ Main. Remember to save.
  22. #
  23. # For users:
  24. #   The Name Input event has been now replaced. It not longer takes you to the
  25. #   name entry scene, but instead, allows the player to enter in the name of
  26. #   the desired actor right in the current screen itself without needing to
  27. #   change the scene altogether.
  28. #
  29. # For scripters:
  30. #   For those curious as to how to retrieve certain letters while coding their
  31. #   Input commands, use the following as reference
  32. #  
  33. #   LETTERS['A'] through LETTERS['Z']
  34. #     This returns true if the respective letter from A through Z is pressed.
  35. #  
  36. #   NUMBERS[0] through NUMBERS[9]
  37. #     This returns true if the respective number on the top row of the keyboard
  38. #     is pressed. This does not include the numberpad.
  39. #  
  40. #   NUMPAD[0] through NUMPAD[9]
  41. #     This returns true if the respective number in the number pad is pressed.
  42. #     This does not include the numbers found at the top row of the keyboard.
  43. #  
  44. #   Symbol Keys
  45. #    
  46. #     USCORE  - This returns true if the - key is pressed.
  47. #     EQUALS  - This returns true if the = key is pressed.
  48. #     SCOLON  - This returns true if the ; key is pressed.
  49. #     QUOTE   - This returns true if the ' key is pressed.
  50. #     COMMA   - This returns true if the , key is pressed.
  51. #     PERIOD  - This returns true if the . key is pressed.
  52. #     SLASH   - This returns true if the / key is pressed.
  53. #     BSLASH  - This returns true if the \ key is pressed.
  54. #     LBRACE  - This returns true if the [ key is pressed.
  55. #     RBRACE  - This returns true if the ] key is pressed.
  56. #     TILDE   - This returns true if the ~ key is pressed.
  57. #  
  58. #   Command Keys
  59. #    
  60. #     BACK    - This returns true if the backspace key is pressed.
  61. #     ENTER   - This returns true if the enter/return key is pressed.
  62. #     SPACE   - This returns true if the space bar is pressed.
  63. #     ESC     - This returns true if the escape key is pressed.
  64. #  
  65. #   Module Methods
  66. #    
  67. #     Input.typing?
  68. #       To check if typing is occurring. Returns true or false.
  69. #    
  70. #     Input.key_type
  71. #       Returns whatever key is being used to type as a string.
  72. #  
  73. #   Example:
  74. #  
  75. #     if Input.typing?
  76. #       string = Input.key_type
  77. #     end
  78. #
  79. #===============================================================================
  80.  
  81. $imported = {} if $imported == nil
  82. $imported["KeyboardInput"] = true
  83.  
  84. #===============================================================================
  85. # Editting anything past this point may potentially result in causing computer
  86. # damage, incontinence, explosion of user's head, coma, death, and/or halitosis.
  87. # Therefore, edit at your own risk.
  88. #===============================================================================
  89.  
  90. #===============================================================================
  91. # Input
  92. #===============================================================================
  93.  
  94. class << Input  
  95.   #--------------------------------------------------------------------------
  96.   # Aliases (Mods - Linked to Module) - Created by OriginalWij
  97.   #--------------------------------------------------------------------------
  98.   alias ow_dt_i_press press? unless $@
  99.   alias ow_dt_i_trigger trigger? unless $@
  100.   alias ow_dt_i_repeat repeat? unless $@
  101.   alias ow_dt_i_update update unless $@
  102. end
  103.  
  104. module Input
  105.   #--------------------------------------------------------------------------
  106.   # constants - Created by OriginalWij and Yanfly
  107.   #--------------------------------------------------------------------------
  108.   LETTERS = {}        
  109.   LETTERS['A'] = 65; LETTERS['B'] = 66; LETTERS['C'] = 67; LETTERS['D'] = 68
  110.   LETTERS['E'] = 69; LETTERS['F'] = 70; LETTERS['G'] = 71; LETTERS['H'] = 72
  111.   LETTERS['I'] = 73; LETTERS['J'] = 74; LETTERS['K'] = 75; LETTERS['L'] = 76
  112.   LETTERS['M'] = 77; LETTERS['N'] = 78; LETTERS['O'] = 79; LETTERS['P'] = 80
  113.   LETTERS['Q'] = 81; LETTERS['R'] = 82; LETTERS['S'] = 83; LETTERS['T'] = 84
  114.   LETTERS['U'] = 85; LETTERS['V'] = 86; LETTERS['W'] = 87; LETTERS['X'] = 88
  115.   LETTERS['Y'] = 89; LETTERS['Z'] = 90
  116.   NUMBERS = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57]
  117.   NUMPAD = [96, 97, 98, 99, 100, 101, 102, 103, 104, 105]
  118.   BACK   = 138; ENTER  = 143; SPACE  = 32;  SCOLON = 186; ESC    = 157
  119.   QUOTE  = 222; EQUALS = 187; COMMA  = 188; USCORE = 189; PERIOD = 190
  120.   SLASH  = 191; LBRACE = 219; RBRACE = 221; BSLASH = 220; TILDE  = 192
  121.   F10    = 121; F11    = 122; CAPS   = 20;  NMUL   = 106; NPLUS  = 107
  122.   NSEP   = 108; NMINUS = 109; NDECI  = 110; NDIV   = 111; Extras =
  123.   [USCORE, EQUALS, LBRACE, RBRACE, BSLASH, SCOLON, QUOTE, COMMA, PERIOD, SLASH,
  124.    NMUL, NPLUS, NSEP, NMINUS, NDECI, NDIV]
  125.  
  126.   #--------------------------------------------------------------------------
  127.   # initial module settings - Created by OriginalWij and Yanfly
  128.   #--------------------------------------------------------------------------
  129.   GetKeyState = Win32API.new("user32", "GetAsyncKeyState", "i", "i")
  130.   GetCapState = Win32API.new("user32", "GetKeyState", "i", "i")
  131.   KeyRepeatCounter = {}
  132.   module_function
  133.   #--------------------------------------------------------------------------
  134.   # alias method: update - Created by OriginalWij
  135.   #--------------------------------------------------------------------------
  136.   def update
  137.     ow_dt_i_update
  138.     for key in KeyRepeatCounter.keys
  139.       if (GetKeyState.call(key).abs & 0x8000 == 0x8000)
  140.         KeyRepeatCounter[key] += 1
  141.       else
  142.         KeyRepeatCounter.delete(key)
  143.       end
  144.     end
  145.   end
  146.  
  147.   #--------------------------------------------------------------------------
  148.   # alias method: press? - Created by OriginalWij
  149.   #--------------------------------------------------------------------------
  150.   def press?(key)
  151.     return ow_dt_i_press(key) if key < 30
  152.     adjusted_key = adjust_key(key)
  153.     return true unless KeyRepeatCounter[adjusted_key].nil?
  154.     return key_pressed?(adjusted_key)
  155.   end
  156.  
  157.   #--------------------------------------------------------------------------
  158.   # alias method: trigger? - Created by OriginalWij
  159.   #--------------------------------------------------------------------------
  160.   def trigger?(key)
  161.     return ow_dt_i_trigger(key) if key < 30
  162.     adjusted_key = adjust_key(key)
  163.     count = KeyRepeatCounter[adjusted_key]
  164.     return ((count == 0) or (count.nil? ? key_pressed?(adjusted_key) : false))
  165.   end
  166.  
  167.   #--------------------------------------------------------------------------
  168.   # alias method: repeat? - Created by OriginalWij
  169.   #--------------------------------------------------------------------------
  170.   def repeat?(key)
  171.     return ow_dt_i_repeat(key) if key < 30
  172.     adjusted_key = adjust_key(key)
  173.     count = KeyRepeatCounter[adjusted_key]
  174.     return true if count == 0
  175.     if count.nil?
  176.       return key_pressed?(adjusted_key)
  177.     else
  178.       return (count >= 23 and (count - 23) % 6 == 0)
  179.     end
  180.   end
  181.  
  182.   #--------------------------------------------------------------------------
  183.   # new method: adjust_key - Created by OriginalWij
  184.   #--------------------------------------------------------------------------
  185.   def adjust_key(key)
  186.     key -= 130 if key.between?(130, 158)
  187.     return key
  188.   end
  189.  
  190.   #--------------------------------------------------------------------------
  191.   # new method: key_pressed? - Created by OriginalWij
  192.   #--------------------------------------------------------------------------
  193.   def key_pressed?(key)
  194.     if (GetKeyState.call(key).abs & 0x8000 == 0x8000)
  195.       KeyRepeatCounter[key] = 0
  196.       return true
  197.     end
  198.     return false
  199.   end
  200.  
  201.   #--------------------------------------------------------------------------
  202.   # new method: typing? - Created by Yanfly
  203.   #--------------------------------------------------------------------------
  204.   def typing?
  205.     return true if repeat?(SPACE)
  206.     for i in 'A'..'Z'
  207.       return true if repeat?(LETTERS[i])
  208.     end
  209.     for i in 0...NUMBERS.size
  210.       return true if repeat?(NUMBERS[i])
  211.       return true if repeat?(NUMPAD[i])
  212.     end
  213.     for key in Extras
  214.       return true if repeat?(key)
  215.     end
  216.     return false
  217.   end
  218.  
  219.   #--------------------------------------------------------------------------
  220.   # new method: key_type - Created by Yanfly
  221.   #--------------------------------------------------------------------------
  222.   def key_type
  223.     return " " if repeat?(SPACE)
  224.     for i in 'A'..'Z'
  225.       next unless repeat?(LETTERS[i])
  226.       return upcase? ? i.upcase : i.downcase
  227.     end
  228.     for i in 0...NUMBERS.size
  229.       return i.to_s if repeat?(NUMPAD[i])
  230.       if !press?(SHIFT)
  231.         return i.to_s if repeat?(NUMBERS[i])
  232.       elsif repeat?(NUMBERS[i])
  233.         case i
  234.         when 1; return "!"
  235.         when 2; return "@"
  236.         when 3; return "#"
  237.         when 4; return "$"
  238.         when 5; return "%"
  239.         when 6; return "^"
  240.         when 7; return "&"
  241.         when 8; return "*"
  242.         when 9; return "("
  243.         when 0; return ")"
  244.         end
  245.       end
  246.     end
  247.     for key in Extras
  248.       next unless repeat?(key)
  249.       case key
  250.       when USCORE; return press?(SHIFT) ? "_" : "-"
  251.       when EQUALS; return press?(SHIFT) ? "+" : "="
  252.       when LBRACE; return press?(SHIFT) ? "{" : "["
  253.       when RBRACE; return press?(SHIFT) ? "}" : "]"
  254.       when BSLASH; return press?(SHIFT) ? "|" : "\\"
  255.       when SCOLON; return press?(SHIFT) ? ":" : ";"
  256.       when QUOTE;  return press?(SHIFT) ? '"' : "'"
  257.       when COMMA;  return press?(SHIFT) ? "<" : ","
  258.       when PERIOD; return press?(SHIFT) ? ">" : "."
  259.       when SLASH;  return press?(SHIFT) ? "?" : "/"
  260.       when NMUL;   return "*"
  261.       when NPLUS;  return "+"
  262.       when NSEP;   return ","
  263.       when NMINUS; return "-"
  264.       when NDECI;  return "."
  265.       when NDIV;   return "/"
  266.       end
  267.     end
  268.     return ""
  269.   end
  270.  
  271.   #--------------------------------------------------------------------------
  272.   # new method: upcase? - Created by Yanfly
  273.   #--------------------------------------------------------------------------
  274.   def upcase?
  275.     return !press?(SHIFT) if GetCapState.call(CAPS) == 1
  276.     return true if press?(SHIFT)
  277.     return false
  278.   end
  279.  
  280. end # Input
  281.  
  282. #===============================================================================
  283. # Game_Interpreter
  284. #===============================================================================
  285.  
  286. class Game_Interpreter
  287.  
  288.   #--------------------------------------------------------------------------
  289.   # overwrite method: command_303 (Name Input Processing)
  290.   #--------------------------------------------------------------------------
  291.   def command_303
  292.     if $data_actors[@params[0]] != nil
  293.       $scene.name_entry(@params[0], @params[1])
  294.     end
  295.     @index += 1
  296.     return false
  297.   end
  298.  
  299. end # Game_Interpreter
  300.  
  301. #===============================================================================
  302. # Window_NameEdit
  303. #===============================================================================
  304.  
  305. class Window_NameEdit < Window_Base
  306.  
  307.   #--------------------------------------------------------------------------
  308.   # overwrite method: initialize
  309.   #--------------------------------------------------------------------------
  310.   def initialize(actor, max_char)
  311.     dw = Graphics.width - 176
  312.     dy = (Graphics.height - 128) / 2
  313.     if $game_message.visible
  314.       difference = Graphics.height - 128
  315.       case $game_message.position
  316.       when 0; dy += 64
  317.       when 1; dy += 0
  318.       when 2; dy -= 64
  319.       end
  320.     end
  321.     super(88, dy, dw, 128)
  322.     @actor = actor
  323.     @name = actor.name
  324.     @max_char = max_char
  325.     name_array = @name.split(//)[0...@max_char]
  326.     @name = ""
  327.     for i in 0...name_array.size
  328.       @name += name_array[i]
  329.     end
  330.     @default_name = @name
  331.     @index = name_array.size
  332.     self.active = false
  333.     refresh
  334.     update_cursor
  335.   end
  336.  
  337.   #--------------------------------------------------------------------------
  338.   # overwrite method: item_rect
  339.   #--------------------------------------------------------------------------
  340.   def item_rect(index)
  341.     if index == @max_char
  342.       rect = Rect.new(0, 0, 0, 0)
  343.     else
  344.       rect = Rect.new(0, 0, 0, 0)
  345.       rect.x = 112 + index * 12
  346.       rect.y = 36
  347.       rect.width = 24
  348.       rect.height = WLH
  349.     end
  350.     return rect
  351.   end
  352.  
  353. end # Window_NameEdit
  354.  
  355. #===============================================================================
  356. # Scene_Base
  357. #===============================================================================
  358.  
  359. class Scene_Base
  360.  
  361.   #--------------------------------------------------------------------------
  362.   # new method: name_entry
  363.   #--------------------------------------------------------------------------
  364.   def name_entry(actor_id, max_char)
  365.     @name_actor_id = actor_id
  366.     @name_entry_max = max_char
  367.     start_name_entry
  368.     end_name_entry
  369.   end
  370.  
  371.   #--------------------------------------------------------------------------
  372.   # new method: start_name_entry
  373.   #--------------------------------------------------------------------------
  374.   def start_name_entry
  375.     Graphics.freeze
  376.     actor = $game_actors[@name_actor_id]
  377.     @edit_window = Window_NameEdit.new(actor, @name_entry_max)
  378.     Graphics.transition(10)
  379.     loop do
  380.       update_name_entry
  381.       if Input.repeat?(Input::BACK) and @edit_window.index > 0
  382.         Sound.play_cancel
  383.         @edit_window.back
  384.       elsif Input.typing? and @edit_window.index != @edit_window.max_char
  385.         Sound.play_cursor
  386.         @edit_window.add(Input.key_type)
  387.       elsif Input.trigger?(Input::ENTER)
  388.         Sound.play_decision
  389.         actor.name = @edit_window.name
  390.         break
  391.       elsif Input.trigger?(Input::ESC)
  392.         Sound.play_cancel
  393.         break
  394.       end
  395.     end
  396.   end
  397.  
  398.   #--------------------------------------------------------------------------
  399.   # new method: update_name_entry
  400.   #--------------------------------------------------------------------------
  401.   def update_name_entry
  402.     update_menu_background
  403.     Graphics.update
  404.     Input.update
  405.     if $scene.is_a?(Scene_Map)
  406.       $game_map.update
  407.       @spriteset.update
  408.     elsif $scene.is_a?(Scene_Battle)
  409.       Graphics.update
  410.       Input.update
  411.       $game_system.update
  412.       $game_troop.update
  413.       @spriteset.update
  414.       @message_window.update
  415.     end
  416.     @edit_window.update
  417.   end
  418.  
  419.   #--------------------------------------------------------------------------
  420.   # end_name_entry
  421.   #--------------------------------------------------------------------------
  422.   def end_name_entry
  423.     @edit_window.dispose
  424.     @edit_window = nil
  425.     @name_actor_id = nil
  426.     @name_entry_max = nil
  427.   end
  428.  
  429. end # Scene_Base
  430.  
  431. #===============================================================================
  432. #
  433. # END OF FILE
  434. #
  435. #===============================================================================
  436.  
  437. # // WASD start
  438. module Input
  439.  
  440.   def self.dir4
  441.     if Input.press?(LETTERS["W"])
  442.       return 8
  443.     elsif Input.press?(LETTERS["A"])  
  444.       return 4
  445.     elsif Input.press?(LETTERS["D"])    
  446.       return 6
  447.     elsif Input.press?(LETTERS["S"])  
  448.       return 2
  449.     end  
  450.     return 0
  451.   end
  452.  
  453.   def self.dir8
  454.     ans = dir4 ; return ans unless ans == 0
  455.     if Input.press?(LETTERS["Q"])
  456.       return 7
  457.     elsif Input.press?(LETTERS["E"])
  458.       return 9
  459.     elsif Input.press?(LETTERS["Z"])  
  460.       return 1
  461.     elsif Input.press?(LETTERS["C"])  
  462.       return 3
  463.     end  
  464.     return 0
  465.   end
  466.  
  467. end
Advertisement
Add Comment
Please, Sign In to add comment