diff --git a/src/elements/Input.lua b/src/elements/Input.lua index bf3f541..6a259a8 100644 --- a/src/elements/Input.lua +++ b/src/elements/Input.lua @@ -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() diff --git a/src/elements/TextBox.lua b/src/elements/TextBox.lua index 03f0c3f..a2efe46 100644 --- a/src/elements/TextBox.lua +++ b/src/elements/TextBox.lua @@ -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