Move computation of text offset from theme to input widget
This commit is contained in:
parent
09a458386c
commit
62b31ac719
2 changed files with 26 additions and 25 deletions
25
input.lua
25
input.lua
|
@ -14,7 +14,8 @@ return function(core, input, ...)
|
||||||
opt.id = opt.id or input
|
opt.id = opt.id or input
|
||||||
opt.font = opt.font or love.graphics.getFont()
|
opt.font = opt.font or love.graphics.getFont()
|
||||||
|
|
||||||
w = w or opt.font:getWidth(text) + 4
|
local text_width = opt.font:getWidth(input.text)
|
||||||
|
w = w or text_width + 4
|
||||||
h = h or opt.font:getHeight() + 4
|
h = h or opt.font:getHeight() + 4
|
||||||
|
|
||||||
input.text = input.text or ""
|
input.text = input.text or ""
|
||||||
|
@ -63,6 +64,28 @@ return function(core, input, ...)
|
||||||
-- TODO
|
-- TODO
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- get size of text and cursor position
|
||||||
|
opt.cursor_pos = 0
|
||||||
|
if input.cursor > 1 then
|
||||||
|
local s = input.text:sub(0, utf8.offset(input.text, input.cursor)-1)
|
||||||
|
opt.cursor_pos = opt.font:getWidth(s)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- compute drawing offset
|
||||||
|
input.drawoffset = input.drawoffset or 0
|
||||||
|
if opt.cursor_pos - input.drawoffset < 0 then
|
||||||
|
-- cursor left of input box
|
||||||
|
input.drawoffset = opt.cursor_pos
|
||||||
|
end
|
||||||
|
if opt.cursor_pos - input.drawoffset > w then
|
||||||
|
-- cursor right of input box
|
||||||
|
input.drawoffset = opt.cursor_pos - w
|
||||||
|
end
|
||||||
|
if text_width - input.drawoffset < w and text_width > w then
|
||||||
|
-- text bigger than input box, but does not fill it
|
||||||
|
input.drawoffset = text_width - w
|
||||||
|
end
|
||||||
|
|
||||||
core:registerDraw(core.theme.Input, input, opt, x,y,w,h)
|
core:registerDraw(core.theme.Input, input, opt, x,y,w,h)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
26
theme.lua
26
theme.lua
|
@ -104,29 +104,7 @@ function theme.Input(input, opt, x,y,w,h)
|
||||||
x = x + 3
|
x = x + 3
|
||||||
w = w - 6
|
w = w - 6
|
||||||
|
|
||||||
-- get size of text and cursor position
|
|
||||||
local th = opt.font:getHeight()
|
local th = opt.font:getHeight()
|
||||||
local tw = opt.font:getWidth(input.text)
|
|
||||||
local cursor_pos = 0
|
|
||||||
if input.cursor > 1 then
|
|
||||||
local s = input.text:sub(0, utf8.offset(input.text, input.cursor)-1)
|
|
||||||
cursor_pos = opt.font:getWidth(s)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- compute drawing offset
|
|
||||||
input.drawoffset = input.drawoffset or 0
|
|
||||||
if cursor_pos - input.drawoffset < 0 then
|
|
||||||
-- cursor left of input box
|
|
||||||
input.drawoffset = cursor_pos
|
|
||||||
end
|
|
||||||
if cursor_pos - input.drawoffset > w then
|
|
||||||
-- cursor right of input box
|
|
||||||
input.drawoffset = cursor_pos - w
|
|
||||||
end
|
|
||||||
if tw - input.drawoffset < w and tw > w then
|
|
||||||
-- text bigger than input box, but does not fill it
|
|
||||||
input.drawoffset = tw - w
|
|
||||||
end
|
|
||||||
|
|
||||||
-- set scissors
|
-- set scissors
|
||||||
local sx, sy, sw, sh = love.graphics.getScissor()
|
local sx, sy, sw, sh = love.graphics.getScissor()
|
||||||
|
@ -142,8 +120,8 @@ function theme.Input(input, opt, x,y,w,h)
|
||||||
if opt.hasKeyboardFocus and (love.timer.getTime() % 1) > .5 then
|
if opt.hasKeyboardFocus and (love.timer.getTime() % 1) > .5 then
|
||||||
love.graphics.setLineWidth(1)
|
love.graphics.setLineWidth(1)
|
||||||
love.graphics.setLineStyle('rough')
|
love.graphics.setLineStyle('rough')
|
||||||
love.graphics.line(x + cursor_pos, y + (h-th)/2,
|
love.graphics.line(x + opt.cursor_pos, y + (h-th)/2,
|
||||||
x + cursor_pos, y + (h+th)/2)
|
x + opt.cursor_pos, y + (h+th)/2)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- reset scissor
|
-- reset scissor
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue