Page 2 of 2

Re: These buttons execute the same callback but they are different tables!

Posted: Thu Oct 14, 2021 12:43 pm
by BrotSagtMist
Actually take the whole example:

Code: Select all

utf8=require("utf8")
Line="1234567890"
Pos=3
Font=love.graphics.newFont()
A,B=string.match(Line, "("..utf8.charpattern:rep(Pos)..")(.*)")
function love.keypressed(k)
 if k =="backspace" then Delete() 
 elseif k== "right" then 
  Pos=Pos+1
  A,B=string.match(Line, "("..utf8.charpattern:rep(Pos)..")(.*)")
 elseif k== "left" then 
  Pos=Pos-1
  A,B=string.match(Line, "("..utf8.charpattern:rep(Pos)..")(.*)")
 end
end
function love.textinput(k)
  Pos=Pos+1
  A,B=string.match(Line, "("..utf8.charpattern:rep(Pos-1)..")(.*)")
  A=A..k
  Line=A.. (B or "")
end
function Delete()
 A,_,B=string.match(Line, "("..utf8.charpattern:rep(Pos-1)..")("..utf8.charpattern..")(.*)")  
 Line=A..(B or "")
 Pos=Pos-1
end
function love.draw()
 love.graphics.print(Line)
 love.graphics.print("|", Pos~=0 and Font:getWidth(A) or nil)  
end
There are a ton of exceptions to add that makes coding a good text input painfully boring tho.