mcgluszak

[Author: Aleworks] Aleworks' Library v1.01

Jul 31st, 2011
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 48.56 KB | None | 0 0
  1. #=============================================================================
  2. # *** Aleworks Library(ALibrary)
  3. #=============================================================================
  4. # Created by Aleworks
  5. # Version: 1.01
  6. # Last Modification: 11/09/2007 (day/month/year)
  7. #=============================================================================
  8. #==== Description ====
  9. # This script is a library, of tools and methods specialy designed for others
  10. # Aleworks scripts, however you are free to use it, as long as you give credit.
  11. #=============================================================================
  12. #==== Credits ====
  13. # * Ruby 1.8
  14. #   - For the Win32::Registry class, that was the base for Aleworks::Registry
  15. # * Cybersam
  16. #   - For some APIs functions for getting mouse position
  17. # * Nishikawa, Yasuhiro
  18. #   - For some APIs for the Aleworks::Clipboard
  19. #=============================================================================
  20. #==== Version History ====
  21. # * Version 1.01
  22. #   - Lots of fixes
  23. #   - New methods:
  24. #     - Aleworks: check_hang_up; translate_string; get_window_rect
  25. #     - API: get_client_rect
  26. #     - String: trans_undrawable_chr; insert
  27. #     - Nil: clone
  28. #     - Array: insert
  29. #     - Bitmap: initialize; bmp_dump
  30. #   - New constants:
  31. #     - Aleworks: ASCII_TABLE; TRANSLATE_TABLE
  32. #=============================================================================
  33. #==== Classes & Methods ====
  34. # ** Module Aleworks
  35. #   * Constants:
  36. #     - Active_Window
  37. #       Id of the Game window.
  38. #     - Current_Process_Id
  39. #       Id of the Game process.
  40. #     - Keyboard_Layout
  41. #       Id of the used keyboard layout.
  42. #     - ASCII_TABLE
  43. #       Hash containing the characters corresponding to each of the 255 ascii
  44. #       codification.
  45. #     - TRANSLATE_TABLE
  46. #       Hash containing the characters corresponding to the back references.
  47. #   * Methods:
  48. #     - Aleworks.get_keyboard_layout_name
  49. #       Returns an array containing the name of the keyboard layout, and its
  50. #       abbreviation.
  51. #     - Aleworks.get_current_desktop_path
  52. #       Returns the path of the user desktop folder.
  53. #     - Aleworks.get_current_userdocuments_path
  54. #       Returns the path of the user personal documents folder.
  55. #     - Aleworks.get_common_desktop_path
  56. #       Returns the path of the common desktop folder.
  57. #     - Aleworks.get_common_usersdocuments_path
  58. #       Returns the path of the common personal documents folder.
  59. #     - Aleworks.get_fonts_folder_path
  60. #       Returns the path of the common users personal documents folder.
  61. #     - Aleworks.get_fonts_names
  62. #       Returns all usable fonts names.
  63. #     - Aleworks.get_mouse_swap_buttons_flag
  64. #       Returns 1 if the mouse button are inverted, 0 instead.
  65. #     - Aleworks.get_mouse_position
  66. #       Returns an array with the coordinates of the mouse cursor.
  67. #     - Aleworks.get_mouse_window_position([hwnd])
  68. #       Returns an array with the coordinates of the mouse cursor, in the given
  69. #       hwnd. If hwnd is not given, it will be used the rgss player.
  70. #     - Aleworks.check_hang_up(time)
  71. #       Updates the module Graphics for prevention of a System Stack Error.
  72. #     - Aleworks.translate_string(string)
  73. #       Returns the translation of back reference characters to normal
  74. #       characters.
  75. #     - Aleworks.get_window_rect([hwnd])
  76. #       Returns an array containing the rect of the given window. If hwnd is not
  77. #       given, it will be the game window.
  78. #       Note: The rect includes the borders and title bar of the window.
  79. # ** Module Aleworks::API
  80. #   * Methods:
  81. #     - API.get_rtp_path([rtp])
  82. #       Returns the folder path of the RTP number 'rtp'-1. If 'rtp' is not used,
  83. #       it will be number 0.
  84. #     - API.get_keyboard_state
  85. #       Returns a 256 byte string containing the virtual keys states.
  86. #     - API.get_keyboard_layout_name
  87. #       Returns a 9 byte string containing an id name of the keyboard layout.
  88. #     - API.vk_to_ascii(vk)
  89. #       Translates the given virtual key to an ASCII character.
  90. #     - API.get_pps(tag, id, file)
  91. #       Returns the value of the given id, of the given tag, of the given ini
  92. #       file.
  93. #     - API.get_window_placement([hwnd])
  94. #       Returns an array with the placement of the given window. If hwnd is not
  95. #       given, it will be used the rgss player.
  96. #     - API.get_cursor_pos
  97. #       Returns an unpacked mouse coordinates.
  98. #     - API.get_client_rect([hwnd])
  99. #       Returns an array containing the rect of the given window. If hwnd is not
  100. #       given, it will be the game window.
  101. # ** Module Aleworks::Ini
  102. #   * Methods:
  103. #     - get_string(id[, tag[, file]])
  104. #       Returns the value of the given id, of the given tag, of the given ini
  105. #       file. If the tag is not given, it will be used 'Game', and if the file
  106. #       is not given, it will be used '.\\Game.ini'.
  107. #     - set_string(string, id[, tag[, file]])
  108. #       Writes the value to the given id, of the given tag, of the given ini
  109. #       file. If the tag is not given, it will be used 'Game', and if the file
  110. #       is not given, it will be used '.\\Game.ini'.
  111. # ** Module Aleworks::Clipboard
  112. #   * Methods:
  113. #     - read
  114. #       Returns the data contained in the Windows Clipboard.
  115. #     - write(data)
  116. #       Writes 'data' to the Windows Clipboard.
  117. #     - empty
  118. #       Clears the data of the Windows Clipboard.
  119. # ** Module Aleworks::Registry
  120. #   * Constants:
  121. #     - HKEYS
  122. #       Used internaly, for translate the strings HKEYS to numeric.
  123. #   * Methods:
  124. #     - read_entry(key, entry)
  125. #       Returns the value of the given entry, of the given key.
  126. #     - enum_keys(key)
  127. #       Returns an Array value containing all subkeys of the given key.
  128. #     - enum_entries(key)
  129. #       Returns an Array value containing all entries of the given key.
  130. # ** Class Array
  131. #   * Methods:
  132. #     - clone_all
  133. #       Creates a copy of the object, and all it's sub values.
  134. #     - to_hash
  135. #       Creates a hash using the index of the values as the keys.
  136. #     - rindexes(*values)
  137. #       Returns the indices of all values contained in 'values'.
  138. #     - deep([deep])
  139. #       Returns the max count of the continuated sub contained arrays/hashs.
  140. #       'deep' is the starting count + 1. If it is not given, will be used -1.
  141. #       Examples:
  142. #         - p [1, 2, 3].deep     => 0
  143. #         - p [1, 2, [3]].deep   => 1
  144. #         - p [[1], 2, [3]].deep => 1
  145. #         - p [[[1], 2, 3]].deep => 2
  146. #         - p [[[[[[1]]]]]].deep => 5
  147. #     - all_flatten; all_flatten!
  148. #       Similar to flatten, but it will affect the Hash, too. Used with ! will
  149. #       modify self.
  150. # ** Class Hash
  151. #   * Methods:
  152. #     - clone_all
  153. #       Creates a copy of the object, and all it's sub values and keys.
  154. #     - deep([deep])
  155. #       See Array.deep.
  156. #     - fusion
  157. #       Returns an Array, where will be combined using '+' each key and value.
  158. #       If at least one key can't be combined with its value, will raise an
  159. #       Errno::EINVAL error.
  160. # ** Class String
  161. #   * Methods:
  162. #     - pixel_width([size[, name]])
  163. #       Returns the width in pixel of the string.
  164. #     - pixel_height([size[, name]])
  165. #       Returns the height in pixel of the string.
  166. #     - width_slice(width[, size[, name]])
  167. #       Returns an Array containing Strings with theirs pixel_width, equal or
  168. #       less than width.
  169. #     - font_size_width(width[, name]])
  170. #       Returns the max font size, that will not cut the String when displayed
  171. #       with the given width. Returns nil, if no size is available.
  172. #     - font_size_height(height[, name]])
  173. #       Returns the max font size, that will not cut the String when displayed
  174. #       with the given height. Returns nil, if no size is available.
  175. #     - font_size(width, height[, name]])
  176. #       Returns the max font size, that will not cut the String when displayed
  177. #       with the given width and height. Returns nil, if no size is available.
  178. #     - trans_undrawable_chr([font[, chr]])
  179. #       Returns a string with all the undrawable characters for the given "font"
  180. #       replaced with "chr". If "font" is not given will be used the default font
  181. #       options.  If "chr is not given, it will be "?".
  182. # ** Class Bitmap
  183. #   * Alias method:
  184. #     - draw_text; Alias Name: alibrary_aleworks_bitmap_drawtext
  185. #       Allows to draw text using arrays, hashs, or other classes.
  186. #     - initialize
  187. #       If a block is given the Bitmap class will be returned and after the
  188. #       block ends the Bitmap will be disposed.
  189. #     - bmp_dump([filename[, background_color])
  190. #       Creates a ".bmp" file with the filename given from the Bitmap class. If
  191. #       filename is not given it will automatically maked. If background_color is
  192. #       given(as Color class) all the pixels from the Bitmap that have
  193. #       transparencies will be mixed with the given color.
  194. # ** Module Graphics
  195. #   * Constants
  196. #     - Update
  197. #       Used for calling automaticaly methods when Graphics module is updated.
  198. #       For adding an auto-update, add an Array to the UPDATE array containg
  199. #       as first value the class/module as a Constant and as second value the
  200. #       method of the class/module to be updated as a symbol.
  201. #   * Alias method:
  202. #     - update; alibrary_aleworks_graphics_update
  203. #       Allows to update multiple frames using Graphics.update(frames), where
  204. #       frames are the count to be updated.
  205. # ** Class Nil
  206. #   * Replaced method:
  207. #     - clone
  208. #       Removes the 'Nil can't be cloned error'.
  209. #=============================================================================
  210.  
  211. #=============================================================================
  212. # ** Module Aleworks
  213. #=============================================================================
  214. module Aleworks
  215.   module_function
  216.   #===========================================================================
  217.   # ** Module Aleworks::API
  218.   #===========================================================================
  219.   module API
  220.     module_function
  221.     #-------------------------------------------------------------------------
  222.     # APIs Definitions
  223.     #-------------------------------------------------------------------------
  224.     BlockInput = Win32API.new('user32', 'BlockInput', 'L', 'L')
  225.     ClientToScreen = Win32API.new('user32', 'ClientToScreen', 'LP', 'L')
  226.     CloseClipboard = Win32API.new('user32', 'CloseClipboard', '', 'L')
  227.     EmptyClipboard = Win32API.new('user32', 'EmptyClipboard', '', 'L')
  228.     GetActiveWindow = Win32API.new('user32', 'GetActiveWindow', '', 'L')
  229.     GetAsyncKeyState = Win32API.new('user32', 'GetAsyncKeyState', 'L', 'L')
  230.     GetClientRect = Win32API.new('user32', 'GetClientRect', 'LP', 'L')
  231.     GetClipboardData = Win32API.new('user32', 'GetClipboardData', 'L', 'L')
  232.     GetCursorPos = Win32API.new('user32', 'GetCursorPos', 'P', 'L')
  233.     GetDoubleClickTime = Win32API.new('user32', 'GetDoubleClickTime', '', 'L')
  234.     GetKeyboardLayout = Win32API.new('user32', 'GetKeyboardLayout','L', 'L')
  235.     GetKeyboardLayoutName =Win32API.new('user32','GetKeyboardLayoutName','P','L')
  236.     GetKeyboardState = Win32API.new('user32', 'GetKeyboardState', 'P', 'L')
  237.     GetKeyState = Win32API.new('user32', 'GetKeyState', 'L', 'L')
  238.     GetSystemMetrics = Win32API.new('user32', 'GetSystemMetrics', 'L', 'L')
  239.     GetWindowPlacement = Win32API.new('user32', 'GetWindowPlacement', 'LP', 'L')
  240.     Keybd_Event = Win32API.new('user32', 'keybd_event', 'LLLL', '')
  241.     OpenClipboard = Win32API.new('user32', 'OpenClipboard', 'L', 'L')
  242.     ScreenToClient = Win32API.new('user32', 'ScreenToClient', 'LP', 'L')
  243.     SetClipboardData = Win32API.new('user32', 'SetClipboardData', 'LL', 'L')
  244.     ShowCursor = Win32API.new('user32', 'ShowCursor', 'L', 'L')
  245.     ToAsciiEx = Win32API.new('user32', 'ToAsciiEx', 'LLPPLL', 'L')
  246.     GetCurrentProcessId = Win32API.new('kernel32','GetCurrentProcessId', '','L')
  247.     GPPSA = Win32API.new('kernel32', 'GetPrivateProfileStringA', 'PPPPLP', 'L')
  248.     WPPSA = Win32API.new('kernel32', 'WritePrivateProfileStringA', 'PPPP', 'L')
  249.     GlobalAlloc = Win32API.new('kernel32', 'GlobalAlloc', 'LL', 'L')
  250.     GlobalLock = Win32API.new('kernel32', 'GlobalLock', 'L', 'L')
  251.     GlobalSize = Win32API.new('kernel32', 'GlobalSize', 'L', 'L')
  252.     GlobalUnlock = Win32API.new('kernel32', 'GlobalUnlock', 'L', '')
  253.     RegCloseKey = Win32API.new('advapi32', 'RegCloseKey', 'L', 'L')
  254.     RegEnumKeyExA = Win32API.new('advapi32', 'RegEnumKeyExA', 'LLPPLLLP', 'L')
  255.     RegEnumValueA = Win32API.new('advapi32', 'RegEnumValueA', 'LLPPPPPP', 'L')
  256.     RegOpenKeyExA = Win32API.new('advapi32', 'RegOpenKeyExA', 'LPLLP', 'L')
  257.     RegQueryValueExA = Win32API.new('advapi32', 'RegQueryValueExA','LPLPPP', 'L')
  258.     begin
  259.       Memcpy = Win32API.new('ntdll', 'memcpy', 'PPL', 'L')
  260.     rescue
  261.       Memcpy = Win32API.new('crtdll', 'memcpy', 'PPL', 'L')
  262.     end
  263.     ['3', '2', '1', '0'].each do |n|
  264.       _break = false
  265.       ['E', 'J'].each do |l|
  266.         dll = 'RGSS10' + n + l
  267.         begin
  268.           RGSSGetRTPPath = Win32API.new(dll, 'RGSSGetRTPPath', 'L', 'L')
  269.           RGSSGetPathWithRTP = Win32API.new(dll, 'RGSSGetPathWithRTP', 'L', 'P')
  270.           _break = true
  271.         rescue
  272.         end
  273.         break if _break
  274.       end
  275.       break if _break
  276.     end
  277.     #-------------------------------------------------------------------------
  278.     # * Get RTP Path
  279.     #-------------------------------------------------------------------------
  280.     def get_rtp_path(rtp = 0)
  281.       RGSSGetPathWithRTP.call(RGSSGetRTPPath.cal(rtp))
  282.     end
  283.     #-------------------------------------------------------------------------
  284.     # * Get Keyboard Input State
  285.     #-------------------------------------------------------------------------
  286.     def get_keyboard_state
  287.       buffer = ' ' * 256
  288.       GetKeyboardState.call(buffer)
  289.       buffer
  290.     end
  291.     #-------------------------------------------------------------------------
  292.     # * Get Keyboard Layout Name
  293.     #-------------------------------------------------------------------------
  294.     def get_keyboard_layout_name
  295.       buffer = ' ' * 9
  296.       GetKeyboardLayoutName.call(buffer)
  297.       buffer.chop
  298.     end
  299.     #-------------------------------------------------------------------------
  300.     # * Virtual Key to String
  301.     #-------------------------------------------------------------------------
  302.     def vk_to_ascii(vk, alt_gr = false)
  303.       buffer = ' ' * 2
  304.       state = API.get_keyboard_state
  305.       if alt_gr
  306.         state[17, 1] = "\201"
  307.         state[18, 1] = "\201"
  308.       end
  309.       result = API::ToAsciiEx.call(vk, 0, state, buffer, 0,
  310.                API.get_keyboard_layout)
  311.       case result
  312.       when 1..2
  313.         buffer[0, 1]
  314.       else
  315.         ''
  316.       end
  317.     end
  318.     #-------------------------------------------------------------------------
  319.     # * Get Private Profile String
  320.     #-------------------------------------------------------------------------
  321.     def get_pps(tag, id, file)
  322.       buffer = "\0" * 255
  323.       GPPSA.call(tag, id, '', buffer, 255, file)
  324.       buffer.delete!("\0")
  325.     end
  326.     #-------------------------------------------------------------------------
  327.     # * Get Window Placement
  328.     #-------------------------------------------------------------------------
  329.     def get_window_placement(hwnd = Aleworks::Active_Window)
  330.       buffer = [48].pack('L') + ' ' * 44
  331.       GetWindowPlacement.call(hwnd, buffer)
  332.       buffer.unpack('L11')
  333.     end
  334.     #-------------------------------------------------------------------------
  335.     # * Get Cursor Pos
  336.     #-------------------------------------------------------------------------
  337.     def get_cursor_pos
  338.       buffer = ' ' * 8
  339.       result = GetCursorPos.call(buffer)
  340.       if result != 0
  341.         buffer
  342.       else
  343.         nil
  344.       end
  345.     end
  346.     #-------------------------------------------------------------------------
  347.     # * Get Keyboard Layout
  348.     #-------------------------------------------------------------------------
  349.     def get_keyboard_layout
  350.       GetKeyboardLayout.call(0)
  351.     end
  352.     #-------------------------------------------------------------------------
  353.     # * Get Client Rect
  354.     #-------------------------------------------------------------------------
  355.     def get_client_rect(hwnd = Aleworks::Active_Window)
  356.       buffer = ' ' * 16
  357.       GetClientRect.call(hwnd, buffer)
  358.       buffer.unpack('L4')
  359.     end
  360.   end
  361.   #===========================================================================
  362.   # ** Module Aleworks::Ini
  363.   #===========================================================================
  364.   module Ini
  365.     module_function
  366.     #-------------------------------------------------------------------------
  367.     # * Get String from Ini File
  368.     #-------------------------------------------------------------------------
  369.     def get_string(id, tag = 'Game', file = '.\\Game.ini')
  370.       get_pps(tag, id, file)
  371.     end
  372.     #-------------------------------------------------------------------------
  373.     # * Write String to Ini File
  374.     #-------------------------------------------------------------------------
  375.     def set_string(string, id, tag = 'Game', file = '.\\Game.ini')
  376.       API::WPPSA.call(tag, id, string, file)
  377.     end
  378.   end
  379.   #===========================================================================
  380.   # ** Module Aleworks::Clipboard
  381.   #===========================================================================
  382.   module Clipboard
  383.     module_function
  384.     #-------------------------------------------------------------------------
  385.     # * Read Clipboard Data
  386.     #-------------------------------------------------------------------------
  387.     def read
  388.       API::OpenClipboard.call(0)
  389.       data = API::GetClipboardData.call(7)
  390.       API::CloseClipboard.call
  391.       return '' if data == 0
  392.       lp = API::GlobalLock.call(data)
  393.       len = API::GlobalSize.call(data)
  394.       data2 = ' ' * (len - 1)
  395.       API::Memcpy.call(data2, lp, len)
  396.       API::GlobalUnlock.call(data)
  397.       data2
  398.     end
  399.     #-------------------------------------------------------------------------
  400.     # * Write Data to Clipboard
  401.     #-------------------------------------------------------------------------
  402.     def write(data)
  403.       API::OpenClipboard.call(0)
  404.       API::EmptyClipboard.call
  405.       set_data = API::GlobalAlloc.call(66, data.length + 1)
  406.       len = [data.size + 1, API::GlobalSize.call(set_data)].min
  407.       lp = API::GlobalLock.call(set_data)
  408.       API::Memcpy.call(lp, "#{data}", len)
  409.       API::GlobalUnlock.call(set_data)
  410.       API::SetClipboardData.call(7, set_data)
  411.       API::CloseClipboard.call
  412.     end
  413.     #-------------------------------------------------------------------------
  414.     # * Clear Clipboard Data
  415.     #-------------------------------------------------------------------------
  416.     def empty
  417.       API::EmptyClipboard.call
  418.     end
  419.   end
  420.   #===========================================================================
  421.   # ** Module Aleworks::Registry
  422.   #===========================================================================
  423.   module Registry
  424.     module_function
  425.     HKEYS = {'HKEY_CLASSES_ROOT' => 0x80000000,'HKEY_CURRENT_USER' => 0x80000001,
  426.       'HKEY_LOCAL_MACHINE' => 0x80000002, 'HKEY_USERS' => 0x80000003,
  427.       'HKEY_CURRENT_CONFIG' => 0x80000005}
  428.     #-------------------------------------------------------------------------
  429.     # * Read an Entry
  430.     #-------------------------------------------------------------------------
  431.     def read_entry(key, entry)
  432.       key.sub!(/(.*?)\\/, '')
  433.       if HKEYS[$1] != nil
  434.         hkey = HKEYS[$1]
  435.       else
  436.         return nil
  437.       end
  438.       opened, type, size = [0].pack('V'), [0].pack('V'), [0].pack('V')
  439.       API::RegOpenKeyExA.call(hkey, key, 0, 131097, opened)
  440.       opened = (opened + [0].pack('V')).unpack('V')[0]
  441.       API::RegQueryValueExA.call(opened, entry, 0, type, 0, size)
  442.       data = ' ' * (size + [0].pack('V')).unpack('V')[0]
  443.       API::RegQueryValueExA.call(opened, entry, 0, type, data, size)
  444.       API::RegCloseKey.call(opened)
  445.       data = data[0, (size + [0].pack('V')).unpack('V')[0]]
  446.       type = (type += [0].pack('V')).unpack('V')[0]
  447.       case type
  448.       when 1
  449.         data.chop
  450.       when 2
  451.         data.chop.gsub(/%([^%]+)%/) { ENV[$1] || $& }
  452.       when 3
  453.         data
  454.       when 4
  455.         (data += [0].pack('V')).unpack('V')[0]
  456.       when 5
  457.         data.unpack('N')[0]
  458.       when 7
  459.         data.split(/\0/)
  460.       when 11
  461.         (data.unpack('VV')[1] << 32) | data[0]
  462.       else
  463.         nil
  464.       end
  465.     end
  466.     #-------------------------------------------------------------------------
  467.     # * Enum Keys
  468.     #-------------------------------------------------------------------------
  469.     def enum_keys(key)
  470.       key.sub!(/(.*?)\\/, '')
  471.       if HKEYS[$1] != nil
  472.         hkey = HKEYS[$1]
  473.       else
  474.         return nil
  475.       end
  476.       index, keys, opened = 0, [], [0].pack('V')
  477.       API::RegOpenKeyExA.call(hkey, key, 0, 131097, opened)
  478.       opened = (opened + [0].pack('V')).unpack('V')[0]
  479.       loop do
  480.         name = ' ' * 514
  481.         size = [514].pack('V')
  482.         result = API::RegEnumKeyExA.call(opened, index, name, size, 0, 0, 0, 0)
  483.         if result == 0
  484.           keys.push(name[0, (size += [0].pack('V')).unpack('V')[0]])
  485.           index += 1
  486.         else
  487.           break
  488.         end
  489.       end
  490.       API::RegCloseKey.call(opened)
  491.       keys
  492.     end
  493.     #-------------------------------------------------------------------------
  494.     # * Enum Entries
  495.     #-------------------------------------------------------------------------
  496.     def enum_entries(key)
  497.       key.sub!(/(.*?)\\/, '')
  498.       if HKEYS[$1] != nil
  499.         hkey = HKEYS[$1]
  500.       else
  501.         return nil
  502.       end
  503.       index, entries, opened = 0, [], [0].pack('V')
  504.       API::RegOpenKeyExA.call(hkey, key, 0, 131097, opened)
  505.       opened = (opened + [0].pack('V')).unpack('V')[0]
  506.       loop do
  507.         name = ' ' * 514
  508.         size = [514].pack('V')
  509.         result = API::RegEnumValueA.call(opened, index, name, size, 0, 0, 0, 0)
  510.         if result == 0
  511.           entries.push(name[0, (size += [0].pack('V')).unpack('V')[0]])
  512.           index += 1
  513.         else
  514.           break
  515.         end
  516.       end
  517.       API::RegCloseKey.call(opened)
  518.       entries
  519.     end
  520.   end
  521.   #---------------------------------------------------------------------------
  522.   # Variables Declaration
  523.   #---------------------------------------------------------------------------
  524.   Active_Window = API::GetActiveWindow.call
  525.   Current_Process_Id = API::GetCurrentProcessId.call
  526.   ASCII_TABLE = {1=>'?',2=>'?',3=>'?',4=>'?',5=>'?',6=>'?',7=>'•',8=>'?',9=>'?',
  527.   10=>'?',11=>'?',12=>'?',13=>'?',14=>'?',15=>'¤',16=>'?',17=>'?',18=>'?',
  528.   19=>'?',20=>'¶',21=>'§',22=>'?',23=>'?',24=>'?',25=>'?',26=>'?',27=>'?',
  529.   28=>'?',29=>'?',30=>'?',31=>'?',32=>' ',33=>'!',34=>'"',35=>'#',36=>'$',
  530.   37=>'%',38=>'&',39=>"'",40=>'(',41=>')',42=>'*',43=>'+',44=>',',45=>'-',
  531.   46=>'.',47=>'/',48=>'0',49=>'1',50=>'2',51=>'3',52=>'4',53=>'5',54=>'6',
  532.   55=>'7',56=>'8',57=>'9',58=>':',59=>';',60=>'<',61=>'=',62=>'>',63=>'?',
  533.   64=>'@',65=>'A',66=>'B',67=>'C',68=>'D',69=>'E',70=>'F',71=>'G',72=>'H',
  534.   73=>'I',74=>'J',75=>'K',76=>'L',77=>'M',78=>'N',79=>'O',80=>'P',81=>'Q',
  535.   82=>'R',83=>'S',84=>'T',85=>'U',86=>'V',87=>'W',88=>'X',89=>'Y',90=>'Z',
  536.   91=>'[',92=>'\\',93=>']',94=>'^',95=>'_',96=>'`',97=>'a',98=>'b',99=>'c',
  537.   100=>'d',101=>'e',102=>'f',103=>'g',104=>'h',105=>'i',106=>'j',107=>'k',
  538.   108=>'l',109=>'m',110=>'n',111=>'o',112=>'p',113=>'q',114=>'r',115=>'s',
  539.   116=>'t',117=>'u',118=>'v',119=>'w',120=>'x',121=>'y',122=>'z',123=>'{',
  540.   124=>'|',125=>'}',126=>'~',127=>'¦',128=>'Ç',129=>'ü',130=>'é',131=>'â',
  541.   132=>'ä',133=>'ŕ',134=>'ĺ',135=>'ç',136=>'ę',137=>'ë',138=>'č',139=>'ď',
  542.   140=>'î',141=>'ě',142=>'Ä',143=>'Ĺ',144=>'É',145=>'ć',146=>'Ć',147=>'ô',
  543.   148=>'ö',149=>'ň',150=>'ű',151=>'ů',152=>'˙',153=>'Ö',154=>'Ü',155=>'ř',
  544.   156=>'Ł',157=>'Ř',158=>'×',159=>'ƒ',160=>'á',161=>'í',162=>'ó',163=>'ú',
  545.   164=>'ń',165=>'Ń',166=>'Ş',167=>'ş',168=>'ż',169=>'®',170=>'¬',171=>'˝',
  546.   172=>'Ľ',173=>'ˇ',174=>'«',175=>'»',176=>'¦',177=>'¦',178=>'¦',179=>'¦',
  547.   180=>'¦',181=>'Á',182=>'Â',183=>'Ŕ',184=>'©',185=>'¦',186=>'¦',187=>'+',
  548.   188=>'+',189=>'˘',190=>'Ą',191=>'+',192=>'+',193=>'-',194=>'-',195=>'+',
  549.   196=>'-',197=>'+',198=>'ă',199=>'Ă',200=>'+',201=>'+',202=>'-',203=>'-',
  550.   204=>'¦',205=>'-',206=>'+',207=>'¤',208=>'đ',209=>'Đ',210=>'Ę',211=>'Ë',
  551.   212=>'Č',213=>'i',214=>'Í',215=>'Î',216=>'Ď',217=>'+',218=>'+',219=>'¦',
  552.   220=>'_',221=>'¦',222=>'Ě',223=>'Ż',224=>'Ó',225=>'ß',226=>'Ô',227=>'Ň',
  553.   228=>'ő',229=>'Ő',230=>'µ',231=>'ţ',232=>'Ţ',233=>'Ú',234=>'Ű',235=>'Ů',
  554.   236=>'ý',237=>'Ý',238=>'Ż',239=>'´',240=>'­',241=>'±',242=>'=',243=>'ľ',
  555.   244=>'¶',245=>'§',246=>'÷',247=>'¸',248=>'°',249=>'¨',250=>'·',251=>'ą',
  556.   252=>'ł',253=>'˛',254=>'¦',255=>' '}
  557.   TRANSLATE_TABLE = {"\225"=>'•',"\244"=>'¤',"\266"=>'¶',"\247"=>'§',"\307"=>'Ç',
  558.   "\374"=>'ü',"\351"=>'é',"\342"=>'â',"\344"=>'ä',"\340"=>'ŕ',"\345"=>'ĺ',
  559.   "\347"=>'ç',"\352"=>'ę',"\353"=>'ë',"\350"=>'č',"\357"=>'ď',"\356"=>'î',
  560.   "\354"=>'ě',"\304"=>'Ä',"\305"=>'Ĺ',"\311"=>'É',"\346"=>'ć',"\306"=>'Ć',
  561.   "\364"=>'ô',"\366"=>'ö',"\362"=>'ň',"\373"=>'ű',"\371"=>'ů',"\377"=>'˙',
  562.   "\326"=>'Ö',"\334"=>'Ü',"\370"=>'ř',"\243"=>'Ł',"\330"=>'Ř',"\327"=>'×',
  563.   "\203"=>'ƒ',"\341"=>'á',"\355"=>'í',"\363"=>'ó',"\372"=>'ú',"\361"=>'ń',
  564.   "\321"=>'Ń',"\252"=>'Ş',"\272"=>'ş',"\277"=>'ż',"\256"=>'®',"\254"=>'¬',
  565.   "\275"=>'˝',"\274"=>'Ľ',"\241"=>'ˇ',"\253"=>'«',"\273"=>'»',"\301"=>'Á',
  566.   "\302"=>'Â',"\300"=>'Ŕ',"\251"=>'©',"\242"=>'˘',"\245"=>'Ą',"\343"=>'ă',
  567.   "\303"=>'Ă',"\244"=>'¤',"\360"=>'đ',"\320"=>'Đ',"\312"=>'Ę',"\313"=>'Ë',
  568.   "\310"=>'Č',"\315"=>'Í',"\316"=>'Î',"\317"=>'Ď',"\314"=>'Ě',"\257"=>'Ż',
  569.   "\323"=>'Ó',"\337"=>'ß',"\324"=>'Ô',"\322"=>'Ň',"\365"=>'ő',"\325"=>'Ő',
  570.   "\265"=>'µ',"\376"=>'ţ',"\336"=>'Ţ',"\332"=>'Ú',"\333"=>'Ű',"\331"=>'Ů',
  571.   "\375"=>'ý',"\335"=>'Ý',"\257"=>'Ż',"\264"=>'´',"\255"=>'­',"\261"=>'±',
  572.   "\276"=>'ľ',"\266"=>'¶',"\247"=>'§',"\367"=>'÷',"\270"=>'¸',"\260"=>'°',
  573.   "\250"=>'¨',"\267"=>'·',"\271"=>'ą',"\263"=>'ł',"\262"=>'˛',"\240"=>' ',
  574.   "\t"=>'        ',"\200"=>'€'}
  575.   #---------------------------------------------------------------------------
  576.   # * Get Current User Deskot Path
  577.   #---------------------------------------------------------------------------
  578.   def get_current_desktop_path
  579.     key = 'HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\'
  580.     Registry.read_entry(key + 'Explorer\\Shell Folders', 'Desktop')
  581.   end
  582.   #---------------------------------------------------------------------------
  583.   # * Get Current User Personal Folder Path
  584.   #---------------------------------------------------------------------------
  585.   def get_current_userdocuments_path
  586.     key = 'HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\'
  587.     Registry.read_entry(key + 'Explorer\\Shell Folders', 'Personal')
  588.   end
  589.   #---------------------------------------------------------------------------
  590.   # * Get Common Deskot Path
  591.   #---------------------------------------------------------------------------
  592.   def get_common_desktop_path
  593.     key = 'HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\Windows\\CurrentVersion\\'
  594.     Registry.read_entry(key + 'Explorer\\Shell Folders', 'Common Desktop')
  595.   end
  596.   #---------------------------------------------------------------------------
  597.   # * Get Common Users Personal Folder Path
  598.   #---------------------------------------------------------------------------
  599.   def get_common_usersdocuments_path
  600.     key = 'HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\'
  601.     Registry.read_entry(key + 'Explorer\\Shell Folders', 'Common Documents')
  602.   end
  603.   #---------------------------------------------------------------------------
  604.   # * Get Fonts Folder Path
  605.   #---------------------------------------------------------------------------
  606.   def get_fonts_folder_path
  607.     key = 'HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\'
  608.     Registry.read_entry(key + 'Explorer\\Shell Folders', 'Fonts')
  609.   end
  610.   #---------------------------------------------------------------------------
  611.   # * Get Fonts Names
  612.   #---------------------------------------------------------------------------
  613.   def get_fonts_names
  614.     key = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\#{ENV['OS'].sub(/_/,' ')}\\"
  615.     fonts = Registry.enum_entries(key + 'CurrentVersion\\Fonts')
  616.     fonts.each_index {|f|
  617.       fonts[f].sub!(' (TrueType)', '')
  618.       fonts[f] = nil if !Font.exist?(fonts[f])
  619.     }.delete(nil)
  620.     fonts
  621.   end
  622.   #---------------------------------------------------------------------------
  623.   # * Get Mouse Swap Buttons Flag
  624.   #---------------------------------------------------------------------------
  625.   def get_mouse_swap_buttons_flag
  626.     key = 'HKEY_CURRENT_USER\\Control Panel\\Mouse'
  627.     Registry.read_entry(key, 'SwapMouseButtons').to_i
  628.   end
  629.   #---------------------------------------------------------------------------
  630.   # * Get Mouse Position
  631.   #---------------------------------------------------------------------------
  632.   def get_mouse_position
  633.     pos = API.get_cursor_pos
  634.     if pos.nil?
  635.       nil
  636.     else
  637.       pos.unpack('L2')
  638.     end
  639.   end
  640.   #---------------------------------------------------------------------------
  641.   # * Get Mouse Window Position
  642.   #---------------------------------------------------------------------------
  643.   def get_mouse_window_position(hwnd = Active_Window)
  644.     pos = API.get_cursor_pos
  645.     if pos.nil?
  646.       nil
  647.     else
  648.       result = API::ScreenToClient.call(hwnd, pos)
  649.       if result != 0
  650.         pos = pos.unpack('L2')
  651.         max_x, max_y = API.get_client_rect[2] - 1, API.get_client_rect[3] - 1
  652.         if pos[0] >= 0 and pos[1] >= 0 and pos[0] <= max_x and pos[1] <= max_y
  653.           pos
  654.         else
  655.           nil
  656.         end
  657.       else
  658.         nil
  659.       end
  660.     end
  661.   end
  662.   #---------------------------------------------------------------------------
  663.   # * Get Window Rect
  664.   #---------------------------------------------------------------------------
  665.   def get_window_rect(hwnd = Active_Window)
  666.     placement = API.get_window_placement(hwnd)[7..10]
  667.     width = placement[2] - placement[0]
  668.     height = placement[3] - placement[1]
  669.     [placement[0], placement[1], width, height]
  670.   end
  671.   #---------------------------------------------------------------------------
  672.   # * Get Keyboard Layout Name
  673.   #---------------------------------------------------------------------------
  674.   def get_keyboard_layout_name
  675.     id = API.get_keyboard_layout_name
  676.     key='HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Keyboard Layout'
  677.     name = Registry.read_entry(key + 's\\' + id, 'Layout Text')
  678.     initials = Registry.read_entry(key + '\\DosKeybCodes', id)
  679.     name = name.nil? ? '' : name
  680.     initials = initials.nil? ? '' :initials
  681.     [translate_string(name), translate_string(initials)]
  682.   end
  683.   #---------------------------------------------------------------------------
  684.   # * Get Keyboard Layout Name
  685.   #---------------------------------------------------------------------------
  686.   def translate_string(string, arrayed = false)
  687.     translation = []
  688.     string.each_chr do |c|
  689.       if TRANSLATE_TABLE.has_key?(c)
  690.         translation << TRANSLATE_TABLE[c]
  691.       else
  692.         translation << c
  693.       end
  694.     end
  695.     if arrayed
  696.       translation
  697.     else
  698.       translation.join
  699.     end
  700.   end
  701.   #---------------------------------------------------------------------------
  702.   # * check_hang_up
  703.   #---------------------------------------------------------------------------
  704.   def check_hang_up(time)
  705.     if time.sec <= Time.now.sec - 6 or time.min != Time.now.min
  706.       Graphics.update
  707.       Time.now
  708.     else
  709.       time
  710.     end
  711.   end
  712. end
  713.  
  714. #=============================================================================
  715. # ** Class Array
  716. #=============================================================================
  717. class Array
  718.   #---------------------------------------------------------------------------
  719.   # * Clone All
  720.   #---------------------------------------------------------------------------
  721.   def clone_all
  722.     cloned = self.clone
  723.     cloned.each_index do |i|
  724.       cloned[i] = cloned[i].clone_all rescue cloned[i].clone rescue cloned[i]
  725.     end
  726.     cloned
  727.   end
  728.   #---------------------------------------------------------------------------
  729.   # * To Hash
  730.   #---------------------------------------------------------------------------
  731.   def to_hash
  732.     hash = Hash.new
  733.     self.each_index {|i| hash[i] = self[i]}
  734.     hash
  735.   end
  736.   #---------------------------------------------------------------------------
  737.   # * Reverse Indexes
  738.   #---------------------------------------------------------------------------
  739.   def rindexes(*values)
  740.     array = Array.new
  741.     self.each_index {|i| array.push(i) if values.include?(self[i])}
  742.     array
  743.   end
  744.   #---------------------------------------------------------------------------
  745.   # * Deep
  746.   #---------------------------------------------------------------------------
  747.   def deep(deep = -1)
  748.     tmp_deep = deep + 1
  749.     deep = tmp_deep
  750.     self.each do |i|
  751.       deep = i.deep(tmp_deep) if deep < i.deep(tmp_deep) rescue nil
  752.     end
  753.     deep
  754.   end
  755.   #---------------------------------------------------------------------------
  756.   # * Flatten All
  757.   #---------------------------------------------------------------------------
  758.   def all_flatten
  759.     array = self.clone_all
  760.     array.each_index do |i|
  761.       if array[i].is_a?(Hash)
  762.         array[i] = array[i].to_a
  763.       end
  764.       array[i] = array[i].all_flatten rescue array[i]
  765.     end
  766.     array.flatten!
  767.     array
  768.   end
  769.   #---------------------------------------------------------------------------
  770.   # * Destructive Flatten All
  771.   #---------------------------------------------------------------------------
  772.   def all_flatten!
  773.     self.each_index do |i|
  774.       if self[i].is_a?(Hash)
  775.         self[i] = self[i].to_a
  776.       end
  777.       self[i] = self[i].all_flatten rescue self[i]
  778.     end
  779.     self.flatten!
  780.     self
  781.   end
  782.   #---------------------------------------------------------------------------
  783.   # * Insert
  784.   #---------------------------------------------------------------------------
  785.   def insert(*args)
  786.     array = self.clone
  787.     to_insert = args[1, args.size - 1]
  788.     args[0] = [args[0], array.size - 1].min
  789.     return array.reverse.concat(to_insert).reverse if args[0] < 0
  790.     array[0..args[0]].concat(to_insert).concat(array[args[0] + 1...array.size])
  791.   end
  792. end
  793.  
  794. #=============================================================================
  795. # ** Class Bitmap
  796. #=============================================================================
  797. class Bitmap
  798.   #---------------------------------------------------------------------------
  799.   # * Initialize
  800.   #---------------------------------------------------------------------------
  801.   if @alibrary_aleworks_bitmap_initialize.nil?
  802.     alias_method(:alibrary_aleworks_bitmap_initialize, :initialize)
  803.     @alibrary_aleworks_bitmap_initialize = true
  804.   end
  805.   def initialize(*args)
  806.     alibrary_aleworks_bitmap_initialize(*args)
  807.     if block_given?
  808.       yield self
  809.       self.dispose unless self.disposed?
  810.     end
  811.   end
  812.   #---------------------------------------------------------------------------
  813.   # * Draw Text
  814.   #---------------------------------------------------------------------------
  815.   unless self.method_defined?(:alibrary_aleworks_bitmap_drawtext)
  816.     alias_method(:alibrary_aleworks_bitmap_drawtext, :draw_text)
  817.   end
  818.   def draw_text(*args)
  819.     if args.size.between?(2,3)
  820.       args[2] = 0 if args.size == 2
  821.       args[4] = args[1]
  822.       args[5] = args[2]
  823.       args[1] = args[0].y
  824.       args[2] = args[0].width
  825.       args[3] = args[0].height
  826.       args[0] = args[0].x
  827.     elsif args.size == 5
  828.       args[5] = 0
  829.     end
  830.     if args[4].is_a?(Array)
  831.       args[4].all_flatten!
  832.       args[4].each do |t|
  833.         t = t.nil? ? 'nil' : "#{t}"
  834.         width = args[2].nil? ? self.text_size(t).width : args[2]
  835.         height = args[3].nil? ? self.text_size('0').height : args[3]
  836.         self.draw_text(args[0], args[1], width, height, t, args[5])
  837.         args[1] += height
  838.       end
  839.       return
  840.     elsif args[4].is_a?(Hash)
  841.       args[4] = args[4].fusion
  842.       args[4].all_flatten!
  843.       args[4].each do |t|
  844.         t = t.nil? ? 'nil' : "#{t}"
  845.         width = args[2].nil? ? self.text_size(t).width : args[2]
  846.         height = args[3].nil? ? self.text_size('0').height : args[3]
  847.         self.draw_text(args[0], args[1], width, height, t, args[5])
  848.         args[1] += height
  849.       end
  850.       return
  851.     elsif !args[4].is_a?(String)
  852.       if args[4].to_s == ''
  853.         args[4] = args[4].inspect
  854.       else
  855.         args[4] = args[4].to_s
  856.       end
  857.     end
  858.     args[2] = args[2].nil? ? self.text_size(args[4]).width + 2 : args[2]
  859.     args[3] = args[3].nil? ? self.text_size('0').height + 2 : args[3]
  860.     alibrary_aleworks_bitmap_drawtext(*args)
  861.   end
  862.   #---------------------------------------------------------------------------
  863.   # * Bitmap Save in Bmp Format
  864.   #---------------------------------------------------------------------------
  865.   def bmp_dump(filename = nil, background_color = nil)
  866.     bmp = self
  867.     filename = "#{bmp.object_id}" if filename.nil?
  868.     filename += '.bmp' if File.extname(filename) != '.bmp'
  869.     if !background_color.nil?
  870.       temp_bmp = bmp.clone
  871.       rect = Rect.new(0, 0, temp_bmp.width, temp_bmp.height)
  872.       bmp = Bitmap.new(temp_bmp.width, temp_bmp.height)
  873.       bmp.fill_rect(rect, background_color)
  874.       bmp.stretch_blt(rect, temp_bmp, rect)
  875.       temp_bmp.dispose
  876.       Graphics.update
  877.     end
  878.     file = File.open(filename, 'wb')
  879.     file.write('BM')
  880.     file.write([54 + (bmp.width * bmp.height * 24) / 8].pack('L'))
  881.     file.write('ALEW')
  882.     file.write([54].pack('L'))
  883.     file.write([40].pack('L'))
  884.     file.write([bmp.width].pack('L'))
  885.     file.write([bmp.height].pack('L'))
  886.     file.write([1].pack('L')[0, 2])
  887.     file.write([24].pack('L')[0, 2])
  888.     file.write([0, 0, 0, 0, 0, 0].pack('L6'))
  889.     null_spaces = 0
  890.     if bmp.width / 4 != bmp.width / 4.0
  891.       if (bmp.width + 1) / 4 == (bmp.width + 1) / 4.0
  892.         null_spaces = 1
  893.       elsif (bmp.width + 2) / 4 == (bmp.width + 2) / 4.0
  894.         null_spaces = 2
  895.       elsif (bmp.width + 3) / 4 == (bmp.width + 3) / 4.0
  896.         null_spaces = 3
  897.       end
  898.     end
  899.     time = Time.now
  900.     last_porcentage = -1
  901.     for y in (-bmp.height + 1)..0
  902.       line = ''
  903.       for x in 0...bmp.width
  904.         color = bmp.get_pixel(x, y.abs)
  905.         line += [color.blue.to_i].pack('L')[0, 1]
  906.         line += [color.green.to_i].pack('L')[0, 1]
  907.         line += [color.red.to_i].pack('L')[0, 1]
  908.         time = Aleworks.check_hang_up(time)
  909.         if block_given?
  910.           porcentage = (((y + bmp.height) * bmp.width + x)) * 100
  911.           porcentage /= bmp.height * bmp.width
  912.           if porcentage > last_porcentage
  913.             yield porcentage
  914.             last_porcentage = porcentage
  915.           end
  916.         end
  917.       end
  918.       file.write(line)
  919.       time = Aleworks.check_hang_up(time)
  920.     end
  921.     file.close
  922.   end
  923. end
  924.  
  925. #=============================================================================
  926. # ** Class Hash
  927. #=============================================================================
  928. class Hash
  929.   #---------------------------------------------------------------------------
  930.   # * Clone All
  931.   #---------------------------------------------------------------------------
  932.   def clone_all
  933.     cloned = self.clone
  934.     cloned.each_key do |i|
  935.       cloned[i] = cloned[i].clone_all rescue cloned[i].clone rescue cloned[i]
  936.     end
  937.     cloned
  938.   end
  939.   #---------------------------------------------------------------------------
  940.   # * Deep
  941.   #---------------------------------------------------------------------------
  942.   def deep(deep = -1)
  943.     tmp_deep = deep + 1
  944.     key_deep = tmp_deep
  945.     value_deep = tmp_deep
  946.     self.each do |k, v|
  947.       key_deep = k.deep(tmp_deep) if key_deep < k.deep(tmp_deep) rescue nil
  948.       value_deep = v.deep(tmp_deep) if value_deep < v.deep(tmp_deep) rescue nil
  949.     end
  950.     if key_deep > value_deep
  951.       key_deep
  952.     else
  953.       value_deep
  954.     end
  955.   end
  956.   #---------------------------------------------------------------------------
  957.   # * To Array, combining Key and Value
  958.   #---------------------------------------------------------------------------
  959.   def fusion
  960.     array = self.sort
  961.     array.each_index do|i|
  962.       begin
  963.         array[i] = array[i][0] + array[i][1]
  964.       rescue
  965.         raise Errno::EINVAL, "Can't fusion Hash"
  966.       end
  967.     end
  968.   end
  969. end
  970.  
  971. #=============================================================================
  972. # ** Nil Class
  973. #=============================================================================
  974. class NilClass
  975.   def clone;end
  976. end
  977.  
  978. #=============================================================================
  979. # ** Class String
  980. #=============================================================================
  981. class String
  982.   #---------------------------------------------------------------------------
  983.   # * Pixel Width of String
  984.   #---------------------------------------------------------------------------
  985.   def pixel_width(*args)
  986.     if args[0].is_a?(Font)
  987.       size = args[0].size
  988.       name = args[0].name
  989.       bold = args[0].bold
  990.     else
  991.       size = args[0].nil? ? Font.default_size : args[0]
  992.       name = args[1].nil? ? Font.default_name : args[1]
  993.       bold = args[2].nil? ? Font.default_bold : args[2]
  994.     end
  995.     pix = 0
  996.     Bitmap.new(1, 1) do |bitmap|
  997.       bitmap.font.name = name
  998.       bitmap.font.size = size
  999.       bitmap.font.bold = bold
  1000.       pix = bitmap.text_size(self).width
  1001.     end
  1002.     pix
  1003.   end
  1004.   #---------------------------------------------------------------------------
  1005.   # * Pixel Height of String
  1006.   #---------------------------------------------------------------------------
  1007.   def pixel_height(*args)
  1008.     if args[0].is_a?(Font)
  1009.       size = args[0].size
  1010.       name = args[0].name
  1011.       bold = args[0].bold
  1012.     else
  1013.       size = args[0].nil? ? Font.default_size : args[0]
  1014.       name = args[1].nil? ? Font.default_name : args[1]
  1015.       bold = args[2].nil? ? Font.default_bold : args[2]
  1016.     end
  1017.     pix = 0
  1018.     Bitmap.new(1, 1) do |bitmap|
  1019.       bitmap.font.name = name
  1020.       bitmap.font.size = size
  1021.       bitmap.font.bold = bold
  1022.       pix = bitmap.text_size(self).height
  1023.     end
  1024.     pix
  1025.   end
  1026.   #---------------------------------------------------------------------------
  1027.   # * Cut String to Strings with less or equal size than Width
  1028.   #---------------------------------------------------------------------------
  1029.   def width_slice(width, *args)
  1030.     if args[0].is_a?(Font)
  1031.       size = args[0].size
  1032.       name = args[0].name
  1033.       bold = args[0].bold
  1034.     else
  1035.       size = args[0].nil? ? Font.default_size : args[0]
  1036.       name = args[1].nil? ? Font.default_name : args[1]
  1037.       bold = args[2].nil? ? Font.default_bold : args[2]
  1038.     end
  1039.     result = ['']
  1040.     txt = self.clone
  1041.     txt = txt.cscan
  1042.     Bitmap.new(1, 1) do |bitmap|
  1043.       bitmap.font.name = name
  1044.       bitmap.font.size = size
  1045.       bitmap.font.bold = bold
  1046.       txt.each_index do |i|
  1047.         if txt[i].is_a?(String)
  1048.           txt[i] = [txt[i], bitmap.text_size(txt[i]).width]
  1049.           if txt.include?(txt[i][0])
  1050.             txt.collect! do |e|
  1051.               if e == txt[i][0]
  1052.                 txt[i]
  1053.               else
  1054.                 e
  1055.               end
  1056.             end
  1057.           end
  1058.         end
  1059.       end
  1060.     end
  1061.     act_width = 0
  1062.     loop do
  1063.       if txt.size > 0
  1064.         tmp_width = act_width + txt[0][1]
  1065.         tmp_width += txt[1][1] if txt.size > 1
  1066.         if width >= act_width + txt[0][1]
  1067.           result[result.size - 1] += txt[0][0]
  1068.           act_width += txt[0][1]
  1069.           txt.delete_at(0)
  1070.         else
  1071.           result.push('')
  1072.           act_width = 0
  1073.         end
  1074.       else
  1075.         break
  1076.       end
  1077.     end
  1078.     result
  1079.   end
  1080.   #---------------------------------------------------------------------------
  1081.   # * Max Font Size for Width
  1082.   #---------------------------------------------------------------------------
  1083.   def font_size_width(width, *args)
  1084.     if args[0].is_a?(Font)
  1085.       name = args[0].name
  1086.       bold = args[0].bold
  1087.     else
  1088.       name = args[0].nil? ? Font.default_name : args[0]
  1089.       bold = args[1].nil? ? Font.default_bold : args[1]
  1090.     end
  1091.     size = 6
  1092.     Bitmap.new(1, 1) do |bitmap|
  1093.       bitmap.font.name = name
  1094.       bitmap.font.bold = bold
  1095.       for i in 6..96
  1096.         bitmap.font.size = i
  1097.         size = i if bitmap.text_size(self).width <= width
  1098.       end
  1099.     end
  1100.     size
  1101.   end
  1102.   #---------------------------------------------------------------------------
  1103.   # * Max Font Size for Height
  1104.   #---------------------------------------------------------------------------
  1105.   def font_size_height(height, *args)
  1106.     if args[0].is_a?(Font)
  1107.       name = args[0].name
  1108.       bold = args[0].bold
  1109.     else
  1110.       name = args[0].nil? ? Font.default_name : args[0]
  1111.       bold = args[1].nil? ? Font.default_bold : args[1]
  1112.     end
  1113.     size = 6
  1114.     Bitmap.new(1, 1) do |bitmap|
  1115.       bitmap.font.name = name
  1116.       bitmap.font.bold = bold
  1117.       for i in 6..96
  1118.         bitmap.font.size = i
  1119.         size = i if bitmap.text_size(self).height <= height
  1120.       end
  1121.     end
  1122.     size
  1123.   end
  1124.   #---------------------------------------------------------------------------
  1125.   # * Max Font Size for Width & Height
  1126.   #---------------------------------------------------------------------------
  1127.   def font_size(width, height, *args)
  1128.     if args[0].is_a?(Font)
  1129.       name = args[0].name
  1130.       bold = args[0].bold
  1131.     else
  1132.       name = args[0].nil? ? Font.default_name : args[0]
  1133.       bold = args[1].nil? ? Font.default_bold : args[1]
  1134.     end
  1135.     size = 6
  1136.     Bitmap.new(1, 1) do |bitmap|
  1137.       bitmap.font.name = name
  1138.       bitmap.font.bold = bold
  1139.       for i in 6..96
  1140.         bitmap.font.size = i
  1141.         if bitmap.text_size(self).width <= width and
  1142.            bitmap.text_size(self).height <= height
  1143.           size = i
  1144.         end
  1145.       end
  1146.     end
  1147.     size
  1148.   end
  1149.   #---------------------------------------------------------------------------
  1150.   # * Insert
  1151.   #---------------------------------------------------------------------------
  1152.   def insert(*args)
  1153.     string = self
  1154.     to_insert = args[1, args.size - 1].join
  1155.     args[0] = [args[0], string.size - 1].min
  1156.     string[0..args[0]] + to_insert + string[args[0] + 1...string.size]
  1157.   end
  1158.   #---------------------------------------------------------------------------
  1159.   # * Iterate each Character
  1160.   #---------------------------------------------------------------------------
  1161.   def each_chr
  1162.     self.cscan.each {|chr| yield chr}
  1163.   end
  1164.   #---------------------------------------------------------------------------
  1165.   # * Character Scan
  1166.   #---------------------------------------------------------------------------
  1167.   def cscan
  1168.     chrs = []
  1169.     for i in 0...self.size
  1170.       chrs.push(self[i..i])
  1171.     end
  1172.     chrs
  1173.   end
  1174.   #---------------------------------------------------------------------------
  1175.   # * One Character Scan
  1176.   #---------------------------------------------------------------------------
  1177.   def oscan
  1178.     self.scan(/./)
  1179.   end
  1180.   #---------------------------------------------------------------------------
  1181.   # * To ASCII Integer
  1182.   #---------------------------------------------------------------------------
  1183.   def to_ascii_i
  1184.     self.unpack('B8')[0].to_i(2)
  1185.   end
  1186.   #---------------------------------------------------------------------------
  1187.   # * Transform the undrawable Characters
  1188.   #---------------------------------------------------------------------------
  1189.   def trans_undrawable_chr(font = Font.new, chr = '?')
  1190.     bmp = Bitmap.new(1, 1)
  1191.     bmp.font= font
  1192.     txt = ''
  1193.     oscan.each {|c| txt += (bmp.text_size(c).width == 0) ? chr : c}
  1194.     txt
  1195.   end
  1196. end
  1197.  
  1198. #=============================================================================
  1199. # ** Module Graphics
  1200. #=============================================================================
  1201. module Graphics
  1202.   Update = []
  1203.   class << self
  1204.     #-------------------------------------------------------------------------
  1205.     # * Multiple Graphics update & objects update
  1206.     #-------------------------------------------------------------------------
  1207.     unless self.method_defined?(:alibrary_aleworks_graphics_update)
  1208.       alias_method(:alibrary_aleworks_graphics_update, :update)
  1209.     end
  1210.     def update(frames = 0)
  1211.       alibrary_aleworks_graphics_update
  1212.       Update.each {|k| k[0].method(k[1]).call}
  1213.       update(frames - 1) if frames > 0
  1214.     end
  1215.   end
  1216. end
Advertisement
Add Comment
Please, Sign In to add comment