Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Draw Text
#1
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.
Reply }


Possibly Related Threads…
Thread Author Replies Views Last Post
   Text Scroll Script - Enhanced DerVVulfman 23 29,459 02-18-2021, 04:16 AM
Last Post: DerVVulfman
   Message Text from Files DerVVulfman 0 4,568 04-13-2016, 03:25 AM
Last Post: DerVVulfman
   Customize/Disable Text Outline + Shadow (v1.1) Zerbu 3 6,894 11-01-2015, 03:56 PM
Last Post: DerVVulfman
   Text Sound Effect (SE) v1.1 Zerbu 3 7,165 11-01-2015, 03:53 PM
Last Post: DerVVulfman
   Text Extraction Generator DerVVulfman 5 11,256 10-13-2014, 09:32 PM
Last Post: Narzew
   Text to RGSS DerVVulfman 10 23,103 05-04-2013, 04:34 AM
Last Post: DerVVulfman
   Damage Text Revamp 2.1 PK8 3 11,536 04-08-2012, 06:47 PM
Last Post: PK8
   Victor Engine - Control Text Victor Sant 0 3,914 01-04-2012, 08:12 AM
Last Post: Victor Sant
   Auto-Sizing Text Windows RPG Advocate 9 16,589 06-11-2011, 06:18 PM
Last Post: sagam12
   Another Scrolling Text Script kyonides 1 5,700 04-12-2010, 01:36 AM
Last Post: Shiroiyuki



Users browsing this thread: