How to .drawtext with automatic line break?

Nightcrow

Warper
Member
Joined
Mar 5, 2018
Messages
2
Reaction score
0
First Language
German
Primarily Uses
RMMV
Hey guys,

so I am currently scripting and playing around with the RPG Maker MV and I am making a profession system/menu from scratch to get to know JavaScript. I have some basic Ruby and advanced Python experience, so I just dug right in.

The scenario is this: I created a help window which shows a description of the selected recipe from the recipe list, but the text in that help window does not break lines automatically. I tried and searched in the Window_Base section for a solution and I am guessing I am searching in the right spot, but I cannot wrap my head around a solution to that problem.
I tried to play around with the .process[...] functions and the .drawtext's maxWidth, but as expected, it just does not work that easily. :rolleyes:

So .. how do I write a function or string of code that checks if the displayed text is exceeding the .drawtext's maxWidth and breaks the line before the word which is being cut off?

Thank you for taking your time,
Nightcrow
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,087
Reaction score
1,508
First Language
EN
Primarily Uses
RMMZ
There may be an easier way, but I'd go about it roughly like this:
  • First, identify break points for new lines (the tricky bit!). There's a textWidth(text) function defined on Window_Base that you could use for this. I'd probably do this by adding words one by one until the resulting line would be wider than the width available (or the end of the text is reached), if that makes sense.
  • Split the input text into an array of text lines based on these points.
  • drawText once per line with an appropriate y-offset, e.g. this.lineHeight().
You could also check out a plugin that has implemented wordwrapping (e.g. Yanfly's Message Core) to see how it's been done before. ^_^
 

Nightcrow

Warper
Member
Joined
Mar 5, 2018
Messages
2
Reaction score
0
First Language
German
Primarily Uses
RMMV
Hey :)

Thanks for your reply! You gave me some good inspiration. I am planning on checking if the width of the string is bigger than the width of the window and if so, to look for the last space character within the string and place a \n just right at that position. It is not that complicated nor hard to write actually, BUT \n does not work.
I tried to:
  • add \n in quotes
  • add \n without quotes
  • add \n right in the original string with quotes/without them
  • save \n in a seperate variable and add it between two strings (which are saved in variables)
  • save the strings in arrays and test that with everything mentioned above
Do you have any suggestions on what I could try out? I cannot open Yanfly's Message Core for some reason .. o_O

Thank you,
Nightcrow
 

LTN Games

Indie Studio
Veteran
Joined
Jun 25, 2015
Messages
704
Reaction score
631
First Language
English
Primarily Uses
RMMV
It's a fairly easy method depending on how you want it done.
Here I created one for my Quest Journal plugin: https://gitlab.com/FeniXEngineMV/plugins/blob/dev/src/FNX_QuestJournal.js#L1087

Here is a slightly modified version of the above, to clean up the unnecessary stuff. It should work, it's nothing special and you can modify as you wish, it's MIT License.

PHP:
 Window_Base.prototype.drawTextAutoWrap = function (text, x, y) {
    if (!text) {
      return
    }
    const words = text.split(' ')
    let x2 = x
    let y2 = y
    words.forEach((word) => {
      word = this.convertEscapeCharacters(word)
      const width = this.textWidth(word + ' ')
      // Check for linebreak symbol '/n'
      if (word === `\x1bn`) {
        y2 += this.lineHeight()
        x2 = 0
      }
      if (x2 + width >= this.contents.width) {
        y2 += this.lineHeight()
        x2 = 0
      }
      this.drawText(word + ' ', x2, y2)
      x2 += width
    })
  }
Edit: It may not work as expected, it's old and could use some testing ;)
 

Frogboy

I'm not weak to fire
Veteran
Joined
Apr 19, 2016
Messages
1,704
Reaction score
2,208
First Language
English
Primarily Uses
RMMV
In RPG Maker MV, \n doesn't work. You'll want to use String.fromCharCode(13). It's either 13 or 10. I can't remember off the top of my head. That'll give you a line break, though.
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,125
Reaction score
10,639
First Language
Czech
Primarily Uses
RMMV
\n does work, it is even defined in the scripts, but you need to use drawTextEx instead of drawText. drawTextEx is the thing that allows you to use escape codes, you can't use them with just drawText. But it is trueusing drawTextEx complicates matters a bit, since it processes text via characters.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 2)

Latest Threads

Latest Posts

Latest Profile Posts

How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c
So I was playing with filters and this looked interesting...

Versus the normal look...

Kind of gives a very different feel. :LZSexcite:
To whom ever person or persons who re-did the DS/DS+ asset packs for MV (as in, they are all 48x48, and not just x2 the pixel scale) .... THANK-YOU!!!!!!!!! XwwwwX

Forum statistics

Threads
105,849
Messages
1,016,977
Members
137,563
Latest member
cexojow
Top