Advertisement
dsiver144

DSI-Bitmap

Feb 20th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 22.23 KB | None | 0 0
  1. #-------------------------------------------------------------------------------
  2. # * Bitmap Word Wrapping
  3. # * Rewrite: dsiver144
  4. # * Original Author: KZIsAwesome
  5. #-------------------------------------------------------------------------------
  6. module KZIsAwesome
  7.   module WordWrap
  8.  
  9.     # change this if you don't want wordwrap on by default.
  10.     DEFAULT_WORDWRAP = true
  11.  
  12.     # change this if you want white space mode on by default.
  13.     DEFAULT_WHITESPACE = false
  14.    
  15.     # change this if you want white space mode on by default.
  16.     DEFAULT_COLLAPSE = true
  17.    
  18.     # change this to add a right margin to the window.
  19.     DEFAULT_RIGHT_MARGIN = 0
  20.  
  21.   end
  22. end
  23.  
  24. $imported = {} if $imported.nil?
  25. $imported["DSI-Bitmap"] = true
  26.  
  27. class Bitmap
  28.     def process_character(c, text, pos)
  29.     c = ' ' if @convert_newlines && c == "\n"
  30.     if @wordwrap && c =~ /[ \t]/
  31.       c = '' if @collapse_whitespace && @lastc =~ /[\s\n\f]/
  32.       if pos[:x] + get_next_word_size(c, text) > self.width - @right_margin
  33.         process_new_line(text, pos)
  34.       else
  35.         process_normal_character(c, pos)
  36.       end
  37.       @lastc = c
  38.     else
  39.       @lastc = c
  40.       case c
  41.       when "\n"   # New line
  42.         process_new_line(text, pos)
  43.       when "\f"   # New page
  44.         process_new_page(text, pos)
  45.       when "\e"   # Control character
  46.         process_escape_character(obtain_escape_code(text), text, pos)
  47.       else        # Normal character
  48.         process_normal_character(c, pos)
  49.       end
  50.     end
  51.   end
  52.   #--------------------------------------------------------------------------
  53.   # * Destructively Get Control Code
  54.   #--------------------------------------------------------------------------
  55.   def obtain_escape_code(text)
  56.     text.slice!(/^[\$\.\|\^!><\{\}\\]|^[A-Z]+/i)
  57.   end
  58.   #--------------------------------------------------------------------------
  59.   # * Control Character Processing
  60.   #     code : the core of the control character
  61.   #            e.g. "C" in the case of the control character \C[1].
  62.   #--------------------------------------------------------------------------
  63.   def process_escape_character(code, text, pos)
  64.     case code.upcase
  65.     when 'C'
  66.       change_color(text_color(obtain_escape_param(text)))
  67.     when 'I'
  68.       process_draw_icon(obtain_escape_param(text), pos)
  69.     when '{'
  70.       make_font_bigger
  71.     when '}'
  72.       make_font_smaller
  73.     end
  74.   end
  75.   def get_next_word_size(c, text)
  76.     # Split text by the next space/line/page and grab the first split
  77.     nextword = text.split(/[\s\n\f]/, 2)[0]
  78.     if nextword
  79.       icons = 0
  80.       if nextword =~ /\e/i
  81.         # Get rid of color codes and YEA Message system outline colors
  82.         nextword = nextword.split(/\e[oOcC]+\[\d*\]/).join
  83.         # Get rid of message timing control codes
  84.         nextword = nextword.split(/\e[\.\|\^<>!]/).join
  85.         # Split text by the first non-icon escape code
  86.         # (the hH is for compatibility with the Icon Hues script)
  87.         nextword = nextword.split(/\e[^iIhH]+/, 2)[0]
  88.         # Erase and count icons in remaining text
  89.         nextword.gsub!(/\e[iIhH]+\[[\d,]*\]/) do
  90.           icons += 1
  91.           ''
  92.         end if nextword
  93.       end
  94.       wordsize = (nextword ? text_size(c + nextword).width : text_size( c ).width)
  95.       wordsize += icons * 24
  96.     else
  97.       wordsize = text_size( c ).width
  98.     end
  99.     return wordsize
  100.   end
  101.  
  102.   def turn_on_wordwraping
  103.     @wordwrap = true
  104.     @right_margin = 0
  105.   end
  106.  
  107.   def process_escape_character(code, text, pos)
  108.     case code.upcase
  109.     when 'WW'
  110.       @wordwrap = true
  111.     when 'NW'
  112.       @wordwrap = false
  113.     when 'WS'
  114.       @convert_newlines = true
  115.     when 'NL'
  116.       @convert_newlines = false
  117.     when 'CS'
  118.       @collapse_whitespace = true
  119.     when 'PRE'
  120.       @collapse_whitespace = false
  121.     when 'BR'
  122.       process_new_line(text, pos)
  123.       @lastc = "\n"
  124.     when 'RM'
  125.       @right_margin = obtain_escape_param(text)
  126.     else
  127.       case code.upcase
  128.       when 'C'
  129.         change_color(text_color(obtain_escape_param(text)))
  130.       when 'I'
  131.         process_draw_icon(obtain_escape_param(text), pos)
  132.       when '{'
  133.         make_font_bigger
  134.       when '}'
  135.         make_font_smaller
  136.       end
  137.     end
  138.     # Recalculate the next word size and insert line breaks
  139.     # (Needed primarily for font changes)
  140.     if pos[:x] + get_next_word_size('', text) > self.width
  141.       process_new_line(text, pos)
  142.     end
  143.   end
  144.   #--------------------------------------------------------------------------
  145.   # * Icon Drawing Process by Control Characters
  146.   #--------------------------------------------------------------------------
  147.   def process_draw_icon(icon_index, pos)
  148.     draw_icon(icon_index, pos[:x], pos[:y])
  149.     pos[:x] += 24
  150.   end
  151.   #--------------------------------------------------------------------------
  152.   # * Destructively Get Control Code Argument
  153.   #--------------------------------------------------------------------------
  154.   def obtain_escape_param(text)
  155.     text.slice!(/^\[\d+\]/)[/\d+/].to_i rescue 0
  156.   end
  157.   #--------------------------------------------------------------------------
  158.   # * Get Text Color
  159.   #     n : Text color number (0..31)
  160.   #--------------------------------------------------------------------------
  161.   def text_color(n)
  162.     @windowskin ||= Cache.system("Window")
  163.     @windowskin.get_pixel(64 + (n % 8) * 8, 96 + (n / 8) * 8)
  164.   end
  165.   #--------------------------------------------------------------------------
  166.   # * Get Alpha Value of Translucent Drawing
  167.   #--------------------------------------------------------------------------
  168.   def translucent_alpha
  169.     return 160
  170.   end
  171.   #--------------------------------------------------------------------------
  172.   # * Change Text Drawing Color
  173.   #     enabled : Enabled flag. When false, draw semi-transparently.
  174.   #--------------------------------------------------------------------------
  175.   def change_color(color, enabled = true)
  176.     self.font.color.set(color)
  177.     self.font.color.alpha = translucent_alpha unless enabled
  178.   end
  179.   #--------------------------------------------------------------------------
  180.   # * Draw Point
  181.   #--------------------------------------------------------------------------
  182.    def draw_point(x,y,color=Color.new(255,255,255))
  183.      self.fill_rect(x,y,2,2,color)
  184.    end
  185.   #--------------------------------------------------------------------------
  186.   # * Reset Font Settings
  187.   #--------------------------------------------------------------------------
  188.   def reset_font_settings
  189.     self.font.out_color = Font.default_out_color
  190.     self.font.color = Font.default_color
  191.     self.font.size = Font.default_size
  192.     self.font.bold = Font.default_bold
  193.     self.font.italic = Font.default_italic
  194.   end
  195.   #--------------------------------------------------------------------------
  196.   # * Get Line Height
  197.   #--------------------------------------------------------------------------
  198.   def line_height
  199.     return 24
  200.   end
  201.   #--------------------------------------------------------------------------
  202.   # * Increase Font Size
  203.   #--------------------------------------------------------------------------
  204.   def make_font_bigger
  205.     self.font.size += 8 if self.font.size <= 64
  206.   end
  207.   #--------------------------------------------------------------------------
  208.   # * Decrease Font Size
  209.   #--------------------------------------------------------------------------
  210.   def make_font_smaller
  211.     self.font.size -= 8 if self.font.size >= 16
  212.   end
  213.   #--------------------------------------------------------------------------
  214.   # * Calculate Line Height
  215.   #     restore_font_size : Return to original font size after calculating
  216.   #--------------------------------------------------------------------------
  217.   def calc_line_height(text, restore_font_size = true)
  218.     result = [line_height, self.font.size].max
  219.     last_font_size = self.font.size
  220.     text.slice(/^.*$/).scan(/\e[\{\}]/).each do |esc|
  221.       make_font_bigger  if esc == "\e{"
  222.       make_font_smaller if esc == "\e}"
  223.       result = [result, self.font.size].max
  224.     end
  225.     self.font.size = last_font_size if restore_font_size
  226.     result
  227.   end
  228.   #--------------------------------------------------------------------------
  229.   # * Draw Text with Control Characters
  230.   #--------------------------------------------------------------------------
  231.   def draw_text_ex(x, y, text,reset_font=true)
  232.     reset_font_settings if reset_font == true
  233.     #self.font.size -= 3
  234.     text = convert_escape_characters(text)
  235.     pos = {:x => x, :y => y, :new_x => x, :height => calc_line_height(text)}
  236.     process_character(text.slice!(0, 1), text, pos) until text.empty?
  237.   end
  238. #~   #--------------------------------------------------------------------------
  239. #~   # * Character Processing
  240. #~   #     c    : Characters
  241. #~   #     text : A character string buffer in drawing processing (destructive)
  242. #~   #     pos  : Draw position {:x, :y, :new_x, :height}
  243. #~   #--------------------------------------------------------------------------
  244. #~   def process_character(c, text, pos)
  245. #~     case c
  246. #~     when "\n"   # New line
  247. #~       process_new_line(text, pos)
  248. #~     else        # Normal character
  249. #~       process_normal_character(c, pos)
  250. #~     end
  251. #~   end
  252.   #--------------------------------------------------------------------------
  253.   # * Normal Character Processing
  254.   #--------------------------------------------------------------------------
  255.   def process_normal_character(c, pos)
  256.     text_width = text_size(c).width
  257.     draw_text(pos[:x], pos[:y], text_width * 2, pos[:height], c)
  258.      pos[:x] += text_width
  259.   end
  260.   #--------------------------------------------------------------------------
  261.   # * New Line Character Processing
  262.   #--------------------------------------------------------------------------
  263.   def process_new_line(text, pos)
  264.     pos[:x] = pos[:new_x]
  265.     pos[:y] += pos[:height]
  266.     pos[:height] = calc_line_height(text)
  267.   end
  268.   #--------------------------------------------------------------------------
  269.   # * Preconvert Control Characters
  270.   #    As a rule, replace only what will be changed into text strings before
  271.   #    starting actual drawing. The character "\" is replaced with the escape
  272.   #    character (\e).
  273.   #--------------------------------------------------------------------------
  274.   def convert_escape_characters(text)
  275.     result = text.to_s.clone
  276.     result.gsub!(/\\/)            { "\e" }
  277.     result.gsub!(/\e\e/)          { "\\" }
  278.     result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i] }
  279.     result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i] }
  280.     result.gsub!(/\eN\[(\d+)\]/i) { actor_name($1.to_i) }
  281.     result.gsub!(/\eP\[(\d+)\]/i) { party_member_name($1.to_i) }
  282.     result.gsub!(/\eG/i)          { Vocab::currency_unit }
  283.     result
  284.   end
  285.   #--------------------------------------------------------------------------
  286.   # * Get Alpha Value of Translucent Drawing
  287.   #--------------------------------------------------------------------------
  288.   def translucent_alpha
  289.     return 160
  290.   end
  291.   #--------------------------------------------------------------------------
  292.   # * Draw Icon
  293.   #     enabled : Enabled flag. When false, draw semi-transparently.
  294.   #--------------------------------------------------------------------------
  295.   def draw_icon(icon_index, x, y, disable = false)
  296.     bitmap = Cache.system("Iconset")
  297.     rect = Rect.new(icon_index % 16 * 24, icon_index / 16 * 24, 24, 24)
  298.     self.blt(x, y, bitmap, rect, 255)
  299.   end
  300. end
  301.  
  302. class DarkBitmap < Bitmap
  303.    attr_accessor :cur_width
  304.    attr_accessor :tline_height
  305.    include KZIsAwesome::WordWrap
  306.  
  307.   def initialize(width,height)
  308.     super(width,height)
  309.     @wordwrap = DEFAULT_WORDWRAP
  310.     @convert_newlines = DEFAULT_WHITESPACE
  311.     @collapse_whitespace = DEFAULT_COLLAPSE
  312.     @right_margin = DEFAULT_RIGHT_MARGIN
  313.     @lastc = "\n"
  314.   end
  315.  
  316.   def process_character(c, text, pos)
  317.     c = ' ' if @convert_newlines && c == "\n"
  318.     if @wordwrap && c =~ /[ \t]/
  319.       c = '' if @collapse_whitespace && @lastc =~ /[\s\n\f]/
  320.       if pos[:x] + get_next_word_size(c, text) > self.width - @right_margin
  321.         process_new_line(text, pos)
  322.       else
  323.         process_normal_character(c, pos)
  324.       end
  325.       @lastc = c
  326.     else
  327.       @lastc = c
  328.       case c
  329.       when "\n"   # New line
  330.         process_new_line(text, pos)
  331.       when "\f"   # New page
  332.         process_new_page(text, pos)
  333.       when "\e"   # Control character
  334.         process_escape_character(obtain_escape_code(text), text, pos)
  335.       else        # Normal character
  336.         process_normal_character(c, pos)
  337.       end
  338.     end
  339.   end
  340.   #--------------------------------------------------------------------------
  341.   # * Destructively Get Control Code
  342.   #--------------------------------------------------------------------------
  343.   def obtain_escape_code(text)
  344.     text.slice!(/^[\$\.\|\^!><\{\}\\]|^[A-Z]+/i)
  345.   end
  346.   #--------------------------------------------------------------------------
  347.   # * Control Character Processing
  348.   #     code : the core of the control character
  349.   #            e.g. "C" in the case of the control character \C[1].
  350.   #--------------------------------------------------------------------------
  351.   def process_escape_character(code, text, pos)
  352.     case code.upcase
  353.     when 'C'
  354.       change_color(text_color(obtain_escape_param(text)))
  355.     when 'I'
  356.       process_draw_icon(obtain_escape_param(text), pos)
  357.     when '{'
  358.       make_font_bigger
  359.     when '}'
  360.       make_font_smaller
  361.     end
  362.   end
  363.   def get_next_word_size(c, text)
  364.     # Split text by the next space/line/page and grab the first split
  365.     nextword = text.split(/[\s\n\f]/, 2)[0]
  366.     if nextword
  367.       icons = 0
  368.       if nextword =~ /\e/i
  369.         # Get rid of color codes and YEA Message system outline colors
  370.         nextword = nextword.split(/\e[oOcC]+\[\d*\]/).join
  371.         # Get rid of message timing control codes
  372.         nextword = nextword.split(/\e[\.\|\^<>!]/).join
  373.         # Split text by the first non-icon escape code
  374.         # (the hH is for compatibility with the Icon Hues script)
  375.         nextword = nextword.split(/\e[^iIhH]+/, 2)[0]
  376.         # Erase and count icons in remaining text
  377.         nextword.gsub!(/\e[iIhH]+\[[\d,]*\]/) do
  378.           icons += 1
  379.           ''
  380.         end if nextword
  381.       end
  382.       wordsize = (nextword ? text_size(c + nextword).width : text_size( c ).width)
  383.       wordsize += icons * 24
  384.     else
  385.       wordsize = text_size( c ).width
  386.     end
  387.     return wordsize
  388.   end
  389.  
  390.   def process_escape_character(code, text, pos)
  391.     case code.upcase
  392.     when 'WW'
  393.       @wordwrap = true
  394.     when 'NW'
  395.       @wordwrap = false
  396.     when 'WS'
  397.       @convert_newlines = true
  398.     when 'NL'
  399.       @convert_newlines = false
  400.     when 'CS'
  401.       @collapse_whitespace = true
  402.     when 'PRE'
  403.       @collapse_whitespace = false
  404.     when 'BR'
  405.       process_new_line(text, pos)
  406.       @lastc = "\n"
  407.     when 'RM'
  408.       @right_margin = obtain_escape_param(text)
  409.     else
  410.       case code.upcase
  411.       when 'C'
  412.         change_color(text_color(obtain_escape_param(text)))
  413.       when 'I'
  414.         process_draw_icon(obtain_escape_param(text), pos)
  415.       when '{'
  416.         make_font_bigger
  417.       when '}'
  418.         make_font_smaller
  419.       end
  420.     end
  421.     # Recalculate the next word size and insert line breaks
  422.     # (Needed primarily for font changes)
  423.     if pos[:x] + get_next_word_size('', text) > self.width
  424.       process_new_line(text, pos)
  425.     end
  426.   end
  427.   #--------------------------------------------------------------------------
  428.   # * Draw Point
  429.   #--------------------------------------------------------------------------
  430.    def draw_point(x,y,color=Color.new(255,255,255))
  431.      self.fill_rect(x,y,2,2,color)
  432.    end
  433.   #--------------------------------------------------------------------------
  434.   # * Reset Font Settings
  435.   #--------------------------------------------------------------------------
  436.   def reset_font_settings
  437.     self.font.out_color = Font.default_out_color
  438.     self.font.color = Font.default_color
  439.     self.font.size = Font.default_size
  440.     self.font.bold = Font.default_bold
  441.     self.font.italic = Font.default_italic
  442.   end
  443.   #--------------------------------------------------------------------------
  444.   # * Get Line Height
  445.   #--------------------------------------------------------------------------
  446.   def line_height
  447.     return tline_height || 24
  448.   end
  449.   #--------------------------------------------------------------------------
  450.   # * Increase Font Size
  451.   #--------------------------------------------------------------------------
  452.   def make_font_bigger
  453.     self.font.size += 8 if self.font.size <= 64
  454.   end
  455.   #--------------------------------------------------------------------------
  456.   # * Decrease Font Size
  457.   #--------------------------------------------------------------------------
  458.   def make_font_smaller
  459.     self.font.size -= 8 if self.font.size >= 16
  460.   end
  461.   #--------------------------------------------------------------------------
  462.   # * Calculate Line Height
  463.   #     restore_font_size : Return to original font size after calculating
  464.   #--------------------------------------------------------------------------
  465.   def calc_line_height(text, restore_font_size = true)
  466.     result = [line_height, self.font.size].max
  467.     last_font_size = self.font.size
  468.     text.slice(/^.*$/).scan(/\e[\{\}]/).each do |esc|
  469.       make_font_bigger  if esc == "\e{"
  470.       make_font_smaller if esc == "\e}"
  471.       result = [result, self.font.size].max
  472.     end
  473.     self.font.size = last_font_size if restore_font_size
  474.     result
  475.   end
  476.   #--------------------------------------------------------------------------
  477.   # * Draw Text with Control Characters
  478.   #--------------------------------------------------------------------------
  479.   def draw_text_ex(x, y, text,reset_font=true)
  480.     reset_font_settings if reset_font == true
  481.     #self.font.size -= 3
  482.     text = convert_escape_characters(text)
  483.     pos = {:x => x, :y => y, :new_x => x, :height => calc_line_height(text)}
  484.     process_character(text.slice!(0, 1), text, pos) until text.empty?
  485.   end
  486. #~   #--------------------------------------------------------------------------
  487. #~   # * Character Processing
  488. #~   #     c    : Characters
  489. #~   #     text : A character string buffer in drawing processing (destructive)
  490. #~   #     pos  : Draw position {:x, :y, :new_x, :height}
  491. #~   #--------------------------------------------------------------------------
  492. #~   def process_character(c, text, pos)
  493. #~     case c
  494. #~     when "\n"   # New line
  495. #~       process_new_line(text, pos)
  496. #~     else        # Normal character
  497. #~       process_normal_character(c, pos)
  498. #~     end
  499. #~   end
  500.   #--------------------------------------------------------------------------
  501.   # * Normal Character Processing
  502.   #--------------------------------------------------------------------------
  503.   def process_normal_character(c, pos)
  504.     text_width = text_size(c).width
  505.     draw_text(pos[:x], pos[:y], text_width * 2, pos[:height], c)
  506.      pos[:x] += text_width
  507.   end
  508.   #--------------------------------------------------------------------------
  509.   # * New Line Character Processing
  510.   #--------------------------------------------------------------------------
  511.   def process_new_line(text, pos)
  512.     pos[:x] = pos[:new_x]
  513.     pos[:y] += pos[:height]
  514.     pos[:height] = calc_line_height(text)
  515.   end
  516.   #--------------------------------------------------------------------------
  517.   # * Preconvert Control Characters
  518.   #    As a rule, replace only what will be changed into text strings before
  519.   #    starting actual drawing. The character "\" is replaced with the escape
  520.   #    character (\e).
  521.   #--------------------------------------------------------------------------
  522.   def convert_escape_characters(text)
  523.     result = text.to_s.clone
  524.     result.gsub!(/\\/)            { "\e" }
  525.     result.gsub!(/\e\e/)          { "\\" }
  526.     result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i] }
  527.     result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i] }
  528.     result.gsub!(/\eN\[(\d+)\]/i) { actor_name($1.to_i) }
  529.     result.gsub!(/\eP\[(\d+)\]/i) { party_member_name($1.to_i) }
  530.     result.gsub!(/\eG/i)          { Vocab::currency_unit }
  531.     result
  532.   end
  533.   #--------------------------------------------------------------------------
  534.   # * Get Alpha Value of Translucent Drawing
  535.   #--------------------------------------------------------------------------
  536.   def translucent_alpha
  537.     return 160
  538.   end
  539.   #--------------------------------------------------------------------------
  540.   # * Draw Icon
  541.   #     enabled : Enabled flag. When false, draw semi-transparently.
  542.   #--------------------------------------------------------------------------
  543.   def draw_icon(icon_index, x, y, disable = false)
  544.     bitmap = Cache.system("Iconset")
  545.     rect = Rect.new(icon_index % 16 * 24, icon_index / 16 * 24, 24, 24)
  546.     self.blt(x, y, bitmap, rect, 255)
  547.   end
  548.   #--------------------------------------------------------------------------
  549.   # * Draw a Line
  550.   #--------------------------------------------------------------------------
  551.    def draw_line(x1,y1,x2,y2,color=Color.new(255,255,255))
  552.       dx = (x2-x1).abs
  553.       dy = (y2-y1).abs
  554.       c = dx - dy
  555.       c2 = 2*c
  556.       x = x1
  557.       y = y1
  558.       x_unit = 1
  559.       y_unit = 1
  560.       if x2 < x1
  561.         x_unit = -x_unit
  562.       end
  563.       if y2 < y1
  564.         y_unit = -y_unit
  565.       end
  566.       if x1 == x2
  567.         while y != y2
  568.           y += y_unit
  569.           self.set_pixel(x,y,color)
  570.           self.fill_rect(x,y,2,2,color)
  571.         end
  572.       else
  573.         if y == y2
  574.           while x != x2
  575.             x += x_unit
  576.             self.set_pixel(x,y,color)
  577.             self.fill_rect(x,y,2,2,color)
  578.           end
  579.         else
  580.           if (x1 != x2 ) and ( y1 != y2 )
  581.             while x != x2+1
  582.                c2 = 2*c
  583.                if c2 > -dy
  584.                  c = c - dy
  585.                  x = x + x_unit
  586.                end
  587.                if c2 < dx
  588.                  c = c + dx
  589.                  y += y_unit
  590.                end
  591.                self.set_pixel(x,y,color)
  592.                self.fill_rect(x,y,2,2,color)
  593.             end
  594.           end
  595.         end
  596.       end
  597.     end
  598.   end
  599.  
  600.   module Math
  601.   def self.get_x_y(angle,radius,offsetx=0,offsety=0)
  602.     x = Math.sin(angle)*radius + offsetx
  603.     y = -Math.cos(angle)*radius + offsety
  604.     return [x,y]
  605.   end
  606. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement