removed redundant methods
This commit is contained in:
@@ -2459,59 +2459,6 @@ function Element:replaceText(startPos, endPos, newText)
|
||||
end
|
||||
end
|
||||
|
||||
--- Split text into lines (for multi-line text)
|
||||
function Element:_splitLines()
|
||||
if not self.editable then
|
||||
return
|
||||
end
|
||||
|
||||
if not self.multiline then
|
||||
self._lines = { self._textBuffer or "" }
|
||||
return
|
||||
end
|
||||
|
||||
self._lines = {}
|
||||
local text = self._textBuffer or ""
|
||||
|
||||
-- Split on newlines
|
||||
for line in (text .. "\n"):gmatch("([^\n]*)\n") do
|
||||
table.insert(self._lines, line)
|
||||
end
|
||||
|
||||
-- Ensure at least one line
|
||||
if #self._lines == 0 then
|
||||
self._lines = { "" }
|
||||
end
|
||||
end
|
||||
|
||||
--- Calculate text wrapping
|
||||
function Element:_calculateWrapping()
|
||||
if not self.editable or not self.textWrap then
|
||||
self._wrappedLines = nil
|
||||
return
|
||||
end
|
||||
|
||||
self._wrappedLines = {}
|
||||
local availableWidth = self.width - self.padding.left - self.padding.right
|
||||
|
||||
for lineNum, line in ipairs(self._lines or {}) do
|
||||
if line == "" then
|
||||
table.insert(self._wrappedLines, {
|
||||
text = "",
|
||||
startIdx = 0,
|
||||
endIdx = 0,
|
||||
lineNum = lineNum,
|
||||
})
|
||||
else
|
||||
local wrappedParts = self:_wrapLine(line, availableWidth)
|
||||
for _, part in ipairs(wrappedParts) do
|
||||
part.lineNum = lineNum
|
||||
table.insert(self._wrappedLines, part)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Wrap a single line of text
|
||||
---@param line string -- Line to wrap
|
||||
---@param maxWidth number -- Maximum width in pixels
|
||||
@@ -2529,154 +2476,6 @@ end
|
||||
-- Input Handling - Mouse Selection
|
||||
-- ====================
|
||||
|
||||
--- Convert mouse coordinates to cursor position in text
|
||||
---@param mouseX number -- Mouse X coordinate (absolute)
|
||||
---@param mouseY number -- Mouse Y coordinate (absolute)
|
||||
---@return number -- Cursor position (character index)
|
||||
function Element:_mouseToTextPosition(mouseX, mouseY)
|
||||
if not self.editable or not self._textBuffer then
|
||||
return 0
|
||||
end
|
||||
|
||||
-- Get content area bounds
|
||||
local contentX = (self._absoluteX or self.x) + self.padding.left
|
||||
local contentY = (self._absoluteY or self.y) + self.padding.top
|
||||
|
||||
-- Calculate relative position within text area
|
||||
local relativeX = mouseX - contentX
|
||||
local relativeY = mouseY - contentY
|
||||
|
||||
-- Get font for measuring text
|
||||
local font = self:_getFont()
|
||||
if not font then
|
||||
return 0
|
||||
end
|
||||
|
||||
local text = self._textBuffer
|
||||
local textLength = utf8.len(text) or 0
|
||||
|
||||
-- === SINGLE-LINE TEXT HANDLING ===
|
||||
if not self.multiline then
|
||||
-- Account for horizontal scroll offset in single-line inputs
|
||||
if self._textScrollX then
|
||||
relativeX = relativeX + self._textScrollX
|
||||
end
|
||||
|
||||
-- Find the character position closest to the click
|
||||
local closestPos = 0
|
||||
local closestDist = math.huge
|
||||
|
||||
-- Check each position in the text
|
||||
for i = 0, textLength do
|
||||
-- Get text up to this position
|
||||
local offset = utf8.offset(text, i + 1)
|
||||
local beforeText = offset and text:sub(1, offset - 1) or text
|
||||
local textWidth = font:getWidth(beforeText)
|
||||
|
||||
-- Calculate distance from click to this position
|
||||
local dist = math.abs(relativeX - textWidth)
|
||||
|
||||
if dist < closestDist then
|
||||
closestDist = dist
|
||||
closestPos = i
|
||||
end
|
||||
end
|
||||
|
||||
return closestPos
|
||||
end
|
||||
|
||||
-- === MULTILINE TEXT HANDLING ===
|
||||
|
||||
-- Update text wrapping if dirty
|
||||
if self._textEditor then
|
||||
self._textEditor:_updateTextIfDirty()
|
||||
end
|
||||
|
||||
-- Split text into lines
|
||||
local lines = {}
|
||||
for line in (text .. "\n"):gmatch("([^\n]*)\n") do
|
||||
table.insert(lines, line)
|
||||
end
|
||||
if #lines == 0 then
|
||||
lines = { "" }
|
||||
end
|
||||
|
||||
local lineHeight = font:getHeight()
|
||||
|
||||
-- Get text area width for wrapping calculations
|
||||
local textAreaWidth = self.width
|
||||
local scaledContentPadding = self:getScaledContentPadding()
|
||||
if scaledContentPadding then
|
||||
local borderBoxWidth = self._borderBoxWidth or (self.width + self.padding.left + self.padding.right)
|
||||
textAreaWidth = borderBoxWidth - scaledContentPadding.left - scaledContentPadding.right
|
||||
end
|
||||
|
||||
-- Determine which line the click is on based on Y coordinate
|
||||
local clickedLineNum = math.floor(relativeY / lineHeight) + 1
|
||||
clickedLineNum = math.max(1, math.min(clickedLineNum, #lines))
|
||||
|
||||
-- Calculate character offset for lines before the clicked line
|
||||
local charOffset = 0
|
||||
for i = 1, clickedLineNum - 1 do
|
||||
local lineLen = utf8.len(lines[i]) or 0
|
||||
charOffset = charOffset + lineLen + 1 -- +1 for newline character
|
||||
end
|
||||
|
||||
-- Get the clicked line
|
||||
local clickedLine = lines[clickedLineNum]
|
||||
local lineLen = utf8.len(clickedLine) or 0
|
||||
|
||||
-- If text wrapping is enabled, handle wrapped segments
|
||||
if self.textWrap and textAreaWidth > 0 then
|
||||
local wrappedSegments = self:_wrapLine(clickedLine, textAreaWidth)
|
||||
|
||||
-- Determine which wrapped segment was clicked
|
||||
local lineYOffset = (clickedLineNum - 1) * lineHeight
|
||||
local segmentNum = math.floor((relativeY - lineYOffset) / lineHeight) + 1
|
||||
segmentNum = math.max(1, math.min(segmentNum, #wrappedSegments))
|
||||
|
||||
local segment = wrappedSegments[segmentNum]
|
||||
|
||||
-- Find closest position within the segment
|
||||
local segmentText = segment.text
|
||||
local segmentLen = utf8.len(segmentText) or 0
|
||||
local closestPos = segment.startIdx
|
||||
local closestDist = math.huge
|
||||
|
||||
for i = 0, segmentLen do
|
||||
local offset = utf8.offset(segmentText, i + 1)
|
||||
local beforeText = offset and segmentText:sub(1, offset - 1) or segmentText
|
||||
local textWidth = font:getWidth(beforeText)
|
||||
local dist = math.abs(relativeX - textWidth)
|
||||
|
||||
if dist < closestDist then
|
||||
closestDist = dist
|
||||
closestPos = segment.startIdx + i
|
||||
end
|
||||
end
|
||||
|
||||
return charOffset + closestPos
|
||||
end
|
||||
|
||||
-- No wrapping - find closest position in the clicked line
|
||||
local closestPos = 0
|
||||
local closestDist = math.huge
|
||||
|
||||
for i = 0, lineLen do
|
||||
local offset = utf8.offset(clickedLine, i + 1)
|
||||
local beforeText = offset and clickedLine:sub(1, offset - 1) or clickedLine
|
||||
local textWidth = font:getWidth(beforeText)
|
||||
local dist = math.abs(relativeX - textWidth)
|
||||
|
||||
if dist < closestDist then
|
||||
closestDist = dist
|
||||
closestPos = i
|
||||
end
|
||||
end
|
||||
|
||||
return charOffset + closestPos
|
||||
end
|
||||
|
||||
--- Handle mouse click on text (set cursor position or start selection)
|
||||
---@param mouseX number -- Mouse X coordinate
|
||||
---@param mouseY number -- Mouse Y coordinate
|
||||
|
||||
Reference in New Issue
Block a user