parent
10e79ca5c8
commit
6960a80eaa
3 changed files with 31 additions and 6 deletions
13
README.md
13
README.md
|
@ -23,7 +23,13 @@ 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 = ""}
|
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
|
-- all the UI is defined in love.update or functions that are called from here
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
|
@ -54,6 +60,11 @@ function love.draw()
|
||||||
suit.draw()
|
suit.draw()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function love.textedited(text, start, length)
|
||||||
|
-- for IME input
|
||||||
|
input.candidate_text = {text = text, start= start, length = length}
|
||||||
|
end
|
||||||
|
|
||||||
function love.textinput(t)
|
function love.textinput(t)
|
||||||
-- forward text input to SUIT
|
-- forward text input to SUIT
|
||||||
suit.textinput(t)
|
suit.textinput(t)
|
||||||
|
|
|
@ -52,7 +52,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 opt.hasKeyboardFocus then
|
if (input.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
|
||||||
|
|
18
theme.lua
18
theme.lua
|
@ -123,12 +123,26 @@ function theme.Input(input, opt, x,y,w,h)
|
||||||
love.graphics.setFont(opt.font)
|
love.graphics.setFont(opt.font)
|
||||||
love.graphics.print(input.text, x, y+(h-th)/2)
|
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
|
-- cursor
|
||||||
if opt.hasKeyboardFocus and (love.timer.getTime() % 1) > .5 then
|
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.setLineWidth(1)
|
||||||
love.graphics.setLineStyle('rough')
|
love.graphics.setLineStyle('rough')
|
||||||
love.graphics.line(x + opt.cursor_pos, y + (h-th)/2,
|
love.graphics.line(x + opt.cursor_pos + ws, y + (h-th)/2,
|
||||||
x + opt.cursor_pos, y + (h+th)/2)
|
x + opt.cursor_pos + ws, y + (h+th)/2)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- reset scissor
|
-- reset scissor
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue