Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2011
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.05 KB | None | 0 0
  1. #===========================================================================
  2. # ** Bitmap and Font expansion [RMVX/RMXP]
  3. #---------------------------------------------------------------------------
  4. #  by Ramiro
  5. #  V 1.0
  6. #--------------------------------------------------------------------------
  7. # Notes:
  8. # Don't use it with big letters or it may lag
  9. #--------------------------------------------------------------------------
  10. #
  11. # New Methods:
  12. #
  13. # Font
  14. # Font#outline=(boolean), choose if the font is outline.
  15. # Font#outline_color1=(Color) Internal outline color
  16. # Font#outline_color2=(Color) External outline color
  17. #
  18. # Bitmap
  19. #
  20. # Bitmap#gradient_fill_rect(*arguments) #[New for XP]
  21. # Bitmap#gradient_draw_text(*arguments) Look for instructions on the script
  22. #
  23. #===========================================================================
  24.  
  25.  
  26. class Font
  27.  
  28.   attr_accessor :outline
  29.   attr_accessor :outline_color1
  30.   attr_accessor :outline_color2
  31.  
  32.   alias fond_expand_initialize initialize
  33.  
  34.   def initialize
  35.     fond_expand_initialize
  36.     @outline = true
  37.     @outline_color1 = Color.new(0,0,0)
  38.     @outline_color2 = Color.new(0,0,0,0)
  39.   end  
  40.  
  41.  
  42. end
  43.  
  44. class Bitmap
  45.  
  46. #--------------------------------------------------------------------------
  47. # Bitmap#draw_text
  48. # Adds to the Outline font
  49. #--------------------------------------------------------------------------
  50.  
  51.   alias org_draw_text draw_text if !method_defined?('org_draw_text')
  52.  
  53.   def draw_text(*args)
  54.     if args[0].is_a?(Rect)
  55.       x = args[0].x
  56.       y = args[0].y
  57.       w = args[0].width
  58.       h = args[0].height
  59.       text = args[1]
  60.       aling = args[2] if args[2]
  61.     else
  62.       x = args[0]
  63.       y = args[1]
  64.       w = args[2]
  65.       h = args[3]
  66.       text = args[4]
  67.       aling = args[5]   if args[5]
  68.     end  
  69.     aling = 0 if !aling
  70.  
  71.     if font.outline
  72.       if font.respond_to?("shadow")
  73.         shval = font.shadow
  74.         font.shadow = false
  75.       end
  76.       color = font.color.clone
  77.       font.color = font.outline_color2
  78.       org_draw_text(x-2,y-2,w,h,text,aling)
  79.       org_draw_text(x,y-2,w,h,text,aling)
  80.       org_draw_text(x-2,y,w,h,text,aling)
  81.       org_draw_text(x+2,y+2,w,h,text,aling)
  82.       org_draw_text(x-2,y+2,w,h,text,aling)
  83.       org_draw_text(x+2,y-2,w,h,text,aling)
  84.       org_draw_text(x+2,y,w,h,text,aling)
  85.       org_draw_text(x,y+2,w,h,text,aling)
  86.       font.color = font.outline_color1
  87.       org_draw_text(x-1,y-1,w,h,text,aling)
  88.       org_draw_text(x,y-1,w,h,text,aling)
  89.       org_draw_text(x-1,y,w,h,text,aling)
  90.       org_draw_text(x+1,y+1,w,h,text,aling)
  91.       org_draw_text(x-1,y+1,w,h,text,aling)
  92.       org_draw_text(x+1,y-1,w,h,text,aling)
  93.       org_draw_text(x+1,y,w,h,text,aling)
  94.       org_draw_text(x,y+1,w,h,text,aling)
  95.       font.color = color.clone
  96.       font.shadow = shval if shval
  97.     end
  98.     org_draw_text(*args)
  99.   end
  100.  
  101.  
  102. #--------------------------------------------------------------------------
  103. # Bitmap#Gradient Fill Rect
  104. # Patched the "vertical bug"
  105. #--------------------------------------------------------------------------
  106.  
  107.  
  108.     def gradient_fill_rect(*args)
  109.       if args[0].is_a?(Rect)
  110.         x  = args[0].x
  111.         y  = args[0].y
  112.         w  = args[0].width
  113.         h  = args[0].height
  114.         c1 = args[1]
  115.         c2 = args[2]
  116.         v  = args[3]
  117.       else
  118.         x  = args[0]
  119.         y  = args[1]
  120.         w  = args[2]
  121.         h  = args[3]
  122.         c1 = args[4]
  123.         c2 = args[5]
  124.         v  = args[6]
  125.       end  
  126.  
  127.       v = false if !v
  128.  
  129.       color_tones = []
  130.  
  131.       if v
  132.         for i in 0...h
  133.           r = c1.red   * (h - i) / h + c2.red   * i / h
  134.           g = c1.green * (h - i) / h + c2.green * i / h
  135.           b = c1.blue  * (h - i) / h + c2.blue  * i / h
  136.           a = c1.alpha * (h - i) / h + c2.alpha * i / h        
  137.           fill_rect(Rect.new(x,y+i,w,1),Color.new(r,g,b,a))
  138.         end  
  139.       else
  140.         for i in 0...w
  141.           r = c1.red   * (w - i) / w + c2.red   * i / w
  142.           g = c1.green * (w - i) / w + c2.green * i / w
  143.           b = c1.blue  * (w - i) / w + c2.blue  * i / w
  144.           a = c1.alpha * (w - i) / w + c2.alpha * i / w          
  145.           fill_rect(Rect.new(x+i,y,1,h),Color.new(r,g,b,a))
  146.         end          
  147.       end
  148.  
  149.     end
  150.  
  151.  
  152.   #--------------------------------------------------------------------------
  153.   # Bitmap#gradient_draw_text
  154.   #
  155.   # Arguments:
  156.   #
  157.   # Rect,name,color1,color2,aling,vertical
  158.   # x,y,width.height,text,color1,color2,aling,vertical
  159.   # Rect, x, y, width,height, text y aling are the same as the
  160.   # 'draw_text' method
  161.   #
  162.   # New argments:
  163.   # color1 = Initial font color
  164.   # color2 = Ending font color
  165.   # vertical: if true the gradient is vertical, false is horizontal
  166.   #
  167.   # - Default parameters:
  168.   #
  169.   # aling = 0
  170.   # vertical = false
  171.   #
  172.   #--------------------------------------------------------------------------
  173.   # ¡WARNING!
  174.   #
  175.   # You need some knowledge to use this!!!
  176.   #--------------------------------------------------------------------------  
  177.  
  178.   def gradient_draw_text(*args)
  179.     if args[0].is_a?(Rect)
  180.       x = args[0].x
  181.       y = args[0].y
  182.       w = args[0].width
  183.       h = args[0].height
  184.       text = args[1]
  185.       color1 = args[2]
  186.       color2 = args[3]
  187.       aling = args[4] if args[4]
  188.       vertical = args[5] if args[5]
  189.     else
  190.       x = args[0]
  191.       y = args[1]
  192.       w = args[2]
  193.       h = args[3]
  194.       text = args[4]
  195.       color1 = args[5]
  196.       color2 = args[6]
  197.       aling = args[7]   if args[7]
  198.       vertical = args[8] if args[8]
  199.     end  
  200.     aling = 0 if !aling
  201.     vertical = false if !vertical
  202.     text_bmp = Bitmap.new(w,h)
  203.     text_bmp.font.name = font.name
  204.     text_bmp.font.size = font.size
  205.     text_bmp.font.shadow = false  if text_bmp.font.respond_to?("shadow")
  206.     text_bmp.font.outline = false if text_bmp.font.respond_to?("outline")
  207.     text_bmp.font.border = false  if text_bmp.font.respond_to?("border")
  208.     text_bmp.font.bold = font.bold
  209.     text_bmp.font.italic = font.italic
  210.     text_bmp.draw_text(0,0,w,h,text,aling)
  211.     gradient_bmp = Bitmap.new(w,h)
  212.     gradient_bmp.gradient_fill_rect(0, 0, w, h, color1, color2, vertical)
  213.     for x2 in 0...w
  214.       for y2 in 0...h
  215.         alpha_txt = text_bmp.get_pixel(x2,y2).alpha
  216.         if alpha_txt > 0
  217.           c = gradient_bmp.get_pixel(x2,y2)
  218.           c.alpha = alpha_txt
  219.           text_bmp.set_pixel(x2,y2,c)
  220.         end
  221.       end
  222.     end
  223.     draw_text(x,y,w,h,text,aling)
  224.     blt(x,y,text_bmp,Rect.new(0,0,w,h))
  225.   end  
  226. end
  227.  
  228. </pre>
  229.  
  230. == Example ==
  231.  
  232. If You want to see somethig like the image use this code:
  233. <pre>
  234. class Window_Base
  235.   #--------------------------------------------------------------------------
  236.   # * Draw Name
  237.   #     actor : actor
  238.   #     x     : draw spot x-coordinate
  239.   #     y     : draw spot y-coordinate
  240.   #--------------------------------------------------------------------------
  241.   def draw_actor_name(actor, x, y)
  242.        contents.gradient_draw_text(x,y,108,WLH,actor.name,Color.new(255,0,0),Color.new(0,255,0),0,false)
  243.   end
  244. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement