封装inputBox控件的文本修改方法

This commit is contained in:
MrZ626
2021-05-13 19:48:07 +08:00
parent 38326bc3b6
commit 85e6810f99
5 changed files with 40 additions and 22 deletions

View File

@@ -742,6 +742,24 @@ function inputBox:reset()
kb.setTextInput(true) kb.setTextInput(true)
end end
end end
function inputBox:hasText()
return #self.value>0
end
function inputBox:getText()
return self.value
end
function inputBox:setText(str)
if type(str)=='string'then
self.value=str
end
end
function inputBox:addText(str)
if type(str)=='string'then
self.value=self.value..str
else
LOG.print("inputBox "..self.name.." dead, addText("..type(str)..")",'warn')
end
end
function inputBox:clear() function inputBox:clear()
self.value="" self.value=""
end end

View File

@@ -604,16 +604,16 @@ end
local combKey={} local combKey={}
function combKey.x() function combKey.x()
love.system.setClipboardText(inputBox.value) love.system.setClipboardText(inputBox:getText())
inputBox.value="" inputBox:clear()
SFX.play('reach') SFX.play('reach')
end end
function combKey.c() function combKey.c()
love.system.setClipboardText(inputBox.value) love.system.setClipboardText(inputBox:getText())
SFX.play('reach') SFX.play('reach')
end end
function combKey.v() function combKey.v()
inputBox.value=inputBox.value..love.system.getClipboardText() inputBox:addText(love.system.getClipboardText())
SFX.play('reach') SFX.play('reach')
end end
@@ -687,7 +687,7 @@ end
function scene.keyDown(k) function scene.keyDown(k)
if k=="return"then if k=="return"then
local input=inputBox.value local input=inputBox:getText()
if input==""then return end if input==""then return end
--Write History --Write History
@@ -736,24 +736,24 @@ function scene.keyDown(k)
if not hisPtr then if not hisPtr then
hisPtr=#history hisPtr=#history
if hisPtr>0 then if hisPtr>0 then
inputBox.value=history[hisPtr] inputBox:setText(history[hisPtr])
end end
elseif hisPtr>1 then elseif hisPtr>1 then
hisPtr=hisPtr-1 hisPtr=hisPtr-1
inputBox.value=history[hisPtr] inputBox:setText(history[hisPtr])
end end
elseif k=="down"then elseif k=="down"then
if hisPtr then if hisPtr then
hisPtr=hisPtr+1 hisPtr=hisPtr+1
if history[hisPtr]then if history[hisPtr]then
inputBox.value=history[hisPtr] inputBox:setText(history[hisPtr])
else else
hisPtr=nil hisPtr=nil
inputBox.value="" inputBox:clear()
end end
end end
elseif k=="tab"then elseif k=="tab"then
local str=inputBox.value local str=inputBox:getText()
if str~=""and not str:find("%s")then if str~=""and not str:find("%s")then
local res={} local res={}
for c in next,commands do for c in next,commands do
@@ -767,7 +767,7 @@ function scene.keyDown(k)
table.sort(res) table.sort(res)
for i=1,#res do log{COLOR.lH,res[i]}end for i=1,#res do log{COLOR.lH,res[i]}end
elseif #res==1 then elseif #res==1 then
inputBox.value=res[1] inputBox:setText(res[1])
end end
end end
elseif k=="scrollup"then outputBox:scroll(-5) elseif k=="scrollup"then outputBox:scroll(-5)

View File

@@ -37,7 +37,7 @@ local function clearResult()
waiting,lastSearch=0,false waiting,lastSearch=0,false
end end
local function search() local function search()
local input=inputBox.value:lower() local input=inputBox:getText():lower()
clearResult() clearResult()
local first local first
for i=1,#dict do for i=1,#dict do
@@ -97,7 +97,7 @@ function scene.keyDown(key)
elseif key=="link"then elseif key=="link"then
love.system.openURL(url) love.system.openURL(url)
elseif key=="delete"then elseif key=="delete"then
if #inputBox.value>0 then if inputBox:hasText()then
clearResult() clearResult()
inputBox:clear() inputBox:clear()
SFX.play('hold') SFX.play('hold')
@@ -105,7 +105,7 @@ function scene.keyDown(key)
elseif key=="backspace"then elseif key=="backspace"then
WIDGET.keyPressed("backspace") WIDGET.keyPressed("backspace")
elseif key=="escape"then elseif key=="escape"then
if #inputBox.value>0 then if inputBox:hasText()then
scene.keyDown("delete") scene.keyDown("delete")
else else
SCN.back() SCN.back()
@@ -115,7 +115,7 @@ function scene.keyDown(key)
end end
function scene.update(dt) function scene.update(dt)
local input=inputBox.value local input=inputBox:getText()
if input~=lastTickInput then if input~=lastTickInput then
if #input==0 then if #input==0 then
clearResult() clearResult()

View File

@@ -4,7 +4,7 @@ local passwordBox=WIDGET.newInputBox{name="password",x=380,y=300,w=620,h=60,secr
local savePW=false local savePW=false
local function login() local function login()
local email,password=emailBox.value,passwordBox.value local email,password=emailBox:getText(),passwordBox:getText()
if not STRING.simpEmailCheck(email)then if not STRING.simpEmailCheck(email)then
LOG.print(text.wrongEmail)return LOG.print(text.wrongEmail)return
elseif #password==0 then elseif #password==0 then
@@ -26,8 +26,8 @@ function scene.sceneInit()
local data=FILE.load("conf/account") local data=FILE.load("conf/account")
if data then if data then
savePW=true savePW=true
emailBox.value=data[1] emailBox:setText(data[1])
passwordBox.value=data[2] passwordBox:setText(data[2])
end end
end end

View File

@@ -1,10 +1,10 @@
local scene={} local scene={}
local function register() local function register()
local username= WIDGET.active.username.value local username= WIDGET.active.username:getText()
local email= WIDGET.active.email.value local email= WIDGET.active.email:getText()
local password= WIDGET.active.password.value local password= WIDGET.active.password:getText()
local password2=WIDGET.active.password2.value local password2=WIDGET.active.password2:getText()
if #username==0 then if #username==0 then
LOG.print(text.noUsername)return LOG.print(text.noUsername)return
elseif not STRING.simpEmailCheck(email)then elseif not STRING.simpEmailCheck(email)then