IceDragon200

ISS018-Input

Aug 14th, 2011
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 15.58 KB | None | 0 0
  1. #==============================================================================#
  2. # ** ISS - Input
  3. #==============================================================================#
  4. # ** Date Created  : 08/10/2011
  5. # ** Date Modified : 08/10/2011
  6. # ** Created By    : IceDragon
  7. # ** Special Thanks: CaptainJet (Jet) (For helping me with the Win32API)
  8. # ** For Game      : ISTS
  9. # ** ID            : 018
  10. # ** Version       : 0.9
  11. # ** Optional      : ISS000 - Core 1.9 (or above)
  12. #==============================================================================#
  13. ($imported ||= {})["ISS-Input"] = true
  14. #==============================================================================#
  15. # ** ISS
  16. #==============================================================================#
  17. module ISS
  18.   install_script( 18, :system, :input ) if $imported["ISS-Core"]
  19.   OverwriteInput = false # // Some default keys wont work if its overwritten
  20. end
  21.  
  22. #==============================================================================#
  23. # ** ISS::Input (MixIn)
  24. #==============================================================================#
  25. module ISS::Input
  26.  
  27.   #--------------------------------------------------------------------------#
  28.   # * Constant(s)
  29.   #--------------------------------------------------------------------------#
  30.   # // Win32API functions
  31.   GKAS = Win32API.new( "user32", "GetAsyncKeyState", "i", "i" )
  32.   GKS  = Win32API.new( "user32", "GetKeyState", "i", "i" )
  33.   KBDE = Win32API.new( "user32", 'keybd_event', ["i", "i", "l", "l"], "v" )
  34.   SNDI = Win32API.new( "user32", "SendInput", ["I","P","I"], "i" )  
  35.  
  36.   # // Key Codes
  37.   LETTERS = {}
  38.   LETTERS['A'] = 0x41 ; LETTERS['B'] = 0x42 ; LETTERS['C'] = 0x43
  39.   LETTERS['D'] = 0x44 ; LETTERS['E'] = 0x45 ; LETTERS['F'] = 0x46
  40.   LETTERS['G'] = 0x47 ; LETTERS['H'] = 0x48 ; LETTERS['I'] = 0x49
  41.   LETTERS['J'] = 0x4A ; LETTERS['K'] = 0x4B ; LETTERS['L'] = 0x4C
  42.   LETTERS['M'] = 0x4D ; LETTERS['N'] = 0x4E ; LETTERS['O'] = 0x4F
  43.   LETTERS['P'] = 0x50 ; LETTERS['Q'] = 0x51 ; LETTERS['R'] = 0x52
  44.   LETTERS['S'] = 0x53 ; LETTERS['T'] = 0x54 ; LETTERS['U'] = 0x55
  45.   LETTERS['V'] = 0x56 ; LETTERS['W'] = 0x57 ; LETTERS['X'] = 0x58
  46.   LETTERS['Y'] = 0x59 ; LETTERS['Z'] = 0x5A
  47.    
  48.   NUMBERS = []
  49.   NUMBERS[0] = 0x30 ; NUMBERS[1] = 0x31 ; NUMBERS[2] = 0x32 ; NUMBERS[3] = 0x33
  50.   NUMBERS[4] = 0x34 ; NUMBERS[5] = 0x35 ; NUMBERS[6] = 0x36 ; NUMBERS[7] = 0x37
  51.   NUMBERS[8] = 0x38 ; NUMBERS[9] = 0x39
  52.    
  53.   NUMPAD = []
  54.   NUMPAD[0] = 0x60 ; NUMPAD[1] = 0x61 ; NUMPAD[2] = 0x62 ; NUMPAD[3] = 0x63
  55.   NUMPAD[4] = 0x64 ; NUMPAD[5] = 0x65 ; NUMPAD[6] = 0x66 ; NUMPAD[7] = 0x67
  56.   NUMPAD[8] = 0x68 ; NUMPAD[9] = 0x69
  57.  
  58.   NPAD_MULT = 0x6A ; NPAD_ADD  = 0x6B ; NPAD_SEP  = 0x6C ; NPAD_SUB  = 0x6D
  59.   NPAD_DECI = 0x6E ; NPAD_DIV  = 0x6F
  60.  
  61.   F1 = 0x70 ; F2 = 0x71 ; F3 = 0x72 ; F4 = 0x73 ; F5 = 0x74 ; F6 = 0x75
  62.   F7 = 0x76 ; F8 = 0x77 ; F9 = 0x78 ; F10 = 0x79 ; F11 = 0x7A ; F12 = 0x7B
  63.  
  64.   LEFT = 0x25 ; UP = 0x26 ; RIGHT = 0x27 ; DOWN = 0x28
  65.  
  66.   BACKSPACE = 0x08 ; TAB       = 0x09
  67.   ENTER     = 0x0D
  68.   SHIFT     = 0x10 ; CTRL      = 0x11 ; ALT       = 0x12
  69.   LSHIFT    = 0xA0 ; LCTRL     = 0xA2 ; LALT      = 0xA4
  70.   RSHIFT    = 0xA1 ; RCTRL     = 0xA3 ; RALT      = 0xA5
  71.   ESCAPE    = 0x1B
  72.   SPACEBAR  = 0x20 ; PAGEUP    = 0x21 ; PAGEDOWN  = 0x22 ; KEND = 0x23
  73.   HOME      = 0x24 ;
  74.  
  75.   INSERT = 0x2D ; DELETE = 0x2E
  76.  
  77.   CAPLOCK    = 0x14
  78.   NUMLOCK    = 0x90 ; SCROLLLOCK = 0x91
  79.  
  80.   SEMICOLON = COLON      = 0xBA
  81.   PLUS      = EQUAL      = 0xBB
  82.   COMMA     = ALEFT      = 0xBC
  83.   MINUS     = UNDERSCORE = 0xBD
  84.   PERIOD    = ARIGHT     = 0xBE
  85.   BACKSLASH = QUESTION   = 0xBF
  86.   TILDE     = ACCENT     = 0xC0
  87.   LBRACE    = LBRACK     = 0xDB
  88.   FSLASH    = SEPERATOR  = 0xDC
  89.   RBRACE    = RBRACK     = 0xDD
  90.   QUOTE     = DQUOTE     = 0xDE
  91.  
  92.   # // Key Sequences used with the key_sequence method
  93.   KEY_SEQUENCES = {}
  94.   KEY_SEQUENCES["FULLSCREEN"] = [ [ALT, 0], [ENTER, 0], [ENTER, 2], [ALT, 2] ]
  95.  
  96.   #--------------------------------------------------------------------------#
  97.   # * Class Variable(s)
  98.   #--------------------------------------------------------------------------#
  99.   @@krc  = {}
  100.  
  101.   module_function()
  102.  
  103.   #--------------------------------------------------------------------------#
  104.   # * new-method :get_key_async_state
  105.   #--------------------------------------------------------------------------#
  106.   def get_key_async_state( key ) return GKAS.call( key ).abs&0x8000 ; end
  107.  
  108.   #--------------------------------------------------------------------------#
  109.   # * new-method :key_pressed?
  110.   #--------------------------------------------------------------------------#
  111.   def key_pressed?( key )
  112.     if get_key_async_state( key )==0x8000 ; @@krc[key] ||= 0 ; return true ; end
  113.     return false
  114.   end
  115.  
  116.   #--------------------------------------------------------------------------#
  117.   # * new-method :adjust_key
  118.   #--------------------------------------------------------------------------#
  119.   def adjust_key( key )
  120.     key -= 130 if key.between?( 130, 158 ) ; return key
  121.   end
  122.  
  123.   #--------------------------------------------------------------------------#
  124.   # * new-method :set_key_state
  125.   #--------------------------------------------------------------------------#
  126.   def set_key_state( key, state ) ; KBDE.call( key, 0, state, 0 ) ; end
  127.  
  128.   #--------------------------------------------------------------------------#
  129.   # * new-method :send_key (still buggy)
  130.   #--------------------------------------------------------------------------#
  131.   def send_key( key, state )
  132.     # // 0 - Mouse, 1 - Keyboard
  133.     # // 0x0001 - Extended, 0x0002 - KeyUp, 0x0008 - ScanCode, 0x0004 - Unicode
  134.     SNDI.call( 1, [0, 0, 2, 0, 0].pack("LLLLL"), 256 ) #0x0002 # // Still Buggy
  135.   end
  136.  
  137.   #--------------------------------------------------------------------------#
  138.   # * new-method :correct_keys
  139.   #--------------------------------------------------------------------------#
  140.   def correct_keys( keys ) ; return keys.flatten() ; end
  141.  
  142.   #--------------------------------------------------------------------------#
  143.   # * new-method :key_sequence
  144.   #--------------------------------------------------------------------------#
  145.   def key_sequence( name )
  146.     KEY_SEQUENCES[name].each { |a| set_key_state(*a) }
  147.   end
  148.  
  149.   #--------------------------------------------------------------------------#
  150.   # * new-method :repeat_check
  151.   #--------------------------------------------------------------------------#
  152.   def repeat_check( key )
  153.     return (@@krc[key] > 23 && (@@krc[key]-23) % 6 == 0)
  154.   end
  155.  
  156.   #--------------------------------------------------------------------------#
  157.   # * new-method :repeating_update
  158.   #--------------------------------------------------------------------------#
  159.   def repeating_update()
  160.     @@krc.keys.each { |key|
  161.       get_key_async_state( key )==0x8000 ? @@krc[key] += 1 : @@krc.delete( key )
  162.     }
  163.   end
  164.  
  165.   #--------------------------------------------------------------------------#
  166.   # * new-method :num_lock?
  167.   #--------------------------------------------------------------------------#
  168.   def num_lock?()    ; return GKS.call( NUM_LOCK    ) == 0x01 ; end
  169.   #--------------------------------------------------------------------------#
  170.   # * new-method :caps_lock?
  171.   #--------------------------------------------------------------------------#
  172.   def caps_lock?()   ; return GKS.call( CAPS_LOCK   ) == 0x01 ; end
  173.   #--------------------------------------------------------------------------#
  174.   # * new-method :scroll_lock?
  175.   #--------------------------------------------------------------------------#
  176.   def scroll_lock?() ; return GKS.call( SCROLL_LOCK ) == 0x01 ; end
  177.    
  178. end
  179.  
  180. if ISS::OverwriteInput
  181. #==============================================================================#
  182. # ** ISS::Input
  183. #==============================================================================#
  184. module ISS::Input  
  185.  
  186.   #--------------------------------------------------------------------------#
  187.   # * Constants
  188.   #--------------------------------------------------------------------------#  
  189.   A, B, C   = ::Input::A, ::Input::B, ::Input::C
  190.   X, Y, Z   = ::Input::X, ::Input::Y, ::Input::Z
  191.   L, R      = ::Input::L, ::Input::R
  192.  
  193.   module_function()
  194.  
  195.   #--------------------------------------------------------------------------#
  196.   # * new-method :all_trigger?
  197.   #--------------------------------------------------------------------------#
  198.   def all_trigger?( *keys )
  199.     correct_keys( keys ).all? { |k|
  200.       adjk = adjust_key( k ) ; (key_pressed?( adjk ) && @@krc[adjk] == 0) }
  201.   end
  202.  
  203.   #--------------------------------------------------------------------------#
  204.   # * overwrite-method :trigger?
  205.   #--------------------------------------------------------------------------#
  206.   def trigger?( *keys )
  207.     correct_keys( keys ).any? { |k|
  208.       adjk = adjust_key( k ) ; (key_pressed?( adjk ) && @@krc[adjk] == 0) }
  209.   end
  210.  
  211.   #--------------------------------------------------------------------------#
  212.   # * new-method :all_repeat?
  213.   #--------------------------------------------------------------------------#
  214.   def all_repeat?( *keys )
  215.     correct_keys( keys ).all? { |k|
  216.       adjk = adjust_key( k ) ; key_pressed?( adjk ) && repeat_check( adjk ) }
  217.   end
  218.  
  219.   #--------------------------------------------------------------------------#
  220.   # * overwritten-method :repeat?
  221.   #--------------------------------------------------------------------------#
  222.   def repeat?( *keys )
  223.     correct_keys( keys ).any? { |k|
  224.       adjk = adjust_key( k ) ; key_pressed?( adjk ) && repeat_check( adjk ) }
  225.   end
  226.  
  227.   #--------------------------------------------------------------------------#
  228.   # * new-method :all_press?
  229.   #--------------------------------------------------------------------------#
  230.   def all_press?( *keys )
  231.     correct_keys( keys ).all? { |k| adjk = adjust_key( k ) ; key_pressed?( adjk ) }
  232.   end
  233.  
  234.   #--------------------------------------------------------------------------#
  235.   # * overwrite-method :repeat?
  236.   #--------------------------------------------------------------------------#
  237.   def press?( *keys )
  238.     correct_keys( keys ).any? { |k| adjk = adjust_key( k ) ; key_pressed?( adjk ) }
  239.   end
  240.  
  241.   #--------------------------------------------------------------------------#
  242.   # * overwrite-method :dir4
  243.   #--------------------------------------------------------------------------#
  244.   def dir4()
  245.     if key_pressed?( LEFT )    ; return 4
  246.     elsif key_pressed?( UP )   ; return 8
  247.     elsif key_pressed?( RIGHT ); return 6
  248.     elsif key_pressed?( DOWN ) ; return 2
  249.     else                       ; return 0
  250.     end  
  251.   end
  252.  
  253.   #--------------------------------------------------------------------------#
  254.   # * overwrite-method :dir8
  255.   #--------------------------------------------------------------------------#
  256.   def dir8()
  257.     res = dir4()
  258.     return res if res > 0
  259.     if key_pressed?( LEFT ) && key_pressed?( UP )        ; return 7
  260.     elsif key_pressed?( RIGHT ) && key_pressed?( UP )    ; return 9    
  261.     elsif key_pressed?( LEFT ) && key_pressed?( DOWN )   ; return 1
  262.     elsif key_pressed?( RIGHT ) && key_pressed?( DOWN )  ; return 3  
  263.     else                                                 ; return 0
  264.     end  
  265.   end
  266.  
  267.   #--------------------------------------------------------------------------#
  268.   # * overwrite-method :update
  269.   #--------------------------------------------------------------------------#
  270.   def update() ; repeating_update() ; end
  271.  
  272. end
  273. #==============================================================================#
  274. # ** Input
  275. #==============================================================================#
  276. module OldInput ; include Input ; end # // Dump old input into a module
  277. Input = ISS::Input # // Replace default input
  278.  
  279. else # // Mixin Input
  280. #==============================================================================#
  281. # ** Input
  282. #==============================================================================#
  283. module Input
  284.  
  285.   #--------------------------------------------------------------------------#
  286.   # * MixIn(s)
  287.   #--------------------------------------------------------------------------#
  288.   include ISS::Input
  289.  
  290.   #--------------------------------------------------------------------------#
  291.   # * alias-methods
  292.   #--------------------------------------------------------------------------#
  293.   class << self
  294.     alias :iss018_input_trigger? :trigger? unless $@
  295.     alias :iss018_input_press? :press? unless $@
  296.     alias :iss018_input_repeat? :repeat? unless $@
  297.     alias :iss018_input_update :update unless $@
  298.   end
  299.  
  300.   #--------------------------------------------------------------------------#
  301.   # * alias-method :trigger?
  302.   #--------------------------------------------------------------------------#
  303.   def trigger?( *keys )
  304.     correct_keys( keys ).any? { |key|
  305.       if key < 30
  306.         iss018_input_trigger?( key )
  307.       else  
  308.         adjk = adjust_key( k ) ; (key_pressed?( adjk ) && @@krc[adjk] == 0)
  309.       end  
  310.     }  
  311.   end
  312.  
  313.   #--------------------------------------------------------------------------#
  314.   # * new-method :all_trigger?
  315.   #--------------------------------------------------------------------------#
  316.   def all_trigger?( *keys )
  317.     correct_keys( keys ).all? { |key|
  318.       if key < 30
  319.         iss018_input_trigger?( key )
  320.       else  
  321.         adjk = adjust_key( k ) ; (key_pressed?( adjk ) && @@krc[adjk] == 0)
  322.       end  
  323.     }  
  324.   end
  325.  
  326.   #--------------------------------------------------------------------------#
  327.   # * alias-method :repeat?
  328.   #--------------------------------------------------------------------------#
  329.   def repeat?( *keys )
  330.     correct_keys( keys ).any? { |key|
  331.       if key < 30
  332.         iss018_input_repeat?( key )
  333.       else  
  334.         adjk = adjust_key( k ) ; (key_pressed?( adjk ) && repeat_check( adjk ) )
  335.       end  
  336.     }  
  337.   end
  338.  
  339.   #--------------------------------------------------------------------------#
  340.   # * new-method :all_repeat?
  341.   #--------------------------------------------------------------------------#
  342.   def all_repeat?( *keys )
  343.     correct_keys( keys ).all? { |key|
  344.       if key < 30
  345.         iss017_input_repeat?( key )
  346.       else  
  347.         adjk = adjust_key( k ) ; (key_pressed?( adjk ) && repeat_check( adjk ) )
  348.       end  
  349.     }  
  350.   end
  351.  
  352.   #--------------------------------------------------------------------------#
  353.   # * alias-method :press?
  354.   #--------------------------------------------------------------------------#
  355.   def press?( *keys )
  356.     correct_keys( keys ).any? { |key|
  357.       if key < 30
  358.         iss018_input_press?( key )
  359.       else  
  360.         adjk = adjust_key( k ) ; key_pressed?( adjk )
  361.       end  
  362.     }  
  363.   end
  364.  
  365.   #--------------------------------------------------------------------------#
  366.   # * new-method :all_press?
  367.   #--------------------------------------------------------------------------#
  368.   def all_press?( *keys )
  369.     correct_keys( keys ).all? { |key|
  370.       if key < 30
  371.         iss018_input_press?( key )
  372.       else  
  373.         adjk = adjust_key( k ) ; key_pressed?( adjk )
  374.       end  
  375.     }  
  376.   end
  377.  
  378.   #--------------------------------------------------------------------------#
  379.   # * alias-method :update
  380.   #--------------------------------------------------------------------------#
  381.   def update( *args, &block )
  382.     iss018_input_update( *args, &block )
  383.     repeating_update()
  384.   end
  385.  
  386. end
  387.  
  388. end # // OverwriteInput
  389.  
  390. #=*==========================================================================*=#
  391. # ** END OF FILE
  392. #=*==========================================================================*=#
Advertisement
Add Comment
Please, Sign In to add comment