Advertisement
neonblack

Gradient Text v1.3

Sep 5th, 2012
1,897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 13.96 KB | None | 0 0
  1. ###--------------------------------------------------------------------------###
  2. #  CP Gradient Text script                                                     #
  3. #  Version 1.3                                                                 #
  4. #                                                                              #
  5. #      Credits:                                                                #
  6. #  Original code by: Neonblack                                                 #
  7. #  Modified by:                                                                #
  8. #                                                                              #
  9. #  This work is licensed under the Creative Commons Attribution-NonCommercial  #
  10. #  3.0 Unported License. To view a copy of this license, visit                 #
  11. #  http://creativecommons.org/licenses/by-nc/3.0/.                             #
  12. #  Permissions beyond the scope of this license are available at               #
  13. #  http://cphouseset.wordpress.com/liscense-and-terms-of-use/.                 #
  14. #                                                                              #
  15. #      Contact:                                                                #
  16. #  NeonBlack - neonblack23@live.com (e-mail) or "neonblack23" on skype         #
  17. ###--------------------------------------------------------------------------###
  18.  
  19. ###--------------------------------------------------------------------------###
  20. #      Revision information:                                                   #
  21. #  V1.3 - 10.14.2012                                                           #
  22. #   Text shape and alignment fix                                               #
  23. #  V1.2c - 10.13.2012                                                          #
  24. #   Font "width" issue fixed                                                   #
  25. #   Fixed a non-gradient issue                                                 #
  26. #  V1.2b - 9.14.2012                                                           #
  27. #   Fixed a divide by zero error                                               #
  28. #  V1.2 - 9.13.2012                                                            #
  29. #   Font positioning overwrite written                                         #
  30. #  V1.1b - 9.8.2012                                                            #
  31. #   4 character bug fix...                                                     #
  32. #  V1.1 - 9.7.2012                                                             #
  33. #   Disposed bitmap bugfix                                                     #
  34. #  V1.0 - 9.2.2012                                                             #
  35. #   Wrote and debugged main script                                             #
  36. ###--------------------------------------------------------------------------###
  37.  
  38. ###--------------------------------------------------------------------------###
  39. #      Compatibility:                                                          #
  40. #  Alias:      - Bitmap: draw_text                                             #
  41. #  New Methods - Bitmap: convert_string, create_char, font_color_light,        #
  42. #                        font_color_dark                                       #
  43. #                Text: get_char, add_char, size, create_big_bitmap, key,       #
  44. #                      clear_cache, w, h, hw, hh                               #
  45. #                Font: array                                                   #
  46. ###--------------------------------------------------------------------------###
  47.  
  48. ###--------------------------------------------------------------------------###
  49. #      Instructions:                                                           #
  50. #  Place this script in the "Materials" section of the scripts above main.     #
  51. #  This script is plug and play with a few different options to allow you to   #
  52. #  customize to taste.                                                         #
  53. ###--------------------------------------------------------------------------###
  54.  
  55. ###--------------------------------------------------------------------------###
  56. #      Config:                                                                 #
  57. #  These are the default values used by several of the functions in the        #
  58. #  script.  You may change these values as you find your game requires in      #
  59. #  order to give the player a better playing experience based on your game.    #
  60. #                                                                              #
  61. module CP    # Do not edit                                                     #
  62. module TEXT  #  these two lines.                                               #
  63. #                                                                              #
  64. ###-----                                                                -----###
  65. # Change this to false to disable this script.  The functions will still be    #
  66. # there, but the steps will be skipped.                                        #
  67. USE = true # Default = true                                                    #
  68. #                                                                              #
  69. # Defines the size of the top and bottom edges of the font.  These pixels will #
  70. # be the lightest and darkest color to make the text appear more "rounded".    #
  71. EDGE = 3 # Default = 3                                                         #
  72. #                                                                              #
  73. # Chooses if the gradient colors are applied.  Can be turned to false if you   #
  74. # just want to use the font buffer.                                            #
  75. GRADIENT = true # Default = true                                               #
  76. #                                                                              #
  77. # The bop and bottom shading of the font.  A number between 0 and 100 should   #
  78. # be used.  The top is changed to black while the bottom is changed to white.  #
  79. LIGHT = 70 # Default = 70                                                      #
  80. DARK = 40 # Default = 40                                                       #
  81. #                                                                              #
  82. # Fixes the positioning of some characters.  Use this for fonts that get cut   #
  83. # off or display too far to the left.  May cause additional overhead and is    #
  84. # not always needed, so false by default.                                      #
  85. O_POSITION = false # Default = false                                           #
  86. ###--------------------------------------------------------------------------###
  87.  
  88.  
  89. ###--------------------------------------------------------------------------###
  90. #  The following lines are the actual core code of the script.  While you are  #
  91. #  certainly invited to look, modifying it may result in undesirable results.  #
  92. #  Modify at your own risk!                                                    #
  93. ###--------------------------------------------------------------------------###
  94.  
  95.  
  96. end
  97. end
  98.  
  99. $imported = {} if $imported == nil
  100. $imported["CP_GRADIENT"] = 1.0
  101.  
  102. class Bitmap  ## Alias draw_text for all the new functions.
  103.   alias cp_draw_grad_text draw_text unless $@
  104.   def draw_text(*args)
  105.     return cp_draw_grad_text(*args) unless CP::TEXT::USE
  106.     case args.size  ## Gets all the default arguments.
  107.     when 2, 3
  108.       rect = args[0].dup
  109.       string = args[1]
  110.       align = args[2] ? args[2] : 0
  111.     when 5, 6
  112.       rect = Rect.new(args[0], args[1], args[2], args[3])
  113.       string = args[4]
  114.       align = args[5] ? args[5] : 0
  115.     else
  116.       return cp_draw_grad_text(*args)  ## Stops if something is somehow off.
  117.     end
  118.     font_bit = convert_string(string)  ## Gets the converted bitmap.
  119.     rect.x -= Text.hw; rect.y -= Text.hh  ## Increases the edges of the rectangle.
  120.     rect.height += Text.h; rect.width += Text.w
  121.     if font_bit.height > rect.height  ## Checks height and adjusts the rect.
  122.       font_bit.clear_rect(0, rect.height, font_bit.width, font_bit.height)
  123.       rect.height = font_bit.height
  124.     else
  125.       unless font_bit.height == rect.height
  126.         rect.y += (rect.height - font_bit.height) / 2
  127.         rect.height = font_bit.height  ## Centers a taller rect.
  128.       end
  129.     end
  130.     if font_bit.width > rect.width  ## Checks width and adjusts.
  131.       pal = (rect.width - Text.w).to_f / (font_bit.width - Text.w)
  132.       pal = (Text.w - (pal * Text.w)).to_i / 2 rescue pal = Text.hw
  133.       rect.x += pal  ## Gets the border of the rect and adjusts.
  134.       rect.width -= (pal * 2)
  135.       stretch_blt(rect, font_bit, font_bit.rect)
  136.     else
  137.       offset = (rect.width - font_bit.width) / 2
  138.       blt(rect.x + (offset * align), rect.y, font_bit, font_bit.rect)
  139.     end
  140.   end
  141.  
  142.   def convert_string(string)  ## Creates the string bitmap from the cache.
  143.     string_r = Text.size(string, font)
  144.     bitmap = Bitmap.new(string_r.width, string_r.height)
  145.     space = 0
  146.     var = string.to_s
  147.     var.each_char do |c|  ## Draws each character from the cache.
  148.       bit = Text.get_char(c, font)
  149.       bitmap.blt(space, 0, bit, bit.rect)
  150.       space += bit.width - Text.w
  151.     end
  152.     result = Bitmap.new(space + Text.w, bitmap.height)
  153.     result.blt(0, 0, bitmap, bitmap.rect)
  154.     return result
  155.   end
  156.  
  157.   def create_char(string)  ## Creates a new character for the cache.
  158.     edge = CP::TEXT::EDGE
  159.     shift = 0
  160.     if ((edge * 2) + Text.h > height) || !CP::TEXT::GRADIENT
  161.       cp_draw_grad_text(1, 0, width, height, string, 1)
  162.       for i in 0...height
  163.         Text.hw.times do |n|
  164.           break unless CP::TEXT::O_POSITION
  165.           blnk = get_pixel(n + 1, i)
  166.           next if blnk.alpha == 0
  167.           shift = [Text.hw - n, shift].max
  168.           break
  169.         end
  170.       end
  171.     else  ## ^ Return a defaul character under certain conditions.
  172.       bitmap = Bitmap.new(width + 1, height)
  173.       bitmap.font = font
  174.       h1 = (height - Text.h - edge * 2) / 2
  175.       h2 = (height - Text.h - edge * 2) - h1
  176.       gr1 = Rect.new(0, 9 + edge, 1, h1)       ## Creates the dark and
  177.       gr2 = Rect.new(0, 9 + edge + h1, 1, h2)  ## light rectangles.
  178.       bitmap.gradient_fill_rect(gr1, font_color_light, font.color, true)
  179.       bitmap.gradient_fill_rect(gr2, font.color, font_color_dark, true)
  180.       rect = Rect.new(1, 0, width, height)
  181.       done = false
  182.       font.color = font_color_light
  183.       cp_draw_grad_text(0, 0, width, height, string, 1)
  184.       for i in 0...height  ## Creates the new image in several steps.
  185.         clr = bitmap.get_pixel(0, i)  ## Think of it like a slow scan tv.
  186.         Text.hw.times do |n|
  187.           break unless CP::TEXT::O_POSITION
  188.           blnk = bitmap.get_pixel(n + 1, i)
  189.           next if blnk.alpha == 0
  190.           shift = [Text.hw - n, shift].max
  191.           break
  192.         end
  193.         if clr.alpha == 0
  194.           next unless done
  195.           cr = Rect.new(1, 0, width, i)
  196.           bitmap.clear_rect(cr)
  197.           blt(1, 0, bitmap, rect)
  198.         else
  199.           unless done
  200.             cr = Rect.new(1, i, width, height)
  201.             clear_rect(cr)
  202.             done = true
  203.           end
  204.           tr = Rect.new(1, i, width, 1)
  205.           bitmap.font.color = clr
  206.           bitmap.clear_rect(rect)
  207.           bitmap.cp_draw_grad_text(1, 0, width, height, string, 1)
  208.           blt(1, i, bitmap, tr)
  209.         end
  210.       end
  211.     end
  212.     shift -= 1
  213.     return if shift <= 0
  214.     holder = Bitmap.new(width, height)
  215.     holder.blt(0 + shift, 0, self, self.rect)
  216.     clear_rect(self.rect)
  217.     blt(0, 0, holder, holder.rect)
  218.   end
  219.  
  220.   def font_color_light  ## Creates the light font color.
  221.     r = font.color.red
  222.     g = font.color.green
  223.     b = font.color.blue
  224.     color = []
  225.     [r, g, b].each {|c| color.push(c + (255 - c) * CP::TEXT::LIGHT / 100) }
  226.     return Color.new(color[0], color[1], color[2], font.color.alpha)
  227.   end
  228.  
  229.   def font_color_dark  ## Creates the dark font color.
  230.     r = font.color.red
  231.     g = font.color.green
  232.     b = font.color.blue
  233.     color = []
  234.     [r, g, b].each {|c| color.push(c - c * CP::TEXT::DARK / 100) }
  235.     return Color.new(color[0], color[1], color[2], font.color.alpha)
  236.   end
  237. end
  238.  
  239. module Text
  240.   WIDTH = 40
  241.   HEIGHT = 20
  242.  
  243.   def self.w; return WIDTH; end
  244.   def self.h; return HEIGHT; end
  245.   def self.hw; return WIDTH / 2; end
  246.   def self.hh; return HEIGHT / 2; end
  247.  
  248.   def self.get_char(char, font = Font.new)
  249.     @letter = {} if @letter.nil?  ## Returns the character bitmap by font.
  250.     add_char(char, font) unless @letter.include?(key(char, font))
  251.     return @letter[key(char, font)]
  252.   end
  253.  
  254.   def self.add_char(char, font)
  255.     rect = size(char, font)  ## Creates and edits a new bitmap for the cache.
  256.     @letter[key(char, font)] = Bitmap.new(rect.width, rect.height)
  257.     @letter[key(char, font)].font = font
  258.     @letter[key(char, font)].create_char(" " + char + " ")
  259.   end
  260.  
  261.   def self.size(char, font = Font.new)
  262.     create_big_bitmap
  263.     @bitmap.font = font  ## Gets a character's size with a buffer.
  264.     rect = @bitmap.text_size(char)
  265.     rect.width += Text.w
  266.     rect.height += Text.h
  267.     return rect
  268.   end
  269.  
  270.   def self.create_big_bitmap
  271.     return unless @bitmap.nil? || @bitmap.disposed?
  272.     @bitmap = Bitmap.new(Graphics.width, Graphics.height)
  273.     clear_cache
  274.   end
  275.  
  276.   def self.key(char, font)  ## Creates a character's key by font.
  277.     res = font.array + [char]
  278.     return res
  279.   end
  280.  
  281.   def self.clear_cache  ## Clears the cache.  Not called by me at all.
  282.     @letter = {}
  283.   end
  284. end
  285.  
  286. class Font   ## Gets the fonts total array.  All aspects of the font
  287.   def array  ## are here for use in making a character bitmap's key.
  288.     [name, size, bold, italic, outline, shadow, color.red, color.green,
  289.      color.blue, color.alpha, out_color.red, out_color.green, out_color.blue,
  290.      out_color.alpha]
  291.   end
  292. end
  293.  
  294.  
  295. ###--------------------------------------------------------------------------###
  296. #  End of script.                                                              #
  297. ###--------------------------------------------------------------------------###
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement