Advertisement
Zeriab

[RGSS2] Read F1 Settings

Oct 6th, 2011
1,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.57 KB | None | 0 0
  1. module Utility
  2.   #############
  3.   # DLL STUFF #
  4.   #############
  5.   READ_REGISTRY    = Win32API.new('advapi32',  'RegGetValue',
  6.                                   %w(l p p l p p p), 'l')
  7.  
  8.   HKEY_CURRENT_USER = 1-0x80000000
  9.  
  10.   ##
  11.   # Read controls from registry
  12.   #
  13.   def self.read_controls
  14.     hkey = HKEY_CURRENT_USER
  15.     subkey = 'Software\Enterbrain\RGSS2'
  16.     value = 'ButtonAssign'
  17.     type = 0x00000008 # Binary
  18.     buftype = "\0" * 5
  19.     buffer_size = 256
  20.     buffer = "\0" * buffer_size
  21.     size = [buffer_size-1].pack("L")
  22.     res = READ_REGISTRY.call(hkey, subkey, value, type, buftype, buffer, size)
  23.     size = size.unpack("L")
  24.     return buffer[0...size[0]].unpack("c*")
  25.   end
  26.  
  27.   def self.read_launchinfullscreen
  28.     read_dword('LaunchInFullScreen') == 1
  29.   end
  30.  
  31.   def self.read_waitforvsync
  32.     read_dword('WaitForVsync') == 1
  33.   end
  34.  
  35.   def self.read_musicplaying
  36.     read_dword('PlayMusic') == 1
  37.   end
  38.  
  39.   def self.read_soundplaying
  40.     read_dword('PlaySound') == 1
  41.   end
  42.  
  43.   ##
  44.   # Reading a given dword
  45.   #
  46.   def self.read_dword(value)
  47.     hkey = HKEY_CURRENT_USER
  48.     subkey = 'Software\Enterbrain\RGSS2'
  49.     type = 0x00000008 # Binary
  50.     buftype = "\0" * 5
  51.     buffer_size = 256
  52.     buffer = "\0" * buffer_size
  53.     size = [buffer_size-1].pack("L")
  54.     res = READ_REGISTRY.call(hkey, subkey, value, type, buftype, buffer, size)
  55.     size = size.unpack("L")
  56.     return buffer[0...size[0]].unpack("L")[0]
  57.   end
  58. end
  59.  
  60. module Input
  61.   VK_RETURN   = 0x0D # ENTER
  62.   VK_SHIFT    = 0x10
  63.   VK_CONTROL  = 0x11 # CTRL
  64.   VK_MENU     = 0x12 # ALT
  65.   VK_ESCAPE   = 0x1B # ESC
  66.   VK_SPACE    = 0x20 # SPACEBAR
  67.   VK_PRIOR    = 0x21 # PAGE UP
  68.   VK_NEXT     = 0x22 # PAGE DOWN
  69.   VK_LEFT     = 0x25 # LEFT ARROW
  70.   VK_UP       = 0x26 # UP ARROW
  71.   VK_RIGHT    = 0x27 # RIGHT ARROW
  72.   VK_DOWN     = 0x28 # DOWN ARROW
  73.   VK_A        = 0x41
  74.   VK_B        = 0x42
  75.   VK_C        = 0x43
  76.   VK_D        = 0x44
  77.   VK_Q        = 0x51
  78.   VK_S        = 0x53
  79.   VK_V        = 0x56
  80.   VK_W        = 0x57
  81.   VK_X        = 0x58
  82.   VK_Z        = 0x5A
  83.   VK_NUMPAD0  = 0x60 # Num0
  84.   VK_F5       = 0x74
  85.   VK_F6       = 0x75
  86.   VK_F7       = 0x76
  87.   VK_F8       = 0x77
  88.   VK_F9       = 0x78
  89.  
  90.   KEYBOARD_TRIGGERS = [VK_RETURN, VK_SHIFT, VK_CONTROL, VK_MENU, VK_ESCAPE,
  91.       VK_SPACE, VK_PRIOR, VK_NEXT, VK_LEFT, VK_UP, VK_DOWN, VK_RIGHT,
  92.       VK_A, VK_B, VK_C, VK_D, VK_Q, VK_S, VK_V, VK_W, VK_X, VK_Z,
  93.       VK_NUMPAD0, VK_F5, VK_F6, VK_F7, VK_F8, VK_F9]
  94.      
  95.   BUTTONS = Utility.read_controls
  96.   BUTTON_MAPPING = {
  97.       VK_RETURN => BUTTONS[11],
  98.       VK_SHIFT => [BUTTONS[14], SHIFT],
  99.       VK_CONTROL => CTRL,
  100.       VK_MENU => ALT,
  101.       VK_ESCAPE => BUTTONS[12],
  102.       VK_SPACE => BUTTONS[10],
  103.       VK_PRIOR => L,
  104.       VK_NEXT => R,
  105.       VK_LEFT => LEFT,
  106.       VK_UP => UP,
  107.       VK_DOWN => DOWN,
  108.       VK_RIGHT => RIGHT,
  109.       VK_A => BUTTONS[20],
  110.       VK_B => BUTTONS[19],
  111.       VK_C => BUTTONS[17],
  112.       VK_D => BUTTONS[22],
  113.       VK_Q => BUTTONS[23],
  114.       VK_S => BUTTONS[21],
  115.       VK_V => BUTTONS[18],
  116.       VK_W => BUTTONS[24],
  117.       VK_X => BUTTONS[16],
  118.       VK_Z => BUTTONS[15],
  119.       VK_NUMPAD0 => BUTTONS[13],
  120.       VK_F5 => F5,
  121.       VK_F6 => F6,
  122.       VK_F7 => F7,
  123.       VK_F8 => F8,
  124.       VK_F9 => F9
  125.   }
  126.      
  127.   ASYNC_KEY = Win32API.new('user32', 'GetAsyncKeyState', 'i', 'i') # Trigger
  128.   KEY_STATE = Win32API.new('user32', 'GetKeyState',      'i', 'i') # Press
  129.  
  130.   # Frames to wait between each repeat
  131.   REPEAT_WAIT = 6
  132.   # Frames to wait from the first repeat to the following repeats
  133.   REPEAT_START_WAIT = 24
  134.  
  135.   # Initialize button presses and triggers
  136.   class << self
  137.     @@button_presses = {}
  138.     @@button_presses.default = 0
  139.     @@button_trigger = {}
  140.     @@button_trigger.default = false
  141.   end
  142.  
  143.   # module functions
  144.   module_function
  145.  
  146.   def update_trigger(state, trigger)
  147.     @@button_trigger[trigger] |= (state == 1)
  148.   end
  149.  
  150.   def update_triggers
  151.     @@button_trigger.clear
  152.     # Check for clicks
  153.     for trigger in KEYBOARD_TRIGGERS
  154.       # Gets key state for presses
  155.       state = KEY_STATE.call(trigger)
  156.       async = ASYNC_KEY.call(trigger) & 1
  157.       if BUTTON_MAPPING[trigger].is_a?(Array)
  158.         for button in BUTTON_MAPPING[trigger]
  159.           next if button == 0
  160.           update_press(state, button)
  161.           update_trigger(async, button)
  162.         end
  163.       else
  164.         next if BUTTON_MAPPING[trigger] == 0
  165.         update_press(state, BUTTON_MAPPING[trigger])
  166.         update_trigger(async, BUTTON_MAPPING[trigger])
  167.       end
  168.     end
  169.   end
  170.  
  171.   def update_press(state, trigger)
  172.     # If 0 or 1
  173.     if state == 0 || state == 1
  174.       if @@button_presses[trigger] > 0
  175.         @@button_presses[trigger] *= -1
  176.       else
  177.         @@button_presses[trigger] = 0
  178.       end
  179.     else
  180.       if @@button_presses[trigger] > 0
  181.         @@button_presses[trigger] += 1
  182.       else
  183.         @@button_presses[trigger] = 1
  184.       end
  185.     end
  186.   end
  187.  
  188.  
  189.   ##
  190.   # Overrides
  191.   #
  192.   def update
  193.     update_triggers
  194.   end
  195.  
  196.   def trigger?(num)
  197.     return @@button_trigger[num]
  198.   end
  199.   def press?(num)
  200.     return @@button_presses[num] >= 1
  201.   end
  202.   def repeat?(num)
  203.     if @@button_presses[num] <= 0
  204.       return false
  205.     elsif @@button_presses[num] == 1
  206.       return true
  207.     elsif @@button_presses[num] > REPEAT_START_WAIT
  208.       return @@button_presses[num] % REPEAT_WAIT == 1
  209.     end
  210.   end
  211.  
  212.   def dir4
  213.     if press?(LEFT)
  214.       return LEFT
  215.     elsif press?(RIGHT)
  216.       return RIGHT
  217.     elsif press?(DOWN)
  218.       return DOWN
  219.     elsif press?(UP)
  220.       return UP
  221.     else
  222.       return 0
  223.     end
  224.   end
  225. end
  226.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement