From 0ae70fed13a7be95ff6ca29b8117987bfbb6fe82 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Fri, 29 Nov 2019 20:15:58 +0000 Subject: [PATCH] Correctly implement mouse movement within read Note to self: if you're going to modify the rom, make sure you test on a computer which doesn't overwrite the rom with something else. --- gradle.properties | 2 +- .../assets/computercraft/lua/bios.lua | 41 +++++++++++-------- .../computercraft/lua/rom/help/changelog.txt | 4 ++ .../computercraft/lua/rom/help/whatsnew.txt | 7 +--- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/gradle.properties b/gradle.properties index 44f473b24..d3002e82e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ # Mod properties -mod_version=1.85.1 +mod_version=1.85.2 # Minecraft properties mc_version=1.12.2 diff --git a/src/main/resources/assets/computercraft/lua/bios.lua b/src/main/resources/assets/computercraft/lua/bios.lua index a6071e8c7..cebfe1fa3 100644 --- a/src/main/resources/assets/computercraft/lua/bios.lua +++ b/src/main/resources/assets/computercraft/lua/bios.lua @@ -291,7 +291,7 @@ function read( _sReplaceChar, _tHistory, _fnComplete, _sDefault ) sLine = "" end local nHistoryPos - local nPos = #sLine + local nPos, nScroll = #sLine, 0 if _sReplaceChar then _sReplaceChar = string.sub( _sReplaceChar, 1, 1 ) end @@ -321,16 +321,20 @@ function read( _sReplaceChar, _tHistory, _fnComplete, _sDefault ) local sx = term.getCursorPos() local function redraw( _bClear ) - local nScroll = 0 - if sx + nPos >= w then - nScroll = (sx + nPos) - w + local cursor_pos = nPos - nScroll + if sx + cursor_pos >= w then + -- We've moved beyond the RHS, ensure we're on the edge. + nScroll = sx + nPos - w + elseif cursor_pos < 0 then + -- We've moved beyond the LHS, ensure we're on the edge. + nScroll = nPos end local _, cy = term.getCursorPos() term.setCursorPos( sx, cy ) local sReplace = (_bClear and " ") or _sReplaceChar if sReplace then - term.write( string.rep( sReplace, math.max( string.len(sLine) - nScroll, 0 ) ) ) + term.write( string.rep( sReplace, math.max( #sLine - nScroll, 0 ) ) ) else term.write( string.sub( sLine, nScroll + 1 ) ) end @@ -345,7 +349,7 @@ function read( _sReplaceChar, _tHistory, _fnComplete, _sDefault ) term.setBackgroundColor( colors.gray ) end if sReplace then - term.write( string.rep( sReplace, string.len( sCompletion ) ) ) + term.write( string.rep( sReplace, #sCompletion ) ) else term.write( sCompletion ) end @@ -373,7 +377,7 @@ function read( _sReplaceChar, _tHistory, _fnComplete, _sDefault ) -- Find the common prefix of all the other suggestions which start with the same letter as the current one local sCompletion = tCompletions[ nCompletion ] sLine = sLine .. sCompletion - nPos = string.len( sLine ) + nPos = #sLine -- Redraw recomplete() @@ -381,7 +385,7 @@ function read( _sReplaceChar, _tHistory, _fnComplete, _sDefault ) end end while true do - local sEvent, param = os.pullEvent() + local sEvent, param, param1, param2 = os.pullEvent() if sEvent == "char" then -- Typed key clear() @@ -394,7 +398,7 @@ function read( _sReplaceChar, _tHistory, _fnComplete, _sDefault ) -- Pasted text clear() sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 ) - nPos = nPos + string.len( param ) + nPos = nPos + #param recomplete() redraw() @@ -419,7 +423,7 @@ function read( _sReplaceChar, _tHistory, _fnComplete, _sDefault ) elseif param == keys.right then -- Right - if nPos < string.len(sLine) then + if nPos < #sLine then -- Move right clear() nPos = nPos + 1 @@ -470,10 +474,10 @@ function read( _sReplaceChar, _tHistory, _fnComplete, _sDefault ) end if nHistoryPos then sLine = _tHistory[nHistoryPos] - nPos = string.len( sLine ) + nPos, nScroll = #sLine, 0 else sLine = "" - nPos = 0 + nPos, nScroll = 0, 0 end uncomplete() redraw() @@ -486,6 +490,7 @@ function read( _sReplaceChar, _tHistory, _fnComplete, _sDefault ) clear() sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 ) nPos = nPos - 1 + if nScroll > 0 then nScroll = nScroll - 1 end recomplete() redraw() end @@ -501,7 +506,7 @@ function read( _sReplaceChar, _tHistory, _fnComplete, _sDefault ) elseif param == keys.delete then -- Delete - if nPos < string.len(sLine) then + if nPos < #sLine then clear() sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 ) recomplete() @@ -510,9 +515,9 @@ function read( _sReplaceChar, _tHistory, _fnComplete, _sDefault ) elseif param == keys["end"] then -- End - if nPos < string.len(sLine ) then + if nPos < #sLine then clear() - nPos = string.len(sLine) + nPos = #sLine recomplete() redraw() end @@ -525,9 +530,9 @@ function read( _sReplaceChar, _tHistory, _fnComplete, _sDefault ) elseif sEvent == "mouse_click" or sEvent == "mouse_drag" and param == 1 then local _, cy = term.getCursorPos() - if param2 >= sx and param2 <= w and param2 == cy then - -- Then ensure we don't scroll beyond the current line - nPos = math.min(math.max(nScroll + x - sx, 0), #sLine) + if param1 >= sx and param1 <= w and param2 == cy then + -- Ensure we don't scroll beyond the current line + nPos = math.min(math.max(nScroll + param1 - sx, 0), #sLine) redraw() end diff --git a/src/main/resources/assets/computercraft/lua/rom/help/changelog.txt b/src/main/resources/assets/computercraft/lua/rom/help/changelog.txt index 284d42827..99140820e 100644 --- a/src/main/resources/assets/computercraft/lua/rom/help/changelog.txt +++ b/src/main/resources/assets/computercraft/lua/rom/help/changelog.txt @@ -1,3 +1,7 @@ +# New features in CC: Tweaked 1.85.2 + +* Fix crashes when using the mouse with advanced computers. + # New features in CC: Tweaked 1.85.1 * Add basic mouse support to `read` diff --git a/src/main/resources/assets/computercraft/lua/rom/help/whatsnew.txt b/src/main/resources/assets/computercraft/lua/rom/help/whatsnew.txt index 06930d735..b1a6d0cff 100644 --- a/src/main/resources/assets/computercraft/lua/rom/help/whatsnew.txt +++ b/src/main/resources/assets/computercraft/lua/rom/help/whatsnew.txt @@ -1,8 +1,5 @@ -New features in CC: Tweaked 1.85.1 +New features in CC: Tweaked 1.85.2 -* Add basic mouse support to `read` - -And several bug fixes: -* Fix turtles not having breaking particles. +* Fix crashes when using the mouse with advanced computers. Type "help changelog" to see the full version history.