Merge pull request #49 from endlesstravel/master

add little IME support
This commit is contained in:
Matthias Richter 2017-10-28 11:58:08 +02:00 committed by GitHub
commit 6b302f777c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 6 deletions

View file

@ -23,7 +23,13 @@ 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 = ""}
local input = {text = "", candidate_text = {text="", start=0, length=0}}
-- make love use font which support CJK text
function love.load()
local font = love.graphics.newFont("NotoSansHans-Regular.otf", 20)
love.graphics.setFont(font)
end
-- all the UI is defined in love.update or functions that are called from here
function love.update(dt)
@ -54,6 +60,11 @@ function love.draw()
suit.draw()
end
function love.textedited(text, start, length)
-- for IME input
input.candidate_text = {text = text, start= start, length = length}
end
function love.textinput(t)
-- forward text input to SUIT
suit.textinput(t)

View file

@ -64,6 +64,7 @@ function suit:isActive(id)
return id == self.active
end
function suit:setHit(id)
self.hit = id
-- simulate mouse release on button -- see suit:mouseReleasedOn()

View file

@ -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 opt.hasKeyboardFocus then
if (input.candidate_text.text == "") and opt.hasKeyboardFocus then
local keycode,char = core:getPressedKey()
-- text input
if char and char ~= "" then

View file

@ -123,12 +123,26 @@ function theme.Input(input, opt, x,y,w,h)
love.graphics.setFont(opt.font)
love.graphics.print(input.text, x, y+(h-th)/2)
-- candidate text
local tw = opt.font:getWidth(input.text)
local ctw = opt.font:getWidth(input.candidate_text.text)
love.graphics.setColor((opt.color and opt.color.normal and opt.color.normal.fg) or theme.color.normal.fg)
love.graphics.print(input.candidate_text.text, x + tw, y+(h-th)/2)
-- candidate text rectangle box
love.graphics.rectangle("line", x + tw, y+(h-th)/2, ctw, th)
-- cursor
if opt.hasKeyboardFocus and (love.timer.getTime() % 1) > .5 then
local ct = input.candidate_text;
local ss = ct.text:sub(1, utf8.offset(ct.text, ct.start))
local ws = opt.font:getWidth(ss)
if ct.start == 0 then ws = 0 end
love.graphics.setLineWidth(1)
love.graphics.setLineStyle('rough')
love.graphics.line(x + opt.cursor_pos, y + (h-th)/2,
x + opt.cursor_pos, y + (h+th)/2)
love.graphics.line(x + opt.cursor_pos + ws, y + (h-th)/2,
x + opt.cursor_pos + ws, y + (h+th)/2)
end
-- reset scissor