mcgluszak

[Author: Aleworks] Aleworks' Library v1.03

Aug 9th, 2011
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 38.64 KB | None | 0 0
  1. #=============================================================================
  2. # *** AWorks Library(ALibrary)
  3. #=============================================================================
  4. # Created by AWorks
  5. # Version: 1.03
  6. # Last Modification: 26/01/2009 (day/month/year)
  7. # Compatible with RMXP & RMVX
  8. #=============================================================================
  9. #==== Description ====
  10. # This script is a compilation of useful tools created and designed for their
  11. # use in the AWorks scripts. However, some functions may be useful for other
  12. # purposes and scripts.
  13. #=============================================================================
  14. #==== Credits ====
  15. # * Ruby 1.8
  16. #   - Win32::Registry class, base of AWorks::Registry
  17. # * Nishikawa, Yasuhiro
  18. #   - APIs for AWorks::Clipboard
  19. #=============================================================================
  20. #==== Requeriments ====
  21. # * AWorks.dll (Place it on the game folder)
  22. #   - Version: 1.00
  23. #=============================================================================
  24. #==== Features ====
  25. # * AWorks::Ini.get_string(id[, tag[, file]])
  26. #   Gets the string of the id field from the tag section from a .ini file.
  27. #
  28. # * AWorks::Ini.set_string(string, id, [, tag[, file]])
  29. #   Sets the string of the id field from the tag section from a .ini file.
  30. #
  31. # * AWorks::Clipboard.read
  32. #   Gets the clipboard data.
  33. #
  34. # * AWorks::Clipboard.write(string)
  35. #   Sets the string to the clipboard data.
  36. #
  37. # * AWorks::Clipboard.empty
  38. #   Empties the clipboard data.
  39. #
  40. # * AWorks::Registry.read_value(key, entry)
  41. #   Read a registry value entry under key.
  42. #
  43. # * AWorks::Registry.enum_keys(key)
  44. #   Enums all subkeys under key.
  45. #
  46. # * AWorks::Registry.enum_values(key)
  47. #   Enums all entries under key.
  48. #
  49. # * AWorks::Registry.create_key(key)
  50. #   Creates the subkey key.
  51. #
  52. # * AWorks::Registry.set_value(key, value[, type])
  53. #   Sets(or creates) the value.
  54. #
  55. # * AWorks::Registry.delete_key(key[, recursive])
  56. #   Deletes a subkey.
  57. #
  58. # * AWorks::Registry.delete_value(key)
  59. #   Deletes a value.
  60. #
  61. # * AWorks.get_fonts_names
  62. #   Returns fonts names from the windows Fonts folder.
  63. #
  64. # * AWorks.get_hwnd
  65. #   Returns the HWND index of the game player.
  66. #
  67. # * AWorks.get_rgssdll
  68. #   Returns the RGSS dll name.
  69. #
  70. # * AWorks.get_rtp_path([id])
  71. #   Returns the rtp id path.
  72. #
  73. # * AWorks.get_mouse_swap_buttons_flag
  74. #   Returns the Swap Mouse Buttons Flag.
  75. #
  76. # * Array.recursive_clone
  77. #   Clones the array and all its subelements.
  78. #
  79. # * Array.to_hash
  80. #   Converts the array in a hash.
  81. #
  82. # * Array.rindexes(*values)
  83. #   Gets the indexes of the values.
  84. #
  85. # * Array.deep
  86. #   Gets the deep of an Array. Deep stands for the max amount of subcontainers
  87. #   (Arrays and Hashs) inside the array in a row.
  88. #
  89. # * Array.recursive_flatten
  90. #   Calls the flatten method to the Array and all its subelements.
  91. #
  92. # * Array.recursive_flatten!
  93. #   Calls the flatten method to the Array and all its subelements.
  94. #
  95. # * Array.insert(i, obj)
  96. #   Inserts the obj object in the i index.
  97. #
  98. # * Hash.recursive_clone
  99. #   Clones the hash and all its subelements.
  100. #
  101. # * Hash.deep(deep = -1)
  102. #   Gets the deep of an Hash. Deep stands for the max amount of subcontainers
  103. #   (Arrays and Hashs) inside the hash in a row.
  104. #
  105. # * Hash.fusion
  106. #   Makes an array combining each key and value.
  107. #
  108. # * NilClass.clone
  109. #   Empty method for allowing the cloning of nil objects.
  110. #
  111. # * Math.radian2degree(r)
  112. #   Converts a radian value to a degree value,
  113. #
  114. # * Math.degree2radian(g)
  115. #   Converts a degree value to a radian value,
  116. #
  117. # * Math.quick_cos(i)
  118. #   Calc the cosine value of an Integer. Faster than Math.cos.
  119. #
  120. # * Math.quick_sin(i)
  121. #   Calc the sine value of an Integer. Faster than Math.sine.
  122. #
  123. # * String.pixel_width(font)
  124. #   Gets the width of the string for the given font in pixels.
  125. #
  126. # * String.pixel_width(name, size, bold)
  127. #   Gets the width of the string for the given font format in pixels.
  128. #
  129. # * String.pixel_height(font)
  130. #   Gets the height of the string for the given font in pixels.
  131. #
  132. # * String.pixel_height(name, size, bold)
  133. #   Gets the height of the string for the given font format in pixels.
  134. #
  135. # * String.width_slice(width, font)
  136. #   Returns in an array the string sliced in pieces which fit in the given width
  137. #   for the given font.
  138. #
  139. # * String.width_slice(width, name, size, bold)
  140. #   Returns in an array the string sliced in pieces which fit in the given width
  141. #   for the given font format.
  142. #
  143. # * String.font_size_width(width, font)
  144. #   Gets the max font size for the string to fit in the given width with the
  145. #   given font. Font size doesn't matter.
  146. #
  147. # * String.font_size_width(width, name, bold)
  148. #   Gets the max font size for the string to fit in the given width with the
  149. #   given font format.
  150. #
  151. # * String.font_size_height(height, font)
  152. #   Gets the max font size for the string to fit in the given height with the
  153. #   given font. Font size doesn't matter.
  154. #
  155. # * String.font_size_height(height, name, bold)
  156. #   Gets the max font size for the string to fit in the given height with the
  157. #   given font format.
  158. #
  159. # * String.font_size(width, height, font)
  160. #   Gets the max font size for the string to fit in the given width and height
  161. #   with the given font. Font size doesn't matter.
  162. #
  163. # * String.font_size(width, height, name, bold)
  164. #   Gets the max font size for the string to fit in the given width and height
  165. #   with the given font format.
  166. #
  167. # * Bitmap.adraw_text(*args)
  168. #   Draws like Bitmap.draw_text, but with some special features.
  169. #
  170. # * Graphics.auto_update (Property)
  171. #   An array. Updates the objects and methods contained each frame. For adding
  172. #   an object for being autoupdated add a new Array to Graphics.auto_update
  173. #   with the following format:
  174. #     [Object, method, frames]
  175. #   The object must be a class and the method a symbol. If frames is nil, the
  176. #   auto update will run undefined frames(Can be removed with .delete method).
  177. #   If it is an Integer, it will act as a backward counter for each frame, and
  178. #   the auto update will end when it reachs 0.
  179. #=============================================================================
  180.  
  181. #=============================================================================
  182. # ** Module AWorks
  183. #=============================================================================
  184. module AWorks
  185.   module_function
  186.   #===========================================================================
  187.   # ** Module AWorks::API
  188.   #===========================================================================
  189.   module_function
  190.   module API
  191.     #-------------------------------------------------------------------------
  192.     # * AWorks DLL API declaration
  193.     #-------------------------------------------------------------------------
  194.     Initialize = Win32API.new('AWorks', 'Initialize', 'L', '')
  195.     InputIni = Win32API.new('AWorks', 'InputInitialize', 'LLLLLL', '')
  196.     InputUpdate = Win32API.new('AWorks', 'InputUpdate', '', '')
  197.     InputMouseIni = Win32API.new('AWorks', 'InputMouseInitialize', 'LLL', '')
  198.     InputMouseConfig = Win32API.new('AWorks', 'InputMouseConfig', 'LLLL', '')
  199.     BitmapRotate = Win32API.new('AWorks', 'BitmapRotate', 'LLL', '')
  200.     #-------------------------------------------------------------------------
  201.     # * API declaration
  202.     #-------------------------------------------------------------------------
  203.     BlockInput = Win32API.new('user32', 'BlockInput', 'L', 'L')
  204.     ClientToScreen = Win32API.new('user32', 'ClientToScreen', 'LP', 'L')
  205.     CloseClipboard = Win32API.new('user32', 'CloseClipboard', '', 'L')
  206.     ClipCursor = Win32API.new('user32', 'ClipCursor', 'P', 'L')
  207.     EmptyClipboard = Win32API.new('user32', 'EmptyClipboard', '', 'L')
  208.     FindWindowA = Win32API.new('user32', 'FindWindowA', 'pp', 'l')
  209.     GetActiveWindow = Win32API.new('user32', 'GetActiveWindow', '', 'L')
  210.     GetAsyncKeyState = Win32API.new('user32', 'GetAsyncKeyState', 'L', 'L')
  211.     GetClientRect = Win32API.new('user32', 'GetClientRect', 'LP', 'L')
  212.     GetClipboardData = Win32API.new('user32', 'GetClipboardData', 'L', 'L')
  213.     GetCursorPos = Win32API.new('user32', 'GetCursorPos', 'P', 'L')
  214.     GetDoubleClickTime = Win32API.new('user32', 'GetDoubleClickTime', '', 'L')
  215.     GetKeyboardLayout = Win32API.new('user32', 'GetKeyboardLayout','L', 'L')
  216.     GetKeyboardLayoutName =Win32API.new('user32','GetKeyboardLayoutName','P','L')
  217.     GetKeyboardState = Win32API.new('user32', 'GetKeyboardState', 'P', 'L')
  218.     GetKeyState = Win32API.new('user32', 'GetKeyState', 'L', 'L')
  219.     GetSystemMetrics = Win32API.new('user32', 'GetSystemMetrics', 'L', 'L')
  220.     GetWindowPlacement = Win32API.new('user32', 'GetWindowPlacement', 'LP', 'L')
  221.     Keybd_Event = Win32API.new('user32', 'keybd_event', 'LLLL', '')
  222.     OpenClipboard = Win32API.new('user32', 'OpenClipboard', 'L', 'L')
  223.     ScreenToClient = Win32API.new('user32', 'ScreenToClient', 'LP', 'L')
  224.     SetClipboardData = Win32API.new('user32', 'SetClipboardData', 'LL', 'L')
  225.     ShowCursor = Win32API.new('user32', 'ShowCursor', 'L', 'L')
  226.     SwapMouseButton = Win32API.new('user32', 'SwapMouseButton', 'L', 'L')
  227.     ToAsciiEx = Win32API.new('user32', 'ToAsciiEx', 'LLPPLL', 'L')
  228.     GetCurrentProcessId = Win32API.new('kernel32','GetCurrentProcessId', '','L')
  229.     GPPSA = Win32API.new('kernel32', 'GetPrivateProfileStringA', 'PPPPLP', 'L')
  230.     WPPSA = Win32API.new('kernel32', 'WritePrivateProfileStringA', 'PPPP', 'L')
  231.     GlobalAlloc = Win32API.new('kernel32', 'GlobalAlloc', 'LL', 'L')
  232.     GlobalLock = Win32API.new('kernel32', 'GlobalLock', 'L', 'L')
  233.     GlobalSize = Win32API.new('kernel32', 'GlobalSize', 'L', 'L')
  234.     GlobalUnlock = Win32API.new('kernel32', 'GlobalUnlock', 'L', '')
  235.     RegCloseKey = Win32API.new('advapi32', 'RegCloseKey', 'L', 'L')
  236.     RegEnumKeyExA = Win32API.new('advapi32', 'RegEnumKeyExA', 'LLPPLLLP', 'L')
  237.     RegEnumValueA = Win32API.new('advapi32', 'RegEnumValueA', 'LLPPPPPP', 'L')
  238.     RegOpenKeyExA = Win32API.new('advapi32', 'RegOpenKeyExA', 'LPLLP', 'L')
  239.     RegQueryValueExA = Win32API.new('advapi32', 'RegQueryValueExA','LPLPPP', 'L')
  240.     RegDeleteValue = Win32API.new('advapi32', 'RegDeleteValue','LP', 'L')
  241.     RegDeleteKey = Win32API.new('advapi32', 'RegDeleteKey','LP', 'L')
  242.     begin
  243.       Memcpy = Win32API.new('ntdll', 'memcpy', 'PPL', 'L')
  244.     rescue
  245.       Memcpy = Win32API.new('crtdll', 'memcpy', 'PPL', 'L')
  246.     end
  247.     dll = "\0" * 255
  248.     GPPSA.call('Game', 'Library', '', dll, 255, '.\\Game.ini')
  249.     dll.delete!("\0")
  250.     RGSSGetRTPPath = Win32API.new(dll, 'RGSSGetRTPPath', 'L', 'L')
  251.     RGSSGetPathWithRTP = Win32API.new(dll, 'RGSSGetPathWithRTP', 'L', 'P')
  252.   end
  253.   #===========================================================================
  254.   # ** Module AWorks::Ini
  255.   #===========================================================================
  256.   module Ini
  257.     module_function
  258.     #-------------------------------------------------------------------------
  259.     # * Get String from Ini File
  260.     #-------------------------------------------------------------------------
  261.     def get_string(id, tag = 'Game', file = '.\\Game.ini')
  262.       buffer = "\0" * 255
  263.       API::GPPSA.call(tag, id, '', buffer, 255, file)
  264.       buffer.delete!("\0")
  265.     end
  266.     #-------------------------------------------------------------------------
  267.     # * Write String to Ini File
  268.     #-------------------------------------------------------------------------
  269.     def set_string(string, id, tag = 'Game', file = '.\\Game.ini')
  270.       API::WPPSA.call(tag, id, string, file)
  271.     end
  272.   end
  273.   #===========================================================================
  274.   # ** Module AWorks::Clipboard
  275.   #===========================================================================
  276.   module Clipboard
  277.     module_function
  278.     #-------------------------------------------------------------------------
  279.     # * Read Clipboard Data
  280.     #-------------------------------------------------------------------------
  281.     def read
  282.       API::OpenClipboard.call(0)
  283.       data = API::GetClipboardData.call(7)
  284.       API::CloseClipboard.call
  285.       return '' if data == 0
  286.       lp = API::GlobalLock.call(data)
  287.       len = API::GlobalSize.call(data)
  288.       data2 = ' ' * (len - 1)
  289.       API::Memcpy.call(data2, lp, len)
  290.       API::GlobalUnlock.call(data)
  291.       data2
  292.     end
  293.     #-------------------------------------------------------------------------
  294.     # * Write Data to Clipboard
  295.     #-------------------------------------------------------------------------
  296.     def write(data)
  297.       API::OpenClipboard.call(0)
  298.       API::EmptyClipboard.call
  299.       set_data = API::GlobalAlloc.call(66, data.length + 1)
  300.       len = [data.size + 1, API::GlobalSize.call(set_data)].min
  301.       lp = API::GlobalLock.call(set_data)
  302.       API::Memcpy.call(lp, "#{data}", len)
  303.       API::GlobalUnlock.call(set_data)
  304.       API::SetClipboardData.call(7, set_data)
  305.       API::CloseClipboard.call
  306.     end
  307.     #-------------------------------------------------------------------------
  308.     # * Clear Clipboard Data
  309.     #-------------------------------------------------------------------------
  310.     def empty
  311.       API::EmptyClipboard.call
  312.     end
  313.   end
  314.   #===========================================================================
  315.   # ** Module AWorks::Registry
  316.   #===========================================================================
  317.   module Registry
  318.     module_function
  319.     T_AUTO = -1
  320.     T_SZ = 1
  321.     T_BINARY = 3
  322.     T_DWORD = 4
  323.     HKEYS = {'HKEY_CLASSES_ROOT' => 0x80000000,'HKEY_CURRENT_USER' =>0x80000001,
  324.       'HKEY_LOCAL_MACHINE' => 0x80000002, 'HKEY_USERS' => 0x80000003,
  325.       'HKEY_CURRENT_CONFIG' => 0x80000005}
  326.     #-------------------------------------------------------------------------
  327.     # * Read a Value
  328.     #-------------------------------------------------------------------------
  329.     def read_value(key, entry)
  330.       key.sub!(/(.*?)\\/, '')
  331.       if !HKEYS[$1].nil?
  332.         hkey = HKEYS[$1]
  333.       else
  334.         return nil
  335.       end
  336.       opened, type, size = [0].pack('V'), [0].pack('V'), [0].pack('V')
  337.       API::RegOpenKeyExA.call(hkey, key, 0, 131097, opened)
  338.       opened = (opened + [0].pack('V')).unpack('V')[0]
  339.       API::RegQueryValueExA.call(opened, entry, 0, type, 0, size)
  340.       data = ' ' * (size + [0].pack('V')).unpack('V')[0]
  341.       API::RegQueryValueExA.call(opened, entry, 0, type, data, size)
  342.       API::RegCloseKey.call(opened)
  343.       data = data[0, (size + [0].pack('V')).unpack('V')[0]]
  344.       type = (type += [0].pack('V')).unpack('V')[0]
  345.       case type
  346.       when 1
  347.         data.chop
  348.       when 2
  349.         data.chop.gsub(/%([^%]+)%/) { ENV[$1] || $& }
  350.       when 3
  351.         data
  352.       when 4
  353.         (data += [0].pack('V')).unpack('V')[0]
  354.       when 5
  355.         data.unpack('N')[0]
  356.       when 7
  357.         data.split(/\0/)
  358.       when 11
  359.         (data.unpack('VV')[1] << 32) | data[0]
  360.       else
  361.         nil
  362.       end
  363.     end
  364.     #-------------------------------------------------------------------------
  365.     # * Enum Keys
  366.     #-------------------------------------------------------------------------
  367.     def enum_keys(key)
  368.       key.sub!(/(.*?)\\/, '')
  369.       if !HKEYS[$1].nil?
  370.         hkey = HKEYS[$1]
  371.       else
  372.         return nil
  373.       end
  374.       index, keys, opened = 0, [], [0].pack('V')
  375.       API::RegOpenKeyExA.call(hkey, key, 0, 131097, opened)
  376.       opened = (opened + [0].pack('V')).unpack('V')[0]
  377.       loop do
  378.         name = ' ' * 514
  379.         size = [514].pack('V')
  380.         result = API::RegEnumKeyExA.call(opened, index, name, size, 0, 0, 0, 0)
  381.         if result == 0
  382.           keys.push(name[0, (size += [0].pack('V')).unpack('V')[0]])
  383.           index += 1
  384.         else
  385.           break
  386.         end
  387.       end
  388.       API::RegCloseKey.call(opened)
  389.       keys
  390.     end
  391.     #-------------------------------------------------------------------------
  392.     # * Enum Values
  393.     #-------------------------------------------------------------------------
  394.     def enum_values(key)
  395.       key.sub!(/(.*?)\\/, '')
  396.       if !HKEYS[$1].nil?
  397.         hkey = HKEYS[$1]
  398.       else
  399.         return nil
  400.       end
  401.       index, entries, opened = 0, [], [0].pack('V')
  402.       API::RegOpenKeyExA.call(hkey, key, 0, 131097, opened)
  403.       opened = (opened + [0].pack('V')).unpack('V')[0]
  404.       loop do
  405.         name = ' ' * 514
  406.         size = [514].pack('V')
  407.         result = API::RegEnumValueA.call(opened, index, name, size, 0, 0, 0, 0)
  408.         if result == 0
  409.           entries.push(name[0, (size += [0].pack('V')).unpack('V')[0]])
  410.           index += 1
  411.         else
  412.           break
  413.         end
  414.       end
  415.       API::RegCloseKey.call(opened)
  416.       entries
  417.     end
  418.     #-------------------------------------------------------------------------
  419.     # * Create Key
  420.     #-------------------------------------------------------------------------
  421.     def create_key(key)
  422.       key.sub!(/(.*?)\\/, '')
  423.       if !HKEYS[$1].nil?
  424.         hkey = HKEYS[$1]
  425.       else
  426.         return nil
  427.       end
  428.       result = [0].pack('V')
  429.       disp = [0].pack('V')
  430.       API::RegCreateKeyExA.call(hkey, key, 0, 0, 0, 131135, 0, result, disp)
  431.       if [disp + [0].pack('V')].unpack('V')[0] == 2
  432.         API::RegCloseKey.call(result)
  433.       end
  434.     end
  435.     #-------------------------------------------------------------------------
  436.     # * Set Value
  437.     #-------------------------------------------------------------------------
  438.     def set_value(key, value, type = T_AUTO)
  439.       key.sub!(/(.*?)\\/, '')
  440.       if !HKEYS[$1].nil?
  441.         hkey = HKEYS[$1]
  442.       else
  443.         return nil
  444.       end
  445.       if type == T_AUTO
  446.         if value.is_a?(Numeric)
  447.           type = T_DWORD
  448.         else
  449.           type = T_SZ
  450.           value = value.to_s
  451.         end
  452.       end
  453.       case type
  454.       when 1, 2
  455.         value = value.to_s + "\0"
  456.       when 7
  457.         value = value.to_a.join("\0") + "\0\0"
  458.       when 3
  459.         value = value.to_s
  460.       when 4
  461.         value = [value.to_i].pack('V')
  462.       when 5
  463.         value = [value.to_i].pack('N')
  464.       when 11
  465.         value = [value.to_i & 0xFFFFFFFF, value.to_i >> 32 ].pack('VV')
  466.       end
  467.       API::RegSetValueExA.call(hkey, key, 0, type, value, value.length)
  468.     end
  469.     #-------------------------------------------------------------------------
  470.     # * Delete Key
  471.     #-------------------------------------------------------------------------
  472.     def delete_key(key, recursive = false)
  473.       skey = key.sub(/(.*?)\\/, '')
  474.       if !HKEYS[$1].nil?
  475.         hkey = HKEYS[$1]
  476.       else
  477.         return nil
  478.       end
  479.       if recursive
  480.        
  481.       else
  482.         API::RegDeleteKey.call(hkey, key)
  483.       end
  484.     end
  485.     #-------------------------------------------------------------------------
  486.     # * Delete Value
  487.     #-------------------------------------------------------------------------
  488.     def delete_value(key)
  489.       key.sub!(/(.*?)\\/, '')
  490.       if !HKEYS[$1].nil?
  491.         hkey = HKEYS[$1]
  492.       else
  493.         return nil
  494.       end
  495.       API::RegDeleteValue.call(hkey, key)
  496.     end
  497.   end
  498.   #---------------------------------------------------------------------------
  499.   # * Variables Declaration
  500.   #---------------------------------------------------------------------------
  501.   ASCII_TABLE = {1=>'☺',2=>'☻',3=>'♥',4=>'♦',5=>'♣',6=>'♠',7=>'•',8=>'◘',9=>'○',
  502.   10=>'◙',11=>'♂',12=>'♀',13=>'♪',14=>'♫',15=>'¤',16=>'►',17=>'◄',18=>'↕',
  503.   19=>'‼',20=>'¶',21=>'§',22=>'▬',23=>'↨',24=>'↑',25=>'↓',26=>'→',27=>'←',
  504.   28=>'∟',29=>'↔',30=>'▲',31=>'▼',32=>' ',33=>'!',34=>'"',35=>'#',36=>'$',
  505.   37=>'%',38=>'&',39=>"'",40=>'(',41=>')',42=>'*',43=>'+',44=>',',45=>'-',
  506.   46=>'.',47=>'/',48=>'0',49=>'1',50=>'2',51=>'3',52=>'4',53=>'5',54=>'6',
  507.   55=>'7',56=>'8',57=>'9',58=>':',59=>';',60=>'<',61=>'=',62=>'>',63=>'?',
  508.   64=>'@',65=>'A',66=>'B',67=>'C',68=>'D',69=>'E',70=>'F',71=>'G',72=>'H',
  509.   73=>'I',74=>'J',75=>'K',76=>'L',77=>'M',78=>'N',79=>'O',80=>'P',81=>'Q',
  510.   82=>'R',83=>'S',84=>'T',85=>'U',86=>'V',87=>'W',88=>'X',89=>'Y',90=>'Z',
  511.   91=>'[',92=>'\\',93=>']',94=>'^',95=>'_',96=>'`',97=>'a',98=>'b',99=>'c',
  512.   100=>'d',101=>'e',102=>'f',103=>'g',104=>'h',105=>'i',106=>'j',107=>'k',
  513.   108=>'l',109=>'m',110=>'n',111=>'o',112=>'p',113=>'q',114=>'r',115=>'s',
  514.   116=>'t',117=>'u',118=>'v',119=>'w',120=>'x',121=>'y',122=>'z',123=>'{',
  515.   124=>'|',125=>'}',126=>'~',127=>'¦',128=>'Ç',129=>'ü',130=>'é',131=>'â',
  516.   132=>'ä',133=>'à',134=>'å',135=>'ç',136=>'ê',137=>'ë',138=>'è',139=>'ï',
  517.   140=>'î',141=>'ì',142=>'Ä',143=>'Å',144=>'É',145=>'æ',146=>'Æ',147=>'ô',
  518.   148=>'ö',149=>'ò',150=>'û',151=>'ù',152=>'ÿ',153=>'Ö',154=>'Ü',155=>'ø',
  519.   156=>'£',157=>'Ø',158=>'×',159=>'ƒ',160=>'á',161=>'í',162=>'ó',163=>'ú',
  520.   164=>'ñ',165=>'Ñ',166=>'ª',167=>'º',168=>'¿',169=>'®',170=>'¬',171=>'½',
  521.   172=>'¼',173=>'¡',174=>'«',175=>'»',176=>'¦',177=>'¦',178=>'¦',179=>'¦',
  522.   180=>'¦',181=>'Á',182=>'Â',183=>'À',184=>'©',185=>'¦',186=>'¦',187=>'+',
  523.   188=>'+',189=>'¢',190=>'¥',191=>'+',192=>'+',193=>'-',194=>'-',195=>'+',
  524.   196=>'-',197=>'+',198=>'ã',199=>'Ã',200=>'+',201=>'+',202=>'-',203=>'-',
  525.   204=>'¦',205=>'-',206=>'+',207=>'¤',208=>'ð',209=>'Ð',210=>'Ê',211=>'Ë',
  526.   212=>'È',213=>'i',214=>'Í',215=>'Î',216=>'Ï',217=>'+',218=>'+',219=>'¦',
  527.   220=>'_',221=>'¦',222=>'Ì',223=>'¯',224=>'Ó',225=>'ß',226=>'Ô',227=>'Ò',
  528.   228=>'õ',229=>'Õ',230=>'µ',231=>'þ',232=>'Þ',233=>'Ú',234=>'Û',235=>'Ù',
  529.   236=>'ý',237=>'Ý',238=>'¯',239=>'´',240=>'­',241=>'±',242=>'=',243=>'¾',
  530.   244=>'¶',245=>'§',246=>'÷',247=>'¸',248=>'°',249=>'¨',250=>'·',251=>'¹',
  531.   252=>'³',253=>'²',254=>'¦',255=>' '}
  532.   TRANSLATE_TABLE ={"\225"=>'•',"\244"=>'¤',"\266"=>'¶',"\247"=>'§',"\307"=>'Ç',
  533.   "\374"=>'ü',"\351"=>'é',"\342"=>'â',"\344"=>'ä',"\340"=>'à',"\345"=>'å',
  534.   "\347"=>'ç',"\352"=>'ê',"\353"=>'ë',"\350"=>'è',"\357"=>'ï',"\356"=>'î',
  535.   "\354"=>'ì',"\304"=>'Ä',"\305"=>'Å',"\311"=>'É',"\346"=>'æ',"\306"=>'Æ',
  536.   "\364"=>'ô',"\366"=>'ö',"\362"=>'ò',"\373"=>'û',"\371"=>'ù',"\377"=>'ÿ',
  537.   "\326"=>'Ö',"\334"=>'Ü',"\370"=>'ø',"\243"=>'£',"\330"=>'Ø',"\327"=>'×',
  538.   "\203"=>'ƒ',"\341"=>'á',"\355"=>'í',"\363"=>'ó',"\372"=>'ú',"\361"=>'ñ',
  539.   "\321"=>'Ñ',"\252"=>'ª',"\272"=>'º',"\277"=>'¿',"\256"=>'®',"\254"=>'¬',
  540.   "\275"=>'½',"\274"=>'¼',"\241"=>'¡',"\253"=>'«',"\273"=>'»',"\301"=>'Á',
  541.   "\302"=>'Â',"\300"=>'À',"\251"=>'©',"\242"=>'¢',"\245"=>'¥',"\343"=>'ã',
  542.   "\303"=>'Ã',"\244"=>'¤',"\360"=>'ð',"\320"=>'Ð',"\312"=>'Ê',"\313"=>'Ë',
  543.   "\310"=>'È',"\315"=>'Í',"\316"=>'Î',"\317"=>'Ï',"\314"=>'Ì',"\257"=>'¯',
  544.   "\323"=>'Ó',"\337"=>'ß',"\324"=>'Ô',"\322"=>'Ò',"\365"=>'õ',"\325"=>'Õ',
  545.   "\265"=>'µ',"\376"=>'þ',"\336"=>'Þ',"\332"=>'Ú',"\333"=>'Û',"\331"=>'Ù',
  546.   "\375"=>'ý',"\335"=>'Ý',"\257"=>'¯',"\264"=>'´',"\255"=>'­',"\261"=>'±',
  547.   "\276"=>'¾',"\266"=>'¶',"\247"=>'§',"\367"=>'÷',"\270"=>'¸',"\260"=>'°',
  548.   "\250"=>'¨',"\267"=>'·',"\271"=>'¹',"\263"=>'³',"\262"=>'²',"\240"=>' ',
  549.   "\t"=>'        ',"\200"=>'€'}
  550.   #---------------------------------------------------------------------------
  551.   # * Get Fonts Names
  552.   #---------------------------------------------------------------------------
  553.   def get_fonts_names
  554.     key = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\#{ENV['OS'].sub(/_/,' ')}\\"
  555.     fonts = Registry.enum_values(key + 'CurrentVersion\\Fonts')
  556.     fonts.each_index {|f|
  557.       fonts[f].sub!(' (TrueType)', '')
  558.       fonts[f] = nil if !Font.exist?(fonts[f])
  559.     }.delete(nil)
  560.     fonts
  561.   end
  562.   #---------------------------------------------------------------------------
  563.   # * Get RGSS Player HWND index
  564.   #---------------------------------------------------------------------------
  565.   def get_hwnd
  566.     if @hwnd.nil?
  567.       @hwnd = API::FindWindowA.call('RGSS Player', Ini.get_string('Title'))
  568.     else
  569.       @hwnd
  570.     end
  571.   end
  572.   #---------------------------------------------------------------------------
  573.   # * Get RGSS DLL
  574.   #---------------------------------------------------------------------------
  575.   def get_rgssdll
  576.     Ini.get_string('Library')
  577.   end
  578.   #-------------------------------------------------------------------------
  579.   # * Get RTP Path
  580.   #-------------------------------------------------------------------------
  581.   def get_rtp_path(rtp = 0)
  582.     API::RGSSGetPathWithRTP.call(API::RGSSGetRTPPath.cal(rtp))
  583.   end
  584.   #-------------------------------------------------------------------------
  585.   # * Get Mouse Swap Buttons Flag
  586.   #-------------------------------------------------------------------------
  587.   def get_mouse_swap_buttons_flag
  588.     result = API::SwapMouseButton.call(0)
  589.     if result == 0
  590.       API::SwapMouseButton.call(0)
  591.       0
  592.     else
  593.       API::SwapMouseButton.call(1)
  594.       1
  595.     end
  596.   end
  597.   #---------------------------------------------------------------------------
  598.   # * Initialize AWorks Library
  599.   #---------------------------------------------------------------------------
  600.   API::Initialize.call(self.get_hwnd)
  601. end
  602.  
  603. #=============================================================================
  604. # ** Class Array
  605. #=============================================================================
  606. class Array
  607.   #---------------------------------------------------------------------------
  608.   # * Recursive Clone
  609.   #---------------------------------------------------------------------------
  610.   def recursive_clone
  611.     clon = self.clone
  612.     clon.each_index do |i|
  613.       clon[i] = clon[i].recursive_clone rescue clon[i].clone rescue clon[i]
  614.     end
  615.     clon
  616.   end
  617.   alias_method(:rclone, :recursive_clone)
  618.   #---------------------------------------------------------------------------
  619.   # * To Hash
  620.   #---------------------------------------------------------------------------
  621.   def to_hash
  622.     hash = Hash.new
  623.     self.each_index {|i| hash[i] = self[i]}
  624.     hash
  625.   end
  626.   #---------------------------------------------------------------------------
  627.   # * Indexes Reversed
  628.   #---------------------------------------------------------------------------
  629.   def rindexes(*values)
  630.     array = Array.new
  631.     self.each_index {|i| array.push(i) if values.include?(self[i])}
  632.     array
  633.   end
  634.   #---------------------------------------------------------------------------
  635.   # * Deep
  636.   #---------------------------------------------------------------------------
  637.   def deep(deep = -1)
  638.     tmp_deep = deep + 1
  639.     deep = tmp_deep
  640.     self.each do |i|
  641.       deep = i.deep(tmp_deep) if deep < i.deep(tmp_deep) rescue nil
  642.     end
  643.     deep
  644.   end
  645.   #---------------------------------------------------------------------------
  646.   # * Recursive Flatten
  647.   #---------------------------------------------------------------------------
  648.   def recursive_flatten
  649.     array = self.recursive_clone
  650.     array.each_index do |i|
  651.       array[i] = array[i].to_a if array[i].is_a?(Hash)
  652.       array[i] = array[i].recursive_flatten rescue array[i]
  653.     end
  654.     array.flatten!
  655.     array
  656.   end
  657.   alias_method(:rflatten, :recursive_flatten)
  658.   #---------------------------------------------------------------------------
  659.   # * Destructive Recursive Flatten
  660.   #---------------------------------------------------------------------------
  661.   def recursive_flatten!
  662.     self.each_index do |i|
  663.       self[i] = self[i].to_a if self[i].is_a?(Hash)
  664.       self[i] = self[i].recursive_flatten! rescue self[i]
  665.     end
  666.     self.flatten!
  667.     self
  668.   end
  669.   alias_method(:rflatten!, :recursive_flatten!)
  670.   #---------------------------------------------------------------------------
  671.   # * Insert
  672.   #---------------------------------------------------------------------------
  673.   def insert(i, obj)
  674.     if i == 0
  675.       obj + self
  676.     elsif i == self.size
  677.       self + obj
  678.     elsif self.size < i
  679.       self + Array.new(i - self.size) +  obj
  680.     else
  681.       self[0, i - 1] + obj + self[i, self.size]
  682.     end
  683.   end
  684. end
  685.  
  686. #=============================================================================
  687. # ** Class Hash
  688. #=============================================================================
  689. class Hash
  690.   #---------------------------------------------------------------------------
  691.   # * Recursive Clone
  692.   #---------------------------------------------------------------------------
  693.   def recursive_clone
  694.     clon = self.clone
  695.     clon.each_key do |i|
  696.       clon[i] = clon[i].recursive_clone rescue clon[i].clone rescue clon[i]
  697.     end
  698.     clon
  699.   end
  700.   alias_method(:rclone, :recursive_clone)
  701.   #---------------------------------------------------------------------------
  702.   # * Deep
  703.   #---------------------------------------------------------------------------
  704.   def deep(deep = -1)
  705.     tmp_deep = deep + 1
  706.     key_deep = tmp_deep
  707.     value_deep = tmp_deep
  708.     self.each do |k, v|
  709.       key_deep = k.deep(tmp_deep) if key_deep < k.deep(tmp_deep) rescue nil
  710.       value_deep = v.deep(tmp_deep) if value_deep < v.deep(tmp_deep) rescue nil
  711.     end
  712.     if key_deep > value_deep
  713.       key_deep
  714.     else
  715.       value_deep
  716.     end
  717.   end
  718.   #---------------------------------------------------------------------------
  719.   # * To Array, combining Key and Value
  720.   #---------------------------------------------------------------------------
  721.   def fusion
  722.     array = self.sort
  723.     array.each_index do|i|
  724.       array[i] = array[i][0] + array[i][1] rescue array[i]
  725.     end
  726.     array
  727.   end
  728. end
  729.  
  730. #=============================================================================
  731. # ** Nil Class
  732. #=============================================================================
  733. class NilClass
  734.   def clone;end
  735. end
  736.  
  737. #=============================================================================
  738. # ** Module Math
  739. #=============================================================================
  740. module Math
  741.   #---------------------------------------------------------------------------
  742.   # * Radians to degrees
  743.   #---------------------------------------------------------------------------
  744.   def Math.radian2degree(r)
  745.     r * 180 / Math::PI
  746.   end
  747.   #---------------------------------------------------------------------------
  748.   # * Degrees to radians
  749.   #---------------------------------------------------------------------------
  750.   def Math.degree2radian(g)
  751.     g * Math::PI / 180
  752.   end
  753.   #---------------------------------------------------------------------------
  754.   # * Quick Cosine and Sine
  755.   #---------------------------------------------------------------------------
  756.   @cos = {}
  757.   @sin = {}
  758.   for i in 0..360
  759.     @cos[i] = Math.cos(Math.degree2radian(i - 90))
  760.     @sin[i] = Math.sin(Math.degree2radian(i - 90))
  761.   end
  762.   def Math.quick_cos(i)
  763.     @cos[i.round % 360]
  764.   end
  765.   def Math.quick_sin(i)
  766.     @sin[i.round % 360]
  767.   end
  768. end
  769.  
  770. #=============================================================================
  771. # ** Class String
  772. #=============================================================================
  773. class String
  774.   #---------------------------------------------------------------------------
  775.   # * Pixel Width of String
  776.   #---------------------------------------------------------------------------
  777.   def pixel_width(*args)
  778.     bitmap = Bitmap.new(1, 1)
  779.     if args[0].is_a?(Font)
  780.       bitmap.font.size = args[0].size
  781.       bitmap.font.name = args[0].name
  782.       bitmap.font.bold = args[0].bold
  783.     else
  784.       bitmap.font.size = args[0].nil? ? Font.default_size : args[0]
  785.       bitmap.font.name = args[1].nil? ? Font.default_name : args[1]
  786.       bitmap.font.bold = args[2].nil? ? Font.default_bold : args[2]
  787.     end
  788.     pix = bitmap.text_size(self).width
  789.     bitmap.dispose
  790.     pix
  791.   end
  792.   #---------------------------------------------------------------------------
  793.   # * Pixel Height of String
  794.   #---------------------------------------------------------------------------
  795.   def pixel_height(*args)
  796.     bitmap = Bitmap.new(1, 1)
  797.     if args[0].is_a?(Font)
  798.       bitmap.font.size = args[0].size
  799.       bitmap.font.name = args[0].name
  800.       bitmap.font.bold = args[0].bold
  801.     else
  802.       bitmap.font.size = args[0].nil? ? Font.default_size : args[0]
  803.       bitmap.font.name = args[1].nil? ? Font.default_name : args[1]
  804.       bitmap.font.bold = args[2].nil? ? Font.default_bold : args[2]
  805.     end
  806.     pix = bitmap.text_size(self).height
  807.     bitmap.dispose
  808.     pix
  809.   end
  810.   #---------------------------------------------------------------------------
  811.   # * Cut String to Strings with less or equal size than Width
  812.   #---------------------------------------------------------------------------
  813.   def width_slice(width, *args)
  814.     bitmap = Bitmap.new(1, 1)
  815.     if args[0].is_a?(Font)
  816.       bitmap.font.size = args[0].size
  817.       bitmap.font.name = args[0].name
  818.       bitmap.font.bold = args[0].bold
  819.     else
  820.       bitmap.font.size = args[0].nil? ? Font.default_size : args[0]
  821.       bitmap.font.name = args[1].nil? ? Font.default_name : args[1]
  822.       bitmap.font.bold = args[2].nil? ? Font.default_bold : args[2]
  823.     end
  824.     result = ['']
  825.     txt = self.clone.scan(/./)
  826.     txt.each_index do |i|
  827.       if txt[i].is_a?(String)
  828.         txt[i] = [txt[i], bitmap.text_size(txt[i]).width]
  829.         if txt.include?(txt[i][0])
  830.           txt.collect! do |e|
  831.             e == txt[i][0] ? txt[i] : e
  832.           end
  833.         end
  834.       end
  835.     end
  836.     bitmap.dispose
  837.     act_width = 0
  838.     loop do
  839.       if txt.size > 0
  840.         tmp_width = act_width + txt[0][1]
  841.         tmp_width += txt[1][1] if txt.size > 1
  842.         if width >= act_width + txt[0][1]
  843.           result[result.size - 1] += txt[0][0]
  844.           act_width += txt[0][1]
  845.           txt.delete_at(0)
  846.         else
  847.           result.push('')
  848.           act_width = 0
  849.         end
  850.       else
  851.         break
  852.       end
  853.     end
  854.     result
  855.   end
  856.   #---------------------------------------------------------------------------
  857.   # * Max Font Size for Width
  858.   #---------------------------------------------------------------------------
  859.   def font_size_width(width, *args)
  860.     bitmap = Bitmap.new(1, 1)
  861.     if args[0].is_a?(Font)
  862.       bitmap.font.name = args[0].name
  863.       bitmap.font.bold = args[0].bold
  864.     else
  865.       bitmap.font.name = args[0].nil? ? Font.default_name : args[0]
  866.       bitmap.font.bold = args[1].nil? ? Font.default_bold : args[1]
  867.     end
  868.     size = 6
  869.     for i in 6..96
  870.       bitmap.font.size = i
  871.       size = i if bitmap.text_size(self).width <= width
  872.     end
  873.     bitmap.dispose
  874.     size
  875.   end
  876.   #---------------------------------------------------------------------------
  877.   # * Max Font Size for Height
  878.   #---------------------------------------------------------------------------
  879.   def font_size_height(height, *args)
  880.     bitmap = Bitmap.new(1, 1)
  881.     if args[0].is_a?(Font)
  882.       bitmap.font.name = args[0].name
  883.       bitmap.font.bold = args[0].bold
  884.     else
  885.       bitmap.font.name = args[0].nil? ? Font.default_name : args[0]
  886.       bitmap.font.bold = args[1].nil? ? Font.default_bold : args[1]
  887.     end
  888.     size = 6
  889.     for i in 6..96
  890.       bitmap.font.size = i
  891.       size = i if bitmap.text_size(self).height <= height
  892.     end
  893.     bitmap.dispose
  894.     size
  895.   end
  896.   #---------------------------------------------------------------------------
  897.   # * Max Font Size for Width & Height
  898.   #---------------------------------------------------------------------------
  899.   def font_size(width, height, *args)
  900.     bitmap = Bitmap.new(1, 1)
  901.     if args[0].is_a?(Font)
  902.       bitmap.font.name = args[0].name
  903.       bitmap.font.bold = args[0].bold
  904.     else
  905.       bitmap.font.name = args[0].nil? ? Font.default_name : args[0]
  906.       bitmap.font.bold = args[1].nil? ? Font.default_bold : args[1]
  907.     end
  908.     size = 6
  909.     for i in 6..96
  910.       bitmap.font.size = i
  911.       if bitmap.text_size(self).width <= width and
  912.          bitmap.text_size(self).height <= height
  913.         size = i
  914.       end
  915.     end
  916.     bitmap.dispose
  917.     size
  918.   end
  919.   #---------------------------------------------------------------------------
  920.   # * Transform the undrawable Characters
  921.   #---------------------------------------------------------------------------
  922.   def trans_undrawable_chr(font = Font.new, chr = '?')
  923.     bmp = Bitmap.new(1, 1)
  924.     bmp.font = font
  925.     txt = ''
  926.     scan(/./).each {|c| txt += (bmp.text_size(c).width == 0) ? chr : c}
  927.     txt
  928.   end
  929. end
  930.  
  931. #=============================================================================
  932. # ** Class Bitmap
  933. #=============================================================================
  934. class Bitmap
  935.   #---------------------------------------------------------------------------
  936.   # * Draw Text AWorks
  937.   #---------------------------------------------------------------------------
  938.   def adraw_text(*args)
  939.     if args.size.between?(2,3)
  940.       args[2] = 0 if args.size == 2
  941.       args[4] = args[1]
  942.       args[5] = args[2]
  943.       args[1] = args[0].y
  944.       args[2] = args[0].width
  945.       args[3] = args[0].height
  946.       args[0] = args[0].x
  947.     elsif args.size == 5
  948.       args[5] = 0
  949.     end
  950.     if args[4].is_a?(Array)
  951.       args[4] = args[4].rflatten
  952.       args[4].each do |t|
  953.         t = t.nil? ? 'nil' : "#{t}"
  954.         width = args[2].nil? ? self.text_size(t).width : args[2]
  955.         height = args[3].nil? ? self.text_size('0').height : args[3]
  956.         self.draw_text(args[0], args[1], width, height, t, args[5])
  957.         args[1] += height
  958.       end
  959.       return
  960.     elsif args[4].is_a?(Hash)
  961.       args[4] = args[4].fusion
  962.       args[4] = args[4].rflatten
  963.       args[4].each do |t|
  964.         t = t.nil? ? 'nil' : "#{t}"
  965.         width = args[2].nil? ? self.text_size(t).width : args[2]
  966.         height = args[3].nil? ? self.text_size('0').height : args[3]
  967.         self.draw_text(args[0], args[1], width, height, t, args[5])
  968.         args[1] += height
  969.       end
  970.       return
  971.     elsif !args[4].is_a?(String)
  972.       if args[4].to_s == ''
  973.         args[4] = args[4].inspect
  974.       else
  975.         args[4] = args[4].to_s
  976.       end
  977.     end
  978.     args[2] = args[2].nil? ? self.text_size(args[4]).width + 2 : args[2]
  979.     args[3] = args[3].nil? ? self.text_size('0').height + 2 : args[3]
  980.     draw_text(*args)
  981.   end
  982. end
  983.  
  984. #=============================================================================
  985. # ** Module Graphics
  986. #=============================================================================
  987. module Graphics
  988.   @auto_update = []
  989.   class << self
  990.     #-------------------------------------------------------------------------
  991.     # * Object method auto-call
  992.     #-------------------------------------------------------------------------
  993.     unless self.method_defined?(:alibrary_update)
  994.       alias_method(:alibrary_update, :update)
  995.     end
  996.     attr_accessor(:auto_update)
  997.     def update
  998.       alibrary_update
  999.       @auto_update.each do |k|
  1000.         k[0].method(k[1]).call
  1001.         if !k[2].nil?
  1002.           k[2] -= 1
  1003.           k = nil if k[2] <= 0
  1004.         end
  1005.       end
  1006.       @auto_update.delete_if {|x| if !x[2].nil? : x[2] <= 0 else false end}
  1007.     end
  1008.   end
  1009. end
Advertisement
Add Comment
Please, Sign In to add comment