The RPG Maker Resource Kit

RMRK RPG Maker Creation => VX Ace => VXA Scripts Database => Topic started by: AbsoluteIce on July 18, 2013, 04:28:01 PM

Title: [VXA] TheoAllen's Bitmap Extra Addons
Post by: AbsoluteIce on July 18, 2013, 04:28:01 PM
<TheoAllen's Bitmap Extra Addons>
Version: <2.0>
Author: <TheoAllen>
Translator: <AbsoluteIce>
Date: <July>, <13>, <2013>

Version History



Planned Future Versions


Description


This script is used to depict the various shapes in the class bitmap.

Features


Screenshots

(https://rmrk.net/proxy.php?request=http%3A%2F%2F3.bp.blogspot.com%2F-dOZ65UXeO-4%2FUa29C0vYYZI%2FAAAAAAAAAfE%2FkSnfLfDePHs%2Fs1600%2Fshape6.jpg&hash=07b41321195f8a5d4fbe0316fb25dfc5195acbc4)

Instructions

Put this script below materials, and above main in the script section.

Script


Code: [Select]
# =============================================================================
# TheoAllen - Bitmap Extra Addons
# Version : 2.0
# Contact : www.rpgmakerid.com (or) http://theolized.blogspot.com
# (This script is translated by AbsoluteIce)
# =============================================================================
($imported ||= {})[:Theo_BitmapAddons]=true
# =============================================================================
# CHANGE LOGS:
# -----------------------------------------------------------------------------
# 2013.06.04 - Massive update. Increment to version 2.0
#            - added draw gradient_line
#            - added draw_polygon
#            - added draw_ellipse
#            - etc ....
# 2013.05.21 - Started and Finished script
# =============================================================================
=begin
  # ===========================================================================
  Introduction :
  This script is used to depict the various shapes in the class bitmap. The
  available methods are ;
 
  Line :
  - draw_line
  - gradient_line
 
  Circle :
  - draw_ellipse
  - draw_circle
  - fill_ellipse
  - fill_circle
 
  Polygon :
  - draw_shape
  - draw_polygon
  - draw_shape_params
  - fill_polygon
  - fill_shape_params
 
  class Color :
  - same?(color1[,color2[,color3,...)
  - empty?
 
  ===========================================================================
  How to use :
  ===========================================================================
  Line :
  ---------------------------------------------------------------------------
  *) bitmap.draw_line(x1,y1,x2,y2[,color])
  *) bitmap.draw_line(x1,y1,x2,y2[,color]) {|coordinates| }
 
  to draw the lines from coordinates (x1,y1) to the coordinates (x2,y2). if you use the
  argument block, the result is the coordinates of each of the individual center in the
  form of an array. by default the coordinates is[0]
  and position x and coordinate[1] are position y.
 
  As default, if the color is ignored, then the default will be white.
  Color.new(255,255,255)
 
  *) bitmap.gradient_line(x1,y1,x2,y2,color1,color2)
 
  to draw the coordinates from (x1,y1) to the coordinates of (x2,y2) to the gradients from color1 and color 2.
  For the moment, this still isn't supported.
 
  ---------------------------------------------------------------------------
  Circle :
  ---------------------------------------------------------------------------
 
  *) bitmap.draw_ellipse(x,y,horz,vert[,color[,think[,step]]])
  To draw the argument with the picture like so :
  x     >> coordinates of X
  y     >> coordinates of Y
  horz  >> Horizontal Radius
  vert  >> Vertical Radius
  color >> color of the line (default : white)
  think >> thickness of line (default : 1)
  step  >> distance from the dot (default : 0.1)
 
  *) bitmap.draw_circle(x,y,radius[,color[,think[,step]]])
  To draw the image circle. The same argument draws the ellipse.
  the only difference is in the radius.
 
  *) bitmap.fill_ellipse(x,y,horz,vert,color1[,color2[,step]])
  To fill the ellipse. x,y,horz,vert are the same. color1 is to outline.
  If the color isn't filled, then the color shall be the same as color 1.
 
  *) bitmap.fill_circle(x,y,radius,color1[,color2[,step]])
  Overall, it's the same. :v
 
  ---------------------------------------------------------------------------
  Polygon :
  ---------------------------------------------------------------------------
 
  *) bitmap.draw_shape(x,y,params,length[,color1[,bone[,fill[,color2])
  Ini adalah method dasar untuk ngedraw poligon. Jadi jangan heran kalo
  argumennya kelewat banyak. Untuk keterangan argumennya sebagai berikut
 
  x       >> Coordinates of X
  y       >> Coordinates of Y
  params  >> parameter of the array. eg : [100,50,100,50,100,50]
  length  >> length (maksimal)
  color1  >> color to draw the line (default : white)
  bone    >> if true maka kerangka poligon akan didraw (default : true)
  fill    >> if true the polygon will be filled with color (default : false)
  color2  >> color to fill (default : same as color1)
 
  *) bitmap.draw_polygon(x,y,corner,length,color1[,bone[,color2]])
 
  Method to draw the polygon. overall same draw_shape. Corner is the
  amount of angle of picture. if you enter 5, it will be by default as
  a pentagon. Bone as default true (picture of framework). color2 is the
  color to the framework (same as color 1).
 
  *) bitmap.draw_shape_params(x,y,params,length,color1[,bone[,color2]])
 
  Method to draw the parameter (atk,def,int,agi,dll...) in the shape of a pentagon.
  Overall, its the same as draw_polygon. The only difference is the argument block. For
  example, the usage is like so :
 
  params = [atk,def,matk,mdf,agi,luk]
  bitmap.draw_shape(50,50,params,50,Color.new(0,255,0))
 
  *) bitmap.fill_polygon(x,y,corner,length,color1[,color2])
  Make the picture of the polygon filled with color. Color1 is the outline,
  and color2 is the fill. if color2 is ignored, Then it will be the same as
  color1.
 
  *) bitmap.fill_shape_params(x,y,params,length,color1[,color2])
  Overall, its the same as fill_polygon. The only difference is the parameters.

  ---------------------------------------------------------------------------
  Other information :
  ---------------------------------------------------------------------------
  I also added something in the class color which is :
 
  *) color.same?(color1[,color2[,color3,dst...]]])
  to check what are the components of color if the methods
  are the same or not.
 
  *) color.empty?
  return true if the alpha is 0.
 
  ===========================================================================
  Terms of use :
  ---------------------------------------------------------------------------
  Credit TheoAllen, and ModernAlgebra for inspiration. Use this only for non-
  commercial projects.
 
  http://creativecommons.org/licenses/by-nc-sa/2.5/ca/
 
  ===========================================================================
  Special Thanks :
  - Modern Algebra for the idea of bitmap addons

=end
# =============================================================================
class Bitmap
  attr_accessor :start_degree
 
  alias theo_bitmap_init initialize
  def initialize(*args)
    theo_bitmap_init(*args)
    @start_degree = 270
  end
 
  def draw_line(x1,y1,x2,y2,color=Color.new(255,255,255),color_set_skip = false)
    if x1 == x2 && y1 == y2
      set_pixel(x1,y1,color)
      yield [x1,x2] if block_given?
      return
    end
    jarak_x = (x2-x1)
    jarak_y = (y2-y1)
    if jarak_y == 0 || jarak_x == 0
      if jarak_y == 0
        draw_horz(x1,y1,jarak_x,color)
        for j in 0..jarak_x
          yield [x1,y1]
          x1 += 1
        end if block_given?
      elsif jarak_x.abs == 0
        draw_vert(x1,y1,jarak_y,color)
        for k in 0..jarak_y
          yield [x1,y1]
          y1 += 1
        end if block_given?
      end
      return
    end
    maximum = [jarak_x.abs,jarak_y.abs].max
    rasio_x = jarak_x / maximum.to_f
    rasio_y = jarak_y / maximum.to_f
    real_x = x1.to_f
    real_y = y1.to_f
    for i in 0..maximum
      set_pixel(x1,y1,color) unless get_pixel(x1,y1).same?(color) ||
        color_set_skip ? !get_pixel(x1,y1).empty? : false
      real_x += rasio_x
      real_y += rasio_y
      yield [x1,y1] if block_given?
      x1 = real_x.round
      y1 = real_y.round
    end
  end
 
  def gradient_line(x1,y1,x2,y2,color1 = Color.new(255,255,255),color2 = color1)
    jarak_x = (x2-x1)
    jarak_y = (y2-y1)
    radius = Math.sqrt((jarak_x**2) + (jarak_y**2))
    red_diff = (color2.red - color1.red) / radius
    green_diff = (color2.green - color1.green) / radius
    blue_diff = (color2.blue - color1.blue) / radius
    alpha_diff = (color2.alpha - color1.alpha) / radius
    red = color1.red
    green = color1.green
    blue = color1.blue
    alpha = color1.alpha
    if jarak_y.abs == 0 || jarak_x.abs == 0
      gradient_fill_rect(x1,y1,1,jarak_y,color1,color2,true) if jarak_y.abs == 0
      gradient_fill_rect(x1,y1,jarak_x,1,color1,color2) if jarak_x.abs == 0
      return
    end
    maximum = [jarak_x.abs,jarak_y.abs].max
    rasio_x = jarak_x / maximum.to_f
    rasio_y = jarak_y / maximum.to_f
    real_x = x1.to_f
    real_y = y1.to_f
    for i in 0..maximum
      new_color = Color.new(red,green,blue,alpha)
      set_pixel(x1,y1,new_color) unless get_pixel(x1,y1).same?(new_color)
      real_x += rasio_x
      real_y += rasio_y
      x1 = real_x.round
      y1 = real_y.round
      red += red_diff
      blue += blue_diff
      green += green_diff
      alpha += alpha_diff
    end
  end
 
  def draw_horz(x,y,width,color = Color.new(255,255,255))
    if width < 0
      fill_rect(x+width+1,y,width.abs,1,color)
      return
    end
    fill_rect(x,y,width,1,color)
  end
 
  def draw_vert(x,y,height,color = Color.new(255,255,255))
    if height < 0
      fill_rect(x,y+height+1,1,height.abs,color)
      return
    end
    fill_rect(x,y,1,height,color)
  end
 
  def draw_ellipse(x,y=0,horz=1,vert=1,color=Color.new(255,255,255),think=1,
      step=0.1)
    return if think < 1
    ori_x = x
    ori_y = y
    x += horz
    degree = 0.0
    while degree <= 360
      yield [x,y] if block_given?
      set_pixel(x,y,color) unless get_pixel(x,y).same?(color)
      x = Math.cos(Math.radian(degree)) * horz + ori_x
      y = Math.sin(Math.radian(degree)) * vert + ori_y
      degree = [degree+step,361].min
    end
    if think > 1
      draw_ellipse(ori_x+1,ori_y+1,horz,vert,think-1,color,step)
      draw_ellipse(ori_x-1,ori_y-1,horz,vert,think-1,color,step)
      draw_ellipse(ori_x+1,ori_y-1,horz,vert,think-1,color,step)
      draw_ellipse(ori_x-1,ori_y+1,horz,vert,think-1,color,step)
    end
  end
 
  def draw_circle(x,y,radius,color=Color.new(255,255,255),think=1)
    draw_ellipse(x,y,radius,radius,think,color) {|coordinate|
      yield coordinate if block_given?
    }
  end
 
  def fill_circle(x,y,radius,color1,color2=color1,step=0.1)
    fill_ellipse(x,y,radius,radius,color1,color2,step)
  end
 
  def fill_ellipse(x,y,horz,vert,color1,color2=color1,step=0.1)
    draw_ellipse(x,y,horz,vert,1,color1,0.5) {|cor|
      draw_line(x,y,cor[0],cor[1],color2,true)
    }
    for i in 0..1
      for baris in 0..horz*2
        for kolom in 0..vert*2
          pos_x = baris-horz+x
          pos_y = kolom-vert+y
          syarat = (get_pixel(pos_x+1,pos_y).same?(color1,color2) &&
          get_pixel(pos_x-1,pos_y).same?(color1,color2)) ||
          (get_pixel(pos_x,pos_y-1).same?(color1,color2) &&
          get_pixel(pos_x,pos_y+1).same?(color1,color2)) ||
          (get_pixel(pos_x+1,pos_y+1).same?(color1,color2) &&
          get_pixel(pos_x-1,pos_y-1).same?(color1,color2)) ||
          (get_pixel(pos_x-1,pos_y+1).same?(color1,color2) &&
          get_pixel(pos_x+1,pos_y-1).same?(color1,color2))
          if syarat && !get_pixel(pos_x,pos_y).same?(color1,color2)
            set_pixel(pos_x,pos_y,color2)
          end
        end
      end
    end
  end
 
  def draw_polygon(x,y,corner,length,color1,bone=true,color2=color1)
    return unless corner.is_a?(Numeric)
    draw_shape(x,y,Array.new(corner){1},length,color1,bone,false,color2)
  end
 
  def fill_polygon(x,y,corner,length,color1,color2=color1)
    return unless corner.is_a?(Numeric)
    draw_shape(x,y,Array.new(corner),length,color1,false,true,color2)
  end
 
  def draw_shape_params(x,y,params,length,color1,bone=true,color2=color1)
    draw_shape(x,y,params,length,color1,bone,false,color2)
  end
 
  def fill_shape_params(x,y,params,length,color1,color2=color1)
    draw_shape(x,y,params,length,color1,false,true,color2)
  end
 
  def draw_shape(x,y,params,length,color1 = Color.new(255,255,255),
      include_bone = true ,fill=false,color2 = color1)
    return unless params.is_a?(Array)
    return unless params.size >= 3
    degree_plus = 360 / params.size
    degree = @start_degree
    coordinate = []
    edge = []
    params.each do |i|
      x_des = x + Math.cos(Math.radian(degree)) * (length*(i.to_f/params.max))
      y_des = y + Math.sin(Math.radian(degree)) * (length*(i.to_f/params.max))
      draw_line(x,y,x_des,y_des,color2) if include_bone
      degree += degree_plus
      coordinate.push(Coordinate.new(x_des,y_des))
    end
    for i in -1..coordinate.size-2
      c = coordinate
      draw_line(c[i].x,c[i].y,c[i+1].x,c[i+1].y,color1) {|cor| edge.push(cor)}
    end
    return unless fill
    edge.each do |line|
      draw_line(x,y,line[0],line[1],color2,true)
    end
    for i in 0..1
      for baris in 0..length*2
        for kolom in 0..length*2
          pos_x = baris-length+x
          pos_y = kolom-length+y
          syarat = (get_pixel(pos_x+1,pos_y).same?(color1,color2) &&
          get_pixel(pos_x-1,pos_y).same?(color1,color2)) ||
          (get_pixel(pos_x,pos_y-1).same?(color1,color2) &&
          get_pixel(pos_x,pos_y+1).same?(color1,color2)) ||
          (get_pixel(pos_x+1,pos_y+1).same?(color1,color2) &&
          get_pixel(pos_x-1,pos_y-1).same?(color1,color2)) ||
          (get_pixel(pos_x-1,pos_y+1).same?(color1,color2) &&
          get_pixel(pos_x+1,pos_y-1).same?(color1,color2))
          if syarat && !get_pixel(pos_x,pos_y).same?(color1,color2)
            set_pixel(pos_x,pos_y,color2)
          end
        end
      end
    end
  end
 
end

class Coordinate
  attr_accessor :x,:y
 
  def initialize(x,y)
    @x = x
    @y = y
  end
 
end

module Math
 
  def self.radian(degree)
    return (degree.to_f/180) * Math::PI
  end
 
  def self.degree(radian)
    return (radian.to_f/Math::PI) * 180
  end
 
end

class Color
 
  def same?(*args)
    return false unless args.all? {|c| c.is_a?(Color)}
    return args.any?{|color| self.red == color.red &&
      self.green == color.green && self.blue == color.blue &&
      self.alpha == color.alpha}
  end
 
  def empty?
    return self.alpha <= 0
  end
 
end
Credits



Thanks


Support


Either post on this thread, or pm TheoAllen on RMRK.

Known Compatibility Issues

None that I know of as of now.

Demo

Unavaliable yet as of now.

Author's Notes


Please bear in mind that there are alot of translation errors in this script. I'll try to correct them as
fast as I possibly can.

Questions

None at the moment.



Terms of Use


Credit the creator, TheoAllen, and Modern Algebra for giving the idea for this script.
Do not claim as your own. If you want to use for a commercial project, share the profit with him. And don't forget to give him a free copy of the game.[/list]
Title: Re: [VXA] TheoAllen's Bitmap Extra Addons
Post by: estriole on July 19, 2013, 02:01:33 AM
nice script.
i guess i'll use that code to improve my status scene :D.
Title: Re: [VXA] TheoAllen's Bitmap Extra Addons
Post by: AbsoluteIce on July 19, 2013, 05:11:17 AM
nice script.
i guess i'll use that code to improve my status scene :D.
Alright, good luck.
Just remember to credit TheoAllen~
Title: Re: [VXA] TheoAllen's Bitmap Extra Addons
Post by: TheoAllen on July 21, 2013, 10:16:38 PM
I forgot to inform that the fill polygon function isn't perfect yet. And I don't know how to fix. Better don't use it
Anyway, thanks for translate :D