this behavior limits the text of an entry field to a user defined width, and scrolls while you type and/or edit.
complete entered text is property pText.
property pPosition -- current cursorposition in complete text
property ptext -- contains the complete text
property pmember -- ref to member
property spritenum
property pFirstChar -- position of first character in visible text
property pLastChar -- position of last character in visible text
property pRestrain -- show pRestrain characters
property pMax -- max number of allowed characters. 0 is unlimited characters
on beginSprite me
pmember = sprite(spritenum).member
reset()
sprite(spritenum).editable = true
end
on reset me
-- reset all props
pMember.text = ""
pText = ""
the selstart = 0
the selend = 0
pPosition = 0
pFirstChar = pPosition + 1
pLastChar = pFirstChar + pRestrain - 1
end
on keydown me
thekey = the keypressed
-- parse input
case the keycode of
36: -- enter
cursor 4
popresults(true)
finddata(pText)
51: -- backspace
-- check if part of text is selected
if the selend <> the selstart then
delete pText.char[the selstart+ (pfirstchar - 1) + 1..the selend+ (pfirstchar - 1)]
the selend = the selstart
pPosition = the selstart + (pfirstchar - 1)
showtext() -- update visible text
else
-- just one character
if pPosition>0 then
pPosition = the selstart + (pfirstchar - 1)
pPosition = Pposition - 1
delete pText.char[pPosition+1]
toLeft() -- scroll to the left
showText() -- update visible text
-- set cursor
the selStart = pPosition - (pfirstchar-1)
the selEnd = the selStart
end if
end if
127: -- delete
-- check if part of text is selected
if the selend <> the selstart then
delete pText.char[the selstart+ (pfirstchar - 1) + 1..the selend+ (pfirstchar - 1)]
the selend = the selstart
pPosition = the selstart + (pfirstchar - 1)
showtext() -- update visible text
else
-- just one character
if pPosition pPosition = the selstart + (pfirstchar - 1)
delete pText.char[pPosition+1]
showText() -- update visible text
-- set cursor
the selStart = pPosition - (pfirstchar-1)
the selEnd = the selStart
end if
end if
123 : -- cursor left
pPosition = the selstart + (pfirstchar-1)
pPosition = pPosition - 1
if pPosition < 0 then pPosition = 0
if the selEnd = 0 then
toLeft() -- scrollleft
showText() -- and update visible text
end if
-- set cursor
the selStart = pPosition - (pfirstchar-1)
the selEnd = the selStart
124 : -- cursor right
pPosition = the selstart + (pfirstchar - 1)
pPosition = pPosition + 1
if pPosition>pText.length then pPosition = pText.length
if the selEnd = pRestrain then
toRight() -- scroll right
showText() -- and update visible text
end if
-- set cursor
the selStart = pPosition - (pfirstchar - 1)
the selEnd = the selStart
otherwise
-- all other keys
if pText.length <= pMax or pMax = 0 then
-- checkinput
if "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0123456789" contains theKey then
-- insert text at pPosition
pPosition = the selStart + pFirstChar-1
pPosition = pPosition + 1
-- set text
put theKey before pText.char[pPosition]
-- scroll right if needed
if the selEnd = pRestrain then
toRight()
end if
-- update visible text
showtext()
-- set pointer
the selstart = pPosition - pfirstchar + 1
the selend = the selstart
end if
end if
end case
end
on showtext me
pMember.text = chars(ptext,pFirstChar,pLastChar)
end
on toRight me
pFirstChar = pFirstChar + 1
pLastChar = pFirstChar + pRestrain - 1
end
on toLeft me
pFirstChar = pFirstChar - 1
if pFirstChar < 1 then pFirstChar = 1
pLastChar = pFirstChar + pRestrain - 1
end