Guest User

Untitled

a guest
Nov 23rd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.95 KB | None | 0 0
  1. #===========================================================================
  2. # ** Font & Bitmap Expansion [RMVX/RMXP]
  3. #---------------------------------------------------------------------------
  4. #  by Ramiro
  5. #  V 1.0
  6. #--------------------------------------------------------------------------
  7. # Notes:
  8. # may cause lag if you use it too mutch per scene
  9. #--------------------------------------------------------------------------
  10. # Sime tips:
  11. # bug in gradient_fill_rect fixed(VX)
  12. # new methods:
  13. #
  14. # Font
  15. # Font#outline=(boolean), draw outline text (auto font shadow killer in VX for better look)
  16. # Font#outline_color1=(Color) Internal outline color
  17. # Font#outline_color2=(Color)External outline color
  18. #
  19. # Bitmap
  20. #
  21. # Bitmap#gradient_fill_rect(*arguments) #[New in XP]
  22. # Bitmap#gradient_draw_text(*arguments) Take a look in gradient_draw_text method here for more info
  23. #
  24. # NOT FOR COMERTIAL USAGE
  25. #===========================================================================
  26.  
  27.  
  28. class Font
  29.   attr_accessor :outline
  30. end
  31.  
  32. class Bitmap
  33.  
  34. #--------------------------------------------------------------------------
  35. # Bitmap#draw_text
  36. # for an "outline" look
  37. #--------------------------------------------------------------------------
  38.  
  39.   alias org_draw_text draw_text if !method_defined?('org_draw_text')
  40.  
  41.   def draw_text(*args)
  42.     if args[0].is_a?(Rect)
  43.       x = args[0].x
  44.       y = args[0].y
  45.       w = args[0].width
  46.       h = args[0].height
  47.       text = args[1]
  48.       align = args[2] if args[2]
  49.     else
  50.       x = args[0]
  51.       y = args[1]
  52.       w = args[2]
  53.       h = args[3]
  54.       text = args[4]
  55.       align = args[5]   if args[5]
  56.     end  
  57.     align = 0 if !align
  58.    
  59.     if font.outline
  60.       color = font.color.clone
  61.       font.color = Color.new(0,0,0)
  62.       org_draw_text(x-1,y-1,w,h,text,align)
  63.       org_draw_text(x,y-1,w,h,text,align)
  64.       org_draw_text(x-1,y,w,h,text,align)
  65.       org_draw_text(x+1,y+1,w,h,text,align)
  66.       org_draw_text(x-1,y+1,w,h,text,align)
  67.       org_draw_text(x+1,y-1,w,h,text,align)
  68.       org_draw_text(x+1,y,w,h,text,align)
  69.       org_draw_text(x,y+1,w,h,text,align)
  70.       font.color = color.clone\
  71.     end
  72.     org_draw_text(*args)
  73.   end
  74.  
  75.  
  76. #--------------------------------------------------------------------------
  77. # Bitmap#Gradient Fill Rect
  78. # Its the same method than RMVX,
  79. # if you need more info take a look at
  80. # the RMVX manual
  81. #--------------------------------------------------------------------------
  82.  
  83.  
  84.     def gradient_fill_rect(*args)
  85.       if args[0].is_a?(Rect)
  86.         x  = args[0].x
  87.         y  = args[0].y
  88.         w  = args[0].width
  89.         h  = args[0].height
  90.         c1 = args[1]
  91.         c2 = args[2]
  92.         v  = args[3]
  93.       else
  94.         x  = args[0]
  95.         y  = args[1]
  96.         w  = args[2]
  97.         h  = args[3]
  98.         c1 = args[4]
  99.         c2 = args[5]
  100.         v  = args[6]
  101.       end  
  102.      
  103.       v = false if !v
  104.      
  105.       color_tones = []
  106.      
  107.       if v
  108.         for i in 0...h
  109.           r = c1.red   * (h - i) / h + c2.red   * i / h
  110.           g = c1.green * (h - i) / h + c2.green * i / h
  111.           b = c1.blue  * (h - i) / h + c2.blue  * i / h
  112.           a = c1.alpha * (h - i) / h + c2.alpha * i / h        
  113.           fill_rect(Rect.new(x,y+i,w,1),Color.new(r,g,b,a))
  114.         end  
  115.       else
  116.         for i in 0...w
  117.           r = c1.red   * (w - i) / w + c2.red   * i / w
  118.           g = c1.green * (w - i) / w + c2.green * i / w
  119.           b = c1.blue  * (w - i) / w + c2.blue  * i / w
  120.           a = c1.alpha * (w - i) / w + c2.alpha * i / w          
  121.           fill_rect(Rect.new(x+i,y,1,h),Color.new(r,g,b,a))
  122.         end          
  123.       end
  124.      
  125.     end
  126.  
  127.  
  128.   #--------------------------------------------------------------------------
  129.   # Bitmap#gradient_draw_text
  130.   #
  131.   # Arguments:
  132.   #
  133.   # Rect,name,color1,color2,align,vertical
  134.   # x,y,width.height,text,color1,color2,align,vertical
  135.   # Rect, x, y, width,height, text y align wors exactly equal as the draw_text_method
  136.   #
  137.   # New Arguments:
  138.   # color1 = Initial gradient color
  139.   # color2 = End gradient color
  140.   # vertical: put it true if you want the gradient vertical
  141.   #
  142.   # - Default arguments:
  143.   #
  144.   # align = 0
  145.   # vertical = false
  146.   #
  147.   #--------------------------------------------------------------------------
  148.   # ¡Atentio!
  149.   #
  150.   # If you want to use this please read about draw_text method
  151.   #--------------------------------------------------------------------------  
  152.  
  153.   def gradient_draw_text(*args)
  154.     if args[0].is_a?(Rect)
  155.       x = args[0].x
  156.       y = args[0].y
  157.       w = args[0].width
  158.       h = args[0].height
  159.       text = args[1]
  160.       color1 = args[2]
  161.       color2 = args[3]
  162.       align = args[4] if args[4]
  163.       vertical = args[5] if args[5]
  164.     else
  165.       x = args[0]
  166.       y = args[1]
  167.       w = args[2]
  168.       h = args[3]
  169.       text = args[4]
  170.       color1 = args[5]
  171.       color2 = args[6]
  172.       align = args[7]   if args[7]
  173.       vertical = args[8] if args[8]
  174.     end  
  175.     align = 0 if !align
  176.     vertical = false if !vertical
  177.     text_bmp = Bitmap.new(w,h)
  178.     text_bmp.font.name = font.name
  179.     text_bmp.font.size = font.size
  180.     text_bmp.font.shadow = false  if text_bmp.font.respond_to?("shadow")
  181.     text_bmp.font.outline = false if text_bmp.font.respond_to?("outline")
  182.     text_bmp.font.border = false  if text_bmp.font.respond_to?("border")
  183.     text_bmp.font.bold = font.bold
  184.     text_bmp.font.italic = font.italic
  185.     text_bmp.draw_text(0,0,w,h,text,align)
  186.     gradient_bmp = Bitmap.new(w,h)
  187.     gradient_bmp.gradient_fill_rect(0, 0, w, h, color1, color2, vertical)
  188.     for x2 in 0...w
  189.       for y2 in 0...h
  190.         alpha_txt = text_bmp.get_pixel(x2,y2).alpha
  191.         if alpha_txt > 0
  192.           c = gradient_bmp.get_pixel(x2,y2)
  193.           c.alpha = alpha_txt
  194.           text_bmp.set_pixel(x2,y2,c)
  195.         end
  196.       end
  197.     end
  198.     draw_text(x,y,w,h,text,align)
  199.     blt(x,y,text_bmp,Rect.new(0,0,w,h))
  200.   end  
  201. end
Add Comment
Please, Sign In to add comment