textinput - Unicode support.

This commit is contained in:
linux-man 2016-11-13 03:55:07 +00:00
parent 1bee7fa8d9
commit 0e69a908d5

View File

@ -6,7 +6,7 @@
local utf8 = require("utf8") local utf8 = require("utf8")
-- get the current require path -- get the current require path
local path = string.sub(..., 1, string.len(...) - string.len(".objects.textinput")) local path = string.sub(..., 1, utf8.len(...) - utf8.len(".objects.textinput"))
local loveframes = require(path .. ".libraries.common") local loveframes = require(path .. ".libraries.common")
-- textinput object -- textinput object
@ -610,7 +610,7 @@ function newobject:RunKey(key, istext)
if indicatornum == 0 then if indicatornum == 0 then
if line > 1 then if line > 1 then
self.line = line - 1 self.line = line - 1
local numchars = string.len(lines[self.line]) local numchars = utf8.len(lines[self.line])
self:MoveIndicator(numchars) self:MoveIndicator(numchars)
end end
else else
@ -628,14 +628,14 @@ function newobject:RunKey(key, istext)
if not multiline then if not multiline then
self:MoveIndicator(1) self:MoveIndicator(1)
local indicatorx = self.indicatorx local indicatorx = self.indicatorx
if indicatorx >= (x + swidth) and indicatornum ~= string.len(text) then if indicatorx >= (x + swidth) and indicatornum ~= utf8.len(text) then
local width = font:getWidth(text:sub(indicatornum, indicatornum)) local width = font:getWidth(text:sub(indicatornum, indicatornum))
self.offsetx = offsetx + width self.offsetx = offsetx + width
elseif indicatornum == string.len(text) and offsetx ~= ((font:getWidth(text)) - swidth + 10) and font:getWidth(text) + textoffsetx > swidth then elseif indicatornum == utf8.len(text) and offsetx ~= ((font:getWidth(text)) - swidth + 10) and font:getWidth(text) + textoffsetx > swidth then
self.offsetx = ((font:getWidth(text)) - swidth + 10) self.offsetx = ((font:getWidth(text)) - swidth + 10)
end end
else else
if indicatornum == string.len(text) then if indicatornum == utf8.len(text) then
if line < numlines then if line < numlines then
self.line = line + 1 self.line = line + 1
self:MoveIndicator(0, true) self:MoveIndicator(0, true)
@ -646,7 +646,7 @@ function newobject:RunKey(key, istext)
end end
if alltextselected then if alltextselected then
self.line = #lines self.line = #lines
self.indicatornum = string.len(lines[#lines]) self.indicatornum = utf8.len(lines[#lines])
self.alltextselected = false self.alltextselected = false
end end
return return
@ -654,8 +654,8 @@ function newobject:RunKey(key, istext)
if multiline then if multiline then
if line > 1 then if line > 1 then
self.line = line - 1 self.line = line - 1
if indicatornum > string.len(lines[self.line]) then if indicatornum > utf8.len(lines[self.line]) then
self.indicatornum = string.len(lines[self.line]) self.indicatornum = utf8.len(lines[self.line])
end end
end end
end end
@ -664,8 +664,8 @@ function newobject:RunKey(key, istext)
if multiline then if multiline then
if line < #lines then if line < #lines then
self.line = line + 1 self.line = line + 1
if indicatornum > string.len(lines[self.line]) then if indicatornum > utf8.len(lines[self.line]) then
self.indicatornum = string.len(lines[self.line]) self.indicatornum = utf8.len(lines[self.line])
end end
end end
end end
@ -695,12 +695,12 @@ function newobject:RunKey(key, istext)
local oldtext = lines[line] local oldtext = lines[line]
table.remove(lines, line) table.remove(lines, line)
self.line = line - 1 self.line = line - 1
if string.len(oldtext) > 0 then if utf8.len(oldtext) > 0 then
newindicatornum = string.len(lines[self.line]) newindicatornum = utf8.len(lines[self.line])
lines[self.line] = lines[self.line] .. oldtext lines[self.line] = lines[self.line] .. oldtext
self:MoveIndicator(newindicatornum) self:MoveIndicator(newindicatornum)
else else
self:MoveIndicator(string.len(lines[self.line])) self:MoveIndicator(utf8.len(lines[self.line]))
end end
end end
end end
@ -708,9 +708,9 @@ function newobject:RunKey(key, istext)
local cwidth = 0 local cwidth = 0
if masked then if masked then
local maskchar = self.maskchar local maskchar = self.maskchar
cwidth = font:getWidth(text:sub(string.len(text)):gsub(".", maskchar)) cwidth = font:getWidth(text:gsub(".", maskchar))
else else
cwidth = font:getWidth(text:sub(string.len(text))) cwidth = font:getWidth(text)
end end
if self.offsetx > 0 then if self.offsetx > 0 then
self.offsetx = self.offsetx - cwidth self.offsetx = self.offsetx - cwidth
@ -728,13 +728,13 @@ function newobject:RunKey(key, istext)
self.alltextselected = false self.alltextselected = false
indicatornum = self.indicatornum indicatornum = self.indicatornum
else else
if text ~= "" and indicatornum < string.len(text) then if text ~= "" and indicatornum < utf8.len(text) then
text = self:RemoveFromText(indicatornum + 1) text = self:RemoveFromText(indicatornum + 1)
lines[line] = text lines[line] = text
elseif indicatornum == string.len(text) and line < #lines then elseif indicatornum == utf8.len(text) and line < #lines then
local oldtext = lines[line + 1] local oldtext = lines[line + 1]
if string.len(oldtext) > 0 then if utf8.len(oldtext) > 0 then
newindicatornum = string.len(lines[self.line]) newindicatornum = utf8.len(lines[self.line])
lines[self.line] = lines[self.line] .. oldtext lines[self.line] = lines[self.line] .. oldtext
end end
table.remove(lines, line + 1) table.remove(lines, line + 1)
@ -758,8 +758,8 @@ function newobject:RunKey(key, istext)
if indicatornum == 0 then if indicatornum == 0 then
newtext = self.lines[line] newtext = self.lines[line]
self.lines[line] = "" self.lines[line] = ""
elseif indicatornum > 0 and indicatornum < string.len(self.lines[line]) then elseif indicatornum > 0 and indicatornum < utf8.len(self.lines[line]) then
newtext = self.lines[line]:sub(indicatornum + 1, string.len(self.lines[line])) newtext = self.lines[line]:sub(indicatornum + 1, utf8.len(self.lines[line]))
self.lines[line] = self.lines[line]:sub(1, indicatornum) self.lines[line] = self.lines[line]:sub(1, indicatornum)
end end
if line ~= #lines then if line ~= #lines then
@ -781,14 +781,14 @@ function newobject:RunKey(key, istext)
end end
ckey = key ckey = key
self.lines[self.line] = self:AddIntoText(self.tabreplacement, self.indicatornum) self.lines[self.line] = self:AddIntoText(self.tabreplacement, self.indicatornum)
self:MoveIndicator(string.len(self.tabreplacement)) self:MoveIndicator(utf8.len(self.tabreplacement))
end end
else else
if not editable then if not editable then
return return
end end
-- do not continue if the text limit has been reached or exceeded -- do not continue if the text limit has been reached or exceeded
if string.len(text) >= self.limit and self.limit ~= 0 and not alltextselected then if utf8.len(text) >= self.limit and self.limit ~= 0 and not alltextselected then
return return
end end
-- check for unusable characters -- check for unusable characters
@ -823,11 +823,11 @@ function newobject:RunKey(key, istext)
lines = self.lines lines = self.lines
line = self.line line = self.line
end end
if indicatornum ~= 0 and indicatornum ~= string.len(text) then if indicatornum ~= 0 and indicatornum ~= utf8.len(text) then
text = self:AddIntoText(key, indicatornum) text = self:AddIntoText(key, indicatornum)
lines[line] = text lines[line] = text
self:MoveIndicator(1) self:MoveIndicator(1)
elseif indicatornum == string.len(text) then elseif indicatornum == utf8.len(text) then
text = text .. key text = text .. key
lines[line] = text lines[line] = text
self:MoveIndicator(1) self:MoveIndicator(1)
@ -1098,7 +1098,7 @@ function newobject:GetTextCollisions(x, y)
end end
local line = self.line local line = self.line
local curline = lines[line] local curline = lines[line]
for i=1, string.len(curline) do for i=1, utf8.len(curline) do
local char = text:sub(i, i) local char = text:sub(i, i)
local width = 0 local width = 0
if masked then if masked then
@ -1118,7 +1118,7 @@ function newobject:GetTextCollisions(x, y)
self:MoveIndicator(i - 1, true) self:MoveIndicator(i - 1, true)
break break
else else
self.indicatornum = string.len(curline) self.indicatornum = utf8.len(curline)
end end
if x < tx then if x < tx then
@ -1126,11 +1126,11 @@ function newobject:GetTextCollisions(x, y)
end end
if x > (tx + width) then if x > (tx + width) then
self:MoveIndicator(string.len(curline), true) self:MoveIndicator(utf8.len(curline), true)
end end
end end
if string.len(curline) == 0 then if utf8.len(curline) == 0 then
self.indicatornum = 0 self.indicatornum = 0
end end
end end
@ -1389,13 +1389,13 @@ function newobject:SetText(text)
self.lines = {""} self.lines = {""}
end end
self.line = #self.lines self.line = #self.lines
self.indicatornum = string.len(self.lines[#self.lines]) self.indicatornum = utf8.len(self.lines[#self.lines])
else else
text = text:gsub(string.char(92) .. string.char(110), "") text = text:gsub(string.char(92) .. string.char(110), "")
text = text:gsub(string.char(10), "") text = text:gsub(string.char(10), "")
self.lines = {text} self.lines = {text}
self.line = 1 self.line = 1
self.indicatornum = string.len(text) self.indicatornum = utf8.len(text)
end end
return self return self