文本框控件支持拖动和清空

This commit is contained in:
MrZ626
2021-01-24 10:10:08 +08:00
parent eb370ba88f
commit 99e63b749a
2 changed files with 65 additions and 12 deletions

View File

@@ -105,7 +105,7 @@ function love.mousemoved(x,y,dx,dy,t)
dx,dy=dx/SCR.k,dy/SCR.k dx,dy=dx/SCR.k,dy/SCR.k
if SCN.mouseMove then SCN.mouseMove(mx,my,dx,dy)end if SCN.mouseMove then SCN.mouseMove(mx,my,dx,dy)end
if ms.isDown(1) then if ms.isDown(1) then
WIDGET.drag(mx,my) WIDGET.drag(mx,my,dx,dy)
else else
WIDGET.moveCursor(mx,my) WIDGET.moveCursor(mx,my)
end end
@@ -141,7 +141,7 @@ function love.touchmoved(id,x,y,dx,dy)
if SCN.touchMove then SCN.touchMove(id,x,y,dx/SCR.k,dy/SCR.k)end if SCN.touchMove then SCN.touchMove(id,x,y,dx/SCR.k,dy/SCR.k)end
if WIDGET.sel then if WIDGET.sel then
if touching then if touching then
WIDGET.drag(x,y) WIDGET.drag(x,y,dx,dy)
end end
else else
WIDGET.moveCursor(x,y) WIDGET.moveCursor(x,y)

View File

@@ -801,12 +801,27 @@ end
local chatBox={ local chatBox={
type="chatBox", type="chatBox",
scrollPos=0, scrollPos=0,
scrollPix=0,
sure=0,
new=false, new=false,
-- texts={}, -- texts={},
} }
function chatBox:reset() function chatBox:reset()
if not self.texts then --haha nothing here, but techmino is fun!
self.texts={} end
function chatBox:isAbove(x,y)
return
x>self.x and
y>self.y and
x<self.x+self.w and
y<self.y+self.h
end
function chatBox:getCenter()
return self.x+self.w*.5,self.y+self.w
end
function chatBox:update()
if self.sure>0 then
self.sure=self.sure-1
end end
end end
function chatBox:push(t) function chatBox:push(t)
@@ -818,6 +833,27 @@ function chatBox:push(t)
self.new=true self.new=true
end end
end end
function chatBox:drag(_,_,_,dy)
_=self.scrollPix+dy
if _>30 then
_=_-30
self:scroll(-1)
elseif _<-30 then
_=_+30
self:scroll(1)
end
self.scrollPix=_
end
function chatBox:press(x,y)
if x>self.x+self.w-40 and y<self.y+40 then
if self.sure>0 then
self:clear()
self.sure=0
else
self.sure=60
end
end
end
function chatBox:scroll(n) function chatBox:scroll(n)
if n<0 then if n<0 then
self.scrollPos=max(self.scrollPos+n,min(#self.texts,self.capacity)) self.scrollPos=max(self.scrollPos+n,min(#self.texts,self.capacity))
@@ -830,6 +866,7 @@ function chatBox:scroll(n)
end end
function chatBox:clear() function chatBox:clear()
self.texts={} self.texts={}
self.scrollPos=0
SFX.play("fall") SFX.play("fall")
end end
function chatBox:draw() function chatBox:draw()
@@ -845,8 +882,12 @@ function chatBox:draw()
gc.setColor(1,1,1) gc.setColor(1,1,1)
gc.rectangle("line",x,y,w,h) gc.rectangle("line",x,y,w,h)
--Texts --Clear button
setFont(30) setFont(30)
mStr(self.sure>0 and"?"or"X",x+w-20,y-1)
gc.rectangle("line",x+w-40,y,40,40)
--Texts
for i=max(scroll-cap+1,1),scroll do for i=max(scroll-cap+1,1),scroll do
gc.printf(texts[i],x+8,y+h-10-30*(scroll-i+1),w) gc.printf(texts[i],x+8,y+h-10-30*(scroll-i+1),w)
end end
@@ -860,10 +901,9 @@ function chatBox:draw()
end end
--Draw --Draw
if self.new and scroll~=#texts then if self.new and self.scrollPos~=#texts then
setFont(40)
gc.setColor(1,TIME()%.4<.2 and 1 or 0,0) gc.setColor(1,TIME()%.4<.2 and 1 or 0,0)
gc.print("v",8,480) gc.print("v",x+w-25,y+h-40)
end end
end end
function chatBox:getInfo() function chatBox:getInfo()
@@ -873,12 +913,25 @@ function WIDGET.newChatBox(D)--name,x,y,w[,h][,font],hide
local _={ local _={
name= D.name, name= D.name,
resCtr={
D.x+D.w*.5,D.y+D.h*.5,
D.x+D.w*.5,D.y,
D.x-D.w*.5,D.y,
D.x,D.y+D.h*.5,
D.x,D.y-D.h*.5,
D.x,D.y,
D.x+D.w,D.y,
D.x,D.y+D.h,
D.x+D.w,D.y+D.h,
},
x= D.x, x= D.x,
y= D.y, y= D.y,
w= D.w, w= D.w,
h= D.h, h= D.h,
capacity=int((D.h-10)/30), capacity=int((D.h-10)/30),
texts={},
hide= D.hide, hide= D.hide,
} }
for k,v in next,chatBox do _[k]=v end for k,v in next,chatBox do _[k]=v end
@@ -948,18 +1001,18 @@ end
function WIDGET.press(x,y) function WIDGET.press(x,y)
local W=WIDGET.sel local W=WIDGET.sel
if not W then return end if not W then return end
if W.type=="button"or W.type=="key"or W.type=="switch"or W.type=="selector"or W.type=="textBox"then if W.type=="button"or W.type=="key"or W.type=="switch"or W.type=="selector"or W.type=="textBox"or W.type=="chatBox"then
W:press(x,y) W:press(x,y)
elseif W.type=="slider"then elseif W.type=="slider"then
WIDGET.drag(x,y) WIDGET.drag(x,y)
end end
if W.hide and W.hide()then WIDGET.sel=false end if W.hide and W.hide()then WIDGET.sel=false end
end end
function WIDGET.drag(x,y) function WIDGET.drag(x,y,dx,dy)
local W=WIDGET.sel local W=WIDGET.sel
if not W then return end if not W then return end
if W.type=="slider"then if W.type=="slider"or W.type=="chatBox"then
W:drag(x,y) W:drag(x,y,dx,dy)
elseif not W:isAbove(x,y)then elseif not W:isAbove(x,y)then
WIDGET.sel=false WIDGET.sel=false
end end