Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #==============================================================================
  2. # ** Victor Engine - SFont
  3. #------------------------------------------------------------------------------
  4. # Author : Victor Sant
  5. #
  6. # Aditional Credit :
  7. #   - RubyGame  (Original Concept)
  8. #   - Trickster (RubyGame SFont conversion to RPG Maker)
  9. #
  10. # Version History:
  11. #  v 1.00 - 2011.12.21 > First release
  12. #  v 1.01 - 2011.12.21 > Change on the configuration module
  13. #------------------------------------------------------------------------------
  14. #  This script allows the use of SFonts. SFonts is a image file that replaces
  15. # the normal font.
  16. #
  17. # What are SFonts? (from http://rubygame.org/docs/)
  18. # SFont is a type of bitmapped font, which is loaded from an image file with
  19. # a meaningful top row of pixels, and the font itself below that. The top row
  20. # provides information about what parts of of the lower area contains font
  21. # data, and which parts are empty. The image file should contain all of the
  22. # glyphs on one row, with the colorkey color at the bottom-left pixel and the
  23. # "skip" color at the top-left pixel.
  24. #
  25. #------------------------------------------------------------------------------
  26. # Compatibility
  27. #   Requires the script 'Victor Engine - Basic Module'
  28. #
  29. # * Alias methods (Default)
  30. #   class Bitmap
  31. #     def draw_text
  32. #     def text_size(str)
  33. #
  34. #   class << DataManager
  35. #     def create_game_objects
  36. #
  37. #   class Sprite_Timer < Sprite
  38. #     def create_bitmap
  39. #
  40. #   class Window_Base < Window
  41. #     def create_contents
  42. #     def text_color(n)
  43. #
  44. #------------------------------------------------------------------------------
  45. # Instructions:
  46. #  To instal the script, open you script editor and paste this script on
  47. #  a new section on bellow the Materials section. This script must also
  48. #  be bellow the script 'Victor Engine - Basic'
  49. #  The SFonts must be placed on the folder "Graphics/SFonts". Create a folder
  50. #  named "SFonts" on the Graphics folder.
  51. #
  52. #------------------------------------------------------------------------------
  53. # Additional instructions:
  54. #
  55. #   On the SFont, the first pixel on the top left corner is considered the
  56. #   "skip color", so, anywhere that this color appears on the first row
  57. #   the part will be skiped, so use it to define the limit from each
  58. #   character. At first it might be hard to get how it work.
  59. #
  60. #   If the constant ALL_SFONT is true, all fonts in all windows will be
  61. #   replaced with SFonts, if false, you will need to add them manually
  62. #   in the code (not recomended for begginers).
  63. #
  64. #   The method "text_color(n)" from Window_Base can be used to change
  65. #   the SFont, where n is the SFont index on the SFONT_NAMES hash.
  66. #   Text on independent bitmaps (outside windows) will need to be changed
  67. #   manually on the code.
  68. #
  69. #   The system fonts can be changed on the Window_Base class, on the
  70. #   following methods. Replace the number in text_color(x) with the index
  71. #   of the color SFont on the SFONT_NAMES hash.
  72. #    def normal_color;      text_color(0);   end;
  73. #    def system_color;      text_color(16);  end;
  74. #    def crisis_color;      text_color(17);  end;
  75. #    def knockout_color;    text_color(18);  end;
  76. #    def mp_cost_color;     text_color(23);  end;
  77. #    def power_up_color;    text_color(24);  end;
  78. #    def power_down_color;  text_color(25);  end;
  79. #    def tp_cost_color;     text_color(29);  end;
  80. #   If you don't feel like changing the default scripts, just create a new
  81. #   script on the Materials section for the class Window_Base, add these
  82. #   medthods, and change the numeric values according to the ones
  83. #   set on SFONT_NAMES hash.
  84. #
  85. #==============================================================================
  86.  
  87. #==============================================================================
  88. # ** Victor Engine
  89. #------------------------------------------------------------------------------
  90. #   Setting module for the Victor Engine
  91. #==============================================================================
  92.  
  93. module Victor_Engine
  94.   #--------------------------------------------------------------------------
  95.   # * Set the use of SFonts for all windows
  96.   #    If true all windows will use SFonts automatically, if false
  97.   #    you must set them manually
  98.   #--------------------------------------------------------------------------
  99.   VE_ALL_SFONT = true
  100.   #--------------------------------------------------------------------------
  101.   # * Set SFonts
  102.   #    Set here the index and name of the SFonts that will be used, add how
  103.   #    many SFont names you want. Refer to the SFont bitmap with $sfont[index]
  104.   #    VE_SFONT_NAMES = { index => name, index => name, ... }
  105.   #--------------------------------------------------------------------------
  106.   VE_SFONT_NAMES = { 1 => "Arial White", 2 => "Arial Red", 3 => "Arial Blue",
  107.     4 => "Arial Green", 5 => "Arial Yellow", 6 => "Arial Gray" }
  108.   #--------------------------------------------------------------------------
  109.   # * Set the digit list
  110.   #    The order of the digits here define the order of of the digits on
  111.   #    the image file, make sure to follow the same order or the text
  112.   #    in game might be totally screwed.
  113.   #    You can add how many characters you want, even complex character.
  114.   #    the character here doesn't need to be exactly the same from the
  115.   #    image file so you can use special characters to show icons and such.
  116.   #--------------------------------------------------------------------------
  117.   VE_SFONT_DIGITS = ["!",'"',"#","$","%","&","'","(",")","*","+",",","-",".",
  118.   "/","0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?","@","A",
  119.   "B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T",
  120.   "U","V","W","X","Y","Z","[","\\","]","^","_","`","a","b","c","d","e","f","g",
  121.   "h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
  122.   "{","|","}","~"]
  123.   #--------------------------------------------------------------------------
  124.   # * required
  125.   #   This method checks for the existance of the basic module and other
  126.   #   VE scripts required for this script to work, don't edit this
  127.   #--------------------------------------------------------------------------
  128.   def self.required(name, req, version, type = nil)
  129.     if !$imported[:ve_basic_module]
  130.       msg = "The script '%s' requires the script\n"
  131.       msg += "'VE - Basic Module' v%s or higher above it to work properly\n"
  132.       msg += "Go to http://victorenginescripts.wordpress.com/ to download this script."
  133.       msgbox(sprintf(msg, self.script_name(name), version))
  134.       exit
  135.     else
  136.       self.required_script(name, req, version, type)
  137.     end
  138.   end
  139.   #--------------------------------------------------------------------------
  140.   # * script_name
  141.   #   Get the script name base on the imported value, don't edit this
  142.   #--------------------------------------------------------------------------
  143.   def self.script_name(name, ext = "VE")
  144.     name = name.to_s.gsub("_", " ").upcase.split
  145.     name.collect! {|char| char == ext ? "#{char} -" : char.capitalize }
  146.     name.join(" ")
  147.   end
  148. end
  149.  
  150. $imported ||= {}
  151. $imported[:ve_sfonts] = 1.01
  152. Victor_Engine.required(:ve_sfonts, :ve_basic_module, 1.00, :above)
  153.  
  154. #==============================================================================
  155. # ** Cache
  156. #------------------------------------------------------------------------------
  157. #  This module loads each of graphics, creates a Bitmap object, and retains it.
  158. # To speed up load times and conserve memory, this module holds the created
  159. # Bitmap object in the internal hash, allowing the program to return
  160. # preexisting objects when the same bitmap is requested again.
  161. #==============================================================================
  162.  
  163. module Cache
  164.   #--------------------------------------------------------------------------
  165.   # * New method: sfont
  166.   #--------------------------------------------------------------------------
  167.   def self.sfont(filename, hue = 0)
  168.     self.load_bitmap("Graphics/SFonts/", filename, hue)
  169.   end
  170. end
  171.  
  172. #==============================================================================
  173. # ** Bitmap
  174. #------------------------------------------------------------------------------
  175. #  This class handles and draws the bitmaps
  176. #==============================================================================
  177.  
  178. class Bitmap
  179.   #--------------------------------------------------------------------------
  180.   # * Public Instance Variables
  181.   #--------------------------------------------------------------------------
  182.   attr_accessor :sfont
  183.   #--------------------------------------------------------------------------
  184.   # * Alias method: draw_text
  185.   #--------------------------------------------------------------------------
  186.   alias :draw_text_ve_sfont :draw_text
  187.   def draw_text(*args)
  188.     sfont ? draw_sfont_text(*args) : draw_text_ve_sfont(*args)
  189.   end
  190.   #--------------------------------------------------------------------------
  191.   # * Alias method: draw_sfont_text
  192.   #--------------------------------------------------------------------------
  193.   def draw_sfont_text(*args)
  194.     is_rect = args[0].is_a?(Rect)
  195.     x      = is_rect ? args[0].x      : args[0]
  196.     y      = is_rect ? args[0].y      : args[1]
  197.     width  = is_rect ? args[0].width  : args[2]
  198.     height = is_rect ? args[0].height : args[3]
  199.     text   = is_rect ? args[1].to_s   : args[4].to_s
  200.     align  = is_rect ? args[2]        : args[5]
  201.     bitmap = sfont.draw_text(text)
  202.     x += width - bitmap.width       if align == 2
  203.     x += (width - bitmap.width) / 2 if align == 1
  204.     y += (height - bitmap.height) / 2 - 4
  205.     blt(x, y, bitmap, bitmap.rect)
  206.   end
  207.   #--------------------------------------------------------------------------
  208.   # * Alias method: text_size
  209.   #--------------------------------------------------------------------------
  210.   alias :text_size_ve_sfont :text_size
  211.   def text_size(text)
  212.     sfont ? sfont.text_size(text) : text_size_ve_sfont(text)
  213.   end
  214. end
  215.  
  216. #==============================================================================
  217. # ** DataManager
  218. #------------------------------------------------------------------------------
  219. #  This module handles the game and database objects used in game.
  220. # Almost all global variables are initialized on this module
  221. #==============================================================================
  222.  
  223. class << DataManager
  224.   #--------------------------------------------------------------------------
  225.   # * Alias method: create_game_objects
  226.   #--------------------------------------------------------------------------
  227.   alias :create_game_objects_ve_sfont :create_game_objects
  228.   def create_game_objects
  229.     create_game_objects_ve_sfont
  230.     create_sfonts
  231.   end
  232.   #--------------------------------------------------------------------------
  233.   # * New method: create_sfonts
  234.   #--------------------------------------------------------------------------
  235.   def create_sfonts
  236.     $sfont = {}
  237.     VE_SFONT_NAMES.keys.each {|i| $sfont[i] = SFont.new(VE_SFONT_NAMES[i]) }
  238.     $sfont.default = $sfont[$sfont.keys.sort.first]
  239.   end
  240. end
  241.  
  242. #==============================================================================
  243. # ** Sprite_Timer
  244. #------------------------------------------------------------------------------
  245. #  This sprite is used to display the timer. It observes the $game_system
  246. # and automatically changes sprite conditions.
  247. #==============================================================================
  248.  
  249. class Sprite_Timer < Sprite
  250.   #--------------------------------------------------------------------------
  251.   # * Alias method: create_bitmap
  252.   #--------------------------------------------------------------------------
  253.   alias :create_bitmap_ve_sfont :create_bitmap
  254.   def create_bitmap
  255.     create_bitmap_ve_sfont
  256.     self.bitmap.sfont = $sfont[0] if VE_ALL_SFONT
  257.   end
  258. end
  259.  
  260. #==============================================================================
  261. # ** Window_Base
  262. #------------------------------------------------------------------------------
  263. #  This is a superclass of all windows in the game.
  264. #==============================================================================
  265.  
  266. class Window_Base < Window
  267.   #--------------------------------------------------------------------------
  268.   # * Alias method: create_contents
  269.   #--------------------------------------------------------------------------
  270.   alias :create_contents_ve_sfont :create_contents
  271.   def create_contents
  272.     create_contents_ve_sfont
  273.     contents.sfont = $sfont[0] if VE_ALL_SFONT
  274.   end
  275.   #--------------------------------------------------------------------------
  276.   # * Alias method: text_color
  277.   #--------------------------------------------------------------------------
  278.   alias :text_color_ve_sfont :text_color
  279.   def text_color(n)
  280.     sfont(n)
  281.     text_color_ve_sfont(n)
  282.   end
  283.   #--------------------------------------------------------------------------
  284.   # * Alias method: change_color
  285.   #--------------------------------------------------------------------------
  286.   alias :change_color_ve_sfont :change_color
  287.   def change_color(color, enabled = true)
  288.     change_color_ve_sfont(color, enabled)
  289.     contents.sfont.alpha = enabled ? 255 : translucent_alpha
  290.   end
  291.   #--------------------------------------------------------------------------
  292.   # * New method: sfont
  293.   #--------------------------------------------------------------------------
  294.   def sfont(n)
  295.     return if !contents.sfont
  296.     contents.sfont = $sfont[n]
  297.   end
  298. end
  299.  
  300. #==============================================================================
  301. # ** SFont
  302. #------------------------------------------------------------------------------
  303. #  This class handles the SFonts
  304. #==============================================================================
  305.  
  306. class SFont
  307.   #--------------------------------------------------------------------------
  308.   # * Public Instance Variables
  309.   #--------------------------------------------------------------------------
  310.   attr_reader   :bitmap
  311.   attr_accessor :alpha
  312.   #--------------------------------------------------------------------------
  313.   # * initialize
  314.   #--------------------------------------------------------------------------
  315.   def initialize(name)
  316.     @bitmap = Cache.sfont(name)
  317.     @height = @bitmap.height + 4
  318.     @skip   = @bitmap.get_pixel(0, 0)
  319.     @digits = VE_SFONT_DIGITS
  320.     @alpha  = 255
  321.     @values = {}
  322.     @values[" "] = Rect.new(-12, 0, 12, @height)
  323.     setup_digits
  324.     @values.default = @values[" "]
  325.   end
  326.   #--------------------------------------------------------------------------
  327.   # * setup_digits
  328.   #--------------------------------------------------------------------------
  329.   def setup_digits
  330.     x1 = x2 = 0
  331.     @digits.each do |digit|
  332.       x1 += 1 while @bitmap.get_pixel(x1, 0) == @skip && x1 < @bitmap.width
  333.       x2 = x1    
  334.       x2 += 1 while @bitmap.get_pixel(x2, 0) != @skip && x2 < @bitmap.width
  335.       @values[digit] = Rect.new(x1, 0, x2 - x1, @height)
  336.       x1 = x2
  337.     end
  338.   end
  339.   #--------------------------------------------------------------------------
  340.   # * text_size
  341.   #--------------------------------------------------------------------------
  342.   def text_size(text)
  343.     size = 0
  344.     text.split("").each {|i| size += @values[i].width}
  345.     rect = Rect.new(0, 0, size, @height)
  346.     rect
  347.   end
  348.   #--------------------------------------------------------------------------
  349.   # * draw_text
  350.   #--------------------------------------------------------------------------
  351.   def draw_text(text)
  352.     width = [text_size(text).width, 1].max
  353.     bitmap = Bitmap.new(width, @height)
  354.     x = 0
  355.     text.split("").each do |i|
  356.       bitmap.blt(x, 4, @bitmap, @values[i], @alpha)
  357.       x += @values[i].width
  358.     end
  359.     bitmap
  360.   end
  361.   #--------------------------------------------------------------------------
  362.   # * dispose
  363.   #--------------------------------------------------------------------------
  364.   def dispose
  365.     return if !@bitmap || @bitmap.disposed?
  366.     @bitmap.dispose
  367.   end
  368. end