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

master
Lorenzo Cogotti 2 years ago
parent 08853ce041
commit 07b8252c22

@ -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)

Loading…
Cancel
Save