mcgluszak

[Author: Aleworks] Aleworks' Input Module v2.02

Jul 31st, 2011
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 40.48 KB | None | 0 0
  1. #=============================================================================
  2. # *** Aleworks Input Module(AIM)
  3. #=============================================================================
  4. # Created by Aleworks
  5. # Version: 2.02
  6. # Last Modification: 13/09/2007 (day/month/year)
  7. #=============================================================================
  8. #==== Description ====
  9. # This script simulates the RGSS module "Input". It is not the original module,
  10. # but it has all the original module functions and some new.
  11. #=============================================================================
  12. #==== Requeriments ====
  13. # * Aleworks Library
  14. #   - Version: 1.01 or superior
  15. #=============================================================================
  16. #==== Version History ====
  17. # * Version 1.00
  18. #   - New methods: Input.reiterate?; Input.activated?; Input.press_all;
  19. #     Input.trigger_all; Input.repeat_all; Input.reiterate_all
  20. #   - New Keys: LOWER_LEFT; LOWER_RIGHT; UPPER_LEFT; UPPER_RIGHT
  21. # * Version 1.10
  22. #   - New methods: Input.event; Input.free_key; Input.free_all_keys
  23. # * Version 1.11
  24. #   - Changed the variables: @repeat_time, @reiterate_time, @reiterate and
  25. #     @update_keys to global variables for easy editing.
  26. # * Version 1.12(Now in Aleworks Input Module Compability Patch 1)
  27. #   - Compability with the event commands: Asing Key and Condition Branch
  28. # * Version 1.20
  29. #   - Little lag reduction
  30. #   - New methods: Input.release?; Input.release_all
  31. # * Version 1.21
  32. #   - Major bug fixed, when using arrays in Input.press?, Input.trigger?,
  33. #     Input.reiterate? and Input.release?
  34. #   - Removed $update_keys and $reiterate
  35. # * Version 2.00
  36. #   - Little lag reduction
  37. #   - Rewrited some code
  38. #   - No more need of Win32::Registry, but need Aleworks Library
  39. #   - Key Combos
  40. #     - New Class: Input::Key_Combo
  41. #     - New Methods: Input.add_combo; Input.combo; Input.combos; Input.combos;
  42. #       Input.delete_combo; Input.delete_ended_combos
  43. #   - Mouse Input
  44. #     - New Methods: Input.mouse_double_click?; Input.mouse_x?; Input.mouse_y?;
  45. #       Input.mouse_dragging?; Input.mouse_drag_rect?; Input.mouse_drag_coor?
  46. #     - New Constants: MOUSE_INPUT; MOUSE_DOUBLE_CLICK_TIME;
  47. #       MOUSE_DOUBLE_CLICK_PIX; MOUSE_DRAG_PIX; MOUSE_PRIMARY; MOUSE_SECONDARY
  48. # * Version 2.01
  49. #   - Input.mouse_x? and Input.mouse_y? will now return nil if the mouse poiter
  50. #     is out of the game screen.
  51. # * Version 2.02
  52. #   - Fixed error in Input.press?
  53. #=============================================================================
  54. #==== Indications ====
  55. # * Constants
  56. #   - REPEAT_TIME
  57. #     Frames for repeat in Input.repeat?. By default 7.
  58. #   - REITERATE_TIME
  59. #     Frames for reiterate in Input.reiterate?. By default 20.
  60. #   - MOUSE_INPUT
  61. #     Flag for mouse input updating. By default true.
  62. #   - MOUSE_DOUBLE_CLICK_TIME = nil
  63. #     The mouse double click time, if nil, will be used the windows default time.
  64. #   - MOUSE_DOUBLE_CLICK_PIX = 4
  65. #     Max pixels count the mouse can move around the first click point for double
  66. #     clicking. By default 4.
  67. #   - MOUSE_DRAG_PIX
  68. #     Pixels needed for the cursor to move around the first point to start
  69. #     dragging. By default 4.
  70. #   - REG_KVALUES
  71. #     Contains values for reading the A, B, C, X, Y, Z, L and R keys from windows
  72. #     registry.
  73. #   - DOWN; LEFT; RIGHT; UP; LOWER_LEFT; LOWER_RIGHT; UPPER_LEFT; UPPER_RIGHT
  74. #     Numbers corresponding to the directions.
  75. #   - A; B; C; X; Y; Z; L; R
  76. #     Numbers corresponding to the various controller buttons.
  77. #   - SHIFT; CTRL; ALT
  78. #     Numbers directly corresponding to the keyboard's SHIFT, CTRL, and ALT keys.
  79. #   - F5; F6; F7; F8; F9
  80. #     Numbers corresponding to the keyboard's function keys.
  81. #   - MOUSE_PRIMARY
  82. #     Number corresponding to the mouse's primary button.
  83. #   - MOUSE_SECONDARY
  84. #     Number corresponding to the mouse's secondary button.
  85. # * Methods
  86. #   - Input.update
  87. #     Updates input data. This method need to be called once per frame.
  88. #   - Input.press?(vk)
  89. #     Determines whether the key vk is currently being pressed. If the key is
  90. #     being pressed, returns true. If not, returns false.
  91. #   - Input.press_all([state])
  92. #     Returns all keys that have its press value same as state. If state is not
  93. #     given, it will be true.
  94. #   - Input.trigger?(vk)
  95. #     Determines whether the key vk is being pressed again. "Pressed again" is
  96. #     seen as time having passed between the button being not pressed and being
  97. #     pressed. If the button is being pressed, returns true. If not, returns
  98. #     false.
  99. #   - Input.trigger_all([state])
  100. #     Returns all keys that have its trigger value same as state. If state is not
  101. #     given, it will be true.
  102. #   - Input.repeat?(vk)
  103. #     Determines whether the key vk is being pressed and some time has passed
  104. #     again. "Being pressed and some time has passed again" is seen as time
  105. #     having passed again after the button was being pressed. If the button is
  106. #     being pressed and some time has passed again, returns true. If not, returns
  107. #     false. The time being passed again, is determined by the REPEAT_TIME
  108. #     constant.
  109. #   - Input.repeat_all([state])
  110. #     Returns all keys that have its repeat value same as state. If state is not
  111. #     given, it will be true.
  112. #   - Input.reiterate?(num)
  113. #     Determines whether the key vk is being pressed again in the given frames.
  114. #     "Being pressed again in the given frames" is seen as time having passed
  115. #     between the button is pressed and being again pressed. If the button is
  116. #     being pressed again in the given frames, returns true. If not, returns
  117. #     false. The given frames is determined by REITERATE_TIME constant.
  118. #   - Input.reiterate_all([state])
  119. #     Returns all keys that have its reiterate value same as state. If state is
  120. #     not given, it will be true.
  121. #   - Input.release?(vk)
  122. #     Determines whether the key vk is being not pressed. "Not pressed" is seen
  123. #     as time having passed between the button is pressed and being not pressed.
  124. #     If the button is being not pressed, returns true. If not, returns false.
  125. #   - Input.release_all([state])
  126. #     Returns all keys that have its release value same as state. If state is not
  127. #     given, it will be true.
  128. #   - Input.dir4
  129. #     Checks the status of the directional buttons, translates the data into a
  130. #     specialized 4-direction input format, and returns the number pad equivalent
  131. #     (2, 4, 6, 8). If no directional buttons are being pressed (or the
  132. #     equivalent), returns 0.
  133. #   - Input.dir8
  134. #     Checks the status of the directional buttons, translates the data into a
  135. #     specialized 8-direction input format, and returns the number pad equivalent
  136. #     (1, 2, 3, 4, 6, 7, 8, 9). If no directional buttons are being pressed (or
  137. #     the equivalent), returns 0.
  138. #   - Input.activated?(vk)
  139. #     Returns true if the key vk is activated. Applicable for mode keys, like
  140. #     num, caps or scroll lock.
  141. #   - Input.key_click(vk)
  142. #     Makes key vk to be pressed, and instantaly after released.
  143. #   - Input.key_release(vk)
  144. #     Makes key vk to be released.
  145. #   - Input.key_press(vk)
  146. #     Makes key vk to be pressed.
  147. #   - Input.mouse_double_click?
  148. #     Return true, if the primary button of the mouse has been twice clicked,
  149. #     false instad.
  150. #   - Input.mouse_x?
  151. #     Returns X pixel coordinates of the mouse pointer. If the cursor is out of
  152. #     the game screen, returns nil.
  153. #   - Input.mouse_y?
  154. #     Returns Y pixel coordinates of the mouse pointer. If the cursor is out of
  155. #     the game screen, returns nil.
  156. #   - Input.mouse_dragging?
  157. #     Returns true if the user is dragging with mouse, false instad.
  158. #   - Input.mouse_drag_rect?
  159. #     Returns a Rect with the dragged rectangle coordinates and size, if no
  160. #     dragging is being maked returns nil.
  161. #   - Input.mouse_drag_coor?
  162. #     Returns an Array with the dragged coordinates, the firts two values are the
  163. #     x and y coordinates of the initial point of draging, the last 2 values are
  164. #     the x and y coordinates of the actual point, if no dragging is being maked
  165. #     returns nil.
  166. #   - Input.add_combo(params, restart_count, res_type[, update[, id]])
  167. #     Add a Key Combo with the given params, restart count, restart
  168. #     type(res_type) and update; with the given id as identifier. If the id is
  169. #     not given it will be auto-selected and returned by this function. If
  170. #     update is not given it will be not given to the Key Combo. See Key Combo
  171. #     for more reference.
  172. #   - Input.combos
  173. #     Returns a hash with all combos, with the keys as theirs identifiers.
  174. #   - Input.combos=(new)
  175. #     Overwrites combos list with new.
  176. #   - Input.delete_combo(id)
  177. #     Deletes the combo that have the given id.
  178. #   - Input.delete_ended_combos(ending = nil)
  179. #     Deletes all ended combos that have the end value equal to "ending". If
  180. #     "ending" is nil both true ended and false ended will be deleted.
  181. #=============================================================================
  182. #==== Key Combo ====
  183. # * Methods
  184. #   - update
  185. #     Updates the timers and the keys combination. Input.update, autoupdate
  186. #     all the combos created in Input.
  187. #   - true?
  188. #     Returns the ending value of the combo. Nil if the combo has not ended.
  189. #   - end?
  190. #     Returns true if the combo has ended, false instead.
  191. #   - frozen?
  192. #     Returns true if the combo is frozen, false instead.
  193. #   - restarts_count?
  194. #     Returns the total restart count that the combo has maked.
  195. #   - start_frame?
  196. #     Returns the the frame, based on Graphics.frame_count, which is assigned
  197. #     when the combo is created or restarted.
  198. #   - end_frame?
  199. #     Returns the the frame, based on Graphics.frame_count, which is assigned
  200. #     when the combo is ended.
  201. #   - freeze
  202. #     Freezes the combo. Frozen combos will be not updated.
  203. #   - unfreeze
  204. #     Unfreezes the combo.
  205. #   - restart
  206. #     Restarts all the combo, like when it had been created.
  207. # * Accessible Variables
  208. #   - params
  209. #     See params in Input Values.
  210. #   - restart_count
  211. #     See restart count in Input Values.
  212. #   - restart_type
  213. #     See restart type count in Input Values.
  214. #   - update
  215. #     See update count in Input Values.
  216. # * Input Values
  217. #   - params
  218. #     This is an Array containing all the checks, the time they have to be
  219. #     checked, and the wait time between each check.
  220. #     A check is a comprobation for if its keys combination is true. When a
  221. #     check is false, the combo will restart depending of the restart count and
  222. #     the restart type.
  223. #       - Numerical values in the main array, are frames to wait before or
  224. #         between the checks. They don't need to be between each check, and can
  225. #         be more than one consecutively. They are countdown timers, and will
  226. #         continue with the next main array value when they reaches 0.
  227. #       - Array values, are the checks.
  228. #         [A, B[, C[, D]]]
  229. #         - A, the first value, is a countdown timer for the keys combination to
  230. #           be checked. When it reaches 0 the check will be false. If it is an
  231. #           Integer, the timer will be reduced, only if the key combination(B) is
  232. #           false. If it is a String convertible to an integer, the timer will be
  233. #           reduced ever.
  234. #         - B, the second value is an array containing the keys combination to
  235. #           be checked(For more info see "Array Input")
  236. #         - C, the third value is a countdown timer, that when it is reaches 0
  237. #           after the first check to key combination(B) results true, the check
  238. #           will be true. If it is an Integer or a String convertible to an
  239. #           integer, C is the exact time needed for the keys combination to be
  240. #           true. If it is a Range, or a Range writed like a String, or an Array
  241. #           containing Integers or Strings convertible to integers, the first
  242. #           value will be the minimum time that the keys combination need to be
  243. #           true; and the second value, will be the limit, so, when the pressed
  244. #           time is more than the limit, this check will be true. When the value/
  245. #           values is/are not strings, if the key combination stop being true
  246. #           after its first press, the check will be false, but if the values are
  247. #           strings the check will be only false when A finish. When C is not
  248. #           given, it will be 0.
  249. #         - D, the last value is the times the check need to be true, for
  250. #           continuing with the combo. If D is -1, the check will be restarted
  251. #           infinitely. If D is not given it will be 0.
  252. #   - restart count
  253. #     -1 => infinite times
  254. #     0  => 1 time
  255. #     >0 => times + 1
  256. #   - restart type
  257. #     0  => start to check all the combo again when restarted
  258. #     1  => start to check from the last no true check when restarted
  259. #   - update
  260. #     Array => The Key Combo will only be updated when the actual Scene(stored
  261. #              in $scene) is included in the Array.
  262. #     Hash  => The Key Combo will be updated when the actual Scene(stored in
  263. #              $scene) is not included in the keys of the Hash, or if it is
  264. #              included and the evaluation of its value returns true.
  265. #=============================================================================
  266. #==== Array Input ====
  267. # This feature allows you to use Arrays for some special virtual keys checking
  268. # in the commands Input.press?, Input.trigger?, Input.reiterate?, Input.release?,
  269. # Input.key_click, Input.key_release, Input.key_press and for the params value
  270. # in the Key Combos.
  271. # * Syntax
  272. #   This Array, can have numerical values and other Arrays, as values. The
  273. #   subarrays can have numerical values. The numerical values are the virtual
  274. #   keys to be checked.
  275. #   - Examples:
  276. #     - [Keys::A, Keys::B, [Keys::C, Keys::D]]
  277. #     - [65, 66, [67, 68]]
  278. # * Return Value
  279. #   The return value will be true when at least one of virtual keys in the array
  280. #   are true for the corresponding method.
  281. #   The Sub-Arrays will have true value, when all of its values are true for the
  282. #   corresponding method.
  283. #   - Examples:
  284. #     - Input.press?( [Keys::A] )
  285. #       Will return true if the key A is pressed.
  286. #     - Input.press?( [Keys::A, Keys::B, Keys::C] )
  287. #       Will return true if one or more of the A, B and C keys are pressed.
  288. #     - Input.press?( [ [Keys::A, Keys::B] ] )
  289. #       Will return true if both A and B keys are pressed.
  290. #     - Input.press?( [ [Keys::A, Keys::B, Keys::C] ] )
  291. #       Will return true if the A, B and C keys are pressed.
  292. #     - Input.press?( [ [Keys::A, Keys::B, Keys::C], Keys::D] )
  293. #       Will return true if the A, B and C keys are pressed or
  294. #       the key D is pressed.
  295. #     - Input.press?( [ [Keys::A, Keys::B, Keys::C], [Keys::D, Keys::E]] )
  296. #       Will return true if the A, B and C keys are pressed or
  297. #       the keys D and E are pressed.
  298. #=============================================================================
  299.  
  300. #=============================================================================
  301. # ** Module Input
  302. #=============================================================================
  303. module Input
  304.   #---------------------------------------------------------------------------
  305.   # * Expand Input module with Aleworks Library
  306.   #---------------------------------------------------------------------------
  307.   include Aleworks
  308.   #---------------------------------------------------------------------------
  309.   # * Options
  310.   #---------------------------------------------------------------------------
  311.   START_REPEAT_TIME = 10
  312.   REPEAT_TIME = 2
  313.   REITERATE_TIME = 20
  314.   MOUSE_INPUT = true
  315.   MOUSE_DOUBLE_CLICK_TIME = nil
  316.   MOUSE_DOUBLE_CLICK_PIX = 4
  317.   MOUSE_DRAG_PIX = 4
  318.   #---------------------------------------------------------------------------
  319.   # * Internal variables definition
  320.   #---------------------------------------------------------------------------
  321.   @trigger, @trigger2, @repeat, @repeat2, @repeat3, @reiterate, @reiterate2,
  322.   @reiterate3, @release, @release2 = ([[]] * 10).clone_all
  323.   256.times do |i|
  324.     @trigger.push(false)
  325.     @trigger2.push(false)
  326.     @repeat.push(false)
  327.     @repeat2.push(0)
  328.     @reiterate.push(false)
  329.     @reiterate2.push(0)
  330.     @reiterate3.push(API::GetKeyState.call(i) & 1)
  331.     @release.push(false)
  332.     @release2.push(false)
  333.   end
  334.   @combos = {}
  335.   @mouse_pos = nil
  336.   @mouse_swap_buttons = Aleworks.get_mouse_swap_buttons_flag
  337.   @mouse_primary_dc = [0, 0, [nil, nil]]
  338.   @mouse_rectdrag = nil
  339.   @mouse_draw = []
  340.   if MOUSE_DOUBLE_CLICK_TIME.nil?
  341.     MOUSE_DOUBLE_CLICK_TIME = Graphics.frame_rate
  342.     MOUSE_DOUBLE_CLICK_TIME *= (API::GetDoubleClickTime.call / 10)
  343.     MOUSE_DOUBLE_CLICK_TIME /= 100
  344.   end
  345.   #---------------------------------------------------------------------------
  346.   # * convert_keys (internal method)
  347.   #---------------------------------------------------------------------------
  348.   # It reads the windows registry and returns the corresponding keys to the
  349.   # F1 keys menu.
  350.   #---------------------------------------------------------------------------
  351.   REG_KVALUES = {0=>32,1=>13,2=>27,3=>96,4=>16,5=>90,6=>88,7=>67,8=>86,9=>66,
  352.                  10=>65,11=>83,12=>68,13=>81,14=>87}
  353.   def convert_keys(key)
  354.     keys = []
  355.     reg_key = 'HKEY_CURRENT_USER\\Software\\Enterbrain\\RGSS'
  356.     data = Registry.read_entry(reg_key, 'ButtonAssign')[10, 25].scan(/./)
  357.     15.times {|i| keys.push(REG_KVALUES[i]) if key == data[i].unpack('C')[0]}
  358.     keys
  359.   end  
  360.   module_function :convert_keys
  361.   #---------------------------------------------------------------------------
  362.   # * Keys Constants definitions
  363.   #---------------------------------------------------------------------------
  364.   LOWER_LEFT = 97
  365.   LOWER_RIGHT = 99
  366.   UPPER_LEFT = 103
  367.   UPPER_RIGHT = 105
  368.   DOWN = [98, 40]
  369.   LEFT = [100, 37]
  370.   RIGHT = [102, 39]
  371.   UP = [104, 38]
  372.   A = convert_keys(11)
  373.   B = convert_keys(12).concat([45])
  374.   C = convert_keys(13)
  375.   X = convert_keys(14)
  376.   Y = convert_keys(15)
  377.   Z = convert_keys(16)
  378.   L = convert_keys(17).concat([33])
  379.   R = convert_keys(18).concat([34])
  380.   SHIFT = 16
  381.   CTRL = 17
  382.   ALT = 18
  383.   F5 = 116
  384.   F6 = 117
  385.   F7 = 118
  386.   F8 = 119
  387.   F9 = 120
  388.   MOUSE_PRIMARY = 1 + @mouse_swap_buttons
  389.   MOUSE_SECONDARY = 2 - @mouse_swap_buttons
  390.   #===========================================================================
  391.   # ** Class Key_Combo
  392.   #===========================================================================
  393.   class Key_Combo
  394.     attr_accessor(:params, :restart_count, :restart_type, :update)
  395.     #-------------------------------------------------------------------------
  396.     # * Initialize
  397.     #-------------------------------------------------------------------------
  398.     def initialize(params, restart_count, restart_type, update)
  399.       params.each_index do |i|
  400.         if params[i].is_a?(Array)
  401.           params[i][2] = [0, 0] if params[i].size == 2
  402.           params[i][3] = 0 if params[i].size == 3
  403.           params[i][1] = params[i][1].to_a
  404.           if params[i][2].is_a?(Numeric)
  405.             params[i][2] = [params[i][2], 0]
  406.           elsif params[i][2].is_a?(Range)
  407.             if params[i][2].exclude_end?
  408.               params[i][2] = [params[i][2].first, params[i][2].last - 1]
  409.             else
  410.               params[i][2] = [params[i][2].first, params[i][2].last]
  411.             end
  412.           elsif params[i][2].is_a?(String)
  413.             if !params[i][2][/(.*)\.\.\.(.*)/].nil?
  414.               params[i][2] = [$1, ($2.to_i - 1).to_s]
  415.             elsif !params[i][2][/(.*)\.\.(.*)/].nil?
  416.               params[i][2] = [$1, $2]
  417.             else
  418.               params[i][2] = [params[i][2], 0]
  419.             end
  420.           elsif params[i][2].is_a?(Array)
  421.             if params[i][2][0].is_a?(String)
  422.               params[i][2][1] = params[i][2][1].to_s
  423.             end
  424.             if params[i][2][1].is_a?(String)
  425.               params[i][2][0] = params[i][2][0].to_s
  426.             end
  427.           end
  428.         end
  429.       end
  430.       if @update.is_a?(Array)
  431.         @update.each_index do |i|
  432.           @update[i] = @update[i].type.to_s
  433.         end
  434.       elsif @update.is_a?(Hash)
  435.         @update = !@update
  436.         @update.each do |k, v|
  437.           @update[k] = @update[k].type.to_s
  438.         end
  439.         @update = !@update
  440.       end
  441.       @params = params
  442.       @state = @params.clone_all
  443.       @restart_count = restart_count
  444.       @_restart_count = restart_count
  445.       @restart_type = restart_type
  446.       @update = update
  447.       @last_state = @state.clone_all if @restart_type == 1
  448.       @frame_start = Graphics.frame_count
  449.       @frame_end = nil
  450.       @_true = nil
  451.       @_end = false
  452.       @start_check = false
  453.       @end_check = false
  454.       @restarts = 0
  455.       @frozen = false
  456.     end
  457.     #-------------------------------------------------------------------------
  458.     # * Update
  459.     #-------------------------------------------------------------------------
  460.     def update
  461.       return if @frozen or @_end or @params.size == 0
  462.       if @update.is_a?(Array)
  463.         return if !@update.include?($scene.type.to_s)
  464.       elsif @update.is_a?(Hash)
  465.         if @update.has_key?($scene.type.to_s)
  466.           return if eval(@update[$scene.type.to_s].to_s)
  467.         end
  468.       end
  469.       if @state[0].is_a?(Numeric)
  470.         @state[0] -= 1
  471.         check_true if @state[0] <= 0
  472.       elsif @state[0].is_a?(Array)
  473.         if Input.press?(@state[0][1])
  474.           input_checked = true
  475.           if @state[0][2][0].is_a?(Numeric)
  476.             @start_check = true
  477.             @state[0][2][0] -= 1 unless @state[0][2][0] <= 0
  478.             @state[0][2][1] -= 1 unless @state[0][2][1] <= 0
  479.             if @state[0][2][0] <= 0 and @state[0][2][1] <= 0
  480.               check_true
  481.               return
  482.             end
  483.           elsif @state[0][2][0].is_a?(String)
  484.             unless @state[0][2][0].to_i <= 0
  485.               @state[0][2][0] = (@state[0][2][0].to_i - 1).to_s
  486.             end
  487.             unless @state[0][2][1].to_i <= 0
  488.               @state[0][2][1] = (@state[0][2][1].to_i - 1).to_s
  489.             end
  490.             if @state[0][2][0].to_i <= 0 and @state[0][2][1].to_i <= 0
  491.               check_true
  492.               return
  493.             end
  494.           end
  495.           if @state[0][0].is_a?(String)
  496.             if @state[0][0].to_i >= 0
  497.               @state[0][0] = (@state[0][0].to_i - 1).to_s
  498.               is_false if @state[0][0].to_i == 0
  499.             end
  500.           end
  501.         else
  502.           if @state[0][2][0].to_i <= 0
  503.             check_true
  504.             return
  505.           elsif @start_check
  506.             is_false
  507.             return
  508.           end
  509.           if @state[0][0].is_a?(String)
  510.             if @state[0][0].to_i >= 0
  511.               @state[0][0] = (@state[0][0].to_i - 1).to_s
  512.               is_false if @state[0][0].to_i == 0
  513.             end
  514.           elsif @state[0][0].is_a?(Numeric)
  515.             if @state[0][0] >= 0
  516.               @state[0][0] -= 1 unless @state[0][0] <= 0
  517.               is_false if @state[0][0] == 0
  518.             end
  519.           end
  520.         end
  521.       end
  522.     end
  523.     #-------------------------------------------------------------------------
  524.     def true?
  525.       @_true
  526.     end
  527.     #-------------------------------------------------------------------------
  528.     def end?
  529.       @_end
  530.     end
  531.     #-------------------------------------------------------------------------
  532.     def frozen?
  533.       @frozen
  534.     end
  535.     #-------------------------------------------------------------------------
  536.     def restarts_count?
  537.       @restarts
  538.     end
  539.     #-------------------------------------------------------------------------
  540.     def start_frame?
  541.       @frame_start
  542.     end
  543.     #-------------------------------------------------------------------------
  544.     def end_frame?
  545.       @frame_end
  546.     end
  547.     #-------------------------------------------------------------------------
  548.     def freeze
  549.       @frozen = true
  550.     end
  551.     #-------------------------------------------------------------------------
  552.     def unfreeze
  553.       @frozen = false
  554.     end
  555.     #-------------------------------------------------------------------------
  556.     def restart
  557.       @state = @params.clone_all
  558.       @last_state = @state.clone_all if @restart_type == 1
  559.       @restart_count = @_restart
  560.       @frame_start = Graphics.frame_count
  561.       @frame_end = nil
  562.       @_true = nil
  563.       @_end = false
  564.       @start_check = false
  565.       @restarts = 0
  566.     end
  567.     #-------------------------------------------------------------------------
  568.     def is_false
  569.       if @restart_count <= -1
  570.         if @restart_type == 1
  571.           @state = @last_state.clone_all
  572.         else
  573.           @state = @params.clone_all
  574.         end
  575.         @restarts += 1
  576.       elsif @restart_count == 0
  577.         @_end = true
  578.         @frame_end = Graphics.frame_count
  579.       else
  580.         if @restart_type == 1
  581.           @state = @last_state.clone_all
  582.         else
  583.           @state = @params.clone_all
  584.         end
  585.         @restart_count -= 1
  586.         @restarts += 1
  587.       end
  588.       @start_check = false
  589.     end
  590.     #-------------------------------------------------------------------------
  591.     def check_true
  592.       if @state[0][3] > 0
  593.         @state[0][3] -= 1
  594.       elsif @state[0][3] == 0
  595.         @state.slice!(0)
  596.         @last_state = @state.clone_all if @restart_type == 1
  597.         if @state.size == 0
  598.           @_true = true
  599.           @_end = true
  600.           @frame_end = Graphics.frame_count
  601.         end
  602.       end
  603.     end
  604.     private :is_false, :check_true
  605.   end
  606.   #---------------------------------------------------------------------------
  607.   # * Keys input state update
  608.   #---------------------------------------------------------------------------
  609.   def Input.update
  610.     state = API.get_keyboard_state.oscan
  611.     state.each_index do |i|
  612.       @repeat2[i] -= 1 if @repeat2[i] > 1
  613.       if state[i] == "\200" or state[i] == "\201"
  614.         if @trigger2[i]
  615.           @trigger[i] = false
  616.         else
  617.           @trigger[i] = true
  618.           @trigger2[i] = true
  619.         end
  620.         if @repeat2[i] <= 1
  621.           @repeat2[i] = @repeat2[i] == 1 ? REPEAT_TIME : START_REPEAT_TIME
  622.           @repeat2[i] += 1
  623.           @repeat[i] = true
  624.         else
  625.           @repeat[i] = false
  626.         end
  627.         @release2[i] = true
  628.       else
  629.         @trigger[i] = false
  630.         @trigger2[i] = false
  631.         @repeat[i] = false
  632.         @repeat2[i] = 0
  633.         @release[i] = @release2[i] ? true : false
  634.         @release2[i] = false
  635.       end
  636.       @reiterate2[i] -= 1 if @reiterate2[i] > 0
  637.       @reiterate[i] = false if @reiterate2[i] == 0
  638.       if (state[i] == "\001" or state[i] == "\201") and @reiterate3[i] == 0
  639.         @reiterate[i] = true
  640.         @reiterate2[i] = REITERATE_TIME
  641.         @reiterate3[i] = 1
  642.       elsif (state[i] == "\000" or state[i] == "\200") and @reiterate3[i] == 1
  643.         @reiterate[i] = true
  644.         @reiterate2[i] = REITERATE_TIME
  645.         @reiterate3[i] = 0
  646.       end
  647.     end
  648.     @combos.each_value {|c| c.update}
  649.     if MOUSE_INPUT
  650.       @mouse_pos = Aleworks.get_mouse_window_position
  651.       case @mouse_primary_dc[0]
  652.       when 0
  653.         if Input.trigger?(MOUSE_PRIMARY) and @mouse_pos != nil
  654.           @mouse_primary_dc = [1, MOUSE_DOUBLE_CLICK_TIME, @mouse_pos.clone]
  655.         end
  656.       when 1
  657.         if @mouse_pos != nil
  658.           if @mouse_primary_dc[1] > 0 and
  659.              @mouse_pos[0].between?(@mouse_primary_dc[2][0] -
  660.              MOUSE_DOUBLE_CLICK_PIX, @mouse_primary_dc[2][0] +
  661.              MOUSE_DOUBLE_CLICK_PIX) and
  662.              @mouse_pos[1].between?(@mouse_primary_dc[2][1] -
  663.              MOUSE_DOUBLE_CLICK_PIX, @mouse_primary_dc[2][1] +
  664.              MOUSE_DOUBLE_CLICK_PIX)
  665.             @mouse_primary_dc[1] -= 1
  666.             @mouse_primary_dc[0, 1] = 2, 0 if Input.trigger?(MOUSE_PRIMARY)
  667.           else
  668.             @mouse_primary_dc = [0, 0, [nil, nil]]
  669.           end
  670.         else
  671.           @mouse_primary_dc = [0, 0, [nil, nil]]
  672.         end
  673.       when 2
  674.         @mouse_primary_dc = [0, 0, [nil, nil]]
  675.       end
  676.       if Input.press?(MOUSE_PRIMARY)
  677.         if @mouse_pos != nil
  678.           if @mouse_rectdrag == nil
  679.             @mouse_rectdrag = @mouse_pos.clone if Input.trigger?(MOUSE_PRIMARY)
  680.           elsif @mouse_rectdrag.size == 2
  681.             if !@mouse_pos[0].between?(@mouse_rectdrag[0] - MOUSE_DRAG_PIX,
  682.                 @mouse_rectdrag[0] + MOUSE_DRAG_PIX) or
  683.                 !@mouse_pos[1].between?(@mouse_rectdrag[1] - MOUSE_DRAG_PIX,
  684.                 @mouse_rectdrag[1] + MOUSE_DRAG_PIX)
  685.               @mouse_rectdrag.push(*@mouse_pos)
  686.             end
  687.           else
  688.             @mouse_rectdrag[2, 3] = *@mouse_pos
  689.           end
  690.         else
  691.           @mouse_rectdrag = nil
  692.         end
  693.       else
  694.         @mouse_rectdrag = nil
  695.       end
  696.       @mouse_draw = []
  697.     end
  698.   end
  699.   #---------------------------------------------------------------------------
  700.   # * Press?
  701.   #---------------------------------------------------------------------------
  702.   def Input.press?(vk)
  703.     if vk.is_a?(Array)
  704.       vk.each do |i|
  705.         if i.is_a?(Array)
  706.           returntrue = true
  707.           i.each do |e|
  708.             if e.is_a?(Array)
  709.               returntrue2 = false
  710.               e.each do |o|
  711.                 if !API::GetAsyncKeyState.call(o).between?(0, 1)
  712.                   returntrue2 = true
  713.                 end
  714.               end
  715.               returntrue = false if !returntrue2
  716.             elsif !API::GetAsyncKeyState.call(e).between?(0, 1)
  717.               returntrue = false
  718.             end
  719.           end
  720.           return true if returntrue
  721.         else
  722.           return true if !API::GetAsyncKeyState.call(i).between?(0, 1)
  723.         end
  724.       end
  725.       false
  726.     else
  727.       !API::GetAsyncKeyState.call(vk).between?(0, 1)
  728.     end
  729.   end
  730.   #---------------------------------------------------------------------------
  731.   # * Press? all
  732.   #---------------------------------------------------------------------------
  733.   def Input.press_all(state = true)
  734.     pressed = API.get_keyboard_state
  735.     pressed = pressed.scan(/./)
  736.     if state
  737.       pressed.rindexes("\200", "\201")
  738.     else
  739.       pressed.rindexes("\000", "\001")
  740.     end
  741.   end
  742.   #---------------------------------------------------------------------------
  743.   # * Trigger?
  744.   #---------------------------------------------------------------------------
  745.   def Input.trigger?(vk)
  746.     if vk.is_a?(Array)
  747.       vk.each do |i|
  748.         if i.is_a?(Array)
  749.           returntrue = true
  750.           i.each do |e|
  751.             if e.is_a?(Array)
  752.               returntrue2 = false
  753.               e.each do |o| returntrue2 = true if @trigger[o] end
  754.               returntrue = false if !returntrue2
  755.             elsif @trigger[e]
  756.               returntrue = false
  757.             end
  758.           end
  759.           return true if returntrue
  760.         else
  761.           return true if @trigger[i]
  762.         end
  763.       end
  764.       false
  765.     else
  766.       @trigger[vk]
  767.     end
  768.   end
  769.   #---------------------------------------------------------------------------
  770.   # * Trigger? all
  771.   #---------------------------------------------------------------------------
  772.   def Input.trigger_all(state = true)
  773.     @trigger.rindexes(state)
  774.   end
  775.   #---------------------------------------------------------------------------
  776.   # * Repeat?
  777.   #---------------------------------------------------------------------------
  778.   def Input.repeat?(vk)
  779.     if vk.is_a?(Array)
  780.       vk.each do |i|
  781.         if i.is_a?(Array)
  782.           returntrue = true
  783.           i.each do |e|
  784.             if e.is_a?(Array)
  785.               returntrue2 = false
  786.               e.each do |o| returntrue2 = true if @repeat[o] end
  787.               returntrue = false if !returntrue2
  788.             elsif @repeat[e]
  789.               returntrue = false
  790.             end
  791.           end
  792.           return true if returntrue
  793.         else
  794.           return true if @repeat[i]
  795.         end
  796.       end
  797.       false
  798.     else
  799.       @repeat[vk]
  800.     end
  801.   end
  802.   #---------------------------------------------------------------------------
  803.   # * Repeat? all
  804.   #---------------------------------------------------------------------------
  805.   def Input.repeat_all(state = true)
  806.     @repeat.rindexes(state)
  807.   end
  808.   #---------------------------------------------------------------------------
  809.   # * Reiterate?
  810.   #---------------------------------------------------------------------------
  811.   def Input.reiterate?(num)
  812.     if vk.is_a?(Array)
  813.       vk.each do |i|
  814.         if i.is_a?(Array)
  815.           returntrue = true
  816.           i.each do |e|
  817.             if e.is_a?(Array)
  818.               returntrue2 = false
  819.               e.each do |o| returntrue2 = true if @reiterate[o] end
  820.               returntrue = false if !returntrue2
  821.             elsif @reiterate[e]
  822.               returntrue = false
  823.             end
  824.           end
  825.           return true if returntrue
  826.         else
  827.           return true if @reiterate[i]
  828.         end
  829.       end
  830.       false
  831.     else
  832.       @reiterate[vk]
  833.     end
  834.   end
  835.   #---------------------------------------------------------------------------
  836.   # * Reiterate? all
  837.   #---------------------------------------------------------------------------
  838.   def Input.reiterate_all(state = true)
  839.     @reiterate.rindexes(state)
  840.   end
  841.   #---------------------------------------------------------------------------
  842.   # * Release?
  843.   #---------------------------------------------------------------------------
  844.   def Input.release?(vk)
  845.     if vk.is_a?(Array)
  846.       vk.each do |i|
  847.         if i.is_a?(Array)
  848.           returntrue = true
  849.           i.each do |e|
  850.             if e.is_a?(Array)
  851.               returntrue2 = false
  852.               e.each do |o| returntrue2 = true if @release[o] end
  853.               returntrue = false if !returntrue2
  854.             elsif @release[e]
  855.               returntrue = false
  856.             end
  857.           end
  858.           return true if returntrue
  859.         else
  860.           return true if @release[i]
  861.         end
  862.       end
  863.       false
  864.     else
  865.       @release[vk]
  866.     end
  867.   end
  868.   #---------------------------------------------------------------------------
  869.   # * Release? all
  870.   #---------------------------------------------------------------------------
  871.   def Input.release_all(state = true)
  872.     @release.rindexes(state)
  873.   end
  874.   #---------------------------------------------------------------------------
  875.   # * 4 Directions
  876.   #---------------------------------------------------------------------------
  877.   def Input.dir4
  878.     return 2 if Input.press?(Input::DOWN)
  879.     return 4 if Input.press?(Input::LEFT)
  880.     return 6 if Input.press?(Input::RIGHT)
  881.     return 8 if Input.press?(Input::UP)
  882.     0
  883.   end
  884.   #---------------------------------------------------------------------------
  885.   # * 8 Directions
  886.   #---------------------------------------------------------------------------
  887.   def Input.dir8
  888.     return 1 if Input.press?([[Input::DOWN, Input::LEFT]])
  889.     return 3 if Input.press?([[Input::DOWN, Input::RIGHT]])
  890.     return 7 if Input.press?([[Input::UP, Input::LEFT]])
  891.     return 9 if Input.press?([[Input::UP, Input::RIGHT]])
  892.     return 1 if Input.press?(Input::LOWER_LEFT)
  893.     return 3 if Input.press?(Input::LOWER_RIGHT)
  894.     return 7 if Input.press?(Input::UPPER_LEFT)
  895.     return 9 if Input.press?(Input::UPPER_RIGHT)
  896.     return 2 if Input.press?(Input::DOWN)
  897.     return 4 if Input.press?(Input::LEFT)
  898.     return 6 if Input.press?(Input::RIGHT)
  899.     return 8 if Input.press?(Input::UP)
  900.     0
  901.   end
  902.   #---------------------------------------------------------------------------
  903.   # * Activated?
  904.   #---------------------------------------------------------------------------
  905.   def Input.activated?(vk)
  906.     API::GetKeyState.call(vk) & 1 == 1
  907.   end
  908.   #---------------------------------------------------------------------------
  909.   # * Key Click
  910.   #---------------------------------------------------------------------------
  911.   def Input.key_click(vk)
  912.     vk = vk.to_a
  913.     vk.each {|k| API::Keybd_Event.call(k, 0, 0, 0)}
  914.     vk.each {|k| API::Keybd_Event.call(k, 0, 2, 0)}
  915.   end
  916.   #---------------------------------------------------------------------------
  917.   # * Key Release
  918.   #---------------------------------------------------------------------------
  919.   def Input.key_release(vk)
  920.     vk = vk.to_a
  921.     vk.each {|k| API::Keybd_Event.call(k, 0, 2, 0)}
  922.   end
  923.   #---------------------------------------------------------------------------
  924.   # * Key Press
  925.   #---------------------------------------------------------------------------
  926.   def Input.key_press(vk)
  927.     vk = vk.to_a
  928.     vk.each {|k| API::Keybd_Event.call(k, 0, 0, 0)}
  929.   end
  930.   #---------------------------------------------------------------------------
  931.   # * Mouse Double Click?
  932.   #---------------------------------------------------------------------------
  933.   def Input.mouse_double_click?
  934.     @mouse_primary_dc[0] == 2
  935.   end
  936.   #---------------------------------------------------------------------------
  937.   # * Mouse X Coordinate
  938.   #---------------------------------------------------------------------------
  939.   def Input.mouse_x?
  940.     @mouse_pos.nil? ? nil : @mouse_pos[0]
  941.   end
  942.   #---------------------------------------------------------------------------
  943.   # * Mouse Y Coordinate
  944.   #---------------------------------------------------------------------------
  945.   def Input.mouse_y?
  946.     @mouse_pos.nil? ? nil : @mouse_pos[1]
  947.   end
  948.   #---------------------------------------------------------------------------
  949.   # * Mouse Dragging?
  950.   #---------------------------------------------------------------------------
  951.   def Input.mouse_dragging?
  952.     return false if @mouse_rectdrag.nil?
  953.     if @mouse_rectdrag.size >= 4
  954.       true
  955.     else
  956.       false
  957.     end
  958.   end
  959.   #---------------------------------------------------------------------------
  960.   # * Mouse Drag Rect
  961.   #---------------------------------------------------------------------------
  962.   def Input.mouse_drag_rect?
  963.     return nil if @mouse_rectdrag.nil?
  964.     if @mouse_rectdrag.size >= 4
  965.       if @mouse_rectdrag[0] <= @mouse_rectdrag[2]
  966.         x = @mouse_rectdrag[0]
  967.         width = @mouse_rectdrag[2] - @mouse_rectdrag[0]
  968.       else
  969.         x = @mouse_rectdrag[2]
  970.         width = @mouse_rectdrag[0] - @mouse_rectdrag[2]
  971.       end
  972.       if @mouse_rectdrag[1] <= @mouse_rectdrag[3]
  973.         y = @mouse_rectdrag[1]
  974.         heigth = @mouse_rectdrag[3] - @mouse_rectdrag[1]
  975.       else
  976.         y = @mouse_rectdrag[3]
  977.         heigth = @mouse_rectdrag[1] - @mouse_rectdrag[3]
  978.       end
  979.       Rect.new(x, y, width, heigth)
  980.     else
  981.       nil
  982.     end
  983.   end
  984.   #---------------------------------------------------------------------------
  985.   # * Mouse Drag Coordinates
  986.   #---------------------------------------------------------------------------
  987.   def Input.mouse_drag_coor?
  988.     return nil if @mouse_rectdrag.nil?
  989.     if @mouse_rectdrag.size >= 4
  990.       @mouse_rectdrag
  991.     else
  992.       nil
  993.     end
  994.   end
  995.   #---------------------------------------------------------------------------
  996.   # * Add Combo
  997.   #---------------------------------------------------------------------------
  998.   def Input.add_combo(params, restart_count, res_type, update = nil, id = nil)
  999.     combo = Key_Combo.new(params, restart_count, res_type, update)
  1000.     if id.nil?
  1001.       if !@combos.has_key?(combo.id)
  1002.         id = combo.id
  1003.       else
  1004.         loop do
  1005.           id = rand(0xFFFF)
  1006.           break if !@combos.has_key?(id)
  1007.         end
  1008.       end
  1009.     else
  1010.       if @combos.has_key?(id)
  1011.         loop do
  1012.           id = rand(0xFFFF)
  1013.           break if !@combos.has_key?(id)
  1014.         end
  1015.       end
  1016.     end
  1017.     @combos[id] = combo
  1018.     return id
  1019.   end
  1020.   #---------------------------------------------------------------------------
  1021.   # * Get All Combos
  1022.   #---------------------------------------------------------------------------
  1023.   def Input.combos
  1024.     @combos
  1025.   end
  1026.   #---------------------------------------------------------------------------
  1027.   # * Edit combos
  1028.   #---------------------------------------------------------------------------
  1029.   def Input.combos=(new)
  1030.     @combos = new
  1031.   end
  1032.   #---------------------------------------------------------------------------
  1033.   # * Delete Combo
  1034.   #---------------------------------------------------------------------------
  1035.   def Input.delete_combo(id)
  1036.     @combos.delete(id)
  1037.   end
  1038.   #---------------------------------------------------------------------------
  1039.   # * Delete Ended Combos
  1040.   #---------------------------------------------------------------------------
  1041.   def Input.delete_ended_combos(ending = true)
  1042.     deleted = {}
  1043.     @combo.each_key do |c|
  1044.       if @combo[c].end?
  1045.         next if !@combo[c].true? == ending
  1046.         deleted[c] = @combo[c]
  1047.         @combos.delete(c)
  1048.       end
  1049.     end
  1050.     return deleted
  1051.   end
  1052. end
Advertisement
Add Comment
Please, Sign In to add comment