Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Utility
- #############
- # DLL STUFF #
- #############
- READ_REGISTRY = Win32API.new('advapi32', 'RegGetValue',
- %w(l p p l p p p), 'l')
- HKEY_CURRENT_USER = 1-0x80000000
- ##
- # Read controls from registry
- #
- def self.read_controls
- hkey = HKEY_CURRENT_USER
- subkey = 'Software\Enterbrain\RGSS2'
- value = 'ButtonAssign'
- type = 0x00000008 # Binary
- buftype = "\0" * 5
- buffer_size = 256
- buffer = "\0" * buffer_size
- size = [buffer_size-1].pack("L")
- res = READ_REGISTRY.call(hkey, subkey, value, type, buftype, buffer, size)
- size = size.unpack("L")
- return buffer[0...size[0]].unpack("c*")
- end
- def self.read_launchinfullscreen
- read_dword('LaunchInFullScreen') == 1
- end
- def self.read_waitforvsync
- read_dword('WaitForVsync') == 1
- end
- def self.read_musicplaying
- read_dword('PlayMusic') == 1
- end
- def self.read_soundplaying
- read_dword('PlaySound') == 1
- end
- ##
- # Reading a given dword
- #
- def self.read_dword(value)
- hkey = HKEY_CURRENT_USER
- subkey = 'Software\Enterbrain\RGSS2'
- type = 0x00000008 # Binary
- buftype = "\0" * 5
- buffer_size = 256
- buffer = "\0" * buffer_size
- size = [buffer_size-1].pack("L")
- res = READ_REGISTRY.call(hkey, subkey, value, type, buftype, buffer, size)
- size = size.unpack("L")
- return buffer[0...size[0]].unpack("L")[0]
- end
- end
- module Input
- VK_RETURN = 0x0D # ENTER
- VK_SHIFT = 0x10
- VK_CONTROL = 0x11 # CTRL
- VK_MENU = 0x12 # ALT
- VK_ESCAPE = 0x1B # ESC
- VK_SPACE = 0x20 # SPACEBAR
- VK_PRIOR = 0x21 # PAGE UP
- VK_NEXT = 0x22 # PAGE DOWN
- VK_LEFT = 0x25 # LEFT ARROW
- VK_UP = 0x26 # UP ARROW
- VK_RIGHT = 0x27 # RIGHT ARROW
- VK_DOWN = 0x28 # DOWN ARROW
- VK_A = 0x41
- VK_B = 0x42
- VK_C = 0x43
- VK_D = 0x44
- VK_Q = 0x51
- VK_S = 0x53
- VK_V = 0x56
- VK_W = 0x57
- VK_X = 0x58
- VK_Z = 0x5A
- VK_NUMPAD0 = 0x60 # Num0
- VK_F5 = 0x74
- VK_F6 = 0x75
- VK_F7 = 0x76
- VK_F8 = 0x77
- VK_F9 = 0x78
- KEYBOARD_TRIGGERS = [VK_RETURN, VK_SHIFT, VK_CONTROL, VK_MENU, VK_ESCAPE,
- VK_SPACE, VK_PRIOR, VK_NEXT, VK_LEFT, VK_UP, VK_DOWN, VK_RIGHT,
- VK_A, VK_B, VK_C, VK_D, VK_Q, VK_S, VK_V, VK_W, VK_X, VK_Z,
- VK_NUMPAD0, VK_F5, VK_F6, VK_F7, VK_F8, VK_F9]
- BUTTONS = Utility.read_controls
- BUTTON_MAPPING = {
- VK_RETURN => BUTTONS[11],
- VK_SHIFT => [BUTTONS[14], SHIFT],
- VK_CONTROL => CTRL,
- VK_MENU => ALT,
- VK_ESCAPE => BUTTONS[12],
- VK_SPACE => BUTTONS[10],
- VK_PRIOR => L,
- VK_NEXT => R,
- VK_LEFT => LEFT,
- VK_UP => UP,
- VK_DOWN => DOWN,
- VK_RIGHT => RIGHT,
- VK_A => BUTTONS[20],
- VK_B => BUTTONS[19],
- VK_C => BUTTONS[17],
- VK_D => BUTTONS[22],
- VK_Q => BUTTONS[23],
- VK_S => BUTTONS[21],
- VK_V => BUTTONS[18],
- VK_W => BUTTONS[24],
- VK_X => BUTTONS[16],
- VK_Z => BUTTONS[15],
- VK_NUMPAD0 => BUTTONS[13],
- VK_F5 => F5,
- VK_F6 => F6,
- VK_F7 => F7,
- VK_F8 => F8,
- VK_F9 => F9
- }
- ASYNC_KEY = Win32API.new('user32', 'GetAsyncKeyState', 'i', 'i') # Trigger
- KEY_STATE = Win32API.new('user32', 'GetKeyState', 'i', 'i') # Press
- # Frames to wait between each repeat
- REPEAT_WAIT = 6
- # Frames to wait from the first repeat to the following repeats
- REPEAT_START_WAIT = 24
- # Initialize button presses and triggers
- class << self
- @@button_presses = {}
- @@button_presses.default = 0
- @@button_trigger = {}
- @@button_trigger.default = false
- end
- # module functions
- module_function
- def update_trigger(state, trigger)
- @@button_trigger[trigger] |= (state == 1)
- end
- def update_triggers
- @@button_trigger.clear
- # Check for clicks
- for trigger in KEYBOARD_TRIGGERS
- # Gets key state for presses
- state = KEY_STATE.call(trigger)
- async = ASYNC_KEY.call(trigger) & 1
- if BUTTON_MAPPING[trigger].is_a?(Array)
- for button in BUTTON_MAPPING[trigger]
- next if button == 0
- update_press(state, button)
- update_trigger(async, button)
- end
- else
- next if BUTTON_MAPPING[trigger] == 0
- update_press(state, BUTTON_MAPPING[trigger])
- update_trigger(async, BUTTON_MAPPING[trigger])
- end
- end
- end
- def update_press(state, trigger)
- # If 0 or 1
- if state == 0 || state == 1
- if @@button_presses[trigger] > 0
- @@button_presses[trigger] *= -1
- else
- @@button_presses[trigger] = 0
- end
- else
- if @@button_presses[trigger] > 0
- @@button_presses[trigger] += 1
- else
- @@button_presses[trigger] = 1
- end
- end
- end
- ##
- # Overrides
- #
- def update
- update_triggers
- end
- def trigger?(num)
- return @@button_trigger[num]
- end
- def press?(num)
- return @@button_presses[num] >= 1
- end
- def repeat?(num)
- if @@button_presses[num] <= 0
- return false
- elsif @@button_presses[num] == 1
- return true
- elsif @@button_presses[num] > REPEAT_START_WAIT
- return @@button_presses[num] % REPEAT_WAIT == 1
- end
- end
- def dir4
- if press?(LEFT)
- return LEFT
- elsif press?(RIGHT)
- return RIGHT
- elsif press?(DOWN)
- return DOWN
- elsif press?(UP)
- return UP
- else
- return 0
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement