Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local str = ""
- local active = true
- local inputindex = #str
- local function setStartStr(nstr)
- str = nstr
- end
- function read(e,maxlen,mslen,x,y)
- term.setCursorBlink(active)
- local function drawstr()
- term.setCursorPos(x,y)
- write(string.rep(" ",mslen+1))
- term.setCursorPos(x,y)
- if(#str > mslen)then
- local dif = #str - mslen
- if(inputindex <= dif)then
- write(str:sub(inputindex,mslen+inputindex))
- else
- write(str:sub(dif,mslen+dif))
- end
- else
- write(str)
- end
- if(inputindex > mslen)then
- term.setCursorPos(x+inputindex-(#str-mslen),y)
- else
- term.setCursorPos(x+inputindex,y)
- end
- end
- local function undrawstr()
- term.setCursorPos(x,y)
- write(string.rep(" ",mslen))
- end
- local function updatestr()
- inputindex = inputindex + 1
- drawstr()
- end
- if(active)then
- if(e[1] == "char" and #str < maxlen)then
- str = str:sub(1,inputindex) .. tostring(e[2]) .. str:sub(inputindex+1,#str)
- updatestr()
- end
- if(e[1] == "key")then
- local key = e[2]
- if(key == keys.enter)then
- term.setCursorBlink(false)
- active = false
- return true
- end
- if(key == keys.backspace and inputindex > 0)then
- undrawstr()
- str = string.sub( str, 1, inputindex - 1 ) .. string.sub( str, inputindex + 1 )
- inputindex = inputindex - 1
- drawstr()
- end
- if(key == keys.left and inputindex > 0)then
- inputindex = inputindex - 1
- drawstr()
- end
- if(key == keys.right and inputindex < #str)then
- inputindex = inputindex + 1
- drawstr()
- end
- if(key == keys.delete and inputindex < #str)then
- undrawstr()
- str = string.sub( str, 1, inputindex ) .. string.sub( str, inputindex + 2 )
- drawstr()
- end
- end
- end
- if(e[1] == "mouse_click" and (e[4] ~= y or e[3] < x or e[3] > x+maxlen) )then
- active = false
- term.setCursorBlink(active)
- elseif(e[1] == "mouse_click" and e[4] == y)then
- if(not active)then
- active = true
- term.setCursorPos(x+inputindex,y)
- term.setCursorBlink(active)
- end
- local mclen = #str
- if(#str > mslen)then
- mclen = mslen
- end
- if(e[3]-x >= 0 and e[3]-x <= mclen)then
- if( inputindex > mslen )then
- inputindex = (inputindex-mslen)+(e[3]-x)
- else
- inputindex = (e[3]-x)
- end
- drawstr()
- end
- end
- end
- function getInput()
- return str
- end
- function resetInput()
- str = ""
- inputindex = #str
- end
- function setInput(nstr)
- str = nstr
- inputindex = #str
- end
- function setActive(act)
- active = act
- end
- function getActive()
- return active
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement