neutale

OneEyedEagle - Draw Text Ex

Nov 14th, 2020
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 11.10 KB | None | 0 0
  1. #==============================================================================
  2. # ■ Draw Text Ex by OneEyedEagle (http://oneeyedeagle.lofter.com/)
  3. # License - MIT License: http://opensource.org/licenses/mit-license.php
  4. #==============================================================================
  5. # - 2020.7.23.21 Adds automatic line feeds; delete assigned line positions
  6. #==============================================================================
  7. # - This script provides a way to draw escaped text on a bitmap.
  8. #-----------------------------------------------------------------------------
  9. # - Create a new text drawing processing object (not yet drawn)
  10. #
  11. #     d = Process_DrawTextEX.new(text[, params, bitmap])
  12. #
  13. #       text - text strings with escaped characters
  14. #       params - control parameter set drawn (see initialize method)
  15. #       bitmap - bitmap objects on which text needs to be drawn.
  16. #
  17. # - Call d.width and d.height to get total width and height for text to be drawn
  18. # - Use d.bind_bitmap(bitmap[, dispose_old]) to rebind the bitmap object to
  19. #   where dispose_old releases the old bitmap when true is passed.
  20. # - Drawing with d.run
  21. #==============================================================================
  22.  
  23. class Process_DrawTextEX
  24.   attr_reader :text, :info
  25.   #--------------------------------------------------------------------------
  26.   # ● Initialize
  27.   #  params
  28.   #   :font_size → Draw the initial text size
  29.   #   :x0 → Specify the x value for each line
  30.   #   :y0 → Specify the y value for each line
  31.   #   :w → Set the max width, if exceeded, automatic line feed will be performed.
  32.   #   :lhd → Distance to the next line on a line feed.
  33.   #--------------------------------------------------------------------------
  34.   def initialize(text, params = {}, bitmap = nil)
  35.     @text = convert_escape_characters(text)
  36.     @bitmap = bitmap || Cache.empty_bitmap
  37.     @params = params
  38.     @params[:font_size] ||= @bitmap.font.size
  39.     @params[:x0] ||= 0
  40.     @params[:y0] ||= 0
  41.     @params[:w] ||= nil
  42.     @params[:lhd] ||= 0
  43.     @info = {}
  44.     @info[:w] ||= [] # line_index => width
  45.     @info[:h] ||= [] # line_index => height
  46.     run(false)
  47.   end
  48.   #--------------------------------------------------------------------------
  49.   # ● Bind bitmap
  50.   #--------------------------------------------------------------------------
  51.   def bind_bitmap(bitmap, dispose_old = true)
  52.     @bitmap.dispose if @bitmap && dispose_old
  53.     @bitmap = bitmap
  54.   end
  55.   #--------------------------------------------------------------------------
  56.   # ● Width
  57.   #--------------------------------------------------------------------------
  58.   def width
  59.     @info[:w].max
  60.   end
  61.   #--------------------------------------------------------------------------
  62.   # ● Height
  63.   #--------------------------------------------------------------------------
  64.   def height
  65.     r = @info[:h].inject(0) { |s, v| s += v }
  66.     r = r + (@info[:h].size - 1) * @params[:lhd] + @params[:y0]
  67.     r
  68.   end
  69.   #--------------------------------------------------------------------------
  70.   # ● Convert controllers
  71.   #    Replaces control with the actual content before the actual drawing.
  72.   #    To reduce ambiguity, this \\ replaced by an escape character (\e) first.
  73.   #--------------------------------------------------------------------------
  74.   def convert_escape_characters(text)
  75.     result = text.to_s.clone
  76.     result.gsub!(/\\/)            { "\e" }
  77.     result.gsub!(/\e\e/)          { "\\" }
  78.     result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i] }
  79.     result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i] }
  80.     result.gsub!(/\eN\[(\d+)\]/i) { actor_name($1.to_i) }
  81.     result.gsub!(/\eP\[(\d+)\]/i) { party_member_name($1.to_i) }
  82.     result.gsub!(/\eG/i)          { Vocab::currency_unit }
  83.     result
  84.   end
  85.   #--------------------------------------------------------------------------
  86.   # ● Actor name (n)
  87.   #--------------------------------------------------------------------------
  88.   def actor_name(n)
  89.     actor = n >= 1 ? $game_actors[n] : nil
  90.     actor ? actor.name : ""
  91.   end
  92.   #--------------------------------------------------------------------------
  93.   # ● Party member name
  94.   #--------------------------------------------------------------------------
  95.   def party_member_name(n)
  96.     actor = n >= 1 ? $game_party.members[n - 1] : nil
  97.     actor ? actor.name : ""
  98.   end
  99.   #--------------------------------------------------------------------------
  100.   # ● Run
  101.   #--------------------------------------------------------------------------
  102.   def run(flag_draw = true)
  103.     text = @text.clone
  104.     pos = { :line => -1, :x0 => @params[:x0], :x => 0,
  105.       :y0 => @params[:y0], :y => 0,
  106.       :w => 0, :h => 0, :flag_draw => flag_draw }
  107.     @bitmap.font.size = @params[:font_size]
  108.     process_new_line(pos)
  109.     process_character(text.slice!(0, 1), text, pos) until text.empty?
  110.   end
  111.   #--------------------------------------------------------------------------
  112.   # ● Process new line
  113.   #--------------------------------------------------------------------------
  114.   def process_new_line(pos)
  115.     pos[:line] += 1
  116.     pos[:x0] = @params[:x0] || pos[:x0]
  117.     pos[:x] = pos[:x0]
  118.     pos[:y0] = (pos[:y0] + pos[:h])
  119.     pos[:y0] += @params[:lhd] if pos[:line] > 0
  120.     pos[:y] = pos[:y0]
  121.     if pos[:flag_draw]
  122.       pos[:w] = @info[:w][pos[:line]]
  123.       pos[:h] = @info[:h][pos[:line]]
  124.     else
  125.       @info[:w][pos[:line]] = 0
  126.       @info[:h][pos[:line]] = 0
  127.     end
  128.   end
  129.   #--------------------------------------------------------------------------
  130.   # ● Process auto new line
  131.   #--------------------------------------------------------------------------
  132.   def process_auto_new_line(pos, w)
  133.     if @params[:w] && pos[:x] + w > @params[:w]
  134.       process_new_line(pos)
  135.       pos[:h] = 0
  136.     end
  137.   end
  138.   #--------------------------------------------------------------------------
  139.   # ● Text controls
  140.   #     c    : Text
  141.   #     text : Drawing string cache in processing (the string may be modified)
  142.   #     pos  : position {:x, :y, :new_x, :height}
  143.   #--------------------------------------------------------------------------
  144.   def process_character(c, text, pos)
  145.     case c
  146.     when "\r"   # return
  147.       return
  148.     when "\n"   # line
  149.       process_new_line(pos)
  150.     when "\e"   # controller
  151.       process_escape_character(obtain_escape_code(text), text, pos)
  152.     else        # alphabet
  153.       process_normal_character(c, pos)
  154.     end
  155.   end
  156.   #--------------------------------------------------------------------------
  157.   # ● Process normal character
  158.   #--------------------------------------------------------------------------
  159.   def process_normal_character(c, pos)
  160.     r = @bitmap.text_size(c); w = r.width; h = r.height
  161.     process_draw_before(pos[:x], pos[:y], w, h, pos)
  162.     @bitmap.draw_text(pos[:x], pos[:y], w * 2, h, c) if pos[:flag_draw]
  163.     process_draw_after(pos[:x], pos[:y], w, h, pos)
  164.   end
  165.   #--------------------------------------------------------------------------
  166.   # ● Process draw before
  167.   #--------------------------------------------------------------------------
  168.   def process_draw_before(x, y, w, h, pos)
  169.     process_auto_new_line(pos, w)
  170.     pos[:y] = pos[:y0] + pos[:h] - h if pos[:h] > h
  171.   end
  172.   #--------------------------------------------------------------------------
  173.   # ● Process draw after
  174.   #--------------------------------------------------------------------------
  175.   def process_draw_after(x, y, w, h, pos)
  176.     pos[:x] += w
  177.     pos[:y] = pos[:y0]
  178.     pos[:w] += w
  179.     pos[:h] = h if pos[:h] < h
  180.     if !pos[:flag_draw]
  181.       @info[:w][pos[:line]] = pos[:w]
  182.       @info[:h][pos[:line]] = pos[:h]
  183.     end
  184.   end
  185.   #--------------------------------------------------------------------------
  186.   # ● Get actual form of the controller (this method corrupts the raw data)
  187.   #--------------------------------------------------------------------------
  188.   def obtain_escape_code(text)
  189.     text.slice!(/^[\$\.\|\^!><\{\}\\]|^[A-Z]+/i)
  190.   end
  191.   #--------------------------------------------------------------------------
  192.   # ● Get parameters of the controller (this method corrupts the raw data)
  193.   #--------------------------------------------------------------------------
  194.   def obtain_escape_param(text)
  195.     text.slice!(/^\[\d+\]/)[/\d+/].to_i rescue 0
  196.   end
  197.   #--------------------------------------------------------------------------
  198.   # ● Process escape character
  199.   #     code : The actual form of the controller (e.g. "\C[1]" is "C").
  200.   #     text : Drawing the string cache in process (the string may be modified)
  201.   #     pos  : Draw position {:x, :y, :new_x, :height}
  202.   #--------------------------------------------------------------------------
  203.   def process_escape_character(code, text, pos)
  204.     case code.upcase
  205.     when 'C'
  206.       change_color(text_color(obtain_escape_param(text)))
  207.     when 'I'
  208.       process_draw_icon(obtain_escape_param(text), pos)
  209.     when '{'
  210.       make_font_bigger
  211.     when '}'
  212.       make_font_smaller
  213.     end
  214.   end
  215.   #--------------------------------------------------------------------------
  216.   # ● Change color
  217.   #     enabled : Valid flags. false. draw with a translucency effect.
  218.   #--------------------------------------------------------------------------
  219.   def change_color(color, enabled = true)
  220.     @bitmap.font.color.set(color)
  221.     @bitmap.font.color.alpha = 120 unless enabled
  222.   end
  223.   #--------------------------------------------------------------------------
  224.   # ● Text color
  225.   #     n : Text Color Number (0..31)
  226.   #--------------------------------------------------------------------------
  227.   def text_color(n)
  228.     Cache.system("Window").get_pixel(64 + (n % 8) * 8, 96 + (n / 8) * 8)
  229.   end
  230.   #--------------------------------------------------------------------------
  231.   # ● Process draw icon
  232.   #--------------------------------------------------------------------------
  233.   def process_draw_icon(icon_index, pos)
  234.     w = 24; h = 24
  235.     process_draw_before(pos[:x], pos[:y], w, h, pos)
  236.     draw_icon(@bitmap, icon_index, pos[:x], pos[:y]) if pos[:flag_draw]
  237.     process_draw_after(pos[:x], pos[:y], w, h, pos)
  238.   end
  239.   #--------------------------------------------------------------------------
  240.   # ● Draw icon
  241.   #     enabled : Valid flags. False, draw with a translucency effect.
  242.   #--------------------------------------------------------------------------
  243.   def draw_icon(bitmap, icon_index, x, y, enabled = true)
  244.     _bitmap = Cache.system("Iconset")
  245.     rect = Rect.new(icon_index % 16 * 24, icon_index / 16 * 24, 24, 24)
  246.     bitmap.blt(x, y, _bitmap, rect, enabled ? 255 : 120)
  247.   end
  248.   #--------------------------------------------------------------------------
  249.   # ● Big font
  250.   #--------------------------------------------------------------------------
  251.   def make_font_bigger
  252.     @bitmap.font.size += 4 if @bitmap.font.size < 64
  253.   end
  254.   #--------------------------------------------------------------------------
  255.   # ● Small font
  256.   #--------------------------------------------------------------------------
  257.   def make_font_smaller
  258.     @bitmap.font.size -= 4 if @bitmap.font.size > 16
  259.   end
  260. end
  261.  
Add Comment
Please, Sign In to add comment