How can I make scrolling text boxes? (or get the number of lines of text?)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
sanjiv
Citizen
Posts: 88
Joined: Mon Feb 27, 2012 5:11 am

How can I make scrolling text boxes? (or get the number of lines of text?)

Post by sanjiv »

I would like to display large bodies of text within the game, likely read from text files.

The challenge is figuring out the "height" or length that a body of text would take on the screen, assuming we can set the width of text (in the love.graphics.printf function), and we know the size of the font.

Worst case scenario, I can control the line breaks in the original text file, and then just count the number of lines in the text file when I load it / read it.

However, I would prefer an option where I didn't have to control the line breaks in the original text file, which would be an extra editorial step.
marco.lizza
Citizen
Posts: 52
Joined: Wed Dec 23, 2015 4:03 pm

Re: How can I make scrolling text boxes? (or get the number of lines of text?)

Post by marco.lizza »

Give the canvas destination rectangle (or, at least, the maximum line length in pixels) I would break the texts on whitespaces. Then, iterating the words I would build every single line of text, measuring the line width. Upon reaching the maximum length, store the current line and start new one.

Code: Select all

local function split_by_words(text)
  local words = {}
  for word in text:gmatch("[^%s]+") do
    words[#words + 1] = word
  end
  return words
end

local function build_lines(words, font, max_width)
  local lines = {}
  local line = ''
  for _, word in ipairs(words) do
  line = line .. ' ' .. word
  width = font:getWidth(line)
  if width >= max_width then
    lines[#lines + 1] = line
    line = ''
  end
  end

  if #line > 0 then
    lines[#lines + 1] = line
  end
  return lines
end

local text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec placerat velit a convallis finibus. Donec eget ultricies turpis. Duis lobortis ornare sem sed gravida. Nunc vel justo aliquam, viverra est non, ullamcorper magna. Morbi ut metus nec lectus imperdiet gravida quis luctus nisi. Proin id metus at dolor pulvinar accumsan. Etiam lobortis ornare suscipit. Suspendisse ultrices, erat eget mollis sollicitudin, ante dui finibus ligula, at ultrices lacus justo in arcu. Morbi condimentum pellentesque mauris, at hendrerit magna hendrerit eget. Nullam maximus tempor nisl ut accumsan. Donec velit ex, fringilla sit amet augue et, mattis posuere velit. Duis pretium nec massa quis consequat.'

local words = split_by_words(text)

local font = love.graphics.newFont(10)

local lines = build_lines(words, 128)
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: How can I make scrolling text boxes? (or get the number of lines of text?)

Post by pgimeno »

Look up [wiki]Font:getWrap[/wiki]
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How can I make scrolling text boxes? (or get the number of lines of text?)

Post by zorg »

pgimeno wrote:Look up [wiki]Font:getWrap[/wiki]
This, along with [wiki]Font:getHeight[/wiki] should do the job; the above even returns the correctly chopped up lines from the original text, given a wrap limit. To get the number of lines it divides into, just do a # on the returned lines table.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
sanjiv
Citizen
Posts: 88
Joined: Mon Feb 27, 2012 5:11 am

Re: How can I make scrolling text boxes? (or get the number of lines of text?)

Post by sanjiv »

Thanks! Why is it that getHeight returns a higher number than what I set with love.graphics.setFont?
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How can I make scrolling text boxes? (or get the number of lines of text?)

Post by zorg »

My guess is that it's Font dependent; or that a size 12 Font, for example, is not really 12 pixels in height... not sure though.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
sanjiv
Citizen
Posts: 88
Joined: Mon Feb 27, 2012 5:11 am

Re: How can I make scrolling text boxes? (or get the number of lines of text?)

Post by sanjiv »

[FIXED. As it turns out I was making a silly mistake with string.sub. I think it's fixed now.]

I'm currently getting my text from "contents = love.filesystem.read( fileName )", but when I go to print it, line breaks and tabs show up as []s. I tried using some logic to avoid printing the last character of a line if it was a line break, but I'm lacking information.

I imagine it has to do with '\0' (null) characters, but I'm out of my depth.

most recently I tried using
local lineBreak = string.sub(s.wrappedText[1],1,-2)
and then testing
if string.sub(s.wrappedText,1,-2) == lineBreak
but each lineBreak seems to be different.
Last edited by sanjiv on Fri Jun 03, 2016 1:35 am, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How can I make scrolling text boxes? (or get the number of lines of text?)

Post by zorg »

Maybe something like this?

Code: Select all

string.gsub(str, '\t', '    ') -- replace tabs with 4 spaces or whatever, useful for monspaced fonts mostly though...
Also, if what you're reading in is a plain text file, then i think love.filesystem has a line iterator too, that detects newlines, but having the contents in only one string spares you a loop, table.inserts and one table.concat function; since you'll probably use Font:getWrap on it anyways. :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests