Save-Point
Draw Text - Printable Version

+- Save-Point (https://www.save-point.org)
+-- Forum: Material Development (https://www.save-point.org/forum-8.html)
+--- Forum: Scripts Database (https://www.save-point.org/forum-39.html)
+---- Forum: RPGMaker XP (RGSS) Engine (https://www.save-point.org/forum-116.html)
+---- Thread: Draw Text (/thread-2828.html)



Draw Text - Trickster - 03-01-2008

Draw Text
Version: 1.0


Introduction
An Little addon for any bitmap that allows you to draw wrapped text or formatted text it automatically wraps to the next line if the text is too long to fit on the line. Also adds features (for formatted text) like color changing, italics, bold, new line, and tabbing.


Screenshots
I'll post them if requested


Script
add this in a new script above main

Code:
#C[RRBBGG] Changes the Color (RR,BB,GG)
      #C[N] Changes to color back to normal
      #I[] Changes the font to Italics
      #B[] Changes the font to Bold
      #T[] A Tab
      #N[] New Line
#==============================================================================
# ** Bitmap
#------------------------------------------------------------------------------
#  Additions add a draw wrap text method which draws word wrapped text
#  Text Automattically goes to the next line if the text can not fit on the line
#==============================================================================
class Bitmap
  def draw_wrap_text(x,y,width, height, text)
    array = text.split
    for i in array
      word = i + ' '
      word_width = text_size(word).width
      if x + word_width > width
        y += height
        x = 0
      end
      self.draw_text(x, y, word_width, height, word)
      x += word_width
    end
  end
  
  def text_wrap_size(x,y,width, height, text)
    array = text.split
    begin_y = y
    y += height
    for i in array
      word = i + ' '
      word_width = text_size(word).width
      if x + word_width > width
        y += height
        x = 0
      end
      x += word_width
    end
    return Rect.new(x, y, width, y-begin_y)
  end
  
  def draw_formatted_text(offset_x, offset_y, width, height, text)
    self.font.color = Color.new(255, 255, 255)
    text = text.dup
    text.gsub!(/[Cc]\[((([A-F]|[a-f]|[0-9])+)|N)\]/) {" \001[#{$1}] "}
    text.gsub!(/[Bb]\[\]/) {" \002 "}
    text.gsub!(/[Ii]\[\]/) {" \003 "}
    text.gsub!(/[Tt]\[\]/) {" \004 "}
    text.gsub!(/[Nn]\[\]/) {" \005 "}
    x = 0
    y = 0
    while ((c = text.slice!(/(\S+\s)|(\S+)/m)) != nil)
      if c.include?("\001")
        c.slice!("\001")
        # Change text color
        c.sub!(/\[((([A-F]|[a-f]|[0-9])+)|N)\]\s/, "")
        if $1 != nil and $1.size > 1
          r = $1.slice(0..1).to_i(16)
          g = $1.slice(2..3).to_i(16)
          b = $1.slice(4..5).to_i(16)
          color = Color.new(r,g,b)
        else
          color = Color.new(255, 255, 255)
        end
        self.font.color = color
        # go to next text
        next
      end
      if c.include?("\002")
        c.slice!("\002")
        self.font.bold = !self.font.bold
        c = ""
        # go to next text
        next
      end
      if c.include?("\003")
        c.slice!("\003")
        self.font.italic = !self.font.italic
        c = ""
        # go to next text
        next
      end
      if c.include?("\004")
        c.slice!("\004")
        x += text_size("    ").width
        c = ""
        next
      end
      if c.include?("\005")
        c.slice!("\005")
        y = x / width + 1
        x = y * width
        c = ""
        next
      end
      word_width = text_size(c).width
      if (x+word_width) % width < x % width
        y = x / width + 1
        x = y * width
      end
      pos_x = x % width + offset_x
      pos_y = x / width * height + offset_y
      self.draw_text(pos_x, pos_y, word_width, 32, c)
      x += word_width
    end
  end
      
  def get_formatted_size(off_x, off_y, width, height, text)
    text = text.dup
    text.gsub!(/[Cc]\[((([A-F]|[a-f]|[0-9])+)|N)\]/) {""}
    text.gsub!(/[Bb]\[\]/) {""}
    text.gsub!(/[Ii]\[\]/) {""}
    text.gsub!(/[Tt]\[\]/) {" \004 "}
    text.gsub!(/[Nn]\[\]/) {" \005 "}
    x = 0
    y = 0
    while ((c = text.slice!(/(\S+\s)|(\S+)/m)) != nil)
      if c.include?("\004")
        c.slice!("\004")
        x += text_size("    ").width
        c = ""
        next
      end
      if c.include?("\005")
        c.slice!("\005")
        y = x / width + 1
        x = y * width
        c = ""
        next
      end
      word_width = text_size(c).width
      if (x+word_width) % width < x % width
        y = x / width + 1
        x = y * width
      end
      x += word_width
      pos_x = x % width + off_x
      pos_y = x / width * height + off_y
    end
    return Rect.new(pos_x, pos_y, width, (pos_y-off_y+height).to_i)
  end
end

Instructions

to call this all you have to do is
self.contents.draw_formatted_text(x,y,width, height, text)
or
self.contents.draw_wrap_text(x,y,width,height,text)

you may also call get_wrap_size, and get_formatted size to get the rectangle the text is drawn on (corresponds to method text_size)


where offset_x is the x coordinate to start drawing text and offset_y is the y coordinate to start drawing, text is the text to draw

If you use C[RRGGBB] where RR GG and BB are hexadecimal color values inside the text then the color will change to that color. To change it back just put C[N]. If you want bold text then type B[] for Italics use I[]. If you want a newline just put N[] and for tab (4 spaces) use T[] you can easily change these flags by modifing the Regexp used in these lines

Code:
text.gsub!(/[Cc]\[((([A-F]|[a-f]|[0-9])+)|N)\]/) {" \001[#{$1}] "}
        text.gsub!(/[Bb]\[\]/) {" \002 "}
        text.gsub!(/[Ii]\[\]/) {" \003 "}
        text.gsub!(/[Tt]\[\]/) {" \004 "}
        text.gsub!(/[Nn]\[\]/) {" \005 "}


FAQ
Note: Permission granted by Trickster to post:
Quote:And if you post what you have now of my stuff then you don't have the latest versions. I'm too lazy/busy to post stuff.
As this is his material, it is deletable upon his request. Due to his current absense, no support is available. Please do not PM or eMail him for support.


Compatibility
Compatible with any script


Author's Notes
If you find any bugs let me know...


Terms and Conditions
Hey, I posted this publicly. You can use it. What do you expect? But if you do use it, I do expect you to spell my name correctly in your game. And yes you can use it in commercial games too.