Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #==============================================================================
- # ■ Draw Text Ex by OneEyedEagle (http://oneeyedeagle.lofter.com/)
- # License - MIT License: http://opensource.org/licenses/mit-license.php
- #==============================================================================
- # - 2020.7.23.21 Adds automatic line feeds; delete assigned line positions
- #==============================================================================
- # - This script provides a way to draw escaped text on a bitmap.
- #-----------------------------------------------------------------------------
- # - Create a new text drawing processing object (not yet drawn)
- #
- # d = Process_DrawTextEX.new(text[, params, bitmap])
- #
- # text - text strings with escaped characters
- # params - control parameter set drawn (see initialize method)
- # bitmap - bitmap objects on which text needs to be drawn.
- #
- # - Call d.width and d.height to get total width and height for text to be drawn
- # - Use d.bind_bitmap(bitmap[, dispose_old]) to rebind the bitmap object to
- # where dispose_old releases the old bitmap when true is passed.
- # - Drawing with d.run
- #==============================================================================
- class Process_DrawTextEX
- attr_reader :text, :info
- #--------------------------------------------------------------------------
- # ● Initialize
- # params
- # :font_size → Draw the initial text size
- # :x0 → Specify the x value for each line
- # :y0 → Specify the y value for each line
- # :w → Set the max width, if exceeded, automatic line feed will be performed.
- # :lhd → Distance to the next line on a line feed.
- #--------------------------------------------------------------------------
- def initialize(text, params = {}, bitmap = nil)
- @text = convert_escape_characters(text)
- @bitmap = bitmap || Cache.empty_bitmap
- @params = params
- @params[:font_size] ||= @bitmap.font.size
- @params[:x0] ||= 0
- @params[:y0] ||= 0
- @params[:w] ||= nil
- @params[:lhd] ||= 0
- @info = {}
- @info[:w] ||= [] # line_index => width
- @info[:h] ||= [] # line_index => height
- run(false)
- end
- #--------------------------------------------------------------------------
- # ● Bind bitmap
- #--------------------------------------------------------------------------
- def bind_bitmap(bitmap, dispose_old = true)
- @bitmap.dispose if @bitmap && dispose_old
- @bitmap = bitmap
- end
- #--------------------------------------------------------------------------
- # ● Width
- #--------------------------------------------------------------------------
- def width
- @info[:w].max
- end
- #--------------------------------------------------------------------------
- # ● Height
- #--------------------------------------------------------------------------
- def height
- r = @info[:h].inject(0) { |s, v| s += v }
- r = r + (@info[:h].size - 1) * @params[:lhd] + @params[:y0]
- r
- end
- #--------------------------------------------------------------------------
- # ● Convert controllers
- # Replaces control with the actual content before the actual drawing.
- # To reduce ambiguity, this \\ replaced by an escape character (\e) first.
- #--------------------------------------------------------------------------
- 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
- #--------------------------------------------------------------------------
- # ● Actor name (n)
- #--------------------------------------------------------------------------
- def actor_name(n)
- actor = n >= 1 ? $game_actors[n] : nil
- actor ? actor.name : ""
- end
- #--------------------------------------------------------------------------
- # ● Party member name
- #--------------------------------------------------------------------------
- def party_member_name(n)
- actor = n >= 1 ? $game_party.members[n - 1] : nil
- actor ? actor.name : ""
- end
- #--------------------------------------------------------------------------
- # ● Run
- #--------------------------------------------------------------------------
- def run(flag_draw = true)
- text = @text.clone
- pos = { :line => -1, :x0 => @params[:x0], :x => 0,
- :y0 => @params[:y0], :y => 0,
- :w => 0, :h => 0, :flag_draw => flag_draw }
- @bitmap.font.size = @params[:font_size]
- process_new_line(pos)
- process_character(text.slice!(0, 1), text, pos) until text.empty?
- end
- #--------------------------------------------------------------------------
- # ● Process new line
- #--------------------------------------------------------------------------
- def process_new_line(pos)
- pos[:line] += 1
- pos[:x0] = @params[:x0] || pos[:x0]
- pos[:x] = pos[:x0]
- pos[:y0] = (pos[:y0] + pos[:h])
- pos[:y0] += @params[:lhd] if pos[:line] > 0
- pos[:y] = pos[:y0]
- if pos[:flag_draw]
- pos[:w] = @info[:w][pos[:line]]
- pos[:h] = @info[:h][pos[:line]]
- else
- @info[:w][pos[:line]] = 0
- @info[:h][pos[:line]] = 0
- end
- end
- #--------------------------------------------------------------------------
- # ● Process auto new line
- #--------------------------------------------------------------------------
- def process_auto_new_line(pos, w)
- if @params[:w] && pos[:x] + w > @params[:w]
- process_new_line(pos)
- pos[:h] = 0
- end
- end
- #--------------------------------------------------------------------------
- # ● Text controls
- # c : Text
- # text : Drawing string cache in processing (the string may be modified)
- # pos : position {:x, :y, :new_x, :height}
- #--------------------------------------------------------------------------
- def process_character(c, text, pos)
- case c
- when "\r" # return
- return
- when "\n" # line
- process_new_line(pos)
- when "\e" # controller
- process_escape_character(obtain_escape_code(text), text, pos)
- else # alphabet
- process_normal_character(c, pos)
- end
- end
- #--------------------------------------------------------------------------
- # ● Process normal character
- #--------------------------------------------------------------------------
- def process_normal_character(c, pos)
- r = @bitmap.text_size(c); w = r.width; h = r.height
- process_draw_before(pos[:x], pos[:y], w, h, pos)
- @bitmap.draw_text(pos[:x], pos[:y], w * 2, h, c) if pos[:flag_draw]
- process_draw_after(pos[:x], pos[:y], w, h, pos)
- end
- #--------------------------------------------------------------------------
- # ● Process draw before
- #--------------------------------------------------------------------------
- def process_draw_before(x, y, w, h, pos)
- process_auto_new_line(pos, w)
- pos[:y] = pos[:y0] + pos[:h] - h if pos[:h] > h
- end
- #--------------------------------------------------------------------------
- # ● Process draw after
- #--------------------------------------------------------------------------
- def process_draw_after(x, y, w, h, pos)
- pos[:x] += w
- pos[:y] = pos[:y0]
- pos[:w] += w
- pos[:h] = h if pos[:h] < h
- if !pos[:flag_draw]
- @info[:w][pos[:line]] = pos[:w]
- @info[:h][pos[:line]] = pos[:h]
- end
- end
- #--------------------------------------------------------------------------
- # ● Get actual form of the controller (this method corrupts the raw data)
- #--------------------------------------------------------------------------
- def obtain_escape_code(text)
- text.slice!(/^[\$\.\|\^!><\{\}\\]|^[A-Z]+/i)
- end
- #--------------------------------------------------------------------------
- # ● Get parameters of the controller (this method corrupts the raw data)
- #--------------------------------------------------------------------------
- def obtain_escape_param(text)
- text.slice!(/^\[\d+\]/)[/\d+/].to_i rescue 0
- end
- #--------------------------------------------------------------------------
- # ● Process escape character
- # code : The actual form of the controller (e.g. "\C[1]" is "C").
- # text : Drawing the string cache in process (the string may be modified)
- # pos : Draw position {:x, :y, :new_x, :height}
- #--------------------------------------------------------------------------
- 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
- #--------------------------------------------------------------------------
- # ● Change color
- # enabled : Valid flags. false. draw with a translucency effect.
- #--------------------------------------------------------------------------
- def change_color(color, enabled = true)
- @bitmap.font.color.set(color)
- @bitmap.font.color.alpha = 120 unless enabled
- end
- #--------------------------------------------------------------------------
- # ● Text color
- # n : Text Color Number (0..31)
- #--------------------------------------------------------------------------
- def text_color(n)
- Cache.system("Window").get_pixel(64 + (n % 8) * 8, 96 + (n / 8) * 8)
- end
- #--------------------------------------------------------------------------
- # ● Process draw icon
- #--------------------------------------------------------------------------
- def process_draw_icon(icon_index, pos)
- w = 24; h = 24
- process_draw_before(pos[:x], pos[:y], w, h, pos)
- draw_icon(@bitmap, icon_index, pos[:x], pos[:y]) if pos[:flag_draw]
- process_draw_after(pos[:x], pos[:y], w, h, pos)
- end
- #--------------------------------------------------------------------------
- # ● Draw icon
- # enabled : Valid flags. False, draw with a translucency effect.
- #--------------------------------------------------------------------------
- def draw_icon(bitmap, icon_index, x, y, enabled = true)
- _bitmap = Cache.system("Iconset")
- rect = Rect.new(icon_index % 16 * 24, icon_index / 16 * 24, 24, 24)
- bitmap.blt(x, y, _bitmap, rect, enabled ? 255 : 120)
- end
- #--------------------------------------------------------------------------
- # ● Big font
- #--------------------------------------------------------------------------
- def make_font_bigger
- @bitmap.font.size += 4 if @bitmap.font.size < 64
- end
- #--------------------------------------------------------------------------
- # ● Small font
- #--------------------------------------------------------------------------
- def make_font_smaller
- @bitmap.font.size -= 4 if @bitmap.font.size > 16
- end
- end
Add Comment
Please, Sign In to add comment