Jump to content
Kayzee

Word Wrapping Message Boxes

Recommended Posts

Has anyone ever found it annoying to manually break up text to fit in the message boxes, especially if you start tweaking with the game resolution or the text box sizes? Well, I sure have. It's rather annoying to me that simple word wrapping is beyond the capability of the vx ace engine... but no more for I bring ye, yonder script to solve yon problem! It also adds a few new escape sequences and a optional HTML-like whitespace mode that converts linebreaks to spaces. More features (like possibly a option to get rid of leading/trailing spaces for a line) may come in the future.

 

 

#========================================================================
# ** Word Wrapping Message Boxes, by: KilloZapit
#------------------------------------------------------------------------
# Changes message boxes so it will automatically wrap long lines.
#
# Note: I consider this script to be public domain, and put no
# restrictions on it's use whatsoever. My only request is that
# a link back to the script is provided so more people can
# access it if they want to.
#
# Version the Second:
#   Now strips color codes and icon codes so they don't break words.
#   Also calculates icon width along with text width for words.
# Version the Third:
#   Now also strips delays and other timing related codes.
#   Splits for non-icon control codes before counting icons.
#   Control codes can now break lines in case of font changes.
#   Added some comments to clarify some code.
# Version the Forth:
#   Fixed a small bug that might cause a error when counting icons.
#   Added a small notice for copyright questions.
# Version the Fifth:
#   Added "collapse" mode, which elimanates extra spaces.
#   Can now use "whitespace" mode outside of wordwrap mode if needed.
# Version the Sixth:
#   Fixed problems with collapsed whitespace not wraping words right.
# Version the Seventh:
#   Added option to add a margin to the right hand side of the window.
#------------------------------------------------------------------------
# Also adds the following new escape sequences:
#
# \ww  - Word Wrap: turns word wrap on if it's off
# \nw  - No Wrap: Turns word wrap off
# \ws  - WhiteSpace mode: Converts newlines to spaces (like HTML)
# \nl  - New Line: Preserves hard returns
# \cs  - Collapse whiteSpace: Eliminates extra spaces (also like HTML)
# \pre - PRE-formatted: Preserves spaces
# \br  - line BRake: manual newline for whitespace mode
# \rm  - Right Margin: extra space on the right side of the window
#========================================================================

# Standard config module.
module KZIsAwesome
  module WordWrap

    # change this if you don't want wordwrap on by default.
    DEFAULT_WORDWRAP = true

    # change this if you want white space mode on by default.
    DEFAULT_WHITESPACE = false
   
    # change this if you want white space mode on by default.
    DEFAULT_COLLAPSE = true
    
    # change this to add a right margin to the window.
    DEFAULT_RIGHT_MARGIN = 0

  end
end

class Window_Base < Window
  include KZIsAwesome::WordWrap

  alias_method :initialize_kz_window_base, :initialize
  def initialize(x, y, width, height)
    initialize_kz_window_base(x, y, width, height)
    @wordwrap = DEFAULT_WORDWRAP
    @convert_newlines = DEFAULT_WHITESPACE
    @collapse_whitespace = DEFAULT_COLLAPSE
    @right_margin = DEFAULT_RIGHT_MARGIN
    @lastc = "\n"
  end

  alias_method :process_character_kz_window_base, :process_character
  def process_character(c, text, pos)
    c = ' ' if @convert_newlines && c == "\n"
    if @wordwrap && c =~ /[ \t]/
      c = '' if @collapse_whitespace && @lastc =~ /[\s\n\f]/
      if pos[:x] + get_next_word_size(c, text) > contents.width - @right_margin
        process_new_line(text, pos)
      else
        process_normal_character(c, pos)
      end
      @lastc = c
    else
      @lastc = c
      process_character_kz_window_base(c, text, pos)
    end
  end

  def get_next_word_size(c, text)
    # Split text by the next space/line/page and grab the first split
    nextword = text.split(/[\s\n\f]/, 2)[0]
    if nextword
      icons = 0
      if nextword =~ /\e/i
        # Get rid of color codes and YEA Message system outline colors
        nextword = nextword.split(/\e[oOcC]+\[\d*\]/).join
        # Get rid of message timing control codes
        nextword = nextword.split(/\e[\.\|\^<>!]/).join
        # Split text by the first non-icon escape code
        # (the hH is for compatibility with the Icon Hues script)
        nextword = nextword.split(/\e[^iIhH]+/, 2)[0]
        # Erase and count icons in remaining text
        nextword.gsub!(/\e[iIhH]+\[[\d,]*\]/) do
          icons += 1
          ''
        end if nextword
      end
      wordsize = (nextword ? text_size(c + nextword).width : text_size( c ).width)
      wordsize += icons * 24
    else
      wordsize = text_size( c ).width
    end
    return wordsize
  end

  alias_method :process_escape_character_kz_window_base, :process_escape_character
  def process_escape_character(code, text, pos)
    case code.upcase
    when 'WW'
      @wordwrap = true
    when 'NW'
      @wordwrap = false
    when 'WS'
      @convert_newlines = true
    when 'NL'
      @convert_newlines = false
    when 'CS'
      @collapse_whitespace = true
    when 'PRE'
      @collapse_whitespace = false
    when 'BR'
      process_new_line(text, pos)
      @lastc = "\n"
    when 'RM'
      @right_margin = obtain_escape_param(text)
    else
      process_escape_character_kz_window_base(code, text, pos)
    end
    # Recalculate the next word size and insert line breaks
    # (Needed primarily for font changes)
    if pos[:x] + get_next_word_size('', text) > contents.width
      process_new_line(text, pos)
    end
  end

end

 

 

Edit: Now up to version SEVEN! Six is afraid of it!)

Edited by KilloZapit

Share this post


Link to post
Share on other sites

Why haven't people raved about this so far? =D ... I believe a script like that has been desired by many until now, and it's even stand-alone without the need to include a whole message system.

 

Thanks a lot, I can use this so much, and keep up the good work! (Meaning: It would be sweet if you took care of the spaces some day, but I haven't noticed any problems so far.)

 

(Oh, and just a hint: Your module title "KZIsAwesome" might offend some Jews, be careful =) )

Edited by Caveras

Share this post


Link to post
Share on other sites
(Oh, and just a hint: Your module title "KZIsAwesome" might offend some Jews, be careful =) )

 

Um... why? I am honestly clueless.

 

@Cavera probably because MA's ATS: Formatting does this already.

 

Oh, I didn't know that existed! I did make mine to work with the YEA message system for whatever that's worth.

Edited by KilloZapit

Share this post


Link to post
Share on other sites

It should work for anything that uses draw_text_ex instead of draw_text. To my surprise, I just checked, and Skill and Item description windows actually DO use draw_text_ex, and this script DOES work with them. I didn't even realize it did!

Share this post


Link to post
Share on other sites

Works without further setup and with any window I've encountered so far, for anyone who's interested. Modern Algebra's State Viewer, YF's Menu scripts, MA's Bestiary/Monster Catalogue, all wrap their texts instantly now. A very versatile script without the need to edit much, if anything at all.

 

Does this also apply to Skill and Item description windows? If not, might be a good idea to also include that.

 

If *not*, then it's a good idea? =) I like it especially because it takes care of those windows too without making you check if the text fits each time (and you will likely have to check if you use custom fonts). But every user has different needs, right?

Edited by Caveras

Share this post


Link to post
Share on other sites
I don't think many have asked because of the response that would come "Just break the lines yourself." kudos to you kind sir for going the extra mile :)

 

And of course "Just break the lines yourself" is kind of useless if you try to mess with variable resolutions or sized windows for some reason or change the size during development.

Share this post


Link to post
Share on other sites

And of course "Just break the lines yourself" is kind of useless if you try to mess with variable resolutions or sized windows for some reason or change the size during development.

 

It doesn't work either for basically anything that is outside of the text box (eg: you have no control over it).

Share this post


Link to post
Share on other sites

Definitely gonna try this out. I'll report back in once I've messed with it.

 

Oh my god! This is insane! I can use default faces with this and not have messages cut off and have to be short, its all so orderly and refined.

 

This is good, I mean, look at this.

DatTextWrappin.png

That never would have fit before, and to think all this time I could have been using Modern Algebra's Formatting instead of being a pedantic dumbass and asking someone to make a face window script.

Edited by DAVE_EBUBBLES

Share this post


Link to post
Share on other sites

It appears your script has an incompatibility with Fomar's Al Bhed script. It causes a "stack too deep" error when trying to display a message in Al Bhed. Other than that, I'm in love with this script. Unfortunately, I'd have to be able to use both so here's to hoping that a fix is released.

Share this post


Link to post
Share on other sites

It appears your script has an incompatibility with Fomar's Al Bhed script. It causes a "stack too deep" error when trying to display a message in Al Bhed. Other than that, I'm in love with this script. Unfortunately, I'd have to be able to use both so here's to hoping that a fix is released.

Stack level too deep typically means you've got a duplicate of a script.

 

Check your script list and see if you have two of the same script.

Share this post


Link to post
Share on other sites

I already checked if they use the same script, and they don't. I just simply cannot reproduce the bug at all... maybe it's a different version of the Al-Bhed script I used?

Share this post


Link to post
Share on other sites

I did a little more testing. The problem lies with the Victor Engine Control Text 1.06 script. Looks as though the method "process_escape_character" is what causes it, since both scripts alias or overwrite this method.

 

Should be easy enough to work a fix on it, by checking to see if the Control Text script is present with an if case or something.

Share this post


Link to post
Share on other sites

Uh... Looking through Victor Engine Control Text 1.06 it dosn't seem to use process_escape_character at all. I tried that script too and it still works fine for me. Could you install this backtrace script and give me the backtrace when it is printed to the console?

 

I should also point out that aliasing the same method multiple times only really runs into problems if the same alias name is used, at least in my experience. Though it's possible to run out of stack space without having a infinite alias lookup loop I guess.

Edited by KilloZapit

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×