I can't create a text box

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.
rusbelito
Prole
Posts: 9
Joined: Fri Jun 19, 2020 5:40 am

Re: I can't create a text box

Post by rusbelito »

darkfrei wrote: Wed Mar 17, 2021 6:05 pm How about this one?
viewtopic.php?t=3056
That didn't work for me, but the "love.text input" works perfectly for me, only I can't get the rectangle to keep the size of the text if it goes beyond two lines, this is the code that I have managed to build, replace the font which I am using just because of the "getfont"

Code: Select all

io.stdout:setvbuf("no")
local utf8 = require("utf8")

function love.load()
 string = ""
 font = love.graphics.getFont()
 strw = font:getWidth(string)
 strh = font:getHeight(string)
 anchorectangulo = 0  + strw * 2
 altorectangulo = 0 + strh * 2
 
love.keyboard.setKeyRepeat(true)
end

function love.textinput(t)
    string = string .. t
end

function love.keypressed(key)
    if key == "backspace" then
 
        local byteoffset = utf8.offset(string, -1)

        if byteoffset then
      
            string = string.sub(string, 1, byteoffset - 1)
        end
    end
end


function love.update()
  anchorectangulo = font:getWidth(string) + 10
  altorectangulo = altorectangulo
 
if  anchorectangulo > love.graphics.getWidth()/4 then
anchorectangulo = love.graphics.getWidth()/4 + 10
end

  
 
end
function love.draw()
 
  love.graphics.setFont(font)
  love.graphics.setColor(1, 1, 1, 0.2)
  love.graphics.rectangle("fill", 0, 0, anchorectangulo  , altorectangulo  )
  love.graphics.setColor(1, 1, 1, 1)
  love.graphics.printf(string, 0 + strw / 2, 0 + strh / 2,love.graphics.getWidth()/4)

end
Attachments
main.lua
(1.09 KiB) Downloaded 202 times
User avatar
darkfrei
Party member
Posts: 1178
Joined: Sat Feb 08, 2020 11:09 pm

Re: I can't create a text box

Post by darkfrei »

Not sure what you want, but you can update your text width here:

Code: Select all

function love.textinput(t)
    string = string .. t
    strw = font:getWidth(string)
    anchorectangulo = strw+10
end
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
rusbelito
Prole
Posts: 9
Joined: Fri Jun 19, 2020 5:40 am

Re: I can't create a text box

Post by rusbelito »

darkfrei wrote: Wed Mar 17, 2021 8:16 pm Not sure what you want, but you can update your text width here:

Code: Select all

function love.textinput(t)
    string = string .. t
    strw = font:getWidth(string)
    anchorectangulo = strw+10
end

oh, I didn't know, I think I put width when I actually wanted to refer to height, in case if you use the code that I put for you and add characters you will see that it reaches a certain range and then makes a line break to make the width of the rectangle I would follow it to that place just as I want but when doing the line break the rectangle does not adjust the height of the text so the text ends up leaving the rectangle, and sorry my English is not very good I have to help the translator XD
rusbelito
Prole
Posts: 9
Joined: Fri Jun 19, 2020 5:40 am

Re: I can't create a text box

Post by rusbelito »

darkfrei wrote: Wed Mar 17, 2021 6:05 pm How about this one?
https://love2d.org/forums/viewtopic.php?t=3056
I couldn't make it work , I was able to achieve that a text could be inserted in a rectangle, but tell me that when I get to x size of the exto I can make the rectangle jump lines but the text does not , my idea was to do something like this ,
but that you could also insert text , but either the text comes out of the rectangle or the rectangle is an odd size , I gave up on the idea :'c
Attachments
jvFCn.png
jvFCn.png (47.35 KiB) Viewed 4864 times
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: I can't create a text box

Post by pgimeno »

love.graphics.printf allows you to impose a horizontal limit to text, and it will wrap it for you.

Combine that with Font:getWrap to find the number of lines printed (the length of the returned table) and with Font:getHeight to find the height of each of these lines, and you can determine the total height of the text.

You can also decide to truncate the text when it doesn't fit, with love.graphics.setScissor.


Example:

Code: Select all

function balloonText(x, y, text, maxTextWidth)
  love.graphics.push("all")
  local margin = 10

  local font = love.graphics.getFont()
  local lineHeight = font:getHeight()
  local actualWidth, lines = font:getWrap(text, maxTextWidth)
  local textHeight = lineHeight * #lines

  love.graphics.setColor(1, 1, .5)
  love.graphics.rectangle("fill", x, y, margin + maxTextWidth + margin, margin + textHeight + margin, margin)
  love.graphics.setColor(0, 0, 0)
  love.graphics.printf(text, margin + x, margin + y, maxTextWidth)
  love.graphics.pop()
end

function love.draw()
  local text = 
  balloonText(300, 200, [[It looks like you're trying to make a game. Woud you like me to...
• Tell your boss you're slacking off again?
• Post a walk-through?
• Hide a clue in the waste paper basket?
• Teach you lua?]], 250)
end
Of course, improvements are possible by properly separating and indenting the different sections.
Post Reply

Who is online

Users browsing this forum: No registered users and 78 guests