Guest User

Untitled

a guest
Mar 2nd, 2015
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.73 KB | None | 0 0
  1. #==============================================================================
  2. # ** JEM (Jennifer's Entry Module)
  3. #------------------------------------------------------------------------------
  4. # by DerVVulfman
  5. # version 1.6
  6. # 07-26-2010
  7. # Both RGSS & RGSS2 (RPGMaker XP & RPGMaker VX)
  8. #------------------------------------------------------------------------------
  9. #
  10. # This system allows for the use of both the default 'standard' input keys
  11. # as well as a full range of 'newly functional' keys. As such, you now
  12. # full control over the keys in your game without interfering with the de-
  13. # fault keys or gamepad controller.
  14. #
  15. #
  16. #-----------------------------------------------------------------------------
  17. #
  18. # NOTE ON THE DEFAULT INPUT COMMANDS:
  19. # The default input commands such as Input.trigger? still work fine, and
  20. # will support 'most' key input. As some keys are preset for use by the
  21. # default system ('Space/Enter/C' for Select, 'Esc,Num0,X' for Cancel),
  22. # these may return values as set by the default RMXP system. As such,
  23. # you may use the default commands to detect most of the keyboard, but
  24. # to retrieve all keyboard commands, the newer modules are recommended.
  25. # The default values of Input::C, Input::B and the like still function.
  26. #
  27. # NOTE ON THE NEW INPUT COMMANDS:
  28. # The new input methods were based on, and named after the original de-
  29. # fault statements. As such, there should be a very small learning curve
  30. # to be used to these statements. In addition to the familiar methods, a
  31. # new method to detect 'double-clicked' input has been included in the
  32. # system. However, all the new input methods are designed solely for
  33. # use with the newer input constants and not the default input values.
  34. # As such, these new statements will not work with Input::B, Input::C or
  35. # the like. But... for the grouped keys of Input::B, you may use the
  36. # newly defined set of Input::A_Alt, Input::B_Alt, and Input::C_Alt. The
  37. # other default keys (X, Y, Z, L and R) can be set up using the new Key
  38. # constants.
  39. #
  40. #-----------------------------------------------------------------------------
  41. #
  42. # STANDARD BUTTONS:
  43. # =================
  44. #
  45. # Input.press?(num)
  46. # Determines whether the button num is currently being pressed.
  47. # If the button is being pressed, returns TRUE. If not, returns FALSE.
  48. #
  49. # Input.trigger?(num)
  50. # Determines whether the button num is being pressed again.
  51. # "Pressed again" is seen as time having passed between the button being
  52. # not pressed and being pressed.
  53. # If the button is being pressed, returns TRUE. If not, returns FALSE.
  54. #
  55. # Input.repeat?(num)
  56. # Determines whether the button num is being pressed again.
  57. # Unlike trigger?, takes into account the repeat input of a button being
  58. # held down continuously.
  59. # If the button is being pressed, returns TRUE. If not, returns FALSE.
  60. #
  61. # -----------------------------------------------------------------------------
  62. #
  63. # NEWLY FUNCTIONAL BUTTONS:
  64. # =========================
  65. #
  66. # Input.enabled?
  67. # This command determines the current state of input keys such as the
  68. # Numberlock, Caps lock or the like. It will return a TRUE value if
  69. # such a button is turned on, and return FALSE if not.
  70. #
  71. # Input.reset
  72. # This command will clear the buffer of currently loaded input arrays
  73. # created with this system.
  74. #
  75. # Input.release?(num)
  76. # Determines whether the button num was pressed and was just released.
  77. # Based on the press? function it takes into account previously pressed
  78. # keys that were just held down.
  79. # If the button is released, returns TRUE. If not, returns FALSE.
  80. #
  81. # Input.key_press?(num)
  82. # Determines whether the button num is currently being pressed.
  83. # If the button is being pressed, returns TRUE. If not, returns FALSE.
  84. #
  85. # Input.key_trigger?(num)
  86. # Determines whether the button num is being pressed again.
  87. # "Pressed again" is seen as time having passed between the button being
  88. # not pressed and being pressed.
  89. # If the button is being pressed, returns TRUE. If not, returns FALSE.
  90. #
  91. # Input.key_repeat?(num)
  92. # Determines whether the button num is being pressed again.
  93. # Unlike trigger?, takes into account the repeat input of a button being
  94. # held down continuously.
  95. # If the button is being pressed, returns TRUE. If not, returns FALSE.
  96. #
  97. # Input.key_double?(num)
  98. # Determines whether the button num was pressed twice in a small time frame.
  99. # "small time frame" is seen as a the amount of time passed between the
  100. # button being pressed and released and to be pressed once again.
  101. # If the button is twice pressed, returns TRUE. If not, returns FALSE.
  102. #
  103. #==============================================================================
  104.  
  105.  
  106.  
  107. #==============================================================================
  108. # ** Module Input
  109. #------------------------------------------------------------------------------
  110. # Adds new Key Constants for use by the Input test functions.
  111. #==============================================================================
  112. module Input
  113.  
  114. #--------------------------------------------------------------------------
  115. # * Function Speed Configurables
  116. #--------------------------------------------------------------------------
  117. #
  118. REPEAT_CLICK = 1 # Frame speed for repeat
  119. DOUBLE_CLICK = 10 # Frame speed for Double-Click Detection
  120.  
  121. #--------------------------------------------------------------------------
  122. # * Constant Definitions
  123. #--------------------------------------------------------------------------
  124. #
  125. # Letter Keys -------------------------------------------------------------
  126. Key_A = 65 ; Key_B = 66 ; Key_C = 67 ; Key_D = 68 ; Key_E = 69 ; Key_F = 70
  127. Key_G = 71 ; Key_H = 72 ; Key_I = 73 ; Key_J = 74 ; Key_K = 75 ; Key_L = 76
  128. Key_M = 77 ; Key_N = 78 ; Key_O = 79 ; Key_P = 80 ; Key_Q = 81 ; Key_R = 82
  129. Key_S = 83 ; Key_T = 84 ; Key_U = 85 ; Key_V = 86 ; Key_W = 87 ; Key_X = 88
  130. Key_Y = 89 ; Key_Z = 90
  131. #
  132. # Top Row Numbers ---------------------------------------------------------
  133. Key_0 = 48 ; Key_1 = 49 ; Key_2 = 50 ; Key_3 = 51 ; Key_4 = 52 ; Key_5 = 53
  134. Key_6 = 54 ; Key_7 = 55 ; Key_8 = 56 ; Key_9 = 57
  135. #
  136. # Keypad Buttons ----------------------------------------------------------
  137. Pad_0 = 96 ; Pad_1 = 97 ; Pad_2 = 98 ; Pad_3 = 99
  138. Pad_4 = 100 ; Pad_5 = 101 ; Pad_6 = 102 ; Pad_7 = 103
  139. Pad_8 = 104 ; Pad_9 = 105 ; Pad_LEFT = 37 ; Pad_UP = 38
  140. Pad_RIGHT = 39 ; Pad_DOWN = 40 ; Pad_MULT = 106 ; Pad_ADD = 107
  141. Pad_SUB = 109 ; Pad_DIV = 111 ; Pad_DEC = 110 ; Pad_CENTER= 12
  142. #
  143. # Function Key Constants --------------------------------------------------
  144. Key_F3 = 114 ; Key_F4 = 115 ; Key_F5 = 116 ; Key_F6 = 117
  145. Key_F7 = 118 ; Key_F8 = 119 ; Key_F9 = 120 ; Key_F10 = 121
  146. Key_F11 = 122
  147. #
  148. # Additional buttons ------------------------------------------------------
  149. BK_SPACE = 8 ; TAB = 9 ; ENTER = 13 ; PAUSE = 19
  150. CAPSLOCK = 20 ; ESC = 27 ; SPACE = 32 ; PGUP = 33
  151. PGDN = 34 ; LINE_END = 35 ; HOME = 36 ; PRSCRN = 44
  152. INSERT = 45 ; DELETE = 46 ; APPS = 93 ; NUMLOCK = 144
  153. SCROLL = 145 ; L_SHIFT = 160 ; R_SHIFT = 161 ; L_CTRL = 162
  154. R_CTRL = 163 ; L_ALT = 164 ; R_ALT = 165 ; COLON = 186
  155. EQUAL = 187 ; COMMA = 188 ; UNDERLINE = 189 ; PERIOD = 190
  156. BACKSLASH = 191 ; TILDE = 192 ; L_BRACKET = 219 ; SLASH = 220
  157. R_BRACKET = 221 ; QUOTE = 222
  158. #
  159. # Alternate replacement & GamePad/Joypad Buttons (Double-Click) -----------
  160. A_Alt = 1001 ; B_Alt = 1002 ; C_Alt = 1003
  161. UP_Alt = 1004 ; DOWN_Alt = 1005 ; LEFT_Alt = 1006 ; RIGHT_Alt = 1007
  162. Joy_1 = 1008 ; Joy_2 = 1009 ; Joy_3 = 1010 ; Joy_4 = 1011
  163. Joy_5 = 1012 ; Joy_6 = 1013 ; Joy_7 = 1014 ; Joy_8 = 1015
  164. Joy_Down_L= 1016 ; Joy_Down = 1017 ; Joy_Down_R= 1018 ; Joy_Left = 1019
  165. Joy_Right = 1020 ; Joy_Up_L = 1021 ; Joy_Up = 1022 ; Joy_Up_R = 1023
  166.  
  167.  
  168. #--------------------------------------------------------------------------
  169. # Load Constants
  170. #--------------------------------------------------------------------------
  171. #
  172. # Master List of Keyboard Keys --------------------------------------------
  173. #
  174. K_LIST = [
  175. # General Uppercase Keys ----------------------------------
  176. Key_A, Key_B, Key_C, Key_D, Key_E, Key_F, Key_G, Key_H,
  177. Key_I, Key_J, Key_K, Key_L, Key_M, Key_N, Key_O, Key_P,
  178. Key_Q, Key_R, Key_S, Key_T, Key_U, Key_V, Key_W, Key_X,
  179. Key_Y, Key_Z,
  180. # Top Row Numbers -----------------------------------------
  181. Key_0, Key_1, Key_2, Key_3, Key_4, Key_5, Key_6, Key_7,
  182. Key_8,Key_9,
  183. # Function Key Constants ----------------------------------
  184. Key_F3, Key_F4, Key_F5, Key_F6, Key_F7, Key_F8, Key_F9,
  185. Key_F10, Key_F11,
  186. # Keypad Buttons ------------------------------------------
  187. Pad_0, Pad_1, Pad_2, Pad_3, Pad_4,
  188. Pad_5, Pad_6, Pad_7, Pad_8, Pad_9,
  189. Pad_LEFT, Pad_UP, Pad_RIGHT, Pad_DOWN, Pad_CENTER,
  190. Pad_MULT, Pad_ADD, Pad_SUB, Pad_DIV, Pad_DEC,
  191. # Additional buttons --------------------------------------
  192. BK_SPACE, TAB, ENTER, PAUSE, CAPSLOCK,
  193. ESC, SPACE, PGUP, PGDN, LINE_END,
  194. HOME, PRSCRN, INSERT, DELETE, APPS,
  195. NUMLOCK, SCROLL, L_SHIFT, R_SHIFT, L_CTRL,
  196. R_CTRL, L_ALT, R_ALT, COLON, EQUAL,
  197. COMMA, UNDERLINE, PERIOD, BACKSLASH, TILDE,
  198. L_BRACKET, SLASH, R_BRACKET, QUOTE
  199. # ---------------------------------------------------------
  200. ]
  201. #
  202. # Secondary List for Replacement keys (double-click) ----------------------
  203. #
  204. R_LIST = []
  205. R_LIST[1] = [L_SHIFT, R_SHIFT, Key_A ]
  206. R_LIST[2] = [ Pad_0, Key_X, ESC ]
  207. R_LIST[3] = [ ENTER, Key_C, SPACE ]
  208. R_LIST[4] = [Pad_UP] ; R_LIST[5] = [Pad_DOWN]
  209. R_LIST[6] = [Pad_LEFT] ; R_LIST[7] = [Pad_RIGHT]
  210. #
  211. # Secondary List for Gamepad Controller keys (double-click) ---------------
  212. #
  213. G_LIST = []
  214. G_LIST[1] = [1] ; G_LIST[2] = [2] ; G_LIST[3] = [4] ; G_LIST[4] = [8]
  215. G_LIST[5] = [16] ; G_LIST[6] = [32] ; G_LIST[7] = [64] ; G_LIST[8] = [128]
  216. G_LIST[9] = [[0 ,65535]] ; G_LIST[10] = [[32767,65535]]
  217. G_LIST[11] = [[65535,65535]] ; G_LIST[12] = [[0 ,32767]]
  218. G_LIST[13] = [[65535,32767]] ; G_LIST[14] = [[0 , 0]]
  219. G_LIST[15] = [[32767, 0]] ; G_LIST[16] = [[65535, 0]]
  220.  
  221. #--------------------------------------------------------------------------
  222. # * End of Definitions
  223. #--------------------------------------------------------------------------
  224. end
  225.  
  226.  
  227. #==============================================================================
  228. # ** Input
  229. #------------------------------------------------------------------------------
  230. # A class that handles input data from a gamepad or keyboard.
  231. #==============================================================================
  232.  
  233. class << Input
  234. #--------------------------------------------------------------------------
  235. # * Defined Win32API Constants
  236. #--------------------------------------------------------------------------
  237. Input_Trigger = Win32API.new('user32', 'GetAsyncKeyState', ['i'], 'i')
  238. Input_Pressed = Win32API.new('user32', 'GetKeyState', ['i'], 'i')
  239. Input_Test_State = Win32API.new('user32', 'GetKeyState', 'L', 'L')
  240. GamePad_Quantity = Win32API.new( 'winmm', 'joyGetNumDevs', '' , 'L')
  241. GamePad_Access = Win32API.new( 'winmm', 'joyGetPos', 'LP', 'L')
  242. GamePad_State = Win32API.new( 'winmm', 'joyGetPosEx', 'LP', 'L')
  243. #--------------------------------------------------------------------------
  244. # * Game Controller Arrays
  245. #--------------------------------------------------------------------------
  246. @@gamepad_list = []
  247. @@gamepad_buffer = [52, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0].pack('L13')
  248. #--------------------------------------------------------------------------
  249. # * Initialize Gamepad
  250. #--------------------------------------------------------------------------
  251. def Input.initialize_gamepad
  252. GamePad_Quantity.call.times do |i|
  253. @@gamepad_list << i if GamePad_Access.call(i, ' ' * 16) == 0
  254. end
  255. end
  256. #--------------------------------------------------------------------------
  257. # * Input.gamepad_state
  258. #--------------------------------------------------------------------------
  259. def Input.gamepad_state
  260. return nil if @@gamepad_list == []
  261. GamePad_State.call(@@gamepad_list.first, @@gamepad_buffer)
  262. result = @@gamepad_buffer.unpack('L13')
  263. return result[2...result.size-2]
  264. end
  265. #--------------------------------------------------------------------------
  266. # * Input.test_trigger
  267. # num : virtually any defined Input_Trigger
  268. #--------------------------------------------------------------------------
  269. def Input.test_trigger(num)
  270. Input_Trigger.call(num) & 0x01 == 1
  271. end
  272. #--------------------------------------------------------------------------
  273. # * Input.test_pressed
  274. # num : virtually any defined Input_Trigger
  275. #--------------------------------------------------------------------------
  276. def Input.test_pressed(num)
  277. return true unless Input_Pressed.call(num).between?(0, 1)
  278. return false
  279. end
  280. #---------------------------------------------------------------------------
  281. # * Test if Enabled?
  282. # num : virtually any defined Input_Pressed
  283. #---------------------------------------------------------------------------
  284. def Input.enabled?(num)
  285. return Input_Pressed.call(num) & 1 == 1
  286. end
  287. #--------------------------------------------------------------------------
  288. # * Input.test_gamepad
  289. # num : values defined for the gamepad controller
  290. #--------------------------------------------------------------------------
  291. def Input_test_gamepad(num)
  292. com = Proc.new {|i| ((i & @@prev_state[6]) != i and (i & @@buttons) == i) }
  293. if num.is_a?(Array)
  294. return (@@prev_state[0..1] != num[0..1] and @@direction == num[0..1])
  295. else
  296. return ( com.call(num) )
  297. end
  298. end
  299. #--------------------------------------------------------------------------
  300. # * Input.reset
  301. #--------------------------------------------------------------------------
  302. def Input.reset
  303. @trigger, @pressed, @released, @alt, @prev_press = [], [], [], [], []
  304. end
  305. #--------------------------------------------------------------------------
  306. # * Input.update
  307. #--------------------------------------------------------------------------
  308. alias rekey_update update unless $@
  309. def Input.update
  310. # Create/Empty the test arrays
  311. @trigger, @pressed, @released, @alt = [], [], [], []
  312. # Only reset the prev keys if nil
  313. @prev_press = [] if @prev_press.nil?
  314. # Game Controller State
  315. unless @@gamepad_list == []
  316. @@prev_state = @@state
  317. @@state = Input.gamepad_state
  318. @@direction = @@state[0..1]
  319. @@buttons = @@state[6]
  320. end
  321. # Create alternate key test flags
  322. for i in 1..23 ; @alt[i] = false ; end
  323. # Sort through all 7 Alternate Master Keys
  324. 7.times{|i| for num in Input::R_LIST[i+1]
  325. if Input.test_trigger(num)
  326. @alt[i+1] = true
  327. @trigger.push(num)
  328. end ; end }
  329. # Sort through all 16 Game Controller buttons and dir arrow keys
  330. unless @@gamepad_list == []
  331. 16.times{|i| for num in Input::G_LIST[i+1]
  332. @alt[i+8] = true if Input_test_gamepad(num)
  333. end }
  334. end
  335. # Last values not part of pre-defined list
  336. for num in Input::K_LIST
  337. @trigger.push(num) if Input.test_trigger(num)
  338. @pressed.push(num) if Input.test_pressed(num)
  339. @released.push(num) if !Input.test_pressed(num) && @prev_press.include?(num)
  340. end
  341. # Reload previously pressed keys for release test
  342. @prev_press = @pressed if @pressed != []
  343. # Counter for repeat feature
  344. @repeat_counter = 1 if @repeat_counter == nil
  345. @repeat_counter -= 1
  346. if @repeat_counter < 0
  347. @repeat_counter = 0
  348. @repeat_num = nil
  349. end
  350. # Counter for double feature
  351. @double_counter = 1 if @double_counter == nil
  352. @double_counter -= 1
  353. if @double_counter < 0
  354. @double_counter = 0
  355. @double_num = nil
  356. end
  357. # Perform the original call
  358. rekey_update
  359. end
  360. #--------------------------------------------------------------------------
  361. # * Input.key_trigger?
  362. # num : virtually any defined Input_Trigger
  363. #--------------------------------------------------------------------------
  364. def Input.key_trigger?(num)
  365. return true if [email protected]? && @trigger.include?(num)
  366. return false
  367. end
  368. #--------------------------------------------------------------------------
  369. # * Input.key_press?
  370. # num : virtually any defined Input_Trigger
  371. #--------------------------------------------------------------------------
  372. def Input.key_press?(num)
  373. return true if rekey_press?(num)
  374. return false
  375. end
  376. #--------------------------------------------------------------------------
  377. # * Input.key_repeat?
  378. # num : virtually any defined Input_Trigger
  379. #--------------------------------------------------------------------------
  380. def Input.key_repeat?(num)
  381. return true if [email protected]? && @trigger.include?(num) && Input.is_repeated?(num)
  382. return false
  383. end
  384. #--------------------------------------------------------------------------
  385. # * Input.key_double?
  386. # num : virtually any defined Input_Trigger
  387. #--------------------------------------------------------------------------
  388. def Input.key_double?(num)
  389. # Test replacement keys for doubleclick
  390. for i in 1..23
  391. return true if [email protected]? && @alt[i] && num == 1000+i && Input.is_dblclicked?(1000+i)
  392. end
  393. # Test keyboard for doubleclick
  394. return true if [email protected]? && @trigger.include?(num) && Input.is_dblclicked?(num)
  395. return false
  396. end
  397. #--------------------------------------------------------------------------
  398. # * Input.trigger?
  399. # num : virtually any defined Input_Trigger
  400. #--------------------------------------------------------------------------
  401. alias rekey_trigger? trigger? unless $@
  402. def Input.trigger?(num)
  403. return true if rekey_trigger?(num)
  404. return true if [email protected]? && @trigger.include?(num)
  405. return false
  406. end
  407. #--------------------------------------------------------------------------
  408. # * Input.release?
  409. # num : virtually any defined Input_Trigger
  410. #--------------------------------------------------------------------------
  411. def Input.release?(num)
  412. return false if num == nil
  413. return false if @released.nil?
  414. return true if @released.include?(num)
  415. return false
  416. end
  417. #--------------------------------------------------------------------------
  418. # * Input.press?
  419. # num : virtually any defined Input_Trigger
  420. #--------------------------------------------------------------------------
  421. alias rekey_press? press? unless $@
  422. def Input.press?(num)
  423. return true if [email protected]? && @pressed.include?(num)
  424. return true if rekey_press?(num)
  425. return false
  426. end
  427. #--------------------------------------------------------------------------
  428. # * Input.repeat?
  429. # num : virtually any defined Input_Trigger
  430. #--------------------------------------------------------------------------
  431. alias rekey_repeat? repeat? unless $@
  432. def Input.repeat?(num)
  433. return true if rekey_repeat?(num)
  434. return true if [email protected]? && @trigger.include?(num) && !Input.is_repeated?(num)
  435. return false
  436. end
  437. #--------------------------------------------------------------------------
  438. # * Input.is_repeated?
  439. # num : virtually any defined Input_Trigger
  440. #--------------------------------------------------------------------------
  441. def Input.is_repeated?(num)
  442. return false if num == nil
  443. unless @repeat_num == num
  444. @repeat_counter = Input::REPEAT_CLICK
  445. @repeat_num = num
  446. return false
  447. end
  448. return true
  449. end
  450. #--------------------------------------------------------------------------
  451. # * Input.is_dblclicked?
  452. # num : virtually any defined Input_Trigger
  453. #--------------------------------------------------------------------------
  454. def Input.is_dblclicked?(num)
  455. return true if @double_num == num
  456. @double_num = num
  457. @double_counter = Input::DOUBLE_CLICK
  458. return false
  459. end
  460. #--------------------------------------------------------------------------
  461. # * Object Initialization
  462. #--------------------------------------------------------------------------
  463. Input.initialize_gamepad
  464. #--------------------------------------------------------------------------
  465. # * State (autonomic)
  466. #--------------------------------------------------------------------------
  467. @@state = @@prev_state = Input.gamepad_state
  468. end
Advertisement
Add Comment
Please, Sign In to add comment