fix 'Input widget will raise error when candidate_text field is not defined'

This commit is contained in:
endlesstravel 2017-10-31 19:38:14 +08:00
parent 6b302f777c
commit 12610bfe8f
4 changed files with 12 additions and 3 deletions

View file

@ -23,7 +23,7 @@ More info and code is over at [readthedocs](http://suit.readthedocs.org/en/lates
local suit = require 'suit' local suit = require 'suit'
-- storage for text input -- storage for text input
local input = {text = "", candidate_text = {text="", start=0, length=0}} local input = {text = ""}
-- make love use font which support CJK text -- make love use font which support CJK text
function love.load() function love.load()
@ -62,7 +62,7 @@ end
function love.textedited(text, start, length) function love.textedited(text, start, length)
-- for IME input -- for IME input
input.candidate_text = {text = text, start= start, length = length} suit.textedited(text, start, length)
end end
function love.textinput(t) function love.textinput(t)

View file

@ -13,6 +13,7 @@ function suit.new(theme)
theme = theme or default_theme, theme = theme or default_theme,
mouse_x = 0, mouse_y = 0, mouse_x = 0, mouse_y = 0,
mouse_button_down = false, mouse_button_down = false,
candidate_text = {text="", start=0, length=0},
draw_queue = {n = 0}, draw_queue = {n = 0},
@ -146,6 +147,12 @@ function suit:textinput(char)
self.textchar = char self.textchar = char
end end
function suit:textedited(text, start, length)
self.candidate_text.text = text
self.candidate_text.start = start
self.candidate_text.length = length
end
function suit:grabKeyboardFocus(id) function suit:grabKeyboardFocus(id)
if self:isActive(id) then if self:isActive(id) then
if love.system.getOS() == "Android" or love.system.getOS() == "iOS" then if love.system.getOS() == "Android" or love.system.getOS() == "iOS" then

View file

@ -32,6 +32,7 @@ return setmetatable({
getPressedKey = function(...) return instance:getPressedKey(...) end, getPressedKey = function(...) return instance:getPressedKey(...) end,
keypressed = function(...) return instance:keypressed(...) end, keypressed = function(...) return instance:keypressed(...) end,
textinput = function(...) return instance:textinput(...) end, textinput = function(...) return instance:textinput(...) end,
textedited = function(...) return instance:textedited(...) end,
grabKeyboardFocus = function(...) return instance:grabKeyboardFocus(...) end, grabKeyboardFocus = function(...) return instance:grabKeyboardFocus(...) end,
hasKeyboardFocus = function(...) return instance:hasKeyboardFocus(...) end, hasKeyboardFocus = function(...) return instance:hasKeyboardFocus(...) end,
keyPressedOn = function(...) return instance:keyPressedOn(...) end, keyPressedOn = function(...) return instance:keyPressedOn(...) end,

View file

@ -57,7 +57,7 @@ return function(core, input, ...)
opt.state = core:registerHitbox(opt.id, x,y,w,h) opt.state = core:registerHitbox(opt.id, x,y,w,h)
opt.hasKeyboardFocus = core:grabKeyboardFocus(opt.id) opt.hasKeyboardFocus = core:grabKeyboardFocus(opt.id)
if (input.candidate_text.text == "") and opt.hasKeyboardFocus then if (core.candidate_text.text == "") and opt.hasKeyboardFocus then
local keycode,char = core:getPressedKey() local keycode,char = core:getPressedKey()
-- text input -- text input
if char and char ~= "" then if char and char ~= "" then
@ -102,6 +102,7 @@ return function(core, input, ...)
end end
end end
input.candidate_text = {text=core.candidate_text.text, start=core.candidate_text.start, length=core.candidate_text.length}
core:registerDraw(opt.draw or core.theme.Input, input, opt, x,y,w,h) core:registerDraw(opt.draw or core.theme.Input, input, opt, x,y,w,h)
return { return {