Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function gui_textfield:edit()
- if self.visible and self.editable then
- local pX, pY = math.floor(self.x), math.floor(self.y)
- -- pX, pY = pX, pY - 1
- -- print(pX .. ", " .. pY)
- local w, h = self.width, self.height
- term.setCursorPos(pX, math.floor(pY + h / 2))
- term.setCursorBlink(true)
- local line = self.text
- local cursorX = line:len()
- local chars = {}
- for i = 1, self.text:len() do
- table.insert(chars, self.text:sub(i, i))
- end
- local function redraw(r_ch)
- term.setTextColor(self.textColor)
- term.setBackgroundColor(self.bgColor)
- term.setCursorPos(pX, math.floor(pY + h / 2))
- line = ""
- for _, c in ipairs(chars) do
- line = line .. c
- end
- local _line = line
- if self.replaceChar then
- line = ""
- for i = 1, _line:len() do
- line = line .. self.replaceChar:sub(1, 1)
- end
- end
- for i = 1, w do
- io.write(" ")
- end
- term.setCursorPos(pX, math.floor(pY + h / 2))
- local currentX = pX
- if line:len() >= w - 1 and cursorX >= w - 1 then
- io.write(line:sub(cursorX - (w - 2), cursorX))
- currentX = pX + cursorX - line:sub(1, cursorX - (w - 1)):len()
- elseif line:len() >= w - 1 and cursorX < w - 1 then
- io.write(line:sub(1, w - 1))
- currentX = pX + cursorX
- else
- io.write(line)
- currentX = pX + cursorX
- end
- line = _line
- term.setCursorPos(currentX, math.floor(pY + h / 2))
- end
- redraw(self.replaceChar)
- while true do
- local ev, key = os.pullEvent()
- if ev == "key" and (m_ch and #chars < m_ch or true) then
- if key == keys.left then
- if cursorX > 0 then
- cursorX = cursorX - 1
- end
- elseif key == keys.right then
- if cursorX < line:len() then
- cursorX = cursorX + 1
- end
- elseif key == keys.enter then
- break
- elseif key == keys.backspace then
- if cursorX > 0 then
- table.remove(chars, cursorX)
- cursorX = cursorX - 1
- end
- elseif key == keys.delete then
- if cursorX < line:len() then
- table.remove(chars, cursorX + 1)
- end
- end
- elseif ev == "char" then
- table.insert(chars, cursorX + 1, key)
- cursorX = cursorX + 1
- end
- redraw()
- end
- term.setCursorBlink(false)
- self.text = line
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment