From 07b8252c2216a4b62a3af0c1d4dccdc1b584bb27 Mon Sep 17 00:00:00 2001 From: Lorenzo Cogotti Date: Thu, 25 Aug 2022 09:35:30 +0200 Subject: [PATCH] [input] Handle drawing text larger than the input box. --- input.lua | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/input.lua b/input.lua index 8d4cdad..053fcaf 100644 --- a/input.lua +++ b/input.lua @@ -27,6 +27,7 @@ function Input.new(args) self.cursor = math.max(1, math.min(utf8.len(self.text)+1, self.cursor or utf8.len(self.text)+1)) self.candidate = { text = "", start = 0, length = 0 } + self.drawofs = 0 return self end @@ -136,10 +137,25 @@ function Input:draw() cursor_pos = font:getWidth(s) end - -- Compute drawing offset + -- Calculate initial text offset + local wm = math.max(w - 6, 0) -- width minus margin + if cursor_pos - self.drawofs < 0 then + -- cursor left of input box + self.drawofs = cursor_pos + end + if cursor_pos - self.drawofs > wm then + -- cursor right of input box + self.drawofs = cursor_pos - wm + end + if tw - self.drawofs < wm and tw > wm then + -- text bigger than input box, but doesn't fill it + self.drawofs = tw - wm + end + + -- Handle cursor movement within the box if self.px ~= nil then -- Mouse movement - local mx = self.px - self.x + local mx = self.px - self.x + self.drawofs self.cursor = utf8.len(self.text) + 1 for c = 1,self.cursor do @@ -153,16 +169,19 @@ function Input:draw() self.px,self.py = nil,nil end + -- Perform actual draw core.drawBox(x,y,w,h, self.color.normal, self.cornerRadius) -- Apply text margins - w = math.max(w - 6, 0) x = math.min(x + 3, x + w) -- Set scissors local sx, sy, sw, sh = love.graphics.getScissor() love.graphics.setScissor(x-1,y,w+2,h) + -- Move to focused text box region + x = x - self.drawofs + -- Text love.graphics.setColor(self.color.normal.fg) love.graphics.setFont(font)