Added text editing functionality to bedit

This commit is contained in:
mcplayer3
2026-06-17 13:59:27 +10:00
parent afe27f2b41
commit 14b25d59fc
+61 -5
View File
@@ -50,6 +50,25 @@ local function renderText(xOffset, yOffset)
gpu.bitblt(0, 1, 1, resX, resY - 1, textBuffer, 1, 1) gpu.bitblt(0, 1, 1, resX, resY - 1, textBuffer, 1, 1)
end end
local function addLine(pos)
if pos <= #lines then
for i = #lines, pos, -1 do
lines[i + 1] = lines[i]
end
lines[pos] = ""
else
table.insert(lines, "")
end
end
local function removeLine(pos)
length = #lines
for i = pos + 1, length do
lines[i - 1] = lines[i]
end
lines[length] = nil
end
-- Initialize screen -- Initialize screen
renderText(0, 0) renderText(0, 0)
gpu.setForeground(0x000000) gpu.setForeground(0x000000)
@@ -113,8 +132,8 @@ while true do
end end
cursorY = cursorY - 1 cursorY = cursorY - 1
-- The cursor absolute position still has to be moved, even if on-screen it won't move -- The cursor absolute position still has to be moved, even if on-screen it won't move
if cursorX > string.len(lines[cursorY]) then -- cursor snapping if cursorX > string.len(lines[cursorY]) + 1 then -- cursor snapping, +1 to be 1 char in front of end of line
cursorX = string.len(lines[cursorY]) cursorX = string.len(lines[cursorY]) + 1
end end
end end
if keyboard.keys[eventArgs[4]] == "down" then if keyboard.keys[eventArgs[4]] == "down" then
@@ -128,8 +147,8 @@ while true do
end end
end end
cursorY = cursorY + 1 cursorY = cursorY + 1
if cursorX > string.len(lines[cursorY]) then -- cursor snapping if cursorX > string.len(lines[cursorY]) + 1 then
cursorX = string.len(lines[cursorY]) cursorX = string.len(lines[cursorY]) + 1
end end
end end
if keyboard.keys[eventArgs[4]] == "left" and cursorX > 1 then if keyboard.keys[eventArgs[4]] == "left" and cursorX > 1 then
@@ -144,7 +163,7 @@ while true do
end end
cursorX = cursorX - 1 cursorX = cursorX - 1
end end
if keyboard.keys[eventArgs[4]] == "right" and cursorX < string.len(lines[cursorY]) then if keyboard.keys[eventArgs[4]] == "right" and cursorX < string.len(lines[cursorY]) + 1 then
if cursorX - scrollX >= resX then if cursorX - scrollX >= resX then
renderBufferFlag = true renderBufferFlag = true
scrollX = scrollX + 1 scrollX = scrollX + 1
@@ -156,6 +175,43 @@ while true do
end end
cursorX = cursorX + 1 cursorX = cursorX + 1
end end
if not keyboard.keys.special[eventArgs[4]] then
local line = lines[cursorY] or ""
line = string.sub(line, 1, cursorX - 1) .. string.char(eventArgs[3]) .. string.sub(line, cursorX, -1)
lines[cursorY] = line
cursorX = cursorX + 1
renderBufferFlag = true
end
if keyboard.keys[eventArgs[4]] == "back" then
local line = lines[cursorY]
local prevline = lines[cursorY - 1]
if cursorX > 1 then
line = string.sub(line, 1, cursorX - 2) .. string.sub(line, cursorX, -1) -- remove 1 char
elseif prevline then
prevline = prevline .. line -- copy line up to the next before removing it
ocelot.log(prevline)
line = nil
end
if line then
lines[cursorY] = line
else
removeLine(cursorY)
end
lines[cursorY - 1] = prevline
if not line then
cursorY = cursorY - 1 -- this can only trigger if the line is not the first
end
if cursorX > 1 then
cursorX = cursorX - 1
end
renderBufferFlag = true
end
if keyboard.keys[eventArgs[4]] == "enter" then
addLine(cursorY + 1)
cursorY = cursorY + 1
cursorX = 1
renderBufferFlag = true
end
end end
until next(eventArgs) == nil until next(eventArgs) == nil
local displayedCursorX = cursorX - scrollX local displayedCursorX = cursorX - scrollX