Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #==============================================================================
- # ** JEM (Jennifer's Entry Module)
- #------------------------------------------------------------------------------
- # by DerVVulfman
- # version 1.6
- # 07-26-2010
- # Both RGSS & RGSS2 (RPGMaker XP & RPGMaker VX)
- #------------------------------------------------------------------------------
- #
- # This system allows for the use of both the default 'standard' input keys
- # as well as a full range of 'newly functional' keys. As such, you now
- # full control over the keys in your game without interfering with the de-
- # fault keys or gamepad controller.
- #
- #
- #-----------------------------------------------------------------------------
- #
- # NOTE ON THE DEFAULT INPUT COMMANDS:
- # The default input commands such as Input.trigger? still work fine, and
- # will support 'most' key input. As some keys are preset for use by the
- # default system ('Space/Enter/C' for Select, 'Esc,Num0,X' for Cancel),
- # these may return values as set by the default RMXP system. As such,
- # you may use the default commands to detect most of the keyboard, but
- # to retrieve all keyboard commands, the newer modules are recommended.
- # The default values of Input::C, Input::B and the like still function.
- #
- # NOTE ON THE NEW INPUT COMMANDS:
- # The new input methods were based on, and named after the original de-
- # fault statements. As such, there should be a very small learning curve
- # to be used to these statements. In addition to the familiar methods, a
- # new method to detect 'double-clicked' input has been included in the
- # system. However, all the new input methods are designed solely for
- # use with the newer input constants and not the default input values.
- # As such, these new statements will not work with Input::B, Input::C or
- # the like. But... for the grouped keys of Input::B, you may use the
- # newly defined set of Input::A_Alt, Input::B_Alt, and Input::C_Alt. The
- # other default keys (X, Y, Z, L and R) can be set up using the new Key
- # constants.
- #
- #-----------------------------------------------------------------------------
- #
- # STANDARD BUTTONS:
- # =================
- #
- # Input.press?(num)
- # Determines whether the button num is currently being pressed.
- # If the button is being pressed, returns TRUE. If not, returns FALSE.
- #
- # Input.trigger?(num)
- # Determines whether the button num is being pressed again.
- # "Pressed again" is seen as time having passed between the button being
- # not pressed and being pressed.
- # If the button is being pressed, returns TRUE. If not, returns FALSE.
- #
- # Input.repeat?(num)
- # Determines whether the button num is being pressed again.
- # Unlike trigger?, takes into account the repeat input of a button being
- # held down continuously.
- # If the button is being pressed, returns TRUE. If not, returns FALSE.
- #
- # -----------------------------------------------------------------------------
- #
- # NEWLY FUNCTIONAL BUTTONS:
- # =========================
- #
- # Input.enabled?
- # This command determines the current state of input keys such as the
- # Numberlock, Caps lock or the like. It will return a TRUE value if
- # such a button is turned on, and return FALSE if not.
- #
- # Input.reset
- # This command will clear the buffer of currently loaded input arrays
- # created with this system.
- #
- # Input.release?(num)
- # Determines whether the button num was pressed and was just released.
- # Based on the press? function it takes into account previously pressed
- # keys that were just held down.
- # If the button is released, returns TRUE. If not, returns FALSE.
- #
- # Input.key_press?(num)
- # Determines whether the button num is currently being pressed.
- # If the button is being pressed, returns TRUE. If not, returns FALSE.
- #
- # Input.key_trigger?(num)
- # Determines whether the button num is being pressed again.
- # "Pressed again" is seen as time having passed between the button being
- # not pressed and being pressed.
- # If the button is being pressed, returns TRUE. If not, returns FALSE.
- #
- # Input.key_repeat?(num)
- # Determines whether the button num is being pressed again.
- # Unlike trigger?, takes into account the repeat input of a button being
- # held down continuously.
- # If the button is being pressed, returns TRUE. If not, returns FALSE.
- #
- # Input.key_double?(num)
- # Determines whether the button num was pressed twice in a small time frame.
- # "small time frame" is seen as a the amount of time passed between the
- # button being pressed and released and to be pressed once again.
- # If the button is twice pressed, returns TRUE. If not, returns FALSE.
- #
- #==============================================================================
- #==============================================================================
- # ** Module Input
- #------------------------------------------------------------------------------
- # Adds new Key Constants for use by the Input test functions.
- #==============================================================================
- module Input
- #--------------------------------------------------------------------------
- # * Function Speed Configurables
- #--------------------------------------------------------------------------
- #
- REPEAT_CLICK = 1 # Frame speed for repeat
- DOUBLE_CLICK = 10 # Frame speed for Double-Click Detection
- #--------------------------------------------------------------------------
- # * Constant Definitions
- #--------------------------------------------------------------------------
- #
- # Letter Keys -------------------------------------------------------------
- Key_A = 65 ; Key_B = 66 ; Key_C = 67 ; Key_D = 68 ; Key_E = 69 ; Key_F = 70
- Key_G = 71 ; Key_H = 72 ; Key_I = 73 ; Key_J = 74 ; Key_K = 75 ; Key_L = 76
- Key_M = 77 ; Key_N = 78 ; Key_O = 79 ; Key_P = 80 ; Key_Q = 81 ; Key_R = 82
- Key_S = 83 ; Key_T = 84 ; Key_U = 85 ; Key_V = 86 ; Key_W = 87 ; Key_X = 88
- Key_Y = 89 ; Key_Z = 90
- #
- # Top Row Numbers ---------------------------------------------------------
- Key_0 = 48 ; Key_1 = 49 ; Key_2 = 50 ; Key_3 = 51 ; Key_4 = 52 ; Key_5 = 53
- Key_6 = 54 ; Key_7 = 55 ; Key_8 = 56 ; Key_9 = 57
- #
- # Keypad Buttons ----------------------------------------------------------
- Pad_0 = 96 ; Pad_1 = 97 ; Pad_2 = 98 ; Pad_3 = 99
- Pad_4 = 100 ; Pad_5 = 101 ; Pad_6 = 102 ; Pad_7 = 103
- Pad_8 = 104 ; Pad_9 = 105 ; Pad_LEFT = 37 ; Pad_UP = 38
- Pad_RIGHT = 39 ; Pad_DOWN = 40 ; Pad_MULT = 106 ; Pad_ADD = 107
- Pad_SUB = 109 ; Pad_DIV = 111 ; Pad_DEC = 110 ; Pad_CENTER= 12
- #
- # Function Key Constants --------------------------------------------------
- Key_F3 = 114 ; Key_F4 = 115 ; Key_F5 = 116 ; Key_F6 = 117
- Key_F7 = 118 ; Key_F8 = 119 ; Key_F9 = 120 ; Key_F10 = 121
- Key_F11 = 122
- #
- # Additional buttons ------------------------------------------------------
- BK_SPACE = 8 ; TAB = 9 ; ENTER = 13 ; PAUSE = 19
- CAPSLOCK = 20 ; ESC = 27 ; SPACE = 32 ; PGUP = 33
- PGDN = 34 ; LINE_END = 35 ; HOME = 36 ; PRSCRN = 44
- INSERT = 45 ; DELETE = 46 ; APPS = 93 ; NUMLOCK = 144
- SCROLL = 145 ; L_SHIFT = 160 ; R_SHIFT = 161 ; L_CTRL = 162
- R_CTRL = 163 ; L_ALT = 164 ; R_ALT = 165 ; COLON = 186
- EQUAL = 187 ; COMMA = 188 ; UNDERLINE = 189 ; PERIOD = 190
- BACKSLASH = 191 ; TILDE = 192 ; L_BRACKET = 219 ; SLASH = 220
- R_BRACKET = 221 ; QUOTE = 222
- #
- # Alternate replacement & GamePad/Joypad Buttons (Double-Click) -----------
- A_Alt = 1001 ; B_Alt = 1002 ; C_Alt = 1003
- UP_Alt = 1004 ; DOWN_Alt = 1005 ; LEFT_Alt = 1006 ; RIGHT_Alt = 1007
- Joy_1 = 1008 ; Joy_2 = 1009 ; Joy_3 = 1010 ; Joy_4 = 1011
- Joy_5 = 1012 ; Joy_6 = 1013 ; Joy_7 = 1014 ; Joy_8 = 1015
- Joy_Down_L= 1016 ; Joy_Down = 1017 ; Joy_Down_R= 1018 ; Joy_Left = 1019
- Joy_Right = 1020 ; Joy_Up_L = 1021 ; Joy_Up = 1022 ; Joy_Up_R = 1023
- #--------------------------------------------------------------------------
- # Load Constants
- #--------------------------------------------------------------------------
- #
- # Master List of Keyboard Keys --------------------------------------------
- #
- K_LIST = [
- # General Uppercase Keys ----------------------------------
- Key_A, Key_B, Key_C, Key_D, Key_E, Key_F, Key_G, Key_H,
- Key_I, Key_J, Key_K, Key_L, Key_M, Key_N, Key_O, Key_P,
- Key_Q, Key_R, Key_S, Key_T, Key_U, Key_V, Key_W, Key_X,
- Key_Y, Key_Z,
- # Top Row Numbers -----------------------------------------
- Key_0, Key_1, Key_2, Key_3, Key_4, Key_5, Key_6, Key_7,
- Key_8,Key_9,
- # Function Key Constants ----------------------------------
- Key_F3, Key_F4, Key_F5, Key_F6, Key_F7, Key_F8, Key_F9,
- Key_F10, Key_F11,
- # Keypad Buttons ------------------------------------------
- Pad_0, Pad_1, Pad_2, Pad_3, Pad_4,
- Pad_5, Pad_6, Pad_7, Pad_8, Pad_9,
- Pad_LEFT, Pad_UP, Pad_RIGHT, Pad_DOWN, Pad_CENTER,
- Pad_MULT, Pad_ADD, Pad_SUB, Pad_DIV, Pad_DEC,
- # Additional buttons --------------------------------------
- BK_SPACE, TAB, ENTER, PAUSE, CAPSLOCK,
- ESC, SPACE, PGUP, PGDN, LINE_END,
- HOME, PRSCRN, INSERT, DELETE, APPS,
- NUMLOCK, SCROLL, L_SHIFT, R_SHIFT, L_CTRL,
- R_CTRL, L_ALT, R_ALT, COLON, EQUAL,
- COMMA, UNDERLINE, PERIOD, BACKSLASH, TILDE,
- L_BRACKET, SLASH, R_BRACKET, QUOTE
- # ---------------------------------------------------------
- ]
- #
- # Secondary List for Replacement keys (double-click) ----------------------
- #
- R_LIST = []
- R_LIST[1] = [L_SHIFT, R_SHIFT, Key_A ]
- R_LIST[2] = [ Pad_0, Key_X, ESC ]
- R_LIST[3] = [ ENTER, Key_C, SPACE ]
- R_LIST[4] = [Pad_UP] ; R_LIST[5] = [Pad_DOWN]
- R_LIST[6] = [Pad_LEFT] ; R_LIST[7] = [Pad_RIGHT]
- #
- # Secondary List for Gamepad Controller keys (double-click) ---------------
- #
- G_LIST = []
- G_LIST[1] = [1] ; G_LIST[2] = [2] ; G_LIST[3] = [4] ; G_LIST[4] = [8]
- G_LIST[5] = [16] ; G_LIST[6] = [32] ; G_LIST[7] = [64] ; G_LIST[8] = [128]
- G_LIST[9] = [[0 ,65535]] ; G_LIST[10] = [[32767,65535]]
- G_LIST[11] = [[65535,65535]] ; G_LIST[12] = [[0 ,32767]]
- G_LIST[13] = [[65535,32767]] ; G_LIST[14] = [[0 , 0]]
- G_LIST[15] = [[32767, 0]] ; G_LIST[16] = [[65535, 0]]
- #--------------------------------------------------------------------------
- # * End of Definitions
- #--------------------------------------------------------------------------
- end
- #==============================================================================
- # ** Input
- #------------------------------------------------------------------------------
- # A class that handles input data from a gamepad or keyboard.
- #==============================================================================
- class << Input
- #--------------------------------------------------------------------------
- # * Defined Win32API Constants
- #--------------------------------------------------------------------------
- Input_Trigger = Win32API.new('user32', 'GetAsyncKeyState', ['i'], 'i')
- Input_Pressed = Win32API.new('user32', 'GetKeyState', ['i'], 'i')
- Input_Test_State = Win32API.new('user32', 'GetKeyState', 'L', 'L')
- GamePad_Quantity = Win32API.new( 'winmm', 'joyGetNumDevs', '' , 'L')
- GamePad_Access = Win32API.new( 'winmm', 'joyGetPos', 'LP', 'L')
- GamePad_State = Win32API.new( 'winmm', 'joyGetPosEx', 'LP', 'L')
- #--------------------------------------------------------------------------
- # * Game Controller Arrays
- #--------------------------------------------------------------------------
- @@gamepad_list = []
- @@gamepad_buffer = [52, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0].pack('L13')
- #--------------------------------------------------------------------------
- # * Initialize Gamepad
- #--------------------------------------------------------------------------
- def Input.initialize_gamepad
- GamePad_Quantity.call.times do |i|
- @@gamepad_list << i if GamePad_Access.call(i, ' ' * 16) == 0
- end
- end
- #--------------------------------------------------------------------------
- # * Input.gamepad_state
- #--------------------------------------------------------------------------
- def Input.gamepad_state
- return nil if @@gamepad_list == []
- GamePad_State.call(@@gamepad_list.first, @@gamepad_buffer)
- result = @@gamepad_buffer.unpack('L13')
- return result[2...result.size-2]
- end
- #--------------------------------------------------------------------------
- # * Input.test_trigger
- # num : virtually any defined Input_Trigger
- #--------------------------------------------------------------------------
- def Input.test_trigger(num)
- Input_Trigger.call(num) & 0x01 == 1
- end
- #--------------------------------------------------------------------------
- # * Input.test_pressed
- # num : virtually any defined Input_Trigger
- #--------------------------------------------------------------------------
- def Input.test_pressed(num)
- return true unless Input_Pressed.call(num).between?(0, 1)
- return false
- end
- #---------------------------------------------------------------------------
- # * Test if Enabled?
- # num : virtually any defined Input_Pressed
- #---------------------------------------------------------------------------
- def Input.enabled?(num)
- return Input_Pressed.call(num) & 1 == 1
- end
- #--------------------------------------------------------------------------
- # * Input.test_gamepad
- # num : values defined for the gamepad controller
- #--------------------------------------------------------------------------
- def Input_test_gamepad(num)
- com = Proc.new {|i| ((i & @@prev_state[6]) != i and (i & @@buttons) == i) }
- if num.is_a?(Array)
- return (@@prev_state[0..1] != num[0..1] and @@direction == num[0..1])
- else
- return ( com.call(num) )
- end
- end
- #--------------------------------------------------------------------------
- # * Input.reset
- #--------------------------------------------------------------------------
- def Input.reset
- @trigger, @pressed, @released, @alt, @prev_press = [], [], [], [], []
- end
- #--------------------------------------------------------------------------
- # * Input.update
- #--------------------------------------------------------------------------
- alias rekey_update update unless $@
- def Input.update
- # Create/Empty the test arrays
- @trigger, @pressed, @released, @alt = [], [], [], []
- # Only reset the prev keys if nil
- @prev_press = [] if @prev_press.nil?
- # Game Controller State
- unless @@gamepad_list == []
- @@prev_state = @@state
- @@state = Input.gamepad_state
- @@direction = @@state[0..1]
- @@buttons = @@state[6]
- end
- # Create alternate key test flags
- for i in 1..23 ; @alt[i] = false ; end
- # Sort through all 7 Alternate Master Keys
- 7.times{|i| for num in Input::R_LIST[i+1]
- if Input.test_trigger(num)
- @alt[i+1] = true
- @trigger.push(num)
- end ; end }
- # Sort through all 16 Game Controller buttons and dir arrow keys
- unless @@gamepad_list == []
- 16.times{|i| for num in Input::G_LIST[i+1]
- @alt[i+8] = true if Input_test_gamepad(num)
- end }
- end
- # Last values not part of pre-defined list
- for num in Input::K_LIST
- @trigger.push(num) if Input.test_trigger(num)
- @pressed.push(num) if Input.test_pressed(num)
- @released.push(num) if !Input.test_pressed(num) && @prev_press.include?(num)
- end
- # Reload previously pressed keys for release test
- @prev_press = @pressed if @pressed != []
- # Counter for repeat feature
- @repeat_counter = 1 if @repeat_counter == nil
- @repeat_counter -= 1
- if @repeat_counter < 0
- @repeat_counter = 0
- @repeat_num = nil
- end
- # Counter for double feature
- @double_counter = 1 if @double_counter == nil
- @double_counter -= 1
- if @double_counter < 0
- @double_counter = 0
- @double_num = nil
- end
- # Perform the original call
- rekey_update
- end
- #--------------------------------------------------------------------------
- # * Input.key_trigger?
- # num : virtually any defined Input_Trigger
- #--------------------------------------------------------------------------
- def Input.key_trigger?(num)
- return true if [email protected]? && @trigger.include?(num)
- return false
- end
- #--------------------------------------------------------------------------
- # * Input.key_press?
- # num : virtually any defined Input_Trigger
- #--------------------------------------------------------------------------
- def Input.key_press?(num)
- return true if rekey_press?(num)
- return false
- end
- #--------------------------------------------------------------------------
- # * Input.key_repeat?
- # num : virtually any defined Input_Trigger
- #--------------------------------------------------------------------------
- def Input.key_repeat?(num)
- return true if [email protected]? && @trigger.include?(num) && Input.is_repeated?(num)
- return false
- end
- #--------------------------------------------------------------------------
- # * Input.key_double?
- # num : virtually any defined Input_Trigger
- #--------------------------------------------------------------------------
- def Input.key_double?(num)
- # Test replacement keys for doubleclick
- for i in 1..23
- return true if [email protected]? && @alt[i] && num == 1000+i && Input.is_dblclicked?(1000+i)
- end
- # Test keyboard for doubleclick
- return true if [email protected]? && @trigger.include?(num) && Input.is_dblclicked?(num)
- return false
- end
- #--------------------------------------------------------------------------
- # * Input.trigger?
- # num : virtually any defined Input_Trigger
- #--------------------------------------------------------------------------
- alias rekey_trigger? trigger? unless $@
- def Input.trigger?(num)
- return true if rekey_trigger?(num)
- return true if [email protected]? && @trigger.include?(num)
- return false
- end
- #--------------------------------------------------------------------------
- # * Input.release?
- # num : virtually any defined Input_Trigger
- #--------------------------------------------------------------------------
- def Input.release?(num)
- return false if num == nil
- return false if @released.nil?
- return true if @released.include?(num)
- return false
- end
- #--------------------------------------------------------------------------
- # * Input.press?
- # num : virtually any defined Input_Trigger
- #--------------------------------------------------------------------------
- alias rekey_press? press? unless $@
- def Input.press?(num)
- return true if [email protected]? && @pressed.include?(num)
- return true if rekey_press?(num)
- return false
- end
- #--------------------------------------------------------------------------
- # * Input.repeat?
- # num : virtually any defined Input_Trigger
- #--------------------------------------------------------------------------
- alias rekey_repeat? repeat? unless $@
- def Input.repeat?(num)
- return true if rekey_repeat?(num)
- return true if [email protected]? && @trigger.include?(num) && !Input.is_repeated?(num)
- return false
- end
- #--------------------------------------------------------------------------
- # * Input.is_repeated?
- # num : virtually any defined Input_Trigger
- #--------------------------------------------------------------------------
- def Input.is_repeated?(num)
- return false if num == nil
- unless @repeat_num == num
- @repeat_counter = Input::REPEAT_CLICK
- @repeat_num = num
- return false
- end
- return true
- end
- #--------------------------------------------------------------------------
- # * Input.is_dblclicked?
- # num : virtually any defined Input_Trigger
- #--------------------------------------------------------------------------
- def Input.is_dblclicked?(num)
- return true if @double_num == num
- @double_num = num
- @double_counter = Input::DOUBLE_CLICK
- return false
- end
- #--------------------------------------------------------------------------
- # * Object Initialization
- #--------------------------------------------------------------------------
- Input.initialize_gamepad
- #--------------------------------------------------------------------------
- # * State (autonomic)
- #--------------------------------------------------------------------------
- @@state = @@prev_state = Input.gamepad_state
- end
Advertisement
Add Comment
Please, Sign In to add comment