From 12610bfe8f9c27c5a05e2ecac921e4ac0d790897 Mon Sep 17 00:00:00 2001 From: endlesstravel Date: Tue, 31 Oct 2017 19:38:14 +0800 Subject: [PATCH] fix 'Input widget will raise error when candidate_text field is not defined' --- README.md | 4 ++-- core.lua | 7 +++++++ init.lua | 1 + input.lua | 3 ++- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7db2ffa..80b3b48 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ More info and code is over at [readthedocs](http://suit.readthedocs.org/en/lates local suit = require 'suit' -- 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 function love.load() @@ -62,7 +62,7 @@ end function love.textedited(text, start, length) -- for IME input - input.candidate_text = {text = text, start= start, length = length} + suit.textedited(text, start, length) end function love.textinput(t) diff --git a/core.lua b/core.lua index 3b92337..5b4d2ee 100644 --- a/core.lua +++ b/core.lua @@ -13,6 +13,7 @@ function suit.new(theme) theme = theme or default_theme, mouse_x = 0, mouse_y = 0, mouse_button_down = false, + candidate_text = {text="", start=0, length=0}, draw_queue = {n = 0}, @@ -146,6 +147,12 @@ function suit:textinput(char) self.textchar = char 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) if self:isActive(id) then if love.system.getOS() == "Android" or love.system.getOS() == "iOS" then diff --git a/init.lua b/init.lua index 3779c90..e2733e4 100644 --- a/init.lua +++ b/init.lua @@ -32,6 +32,7 @@ return setmetatable({ getPressedKey = function(...) return instance:getPressedKey(...) end, keypressed = function(...) return instance:keypressed(...) end, textinput = function(...) return instance:textinput(...) end, + textedited = function(...) return instance:textedited(...) end, grabKeyboardFocus = function(...) return instance:grabKeyboardFocus(...) end, hasKeyboardFocus = function(...) return instance:hasKeyboardFocus(...) end, keyPressedOn = function(...) return instance:keyPressedOn(...) end, diff --git a/input.lua b/input.lua index 5c9f29d..f8e2833 100644 --- a/input.lua +++ b/input.lua @@ -57,7 +57,7 @@ return function(core, input, ...) opt.state = core:registerHitbox(opt.id, x,y,w,h) 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() -- text input if char and char ~= "" then @@ -102,6 +102,7 @@ return function(core, input, ...) 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) return {