#==============================================================================
# Blizzard's Enhanced module Input
#------------------------------------------------------------------------------
# This module handles input.
#==============================================================================
module Input
#----------------------------------------------------------------------------
# Setup of Controls (ASCII)
#----------------------------------------------------------------------------
LMB, RMB, MMB, Backspace, Tab, Enter, Shift, Ctrl, Alt, Esc, D_Down, D_Left,
D_Right, D_Up, Space = 1, 2, 4, 8, 9, 13, 16, 17, 18, 27, 40, 37, 39, 38, 32
NumKeys = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57]
NumPad = [45, 35, 40, 34, 37, 12, 39, 36, 38, 33]
Let = {'A' => 65, 'B' => 66, 'C' => 67, 'D' => 68, 'E' => 69, 'F' => 70,
'G' => 71, 'H' => 72, 'I' => 73, 'J' => 74, 'K' => 75, 'L' => 76,
'M' => 77, 'N' => 78, 'O' => 79, 'P' => 80, 'Q' => 81, 'R' => 82,
'S' => 83, 'T' => 84, 'U' => 85, 'V' => 86, 'W' => 87, 'X' => 88,
'Y' => 89, 'Z' => 90}
Fkeys = [-1, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123]
Colon, Equal, Comma, Underscore, Dot, Backslash, Lb, Rb, Quote = 186, 187,
188, 189, 190, 191, 219, 221, 222
# dll call
State = Win32API.new('user32', 'GetKeyState', ['i'], 'i')
Key = Win32API.new('user32', 'GetAsyncKeyState', ['i'], 'i')
# All ASCII keys
All_keys = 0..255
@repeating = []
256.times{@repeating.push(-1)}
# default controls
Up = [Let['W'], D_Up, Joy1['D_Up']]
Left = [Let['A'], D_Left, Joy1['D_Left'] ]
Down = [Let['S'], D_Down, Joy1['D_Right'] ]
Right = [Let['D'], D_Right, Joy1['D_Down'] ]
Confirm = [Let['J'], Enter, Joy1['X']]
Cancel = [Let['K'], Backspace, Joy1['T']]
Prevpage = [Let['Q'], LMB, Joy1['L1']]
Nextpage = [Let['O'], RMB, Joy1['R1']]
#--------------------------------------------------------------------------
# get_current_state
# key - the ASCII number of the key
# This method checks if the key is being pressed.
#--------------------------------------------------------------------------
def Input.get_current_state(key)
return State.call(key).between?(0, 1)
end
#--------------------------------------------------------------------------
# test_key
# key - the ASCII number of the key
# This method checks if the key was pressed.
#--------------------------------------------------------------------------
def Input.test_key(key)
Key.call(key) & 0x01 == 1
end
#--------------------------------------------------------------------------
# get_symbol
# sym - the given symbol
# Transforms given number into ASCII character.
#--------------------------------------------------------------------------
def Input.get_symbol(sym)
return ((sym != 0 && sym != nil) ? sym.chr : '')
end
#--------------------------------------------------------------------------
# update
# Updates input.
#--------------------------------------------------------------------------
def Input.update
# storing old keys
@_keys = (@_keys == nil ? [] : @_keys) | (@keys == nil ? [] : @keys.clone)
# empty current keys
@keys, @pressed = [], []
# checking through all possible keys
All_keys.each {|key|
# key is triggered if not triggered before
@keys.push(key) if Input.test_key(key) && !@_keys.include?(key)
# if key is not being hold
if Input.get_current_state(key)
# remove from helping array
@_keys.delete(key)
# remove repeat? flag
@repeating[key] = 0
else
# push into pressed array
@pressed.push(key)
# needed for repeat? and repeated?
if @repeating[key] > 0
@repeating[key] < 17 ? @repeating[key] += 1 : @repeating[key] = 14
else
@repeating[key] = 1
end
end}
end
#--------------------------------------------------------------------------
# triggered?
# Internal method to check the trigger state.
#--------------------------------------------------------------------------
def Input.triggered?(key)
return (@keys.include?(key) && !@_keys.include?(key))
end
#--------------------------------------------------------------------------
# pressed?
# Internal method to check the pressed state.
#--------------------------------------------------------------------------
def Input.pressed?(key)
return (@pressed.include?(key))
end
#--------------------------------------------------------------------------
# repeated?
# Internal method to check the pressed state for repeat?.
#--------------------------------------------------------------------------
def Input.repeated?(key)
return ([1, 16].include?(@repeating[key]))
end
#--------------------------------------------------------------------------
# dir4
# 4 direction check.
#--------------------------------------------------------------------------
def Input.dir4
return 2 if Input.press?(Down)
return 4 if Input.press?(Left)
return 6 if Input.press?(Right)
return 8 if Input.press?(Up)
return 0
end
#--------------------------------------------------------------------------
# dir8
# 8 direction check.
#--------------------------------------------------------------------------
def Input.dir8
return 1 if Input.press?(Down) && Input.press?(Left)
return 3 if Input.press?(Down) && Input.press?(Right)
return 7 if Input.press?(Up) && Input.press?(Left)
return 9 if Input.press?(Up) && Input.press?(Right)
return 2 if Input.press?(Down)
return 4 if Input.press?(Left)
return 6 if Input.press?(Right)
return 8 if Input.press?(Up)
return 0
end
#--------------------------------------------------------------------------
# trigger?
# Test if key was triggered once.
#--------------------------------------------------------------------------
def Input.trigger?(keys)
if keys.is_a?(Array)
keys.each {|key| Input.check_old_keys(key).each {|k|
return true if Input.triggered?(k)}}
else
Input.check_old_keys(keys).each {|k| return true if Input.triggered?(k)}
end
return false
end
#--------------------------------------------------------------------------
# press?
# Test if key is being pressed.
#--------------------------------------------------------------------------
def Input.press?(keys)
if keys.is_a?(Array)
keys.each {|key| Input.check_old_keys(key).each {|k|
return true if Input.pressed?(k)}}
else
Input.check_old_keys(keys).each {|k| return true if Input.pressed?(k)}
end
return false
end
#--------------------------------------------------------------------------
# repeat?
# Test if key is being pressed for repeating.
#--------------------------------------------------------------------------
def Input.repeat?(keys)
if keys.is_a?(Array)
keys.each {|key| Input.check_old_keys(key).each {|k|
return true if Input.repeated?(k)}}
else
Input.check_old_keys(keys).each {|k| return true if Input.repeated?(k)}
end
return false
end
#--------------------------------------------------------------------------
# check_old_keys
# Converts all the old keys into the new format if not overriding RMXP's
# controls.
#--------------------------------------------------------------------------
def Input.check_old_keys(key)
case key
when UP then return Up
when LEFT then return Left
when DOWN then return Down
when RIGHT then return Right
when A then return [Shift]
when B then return ([Esc, NumPad[0]] | Cancel)
when C then return ([Space, Enter] | Confirm)
when X then return [Let['X']]
when Y then return [Let['A']]
when Z then return [Let['Z']]
when L then return ([Let['Q']] | Prevpage)
# when R then return ([Let['W']] | Nextpage)
when F5 then return [Fkeys[5]]
when F6 then return [Fkeys[6]]
when F7 then return [Fkeys[7]]
when F8 then return [Fkeys[8]]
when F9 then return [Fkeys[9]]
when SHIFT then return [Shift]
when CTRL then return [Ctrl]
when ALT then return [Alt]
end
return (key.is_a?(Array) ? key : [key])
end
#--------------------------------------------------------------------------
# Anykey
# Checks if ANY key was pressed.
#--------------------------------------------------------------------------
def Input.Anykey
return (@keys != [])
end
end