[input] Handle drawing text larger than the input box.

This commit is contained in:
Lorenzo Cogotti 2022-08-25 09:35:30 +02:00
parent 08853ce041
commit 07b8252c22
1 changed files with 22 additions and 3 deletions

View File

@ -27,6 +27,7 @@ function Input.new(args)
self.cursor = math.max(1, math.min(utf8.len(self.text)+1, self.cursor = math.max(1, math.min(utf8.len(self.text)+1,
self.cursor or utf8.len(self.text)+1)) self.cursor or utf8.len(self.text)+1))
self.candidate = { text = "", start = 0, length = 0 } self.candidate = { text = "", start = 0, length = 0 }
self.drawofs = 0
return self return self
end end
@ -136,10 +137,25 @@ function Input:draw()
cursor_pos = font:getWidth(s) cursor_pos = font:getWidth(s)
end 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 if self.px ~= nil then
-- Mouse movement -- Mouse movement
local mx = self.px - self.x local mx = self.px - self.x + self.drawofs
self.cursor = utf8.len(self.text) + 1 self.cursor = utf8.len(self.text) + 1
for c = 1,self.cursor do for c = 1,self.cursor do
@ -153,16 +169,19 @@ function Input:draw()
self.px,self.py = nil,nil self.px,self.py = nil,nil
end end
-- Perform actual draw
core.drawBox(x,y,w,h, self.color.normal, self.cornerRadius) core.drawBox(x,y,w,h, self.color.normal, self.cornerRadius)
-- Apply text margins -- Apply text margins
w = math.max(w - 6, 0)
x = math.min(x + 3, x + w) x = math.min(x + 3, x + w)
-- Set scissors -- Set scissors
local sx, sy, sw, sh = love.graphics.getScissor() local sx, sy, sw, sh = love.graphics.getScissor()
love.graphics.setScissor(x-1,y,w+2,h) love.graphics.setScissor(x-1,y,w+2,h)
-- Move to focused text box region
x = x - self.drawofs
-- Text -- Text
love.graphics.setColor(self.color.normal.fg) love.graphics.setColor(self.color.normal.fg)
love.graphics.setFont(font) love.graphics.setFont(font)