Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #-------------------------------------------------------------------------------
- # * Bitmap Word Wrapping
- # * Rewrite: dsiver144
- # * Original Author: KZIsAwesome
- #-------------------------------------------------------------------------------
- module KZIsAwesome
- module WordWrap
- # change this if you don't want wordwrap on by default.
- DEFAULT_WORDWRAP = true
- # change this if you want white space mode on by default.
- DEFAULT_WHITESPACE = false
- # change this if you want white space mode on by default.
- DEFAULT_COLLAPSE = true
- # change this to add a right margin to the window.
- DEFAULT_RIGHT_MARGIN = 0
- end
- end
- $imported = {} if $imported.nil?
- $imported["DSI-Bitmap"] = true
- class Bitmap
- def process_character(c, text, pos)
- c = ' ' if @convert_newlines && c == "\n"
- if @wordwrap && c =~ /[ \t]/
- c = '' if @collapse_whitespace && @lastc =~ /[\s\n\f]/
- if pos[:x] + get_next_word_size(c, text) > self.width - @right_margin
- process_new_line(text, pos)
- else
- process_normal_character(c, pos)
- end
- @lastc = c
- else
- @lastc = c
- case c
- when "\n" # New line
- process_new_line(text, pos)
- when "\f" # New page
- process_new_page(text, pos)
- when "\e" # Control character
- process_escape_character(obtain_escape_code(text), text, pos)
- else # Normal character
- process_normal_character(c, pos)
- end
- end
- end
- #--------------------------------------------------------------------------
- # * Destructively Get Control Code
- #--------------------------------------------------------------------------
- def obtain_escape_code(text)
- text.slice!(/^[\$\.\|\^!><\{\}\\]|^[A-Z]+/i)
- end
- #--------------------------------------------------------------------------
- # * Control Character Processing
- # code : the core of the control character
- # e.g. "C" in the case of the control character \C[1].
- #--------------------------------------------------------------------------
- def process_escape_character(code, text, pos)
- case code.upcase
- when 'C'
- change_color(text_color(obtain_escape_param(text)))
- when 'I'
- process_draw_icon(obtain_escape_param(text), pos)
- when '{'
- make_font_bigger
- when '}'
- make_font_smaller
- end
- end
- def get_next_word_size(c, text)
- # Split text by the next space/line/page and grab the first split
- nextword = text.split(/[\s\n\f]/, 2)[0]
- if nextword
- icons = 0
- if nextword =~ /\e/i
- # Get rid of color codes and YEA Message system outline colors
- nextword = nextword.split(/\e[oOcC]+\[\d*\]/).join
- # Get rid of message timing control codes
- nextword = nextword.split(/\e[\.\|\^<>!]/).join
- # Split text by the first non-icon escape code
- # (the hH is for compatibility with the Icon Hues script)
- nextword = nextword.split(/\e[^iIhH]+/, 2)[0]
- # Erase and count icons in remaining text
- nextword.gsub!(/\e[iIhH]+\[[\d,]*\]/) do
- icons += 1
- ''
- end if nextword
- end
- wordsize = (nextword ? text_size(c + nextword).width : text_size( c ).width)
- wordsize += icons * 24
- else
- wordsize = text_size( c ).width
- end
- return wordsize
- end
- def turn_on_wordwraping
- @wordwrap = true
- @right_margin = 0
- end
- def process_escape_character(code, text, pos)
- case code.upcase
- when 'WW'
- @wordwrap = true
- when 'NW'
- @wordwrap = false
- when 'WS'
- @convert_newlines = true
- when 'NL'
- @convert_newlines = false
- when 'CS'
- @collapse_whitespace = true
- when 'PRE'
- @collapse_whitespace = false
- when 'BR'
- process_new_line(text, pos)
- @lastc = "\n"
- when 'RM'
- @right_margin = obtain_escape_param(text)
- else
- case code.upcase
- when 'C'
- change_color(text_color(obtain_escape_param(text)))
- when 'I'
- process_draw_icon(obtain_escape_param(text), pos)
- when '{'
- make_font_bigger
- when '}'
- make_font_smaller
- end
- end
- # Recalculate the next word size and insert line breaks
- # (Needed primarily for font changes)
- if pos[:x] + get_next_word_size('', text) > self.width
- process_new_line(text, pos)
- end
- end
- #--------------------------------------------------------------------------
- # * Icon Drawing Process by Control Characters
- #--------------------------------------------------------------------------
- def process_draw_icon(icon_index, pos)
- draw_icon(icon_index, pos[:x], pos[:y])
- pos[:x] += 24
- end
- #--------------------------------------------------------------------------
- # * Destructively Get Control Code Argument
- #--------------------------------------------------------------------------
- def obtain_escape_param(text)
- text.slice!(/^\[\d+\]/)[/\d+/].to_i rescue 0
- end
- #--------------------------------------------------------------------------
- # * Get Text Color
- # n : Text color number (0..31)
- #--------------------------------------------------------------------------
- def text_color(n)
- @windowskin ||= Cache.system("Window")
- @windowskin.get_pixel(64 + (n % 8) * 8, 96 + (n / 8) * 8)
- end
- #--------------------------------------------------------------------------
- # * Get Alpha Value of Translucent Drawing
- #--------------------------------------------------------------------------
- def translucent_alpha
- return 160
- end
- #--------------------------------------------------------------------------
- # * Change Text Drawing Color
- # enabled : Enabled flag. When false, draw semi-transparently.
- #--------------------------------------------------------------------------
- def change_color(color, enabled = true)
- self.font.color.set(color)
- self.font.color.alpha = translucent_alpha unless enabled
- end
- #--------------------------------------------------------------------------
- # * Draw Point
- #--------------------------------------------------------------------------
- def draw_point(x,y,color=Color.new(255,255,255))
- self.fill_rect(x,y,2,2,color)
- end
- #--------------------------------------------------------------------------
- # * Reset Font Settings
- #--------------------------------------------------------------------------
- def reset_font_settings
- self.font.out_color = Font.default_out_color
- self.font.color = Font.default_color
- self.font.size = Font.default_size
- self.font.bold = Font.default_bold
- self.font.italic = Font.default_italic
- end
- #--------------------------------------------------------------------------
- # * Get Line Height
- #--------------------------------------------------------------------------
- def line_height
- return 24
- end
- #--------------------------------------------------------------------------
- # * Increase Font Size
- #--------------------------------------------------------------------------
- def make_font_bigger
- self.font.size += 8 if self.font.size <= 64
- end
- #--------------------------------------------------------------------------
- # * Decrease Font Size
- #--------------------------------------------------------------------------
- def make_font_smaller
- self.font.size -= 8 if self.font.size >= 16
- end
- #--------------------------------------------------------------------------
- # * Calculate Line Height
- # restore_font_size : Return to original font size after calculating
- #--------------------------------------------------------------------------
- def calc_line_height(text, restore_font_size = true)
- result = [line_height, self.font.size].max
- last_font_size = self.font.size
- text.slice(/^.*$/).scan(/\e[\{\}]/).each do |esc|
- make_font_bigger if esc == "\e{"
- make_font_smaller if esc == "\e}"
- result = [result, self.font.size].max
- end
- self.font.size = last_font_size if restore_font_size
- result
- end
- #--------------------------------------------------------------------------
- # * Draw Text with Control Characters
- #--------------------------------------------------------------------------
- def draw_text_ex(x, y, text,reset_font=true)
- reset_font_settings if reset_font == true
- #self.font.size -= 3
- text = convert_escape_characters(text)
- pos = {:x => x, :y => y, :new_x => x, :height => calc_line_height(text)}
- process_character(text.slice!(0, 1), text, pos) until text.empty?
- end
- #~ #--------------------------------------------------------------------------
- #~ # * Character Processing
- #~ # c : Characters
- #~ # text : A character string buffer in drawing processing (destructive)
- #~ # pos : Draw position {:x, :y, :new_x, :height}
- #~ #--------------------------------------------------------------------------
- #~ def process_character(c, text, pos)
- #~ case c
- #~ when "\n" # New line
- #~ process_new_line(text, pos)
- #~ else # Normal character
- #~ process_normal_character(c, pos)
- #~ end
- #~ end
- #--------------------------------------------------------------------------
- # * Normal Character Processing
- #--------------------------------------------------------------------------
- def process_normal_character(c, pos)
- text_width = text_size(c).width
- draw_text(pos[:x], pos[:y], text_width * 2, pos[:height], c)
- pos[:x] += text_width
- end
- #--------------------------------------------------------------------------
- # * New Line Character Processing
- #--------------------------------------------------------------------------
- def process_new_line(text, pos)
- pos[:x] = pos[:new_x]
- pos[:y] += pos[:height]
- pos[:height] = calc_line_height(text)
- end
- #--------------------------------------------------------------------------
- # * Preconvert Control Characters
- # As a rule, replace only what will be changed into text strings before
- # starting actual drawing. The character "\" is replaced with the escape
- # character (\e).
- #--------------------------------------------------------------------------
- def convert_escape_characters(text)
- result = text.to_s.clone
- result.gsub!(/\\/) { "\e" }
- result.gsub!(/\e\e/) { "\\" }
- result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i] }
- result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i] }
- result.gsub!(/\eN\[(\d+)\]/i) { actor_name($1.to_i) }
- result.gsub!(/\eP\[(\d+)\]/i) { party_member_name($1.to_i) }
- result.gsub!(/\eG/i) { Vocab::currency_unit }
- result
- end
- #--------------------------------------------------------------------------
- # * Get Alpha Value of Translucent Drawing
- #--------------------------------------------------------------------------
- def translucent_alpha
- return 160
- end
- #--------------------------------------------------------------------------
- # * Draw Icon
- # enabled : Enabled flag. When false, draw semi-transparently.
- #--------------------------------------------------------------------------
- def draw_icon(icon_index, x, y, disable = false)
- bitmap = Cache.system("Iconset")
- rect = Rect.new(icon_index % 16 * 24, icon_index / 16 * 24, 24, 24)
- self.blt(x, y, bitmap, rect, 255)
- end
- end
- class DarkBitmap < Bitmap
- attr_accessor :cur_width
- attr_accessor :tline_height
- include KZIsAwesome::WordWrap
- def initialize(width,height)
- super(width,height)
- @wordwrap = DEFAULT_WORDWRAP
- @convert_newlines = DEFAULT_WHITESPACE
- @collapse_whitespace = DEFAULT_COLLAPSE
- @right_margin = DEFAULT_RIGHT_MARGIN
- @lastc = "\n"
- end
- def process_character(c, text, pos)
- c = ' ' if @convert_newlines && c == "\n"
- if @wordwrap && c =~ /[ \t]/
- c = '' if @collapse_whitespace && @lastc =~ /[\s\n\f]/
- if pos[:x] + get_next_word_size(c, text) > self.width - @right_margin
- process_new_line(text, pos)
- else
- process_normal_character(c, pos)
- end
- @lastc = c
- else
- @lastc = c
- case c
- when "\n" # New line
- process_new_line(text, pos)
- when "\f" # New page
- process_new_page(text, pos)
- when "\e" # Control character
- process_escape_character(obtain_escape_code(text), text, pos)
- else # Normal character
- process_normal_character(c, pos)
- end
- end
- end
- #--------------------------------------------------------------------------
- # * Destructively Get Control Code
- #--------------------------------------------------------------------------
- def obtain_escape_code(text)
- text.slice!(/^[\$\.\|\^!><\{\}\\]|^[A-Z]+/i)
- end
- #--------------------------------------------------------------------------
- # * Control Character Processing
- # code : the core of the control character
- # e.g. "C" in the case of the control character \C[1].
- #--------------------------------------------------------------------------
- def process_escape_character(code, text, pos)
- case code.upcase
- when 'C'
- change_color(text_color(obtain_escape_param(text)))
- when 'I'
- process_draw_icon(obtain_escape_param(text), pos)
- when '{'
- make_font_bigger
- when '}'
- make_font_smaller
- end
- end
- def get_next_word_size(c, text)
- # Split text by the next space/line/page and grab the first split
- nextword = text.split(/[\s\n\f]/, 2)[0]
- if nextword
- icons = 0
- if nextword =~ /\e/i
- # Get rid of color codes and YEA Message system outline colors
- nextword = nextword.split(/\e[oOcC]+\[\d*\]/).join
- # Get rid of message timing control codes
- nextword = nextword.split(/\e[\.\|\^<>!]/).join
- # Split text by the first non-icon escape code
- # (the hH is for compatibility with the Icon Hues script)
- nextword = nextword.split(/\e[^iIhH]+/, 2)[0]
- # Erase and count icons in remaining text
- nextword.gsub!(/\e[iIhH]+\[[\d,]*\]/) do
- icons += 1
- ''
- end if nextword
- end
- wordsize = (nextword ? text_size(c + nextword).width : text_size( c ).width)
- wordsize += icons * 24
- else
- wordsize = text_size( c ).width
- end
- return wordsize
- end
- def process_escape_character(code, text, pos)
- case code.upcase
- when 'WW'
- @wordwrap = true
- when 'NW'
- @wordwrap = false
- when 'WS'
- @convert_newlines = true
- when 'NL'
- @convert_newlines = false
- when 'CS'
- @collapse_whitespace = true
- when 'PRE'
- @collapse_whitespace = false
- when 'BR'
- process_new_line(text, pos)
- @lastc = "\n"
- when 'RM'
- @right_margin = obtain_escape_param(text)
- else
- case code.upcase
- when 'C'
- change_color(text_color(obtain_escape_param(text)))
- when 'I'
- process_draw_icon(obtain_escape_param(text), pos)
- when '{'
- make_font_bigger
- when '}'
- make_font_smaller
- end
- end
- # Recalculate the next word size and insert line breaks
- # (Needed primarily for font changes)
- if pos[:x] + get_next_word_size('', text) > self.width
- process_new_line(text, pos)
- end
- end
- #--------------------------------------------------------------------------
- # * Draw Point
- #--------------------------------------------------------------------------
- def draw_point(x,y,color=Color.new(255,255,255))
- self.fill_rect(x,y,2,2,color)
- end
- #--------------------------------------------------------------------------
- # * Reset Font Settings
- #--------------------------------------------------------------------------
- def reset_font_settings
- self.font.out_color = Font.default_out_color
- self.font.color = Font.default_color
- self.font.size = Font.default_size
- self.font.bold = Font.default_bold
- self.font.italic = Font.default_italic
- end
- #--------------------------------------------------------------------------
- # * Get Line Height
- #--------------------------------------------------------------------------
- def line_height
- return tline_height || 24
- end
- #--------------------------------------------------------------------------
- # * Increase Font Size
- #--------------------------------------------------------------------------
- def make_font_bigger
- self.font.size += 8 if self.font.size <= 64
- end
- #--------------------------------------------------------------------------
- # * Decrease Font Size
- #--------------------------------------------------------------------------
- def make_font_smaller
- self.font.size -= 8 if self.font.size >= 16
- end
- #--------------------------------------------------------------------------
- # * Calculate Line Height
- # restore_font_size : Return to original font size after calculating
- #--------------------------------------------------------------------------
- def calc_line_height(text, restore_font_size = true)
- result = [line_height, self.font.size].max
- last_font_size = self.font.size
- text.slice(/^.*$/).scan(/\e[\{\}]/).each do |esc|
- make_font_bigger if esc == "\e{"
- make_font_smaller if esc == "\e}"
- result = [result, self.font.size].max
- end
- self.font.size = last_font_size if restore_font_size
- result
- end
- #--------------------------------------------------------------------------
- # * Draw Text with Control Characters
- #--------------------------------------------------------------------------
- def draw_text_ex(x, y, text,reset_font=true)
- reset_font_settings if reset_font == true
- #self.font.size -= 3
- text = convert_escape_characters(text)
- pos = {:x => x, :y => y, :new_x => x, :height => calc_line_height(text)}
- process_character(text.slice!(0, 1), text, pos) until text.empty?
- end
- #~ #--------------------------------------------------------------------------
- #~ # * Character Processing
- #~ # c : Characters
- #~ # text : A character string buffer in drawing processing (destructive)
- #~ # pos : Draw position {:x, :y, :new_x, :height}
- #~ #--------------------------------------------------------------------------
- #~ def process_character(c, text, pos)
- #~ case c
- #~ when "\n" # New line
- #~ process_new_line(text, pos)
- #~ else # Normal character
- #~ process_normal_character(c, pos)
- #~ end
- #~ end
- #--------------------------------------------------------------------------
- # * Normal Character Processing
- #--------------------------------------------------------------------------
- def process_normal_character(c, pos)
- text_width = text_size(c).width
- draw_text(pos[:x], pos[:y], text_width * 2, pos[:height], c)
- pos[:x] += text_width
- end
- #--------------------------------------------------------------------------
- # * New Line Character Processing
- #--------------------------------------------------------------------------
- def process_new_line(text, pos)
- pos[:x] = pos[:new_x]
- pos[:y] += pos[:height]
- pos[:height] = calc_line_height(text)
- end
- #--------------------------------------------------------------------------
- # * Preconvert Control Characters
- # As a rule, replace only what will be changed into text strings before
- # starting actual drawing. The character "\" is replaced with the escape
- # character (\e).
- #--------------------------------------------------------------------------
- def convert_escape_characters(text)
- result = text.to_s.clone
- result.gsub!(/\\/) { "\e" }
- result.gsub!(/\e\e/) { "\\" }
- result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i] }
- result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i] }
- result.gsub!(/\eN\[(\d+)\]/i) { actor_name($1.to_i) }
- result.gsub!(/\eP\[(\d+)\]/i) { party_member_name($1.to_i) }
- result.gsub!(/\eG/i) { Vocab::currency_unit }
- result
- end
- #--------------------------------------------------------------------------
- # * Get Alpha Value of Translucent Drawing
- #--------------------------------------------------------------------------
- def translucent_alpha
- return 160
- end
- #--------------------------------------------------------------------------
- # * Draw Icon
- # enabled : Enabled flag. When false, draw semi-transparently.
- #--------------------------------------------------------------------------
- def draw_icon(icon_index, x, y, disable = false)
- bitmap = Cache.system("Iconset")
- rect = Rect.new(icon_index % 16 * 24, icon_index / 16 * 24, 24, 24)
- self.blt(x, y, bitmap, rect, 255)
- end
- #--------------------------------------------------------------------------
- # * Draw a Line
- #--------------------------------------------------------------------------
- def draw_line(x1,y1,x2,y2,color=Color.new(255,255,255))
- dx = (x2-x1).abs
- dy = (y2-y1).abs
- c = dx - dy
- c2 = 2*c
- x = x1
- y = y1
- x_unit = 1
- y_unit = 1
- if x2 < x1
- x_unit = -x_unit
- end
- if y2 < y1
- y_unit = -y_unit
- end
- if x1 == x2
- while y != y2
- y += y_unit
- self.set_pixel(x,y,color)
- self.fill_rect(x,y,2,2,color)
- end
- else
- if y == y2
- while x != x2
- x += x_unit
- self.set_pixel(x,y,color)
- self.fill_rect(x,y,2,2,color)
- end
- else
- if (x1 != x2 ) and ( y1 != y2 )
- while x != x2+1
- c2 = 2*c
- if c2 > -dy
- c = c - dy
- x = x + x_unit
- end
- if c2 < dx
- c = c + dx
- y += y_unit
- end
- self.set_pixel(x,y,color)
- self.fill_rect(x,y,2,2,color)
- end
- end
- end
- end
- end
- end
- module Math
- def self.get_x_y(angle,radius,offsetx=0,offsety=0)
- x = Math.sin(angle)*radius + offsetx
- y = -Math.cos(angle)*radius + offsety
- return [x,y]
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement