mad1231999

Untitled

Jan 31st, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.09 KB | None | 0 0
  1. function gui_textfield:edit()
  2.         if self.visible and self.editable then
  3.                 local pX, pY = math.floor(self.x), math.floor(self.y)
  4.                 -- pX, pY = pX, pY - 1
  5.                 -- print(pX .. ", " .. pY)
  6.                 local w, h = self.width, self.height
  7.                 term.setCursorPos(pX, math.floor(pY + h / 2))
  8.                 term.setCursorBlink(true)
  9.  
  10.                 local line = self.text
  11.                 local cursorX = line:len()
  12.                 local chars = {}
  13.  
  14.                 for i = 1, self.text:len() do
  15.                         table.insert(chars, self.text:sub(i, i))
  16.                 end
  17.  
  18.                 local function redraw(r_ch)
  19.                         term.setTextColor(self.textColor)
  20.                         term.setBackgroundColor(self.bgColor)
  21.                         term.setCursorPos(pX, math.floor(pY + h / 2))
  22.  
  23.                         line = ""
  24.  
  25.                         for _, c in ipairs(chars) do
  26.                                 line = line .. c
  27.                         end
  28.  
  29.                         local _line = line
  30.  
  31.                         if self.replaceChar then
  32.                                 line = ""
  33.  
  34.                                 for i = 1, _line:len() do
  35.                                         line = line .. self.replaceChar:sub(1, 1)
  36.                                 end
  37.                         end
  38.  
  39.                         for i = 1, w do
  40.                                 io.write(" ")
  41.                         end
  42.  
  43.                         term.setCursorPos(pX, math.floor(pY + h / 2))
  44.  
  45.                         local currentX = pX
  46.  
  47.                         if line:len() >= w - 1 and cursorX >= w - 1 then
  48.                                 io.write(line:sub(cursorX - (w - 2), cursorX))
  49.                                 currentX = pX + cursorX - line:sub(1, cursorX - (w - 1)):len()
  50.                         elseif line:len() >= w - 1 and cursorX < w - 1 then
  51.                                 io.write(line:sub(1, w - 1))
  52.                                 currentX = pX + cursorX
  53.                         else
  54.                                 io.write(line)
  55.                                 currentX = pX + cursorX
  56.                         end
  57.  
  58.                         line = _line
  59.  
  60.                         term.setCursorPos(currentX, math.floor(pY + h / 2))
  61.                 end
  62.  
  63.                 redraw(self.replaceChar)
  64.  
  65.                 while true do
  66.                         local ev, key = os.pullEvent()
  67.  
  68.                         if ev == "key" and (m_ch and #chars < m_ch or true) then
  69.                                 if key == keys.left then
  70.                                         if cursorX > 0 then
  71.                                                 cursorX = cursorX - 1
  72.                                         end
  73.                                 elseif key == keys.right then
  74.                                         if cursorX < line:len() then
  75.                                                 cursorX = cursorX + 1
  76.                                         end
  77.                                 elseif key == keys.enter then
  78.                                         break
  79.                                 elseif key == keys.backspace then
  80.                                         if cursorX > 0 then
  81.                                                 table.remove(chars, cursorX)
  82.                                                 cursorX = cursorX - 1
  83.                                         end
  84.                                 elseif key == keys.delete then
  85.                                         if cursorX < line:len() then
  86.                                                 table.remove(chars, cursorX + 1)
  87.                                         end
  88.                                 end
  89.                         elseif ev == "char" then
  90.                                 table.insert(chars, cursorX + 1, key)
  91.                                 cursorX = cursorX + 1
  92.                         end
  93.  
  94.                         redraw()
  95.                 end
  96.  
  97.                 term.setCursorBlink(false)
  98.                 self.text = line
  99.         end
  100. end
Advertisement
Add Comment
Please, Sign In to add comment