TheoAllen

Theo - Bitmap Addon v2.0

Jun 4th, 2013
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 14.31 KB | None | 0 0
  1. # =============================================================================
  2. # TheoAllen - Bitmap Extra Addons
  3. # Version : 2.0
  4. # Contact : www.rpgmakerid.com (or) http://theolized.blogspot.com
  5. # (This script documentation is written in informal indonesian language)
  6. # =============================================================================
  7. ($imported ||= {})[:Theo_BitmapAddons]=true
  8. # =============================================================================
  9. # CHANGE LOGS:
  10. # -----------------------------------------------------------------------------
  11. # 2013.06.04 - Massive update. Increment to version 2.0
  12. #            - add draw gradient_line
  13. #            - add draw_polygon
  14. #            - add draw_ellipse
  15. #            - etc ....
  16. # 2013.05.21 - Started and Finished script
  17. # =============================================================================
  18. =begin
  19.   # ===========================================================================
  20.   Perkenalan :
  21.   Script ini adalah untuk menggambar bentuk2 dasar dalam class bitmap. Dan ini
  22.   cuman sarana untuk para scripter. Method2 yg wa tambahin yaitu,
  23.  
  24.   Garis :
  25.   - draw_line
  26.   - gradient_line
  27.  
  28.   Lingkaran :
  29.   - draw_ellipse
  30.   - draw_circle
  31.   - fill_ellipse
  32.   - fill_circle
  33.  
  34.   Poligon :
  35.   - draw_shape
  36.   - draw_polygon
  37.   - draw_shape_params
  38.   - fill_polygon
  39.   - fill_shape_params
  40.  
  41.   class Color :
  42.   - same?(color1[,color2[,color3,...)
  43.   - empty?
  44.  
  45.   ===========================================================================
  46.   Cara penggunaan :
  47.   ===========================================================================
  48.   Garis :
  49.   ---------------------------------------------------------------------------
  50.   *) bitmap.draw_line(x1,y1,x2,y2[,color])
  51.   *) bitmap.draw_line(x1,y1,x2,y2[,color]) {|coordinate| }
  52.  
  53.   Untuk ngedraw line dari koordinat (x1,y1) ampe ke koordinat (x2,y2). Jika
  54.   kamu gunain argument block, maka yg dikeluarkan adalah nilai koordinat
  55.   masing2 titik yg digambar dalam bentuk array. misalnya coordinate[0]
  56.   adalah posisi x dan coordinate[1] adalah posisi y.
  57.  
  58.   Secara default, jika color diabaikan maka warna yg dipake adalah putih
  59.   Color.new(255,255,255)
  60.  
  61.   *) bitmap.gradient_line(x1,y1,x2,y2,color1,color2)
  62.  
  63.   Untuk ngedraw line dari koordinat (x1,y1) ampe ke koordinat (x2,y2) dengan
  64.   gradient dari color1 ke color2. Untuk saat ini masi belom support argument
  65.   block
  66.  
  67.   ---------------------------------------------------------------------------
  68.   Lingkaran :
  69.   ---------------------------------------------------------------------------
  70.  
  71.   *) bitmap.draw_ellipse(x,y,horz,vert[,color[,think[,step]]])
  72.   Untuk ngedraw gambar poligon dengan argumen sebagai berikut :
  73.   x     >> koordinat x titik pusat
  74.   y     >> koordinat y titik pusat
  75.   horz  >> jari2 horizontal
  76.   vert  >> jari2 vertikal
  77.   color >> warna garisnya (default : putih)
  78.   think >> ketebalan garis (default : 1)
  79.   step  >> jarak antar titik (default : 0.1)
  80.  
  81.   *) bitmap.draw_circle(x,y,radius[,color[,think[,step]]])
  82.   Untuk ngedraw gambar lingkaran. Argumen sama kek draw ellipse. bedanya cuman
  83.   di radius (jari2) doank
  84.  
  85.   *) bitmap.fill_ellipse(x,y,horz,vert,color1[,color2[,step]])
  86.   Untuk ngefill ellipse. x,y,horz,vert sama kek yg lainnya. color1 adalah untuk
  87.   outline. Sedangkan color2 untuk fill. Jika color2 ga diisi, maka warnanya
  88.   adalah sama kek color1
  89.  
  90.   *) bitmap.fill_circle(x,y,radius,color1[,color2[,step]])
  91.   Overall sama juga :v
  92.  
  93.   ---------------------------------------------------------------------------
  94.   Poligon :
  95.   ---------------------------------------------------------------------------
  96.  
  97.   *) bitmap.draw_shape(x,y,params,length[,color1[,bone[,fill[,color2])
  98.   Ini adalah method dasar untuk ngedraw poligon. Jadi jangan heran kalo
  99.   argumennya kelewat banyak. Untuk keterangan argumennya sebagai berikut
  100.  
  101.   x       >> koordinat x titik pusat
  102.   y       >> koordinat y titik pusat
  103.   params  >> parameter dalam array. cnth : [100,50,100,50,100,50]
  104.   length  >> panjang (maksimal)
  105.   color1  >> warna untuk ngedraw garisnya (default : putih)
  106.   bone    >> jika true maka kerangka poligon akan didraw (default : true)
  107.   fill    >> jika true maka poligon akan diisi wanra (default : false)
  108.   color2  >> warna untuk ngefill (default : sama kek color1)
  109.  
  110.   *) bitmap.draw_polygon(x,y,corner,length,color1[,bone[,color2]])
  111.  
  112.   Method untuk ngedraw poligon. overall sama kek draw_shape. Hanya saja, corner
  113.   adalah jumlah sudut yg nantinya mau u gambar. Semisal kamu masukin 5, maka
  114.   akan jadi segi lima. Bone secara default true (gambar kerangka). color2 adalah
  115.   warna untuk ngegambar kerangka itu (warna sama kek color1).
  116.  
  117.   *) bitmap.draw_shape_params(x,y,params,length,color1[,bone[,color2]])
  118.  
  119.   Method untuk ngedraw parameter (atk,def,int,agi,dll...) dalam bentuk poligon.
  120.   Overall sama kek draw_polygon. Hanya saja berbeda pada argumen params. Contoh
  121.   penggunaannya seperti ini :
  122.  
  123.   params = [atk,def,matk,mdf,agi,luk]
  124.   bitmap.draw_shape(50,50,params,50,Color.new(0,255,0))
  125.  
  126.   *) bitmap.fill_polygon(x,y,corner,length,color1[,color2])
  127.   Bikin gambar poligon dan sekalian difill dengan warna. Color1 adalah outline,
  128.   dan color2 adalah fillnya. Jika color2 diabaikan, maka warnanya akan sama
  129.   kek color1
  130.  
  131.   *) bitmap.fill_shape_params(x,y,params,length,color1[,color2])
  132.   Overall sama kek fill_polygon. Hanya berbeda pada params saja
  133.  
  134.   ---------------------------------------------------------------------------
  135.   Lain-lain :
  136.   ---------------------------------------------------------------------------
  137.   Wa juga nambahin sesuatu di class Color. Yaitu :
  138.  
  139.   *) color.same?(color1[,color2[,color3,dst...]]])
  140.   Untuk ngecek apakah komponen warna yg dibandingkan itu sama atau tidak dengan
  141.   methode or
  142.  
  143.   *) color.empty?
  144.   return true kalo semisal alphanya adalah 0
  145.  
  146.   ===========================================================================
  147.   Terms of use :
  148.   ---------------------------------------------------------------------------
  149.   Credit gw, TheoAllen dan juga Modern Algebra sebagai inspirator ide ini.
  150.   Script ini hanya digunakan untuk non-komersil, atas dasar yg tertulis dari
  151.   creative common pada link berikut :
  152.  
  153.   http://creativecommons.org/licenses/by-nc-sa/2.5/ca/
  154.  
  155.   ===========================================================================
  156.   Special Thanks :
  157.   - Modern Algebra atas ide bitmap addonnya
  158.  
  159. =end
  160. # =============================================================================
  161. class Bitmap
  162.   attr_accessor :start_degree
  163.  
  164.   alias theo_bitmap_init initialize
  165.   def initialize(*args)
  166.     theo_bitmap_init(*args)
  167.     @start_degree = 270
  168.   end
  169.  
  170.   def draw_line(x1,y1,x2,y2,color=Color.new(255,255,255),color_set_skip = false)
  171.     if x1 == x2 && y1 == y2
  172.       set_pixel(x1,y1,color)
  173.       yield [x1,x2] if block_given?
  174.       return
  175.     end
  176.     jarak_x = (x2-x1)
  177.     jarak_y = (y2-y1)
  178.     if jarak_y == 0 || jarak_x == 0
  179.       if jarak_y == 0
  180.         draw_horz(x1,y1,jarak_x,color)
  181.         for j in 0..jarak_x
  182.           yield [x1,y1]
  183.           x1 += 1
  184.         end if block_given?
  185.       elsif jarak_x.abs == 0
  186.         draw_vert(x1,y1,jarak_y,color)
  187.         for k in 0..jarak_y
  188.           yield [x1,y1]
  189.           y1 += 1
  190.         end if block_given?
  191.       end
  192.       return
  193.     end
  194.     maximum = [jarak_x.abs,jarak_y.abs].max
  195.     rasio_x = jarak_x / maximum.to_f
  196.     rasio_y = jarak_y / maximum.to_f
  197.     real_x = x1.to_f
  198.     real_y = y1.to_f
  199.     for i in 0..maximum
  200.       set_pixel(x1,y1,color) unless get_pixel(x1,y1).same?(color) ||
  201.         color_set_skip ? !get_pixel(x1,y1).empty? : false
  202.       real_x += rasio_x
  203.       real_y += rasio_y
  204.       yield [x1,y1] if block_given?
  205.       x1 = real_x.round
  206.       y1 = real_y.round
  207.     end
  208.   end
  209.  
  210.   def gradient_line(x1,y1,x2,y2,color1 = Color.new(255,255,255),color2 = color1)
  211.     jarak_x = (x2-x1)
  212.     jarak_y = (y2-y1)
  213.     radius = Math.sqrt((jarak_x**2) + (jarak_y**2))
  214.     red_diff = (color2.red - color1.red) / radius
  215.     green_diff = (color2.green - color1.green) / radius
  216.     blue_diff = (color2.blue - color1.blue) / radius
  217.     alpha_diff = (color2.alpha - color1.alpha) / radius
  218.     red = color1.red
  219.     green = color1.green
  220.     blue = color1.blue
  221.     alpha = color1.alpha
  222.     if jarak_y.abs == 0 || jarak_x.abs == 0
  223.       gradient_fill_rect(x1,y1,1,jarak_y,color1,color2,true) if jarak_y.abs == 0
  224.       gradient_fill_rect(x1,y1,jarak_x,1,color1,color2) if jarak_x.abs == 0
  225.       return
  226.     end
  227.     maximum = [jarak_x.abs,jarak_y.abs].max
  228.     rasio_x = jarak_x / maximum.to_f
  229.     rasio_y = jarak_y / maximum.to_f
  230.     real_x = x1.to_f
  231.     real_y = y1.to_f
  232.     for i in 0..maximum
  233.       new_color = Color.new(red,green,blue,alpha)
  234.       set_pixel(x1,y1,new_color) unless get_pixel(x1,y1).same?(new_color)
  235.       real_x += rasio_x
  236.       real_y += rasio_y
  237.       x1 = real_x.round
  238.       y1 = real_y.round
  239.       red += red_diff
  240.       blue += blue_diff
  241.       green += green_diff
  242.       alpha += alpha_diff
  243.     end
  244.   end
  245.  
  246.   def draw_horz(x,y,width,color = Color.new(255,255,255))
  247.     if width < 0
  248.       fill_rect(x+width+1,y,width.abs,1,color)
  249.       return
  250.     end
  251.     fill_rect(x,y,width,1,color)
  252.   end
  253.  
  254.   def draw_vert(x,y,height,color = Color.new(255,255,255))
  255.     if height < 0
  256.       fill_rect(x,y+height+1,1,height.abs,color)
  257.       return
  258.     end
  259.     fill_rect(x,y,1,height,color)
  260.   end
  261.  
  262.   def draw_ellipse(x,y=0,horz=1,vert=1,color=Color.new(255,255,255),think=1,
  263.       step=0.1)
  264.     return if think < 1
  265.     ori_x = x
  266.     ori_y = y
  267.     x += horz
  268.     degree = 0.0
  269.     while degree <= 360
  270.       yield [x,y] if block_given?
  271.       set_pixel(x,y,color) unless get_pixel(x,y).same?(color)
  272.       x = Math.cos(Math.radian(degree)) * horz + ori_x
  273.       y = Math.sin(Math.radian(degree)) * vert + ori_y
  274.       degree = [degree+step,361].min
  275.     end
  276.     if think > 1
  277.       draw_ellipse(ori_x+1,ori_y+1,horz,vert,think-1,color,step)
  278.       draw_ellipse(ori_x-1,ori_y-1,horz,vert,think-1,color,step)
  279.       draw_ellipse(ori_x+1,ori_y-1,horz,vert,think-1,color,step)
  280.       draw_ellipse(ori_x-1,ori_y+1,horz,vert,think-1,color,step)
  281.     end
  282.   end
  283.  
  284.   def draw_circle(x,y,radius,color=Color.new(255,255,255),think=1)
  285.     draw_ellipse(x,y,radius,radius,think,color) {|coordinate|
  286.       yield coordinate if block_given?
  287.     }
  288.   end
  289.  
  290.   def fill_circle(x,y,radius,color1,color2=color1,step=0.1)
  291.     fill_ellipse(x,y,radius,radius,color1,color2,step)
  292.   end
  293.  
  294.   def fill_ellipse(x,y,horz,vert,color1,color2=color1,step=0.1)
  295.     draw_ellipse(x,y,horz,vert,1,color1,0.5) {|cor|
  296.       draw_line(x,y,cor[0],cor[1],color2,true)
  297.     }
  298.     for i in 0..1
  299.       for baris in 0..horz*2
  300.         for kolom in 0..vert*2
  301.           pos_x = baris-horz+x
  302.           pos_y = kolom-vert+y
  303.           syarat = (get_pixel(pos_x+1,pos_y).same?(color1,color2) &&
  304.           get_pixel(pos_x-1,pos_y).same?(color1,color2)) ||
  305.           (get_pixel(pos_x,pos_y-1).same?(color1,color2) &&
  306.           get_pixel(pos_x,pos_y+1).same?(color1,color2)) ||
  307.           (get_pixel(pos_x+1,pos_y+1).same?(color1,color2) &&
  308.           get_pixel(pos_x-1,pos_y-1).same?(color1,color2)) ||
  309.           (get_pixel(pos_x-1,pos_y+1).same?(color1,color2) &&
  310.           get_pixel(pos_x+1,pos_y-1).same?(color1,color2))
  311.           if syarat && !get_pixel(pos_x,pos_y).same?(color1,color2)
  312.             set_pixel(pos_x,pos_y,color2)
  313.           end
  314.         end
  315.       end
  316.     end
  317.   end
  318.  
  319.   def draw_polygon(x,y,corner,length,color1,bone=true,color2=color1)
  320.     return unless corner.is_a?(Numeric)
  321.     draw_shape(x,y,Array.new(corner){1},length,color1,bone,false,color2)
  322.   end
  323.  
  324.   def fill_polygon(x,y,corner,length,color1,color2=color1)
  325.     return unless corner.is_a?(Numeric)
  326.     draw_shape(x,y,Array.new(corner),length,color1,false,true,color2)
  327.   end
  328.  
  329.   def draw_shape_params(x,y,params,length,color1,bone=true,color2=color1)
  330.     draw_shape(x,y,params,length,color1,bone,false,color2)
  331.   end
  332.  
  333.   def fill_shape_params(x,y,params,length,color1,color2=color1)
  334.     draw_shape(x,y,params,length,color1,false,true,color2)
  335.   end
  336.  
  337.   def draw_shape(x,y,params,length,color1 = Color.new(255,255,255),
  338.       include_bone = true ,fill=false,color2 = color1)
  339.     return unless params.is_a?(Array)
  340.     return unless params.size >= 3
  341.     degree_plus = 360 / params.size
  342.     degree = @start_degree
  343.     coordinate = []
  344.     edge = []
  345.     params.each do |i|
  346.       x_des = x + Math.cos(Math.radian(degree)) * (length*(i.to_f/params.max))
  347.       y_des = y + Math.sin(Math.radian(degree)) * (length*(i.to_f/params.max))
  348.       draw_line(x,y,x_des,y_des,color2) if include_bone
  349.       degree += degree_plus
  350.       coordinate.push(Coordinate.new(x_des,y_des))
  351.     end
  352.     for i in -1..coordinate.size-2
  353.       c = coordinate
  354.       draw_line(c[i].x,c[i].y,c[i+1].x,c[i+1].y,color1) {|cor| edge.push(cor)}
  355.     end
  356.     return unless fill
  357.     edge.each do |line|
  358.       draw_line(x,y,line[0],line[1],color2,true)
  359.     end
  360.     for i in 0..1
  361.       for baris in 0..length*2
  362.         for kolom in 0..length*2
  363.           pos_x = baris-length+x
  364.           pos_y = kolom-length+y
  365.           syarat = (get_pixel(pos_x+1,pos_y).same?(color1,color2) &&
  366.           get_pixel(pos_x-1,pos_y).same?(color1,color2)) ||
  367.           (get_pixel(pos_x,pos_y-1).same?(color1,color2) &&
  368.           get_pixel(pos_x,pos_y+1).same?(color1,color2)) ||
  369.           (get_pixel(pos_x+1,pos_y+1).same?(color1,color2) &&
  370.           get_pixel(pos_x-1,pos_y-1).same?(color1,color2)) ||
  371.           (get_pixel(pos_x-1,pos_y+1).same?(color1,color2) &&
  372.           get_pixel(pos_x+1,pos_y-1).same?(color1,color2))
  373.           if syarat && !get_pixel(pos_x,pos_y).same?(color1,color2)
  374.             set_pixel(pos_x,pos_y,color2)
  375.           end
  376.         end
  377.       end
  378.     end
  379.   end
  380.  
  381. end
  382.  
  383. class Coordinate
  384.   attr_accessor :x,:y
  385.  
  386.   def initialize(x,y)
  387.     @x = x
  388.     @y = y
  389.   end
  390.  
  391. end
  392.  
  393. module Math
  394.  
  395.   def self.radian(degree)
  396.     return (degree.to_f/180) * Math::PI
  397.   end
  398.  
  399.   def self.degree(radian)
  400.     return (radian.to_f/Math::PI) * 180
  401.   end
  402.  
  403. end
  404.  
  405. class Color
  406.  
  407.   def same?(*args)
  408.     return false unless args.all? {|c| c.is_a?(Color)}
  409.     return args.any?{|color| self.red == color.red &&
  410.       self.green == color.green && self.blue == color.blue &&
  411.       self.alpha == color.alpha}
  412.   end
  413.  
  414.   def empty?
  415.     return self.alpha <= 0
  416.   end
  417.  
  418. end
Advertisement
Add Comment
Please, Sign In to add comment