From 2d9efbbb5a975ea3d129019d4609dc0bccd1ff79 Mon Sep 17 00:00:00 2001 From: Robert Jelic <36573031+NoryiE@users.noreply.github.com> Date: Sun, 21 May 2023 15:45:02 +0200 Subject: [PATCH] Update Textfield.lua Fixed small bug when position is bigger than container size Fixed paste event not working --- Basalt/objects/Textfield.lua | 78 ++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/Basalt/objects/Textfield.lua b/Basalt/objects/Textfield.lua index c802b30..48e571f 100644 --- a/Basalt/objects/Textfield.lua +++ b/Basalt/objects/Textfield.lua @@ -61,7 +61,7 @@ return function(name, basalt) end local function removeSelection(self) - local sx, ex, sy, ey = getSelectionCoordinates(self) + local sx, ex, sy, ey = getSelectionCoordinates() local startLine = lines[sy] local endLine = lines[ey] lines[sy] = startLine:sub(1, sx - 1) .. endLine:sub(ex + 1, endLine:len()) @@ -305,7 +305,7 @@ return function(name, basalt) return self end, - getXOffset = function(self, xOff) + getXOffset = function(self) return wIndex end, @@ -313,7 +313,7 @@ return function(name, basalt) return self:setOffset(xOff, nil) end, - getYOffset = function(self, xOff) + getYOffset = function(self) return hIndex end, @@ -333,7 +333,7 @@ return function(name, basalt) end, keyHandler = function(self, key) - if (base.keyHandler(self, event, key)) then + if (base.keyHandler(self, key)) then local parent = self:getParent() local obx, oby = self:getPosition() local w,h = self:getSize() @@ -586,29 +586,28 @@ return function(name, basalt) if (base.dragHandler(self, button, x, y)) then local parent = self:getParent() local obx, oby = self:getAbsolutePosition() - local anchx, anchy = self:getPosition() + local ox, oy = self:getPosition() local w,h = self:getSize() if (lines[y - oby + hIndex] ~= nil) then - if anchx <= x - obx + wIndex and anchx + w > x - obx + wIndex then + if(x - obx + wIndex > 0)and(x - obx + wIndex <= w)then textX = x - obx + wIndex textY = y - oby + hIndex - + if textX > lines[textY]:len() then textX = lines[textY]:len() + 1 end - endSelX = textX endSelY = textY - + if textX < wIndex then wIndex = textX - 1 if wIndex < 1 then wIndex = 1 end end - parent:setCursor(not isSelected(), anchx + textX - wIndex, anchy + textY - hIndex, self:getForeground()) + parent:setCursor(not isSelected(), ox + textX - wIndex, oy + textY - hIndex, self:getForeground()) self:updateDraw() - end + end end return true end @@ -671,7 +670,6 @@ return function(name, basalt) mouseUpHandler = function(self, button, x, y) if (base.mouseUpHandler(self, button, x, y)) then local obx, oby = self:getAbsolutePosition() - local anchx, anchy = self:getPosition() if (lines[y - oby + hIndex] ~= nil) then endSelX = x - obx + wIndex endSelY = y - oby + hIndex @@ -680,32 +678,31 @@ return function(name, basalt) end if(startSelX==endSelX)and(startSelY==endSelY)then startSelX, endSelX, startSelY, endSelY = nil, nil, nil, nil - end + end self:updateDraw() end return true end end, - eventHandler = function(self, event, paste, p2, p3, p4) - if(base.eventHandler(self, event, paste, p2, p3, p4))then - if(event=="paste")then - if(self:isFocused())then - local parent = self:getParent() - local fgColor, bgColor = self:getForeground(), self:getBackground() - local w, h = self:getSize() - lines[textY] = lines[textY]:sub(1, textX - 1) .. paste .. lines[textY]:sub(textX, lines[textY]:len()) - fgLines[textY] = fgLines[textY]:sub(1, textX - 1) .. tHex[fgColor]:rep(paste:len()) .. fgLines[textY]:sub(textX, fgLines[textY]:len()) - bgLines[textY] = bgLines[textY]:sub(1, textX - 1) .. tHex[bgColor]:rep(paste:len()) .. bgLines[textY]:sub(textX, bgLines[textY]:len()) - textX = textX + paste:len() - if (textX >= w + wIndex) then - wIndex = (textX+1)-w - end - local anchx, anchy = self:getPosition() - parent:setCursor(true, anchx + textX - wIndex, anchy + textY - hIndex, fgColor) - updateColors(self) - self:updateDraw() + eventHandler = function(self, event, paste, ...) + base.eventHandler(self, event, paste, ...) + if(event=="paste")then + if(self:isFocused())then + local parent = self:getParent() + local fgColor, bgColor = self:getForeground(), self:getBackground() + local w, h = self:getSize() + lines[textY] = lines[textY]:sub(1, textX - 1) .. paste .. lines[textY]:sub(textX, lines[textY]:len()) + fgLines[textY] = fgLines[textY]:sub(1, textX - 1) .. tHex[fgColor]:rep(paste:len()) .. fgLines[textY]:sub(textX, fgLines[textY]:len()) + bgLines[textY] = bgLines[textY]:sub(1, textX - 1) .. tHex[bgColor]:rep(paste:len()) .. bgLines[textY]:sub(textX, bgLines[textY]:len()) + textX = textX + paste:len() + if (textX >= w + wIndex) then + wIndex = (textX+1)-w end + local anchx, anchy = self:getPosition() + parent:setCursor(true, anchx + textX - wIndex, anchy + textY - hIndex, fgColor) + updateColors(self) + self:updateDraw() end end end, @@ -713,8 +710,6 @@ return function(name, basalt) draw = function(self) base.draw(self) self:addDraw("textfield", function() - local parent = self:getParent() - local obx, oby = self:getPosition() local w, h = self:getSize() local bgColor = tHex[self:getBackground()] local fgColor = tHex[self:getForeground()] @@ -740,21 +735,24 @@ return function(name, basalt) end if startSelX and endSelX and startSelY and endSelY then - local sx, ex, sy, ey = getSelectionCoordinates(self) + local sx, ex, sy, ey = getSelectionCoordinates() for n = sy, ey do local line = #lines[n] local xOffset = 0 if n == sy and n == ey then - xOffset = sx - 1 - line = line - (sx - 1) - (line - ex) + xOffset = sx - 1 - (wIndex - 1) + line = line - (sx - 1 - (wIndex - 1)) - (line - ex + (wIndex - 1)) elseif n == ey then - line = line - (line - ex) + line = line - (line - ex + (wIndex - 1)) elseif n == sy then line = line - (sx - 1) - xOffset = sx - 1 + xOffset = sx - 1 - (wIndex - 1) end - self:addBG(1 + xOffset, n, rep(tHex[selectionBG], line)) - self:addFG(1 + xOffset, n, rep(tHex[selectionFG], line)) + + local visible_line_length = math.min(line, w - xOffset) + + self:addBG(1 + xOffset, n, rep(tHex[selectionBG], visible_line_length)) + self:addFG(1 + xOffset, n, rep(tHex[selectionFG], visible_line_length)) end end end)