Update Textfield.lua

Fixed small bug when position is bigger than container size
Fixed paste event not working
This commit is contained in:
Robert Jelic
2023-05-21 15:45:02 +02:00
parent 9b0af8fcb3
commit 2d9efbbb5a

View File

@@ -61,7 +61,7 @@ return function(name, basalt)
end end
local function removeSelection(self) local function removeSelection(self)
local sx, ex, sy, ey = getSelectionCoordinates(self) local sx, ex, sy, ey = getSelectionCoordinates()
local startLine = lines[sy] local startLine = lines[sy]
local endLine = lines[ey] local endLine = lines[ey]
lines[sy] = startLine:sub(1, sx - 1) .. endLine:sub(ex + 1, endLine:len()) lines[sy] = startLine:sub(1, sx - 1) .. endLine:sub(ex + 1, endLine:len())
@@ -305,7 +305,7 @@ return function(name, basalt)
return self return self
end, end,
getXOffset = function(self, xOff) getXOffset = function(self)
return wIndex return wIndex
end, end,
@@ -313,7 +313,7 @@ return function(name, basalt)
return self:setOffset(xOff, nil) return self:setOffset(xOff, nil)
end, end,
getYOffset = function(self, xOff) getYOffset = function(self)
return hIndex return hIndex
end, end,
@@ -333,7 +333,7 @@ return function(name, basalt)
end, end,
keyHandler = function(self, key) keyHandler = function(self, key)
if (base.keyHandler(self, event, key)) then if (base.keyHandler(self, key)) then
local parent = self:getParent() local parent = self:getParent()
local obx, oby = self:getPosition() local obx, oby = self:getPosition()
local w,h = self:getSize() local w,h = self:getSize()
@@ -586,17 +586,16 @@ return function(name, basalt)
if (base.dragHandler(self, button, x, y)) then if (base.dragHandler(self, button, x, y)) then
local parent = self:getParent() local parent = self:getParent()
local obx, oby = self:getAbsolutePosition() local obx, oby = self:getAbsolutePosition()
local anchx, anchy = self:getPosition() local ox, oy = self:getPosition()
local w,h = self:getSize() local w,h = self:getSize()
if (lines[y - oby + hIndex] ~= nil) then 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 textX = x - obx + wIndex
textY = y - oby + hIndex textY = y - oby + hIndex
if textX > lines[textY]:len() then if textX > lines[textY]:len() then
textX = lines[textY]:len() + 1 textX = lines[textY]:len() + 1
end end
endSelX = textX endSelX = textX
endSelY = textY endSelY = textY
@@ -606,7 +605,7 @@ return function(name, basalt)
wIndex = 1 wIndex = 1
end end
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() self:updateDraw()
end end
end end
@@ -671,7 +670,6 @@ return function(name, basalt)
mouseUpHandler = function(self, button, x, y) mouseUpHandler = function(self, button, x, y)
if (base.mouseUpHandler(self, button, x, y)) then if (base.mouseUpHandler(self, button, x, y)) then
local obx, oby = self:getAbsolutePosition() local obx, oby = self:getAbsolutePosition()
local anchx, anchy = self:getPosition()
if (lines[y - oby + hIndex] ~= nil) then if (lines[y - oby + hIndex] ~= nil) then
endSelX = x - obx + wIndex endSelX = x - obx + wIndex
endSelY = y - oby + hIndex endSelY = y - oby + hIndex
@@ -687,25 +685,24 @@ return function(name, basalt)
end end
end, end,
eventHandler = function(self, event, paste, p2, p3, p4) eventHandler = function(self, event, paste, ...)
if(base.eventHandler(self, event, paste, p2, p3, p4))then base.eventHandler(self, event, paste, ...)
if(event=="paste")then if(event=="paste")then
if(self:isFocused())then if(self:isFocused())then
local parent = self:getParent() local parent = self:getParent()
local fgColor, bgColor = self:getForeground(), self:getBackground() local fgColor, bgColor = self:getForeground(), self:getBackground()
local w, h = self:getSize() local w, h = self:getSize()
lines[textY] = lines[textY]:sub(1, textX - 1) .. paste .. lines[textY]:sub(textX, lines[textY]:len()) 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()) 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()) bgLines[textY] = bgLines[textY]:sub(1, textX - 1) .. tHex[bgColor]:rep(paste:len()) .. bgLines[textY]:sub(textX, bgLines[textY]:len())
textX = textX + paste:len() textX = textX + paste:len()
if (textX >= w + wIndex) then if (textX >= w + wIndex) then
wIndex = (textX+1)-w 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
local anchx, anchy = self:getPosition()
parent:setCursor(true, anchx + textX - wIndex, anchy + textY - hIndex, fgColor)
updateColors(self)
self:updateDraw()
end end
end end
end, end,
@@ -713,8 +710,6 @@ return function(name, basalt)
draw = function(self) draw = function(self)
base.draw(self) base.draw(self)
self:addDraw("textfield", function() self:addDraw("textfield", function()
local parent = self:getParent()
local obx, oby = self:getPosition()
local w, h = self:getSize() local w, h = self:getSize()
local bgColor = tHex[self:getBackground()] local bgColor = tHex[self:getBackground()]
local fgColor = tHex[self:getForeground()] local fgColor = tHex[self:getForeground()]
@@ -740,21 +735,24 @@ return function(name, basalt)
end end
if startSelX and endSelX and startSelY and endSelY then 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 for n = sy, ey do
local line = #lines[n] local line = #lines[n]
local xOffset = 0 local xOffset = 0
if n == sy and n == ey then if n == sy and n == ey then
xOffset = sx - 1 xOffset = sx - 1 - (wIndex - 1)
line = line - (sx - 1) - (line - ex) line = line - (sx - 1 - (wIndex - 1)) - (line - ex + (wIndex - 1))
elseif n == ey then elseif n == ey then
line = line - (line - ex) line = line - (line - ex + (wIndex - 1))
elseif n == sy then elseif n == sy then
line = line - (sx - 1) line = line - (sx - 1)
xOffset = sx - 1 xOffset = sx - 1 - (wIndex - 1)
end 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 end
end) end)