ondras / rot.js

ROguelike Toolkit in JavaScript. Cool dungeon-related stuff, interactive manual, documentation, tests!
https://ondras.github.io/rot.js/hp/
BSD 3-Clause "New" or "Revised" License
2.33k stars 254 forks source link

[Question] How to get the height of wrapped text? #138

Open Bozar opened 6 years ago

Bozar commented 6 years ago

Hi Ondras, I have made a game with rot.js, the libray is very handy! <3

But there is one feature I hope rot.js could provide natively. When I want to print a long text in the message board, the width and height of which is fixed, I need to know how 'tall' the wrapped text is. For example, the height of the following text is 3:

This is a very long and warpped text.

I have tried myself following these steps:

The length of the array (wrappedText) is the height of the wrapped text.

I don't know whether there are some better solutions. Could you please shed some light on this? Thank you. :)

blinkdog commented 6 years ago

I think you're looking for ROT.Text.measure:

measure: function(str, maxWidth)

This returns an object with fields width and height.

> ROT.Text.measure("This is a very long and wrapped text.", 13)
{ width: 13, height: 3 }

Maybe @ondras can add an entry to the interactive manual?

atiaxi commented 6 years ago

FWIW display.drawText will do the wrapping if you give it a maxWidth argument at the end; the function also returns the number of lines it ended up drawing so you can get the height-measure there.

That said, that's only useful if you don't need the height before you do the drawing :)

On Thu, Aug 16, 2018 at 1:14 PM Patrick Meade notifications@github.com wrote:

I think you're looking for ROT.Text.measure:

measure: function(str, maxWidth)

This returns an object with fields width and height.

ROT.Text.measure("This is a very long and wrapped text.", 13) { width: 13, height: 3 }

Maybe @ondras https://github.com/ondras can add an entry to the interactive manual?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ondras/rot.js/issues/138#issuecomment-413619352, or mute the thread https://github.com/notifications/unsubscribe-auth/AAA1TTCzXAWrEyfNf_MUxyw7vaRMtKeTks5uRaiNgaJpZM4V_ETi .

-- Roger Ostrander

Bozar commented 6 years ago

Wow, this is amazing. Thank you all!

That said, that's only useful if you don't need the height before you do the drawing :)

I have figured out a way to work around this.

let width = display.drawText(999, 999, "This is a very long and wrapped text.", 13);
display.clear();
display.drawText(1, 1, "This is a very long and wrapped text.", 13);

console.log("The width of the wrapped text is: " + width);