Advertisement
Redxone

[Computercraft] NovaRead

Jun 2nd, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.38 KB | None | 0 0
  1. local str = ""
  2. local active = true
  3. local inputindex = #str
  4. local function setStartStr(nstr)
  5.     str = nstr
  6. end
  7. function read(e,maxlen,mslen,x,y)
  8.     term.setCursorBlink(active)
  9.     local function drawstr()
  10.         term.setCursorPos(x,y)
  11.         write(string.rep(" ",mslen+1))
  12.         term.setCursorPos(x,y)
  13.         if(#str > mslen)then
  14.             local dif = #str - mslen
  15.             if(inputindex <= dif)then
  16.                 write(str:sub(inputindex,mslen+inputindex))
  17.             else
  18.                 write(str:sub(dif,mslen+dif))
  19.             end
  20.         else
  21.             write(str)
  22.         end
  23.         if(inputindex > mslen)then
  24.             term.setCursorPos(x+inputindex-(#str-mslen),y)
  25.         else
  26.             term.setCursorPos(x+inputindex,y)
  27.         end
  28.     end
  29.     local function undrawstr()
  30.         term.setCursorPos(x,y)
  31.         write(string.rep(" ",mslen))
  32.     end
  33.     local function updatestr()
  34.         inputindex = inputindex + 1
  35.         drawstr()
  36.     end
  37.     if(active)then
  38.         if(e[1] == "char" and #str < maxlen)then
  39.             str = str:sub(1,inputindex) .. tostring(e[2]) .. str:sub(inputindex+1,#str)
  40.             updatestr()
  41.         end
  42.         if(e[1] == "key")then
  43.             local key = e[2]
  44.              if(key == keys.enter)then
  45.                 term.setCursorBlink(false)
  46.                 active = false
  47.                 return true
  48.              end
  49.              if(key == keys.backspace and inputindex > 0)then
  50.                  undrawstr()
  51.                  str =  string.sub( str, 1, inputindex - 1 ) .. string.sub( str, inputindex + 1 )
  52.                  inputindex = inputindex - 1
  53.                  drawstr()
  54.              end
  55.              if(key == keys.left and inputindex > 0)then
  56.                  inputindex = inputindex - 1
  57.                  drawstr()
  58.              end
  59.              if(key == keys.right and inputindex < #str)then
  60.                  inputindex = inputindex + 1
  61.                  drawstr()
  62.              end
  63.              if(key == keys.delete and inputindex < #str)then
  64.                  undrawstr()
  65.                  str = string.sub( str, 1, inputindex ) .. string.sub( str, inputindex + 2 )
  66.                  drawstr()
  67.              end
  68.         end
  69.     end
  70.     if(e[1] == "mouse_click" and (e[4] ~= y or e[3] < x or e[3] > x+maxlen) )then
  71.         active = false
  72.         term.setCursorBlink(active)
  73.     elseif(e[1] == "mouse_click" and e[4] == y)then
  74.         if(not active)then
  75.             active = true
  76.             term.setCursorPos(x+inputindex,y)
  77.             term.setCursorBlink(active)
  78.         end
  79.         if(e[3]-x >= 0 and e[3]-x <= #str)then
  80.             inputindex = e[3]-x
  81.             drawstr()
  82.         end
  83.     end
  84. end
  85. function getInput()
  86.     return str
  87. end
  88. function resetInput()
  89.     str = ""
  90.     inputindex = #str
  91. end
  92. function setInput(nstr)
  93.     str = nstr
  94.     inputindex = #str
  95. end
  96. function setActive(act)
  97.     active = act
  98. end
  99. function getActive()
  100.     return active
  101. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement