Added paste

This commit is contained in:
Robert Jelic
2025-05-11 03:13:57 +02:00
parent f7c4165aa4
commit d4f875836d
2 changed files with 37 additions and 0 deletions

View File

@@ -34,6 +34,7 @@ Input.defineProperty(Input, "replaceChar", {default = nil, type = "string", canT
Input.defineEvent(Input, "mouse_click")
Input.defineEvent(Input, "key")
Input.defineEvent(Input, "char")
Input.defineEvent(Input, "paste")
--- @shortDescription Creates a new Input instance
--- @return Input object The newly created Input instance
@@ -192,6 +193,24 @@ function Input:blur()
self:updateRender()
end
function Input:paste(content)
if not self.get("focused") then return false end
local text = self.get("text")
local pos = self.get("cursorPos")
local maxLength = self.get("maxLength")
local pattern = self.get("pattern")
local newText = text:sub(1, pos - 1) .. content .. text:sub(pos)
if maxLength and #newText > maxLength then
newText = newText:sub(1, maxLength)
end
if pattern and not newText:match(pattern) then
return false
end
self.set("text", newText)
self.set("cursorPos", pos + #content)
self:updateViewport()
end
--- @shortDescription Renders the input element
--- @protected
function Input:render()

View File

@@ -29,6 +29,7 @@ TextBox.defineEvent(TextBox, "mouse_click")
TextBox.defineEvent(TextBox, "key")
TextBox.defineEvent(TextBox, "char")
TextBox.defineEvent(TextBox, "mouse_scroll")
TextBox.defineEvent(TextBox, "paste")
--- Creates a new TextBox instance
--- @shortDescription Creates a new TextBox instance
@@ -235,6 +236,23 @@ function TextBox:mouse_click(button, x, y)
return false
end
function TextBox:paste(text)
if not self.get("editable") or not self.get("focused") then return false end
local lines = self.get("lines")
local cursorX = self.get("cursorX")
local cursorY = self.get("cursorY")
for char in text:gmatch(".") do
if char == "\n" then
newLine(self)
else
insertChar(self, char)
end
end
return true
end
--- Sets the text of the TextBox
--- @shortDescription Sets the text of the TextBox
--- @param text string The text to set