0.9.2:方向键选择控件
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
local gc=love.graphics
|
||||
local kb=love.keyboard
|
||||
local int=math.floor
|
||||
local int,abs=math.floor,math.abs
|
||||
local format=string.format
|
||||
local next=next
|
||||
local color=color
|
||||
|
||||
local EMPTY={}
|
||||
|
||||
local button={
|
||||
@@ -15,6 +16,9 @@ end
|
||||
function button:isAbove(x,y)
|
||||
return x>self.x-self.ATV and x<self.x+self.w+2*self.ATV and y>self.y-self.ATV and y<self.y+self.h+2*self.ATV
|
||||
end
|
||||
function button:getCenter()
|
||||
return self.x+self.w*.5,self.y+self.h*.5
|
||||
end
|
||||
function button:FX()
|
||||
sysFX.new("ripple",.16,self.x-self.ATV,self.y-self.ATV,self.w+2*self.ATV,self.h+2*self.ATV)
|
||||
end
|
||||
@@ -65,6 +69,9 @@ end
|
||||
function switch:isAbove(x,y)
|
||||
return x>self.x and x<self.x+50 and y>self.y-25 and y<self.y+25
|
||||
end
|
||||
function switch:getCenter()
|
||||
return self.x,self.y
|
||||
end
|
||||
function switch:update()
|
||||
local _=self.ATV
|
||||
if WIDGET.sel==self then if _<8 then self.ATV=_+1 end
|
||||
@@ -77,6 +84,8 @@ function switch:update()
|
||||
end
|
||||
function switch:draw()
|
||||
local x,y=self.x,self.y-25
|
||||
|
||||
--Checked
|
||||
if self.ATV>0 then
|
||||
gc.setColor(1,1,1,self.ATV*.08)
|
||||
gc.rectangle("fill",x,y,50,50)
|
||||
@@ -86,11 +95,13 @@ function switch:draw()
|
||||
gc.setLineWidth(6)
|
||||
gc.line(x+5,y+25,x+18,y+38,x+45,y+11)
|
||||
end
|
||||
--checked
|
||||
|
||||
--Frame
|
||||
gc.setLineWidth(4)
|
||||
gc.setColor(1,1,1,.6+self.ATV*.05)
|
||||
gc.rectangle("line",x,y,50,50)
|
||||
--frame
|
||||
|
||||
--Text
|
||||
local t=self.text
|
||||
if t then
|
||||
gc.setColor(1,1,1)
|
||||
@@ -114,11 +125,15 @@ end
|
||||
function slider:isAbove(x,y)
|
||||
return x>self.x-10 and x<self.x+self.w+10 and y>self.y-20 and y<self.y+20
|
||||
end
|
||||
function slider:getCenter()
|
||||
return self.x+self.w*(self.pos/self.unit),self.y
|
||||
end
|
||||
function slider:update()
|
||||
local _=self.ATV
|
||||
if WIDGET.sel==self then
|
||||
if self.ATV<6 then self.ATV=self.ATV+1 end
|
||||
if _<6 then self.ATV=_+1 end
|
||||
else
|
||||
if self.ATV>0 then self.ATV=self.ATV-1 end
|
||||
if _>0 then self.ATV=_-1 end
|
||||
end
|
||||
if not(self.hide and self.hide())then
|
||||
self.pos=self.pos*.7+self.disp()*.3
|
||||
@@ -126,6 +141,8 @@ function slider:update()
|
||||
end
|
||||
function slider:draw()
|
||||
local x,y=self.x,self.y
|
||||
|
||||
--Units
|
||||
gc.setColor(1,1,1,.5+self.ATV*.06)
|
||||
gc.setLineWidth(2)
|
||||
local x1,x2=x,x+self.w
|
||||
@@ -133,17 +150,20 @@ function slider:draw()
|
||||
local x=x1+(x2-x1)*p/self.unit
|
||||
gc.line(x,y+7,x,y-7)
|
||||
end
|
||||
--units
|
||||
|
||||
--Axis
|
||||
gc.setLineWidth(4)
|
||||
gc.line(x1,y,x2,y)
|
||||
--axis
|
||||
|
||||
--Text
|
||||
local t=self.text
|
||||
if t then
|
||||
gc.setColor(1,1,1)
|
||||
setFont(self.font)
|
||||
gc.printf(t,x-312,y-self.font*.7,300,"right")
|
||||
end
|
||||
--text
|
||||
|
||||
--Block
|
||||
local x,y,w,h=x1+(x2-x1)*self.pos/self.unit-10-self.ATV*.5,y-16-self.ATV,20+self.ATV,32+2*self.ATV
|
||||
gc.setColor(.8,.8,.8)
|
||||
gc.rectangle("fill",x,y,w,h)
|
||||
@@ -152,7 +172,6 @@ function slider:draw()
|
||||
gc.setColor(1,1,1,self.ATV*.16)
|
||||
gc.rectangle("line",x+1,y+1,w-2,h-2)
|
||||
end
|
||||
--block
|
||||
end
|
||||
function slider:getInfo()
|
||||
print(format("x=%d,y=%d,w=%d",self.x,self.y,self.w))
|
||||
@@ -172,36 +191,96 @@ function WIDGET.set(L)
|
||||
end
|
||||
|
||||
WIDGET.new={}
|
||||
function WIDGET.new.button(x,y,w,h,color,font,code,hide,N)
|
||||
function WIDGET.newText(D)
|
||||
local _={
|
||||
x=x-w*.5,y=y-h*.5,
|
||||
w=w,h=h,
|
||||
color=color,
|
||||
font=font,
|
||||
code=code,
|
||||
hide=hide,
|
||||
next=N,
|
||||
name= D.name,
|
||||
x= D.x,
|
||||
y= D.y,
|
||||
align= D.align,
|
||||
color= color[D.color]or D.color,
|
||||
font= D.font,
|
||||
hide= D.hide,
|
||||
}for k,v in next,button do _[k]=v end return _
|
||||
end
|
||||
function WIDGET.new.switch(x,y,font,disp,code,hide,N)
|
||||
function WIDGET.newImage(D)
|
||||
local _={
|
||||
x=x,y=y,font=font,
|
||||
disp=disp,
|
||||
code=code,
|
||||
hide=hide,
|
||||
next=N,
|
||||
name= D.name,
|
||||
x= D.x-w*.5,
|
||||
y= D.y-h*.5,
|
||||
w= D.w,
|
||||
h= D.h,
|
||||
color= color[D.color]or D.color,
|
||||
font= D.font,
|
||||
code= D.code,
|
||||
hide= D.hide,
|
||||
}for k,v in next,button do _[k]=v end return _
|
||||
end
|
||||
function WIDGET.newButton(D)
|
||||
local _={
|
||||
name= D.name,
|
||||
|
||||
x= D.x-D.w*.5,
|
||||
y= D.y-D.h*.5,
|
||||
w= D.w,
|
||||
h= D.h,
|
||||
|
||||
resCtr={
|
||||
D.x,D.y,
|
||||
D.x-D.w*.35,D.y-D.h*.35,
|
||||
D.x-D.w*.35,D.y+D.h*.35,
|
||||
D.x+D.w*.35,D.y-D.h*.35,
|
||||
D.x+D.w*.35,D.y+D.h*.35,
|
||||
},
|
||||
|
||||
color= color[D.color]or D.color,
|
||||
font= D.font,
|
||||
code= D.code,
|
||||
hide= D.hide,
|
||||
}for k,v in next,button do _[k]=v end return _
|
||||
end
|
||||
function WIDGET.newSwitch(D)
|
||||
local _={
|
||||
name= D.name,
|
||||
|
||||
x= D.x,
|
||||
y= D.y,
|
||||
|
||||
cx= D.x+25,
|
||||
cy= D.y,
|
||||
resCtr={
|
||||
D.x+25,D.y,
|
||||
},
|
||||
|
||||
font= D.font,
|
||||
disp= D.disp,
|
||||
code= D.code,
|
||||
hide= D.hide,
|
||||
}for k,v in next,switch do _[k]=v end return _
|
||||
end
|
||||
function WIDGET.new.slider(x,y,w,unit,font,change,disp,code,hide,N)
|
||||
function WIDGET.newSlider(D)
|
||||
local _={
|
||||
x=x,y=y,
|
||||
w=w,unit=unit,
|
||||
font=font,
|
||||
change=change,
|
||||
disp=disp,
|
||||
code=code,
|
||||
hide=hide,
|
||||
next=N,
|
||||
name= D.name,
|
||||
|
||||
x= D.x,
|
||||
y= D.y,
|
||||
w= D.w,
|
||||
|
||||
cx= D.x+D.w*.5,
|
||||
cy= D.y,
|
||||
resCtr={
|
||||
D.x,D.y,
|
||||
D.x+D.w*.25,D.y,
|
||||
D.x+D.w*.5,D.y,
|
||||
D.x+D.w*.75,D.y,
|
||||
D.x+D.w,D.y,
|
||||
},
|
||||
|
||||
unit= D.unit,
|
||||
font= D.font,
|
||||
change= D.change,
|
||||
disp= D.disp,
|
||||
code= D.code,
|
||||
hide= D.hide,
|
||||
}for k,v in next,slider do _[k]=v end return _
|
||||
end
|
||||
|
||||
@@ -247,27 +326,74 @@ function WIDGET.drag(x,y,dx,dy)
|
||||
end
|
||||
end
|
||||
function WIDGET.keyPressed(i)
|
||||
if i=="tab"then
|
||||
if i=="space"or i=="return"then
|
||||
if WIDGET.sel then
|
||||
WIDGET.sel=kb.isDown("lshift")and WIDGET.sel.prev or WIDGET.sel.next or WIDGET.sel
|
||||
else
|
||||
WIDGET.sel=select(2,next(WIDGET.active))
|
||||
WIDGET.press()
|
||||
end
|
||||
elseif i=="space"or i=="return"then
|
||||
if WIDGET.sel then
|
||||
WIDGET.press(WIDGET.sel)
|
||||
end
|
||||
elseif i=="left"or i=="right"then
|
||||
local W=WIDGET.sel
|
||||
if W then
|
||||
if W.type=="slider"then
|
||||
local p=W.disp()
|
||||
local P=i=="left"and(p>0 and p-1)or p<W.unit and p+1
|
||||
if p==P or not P then return end
|
||||
W.code(P)
|
||||
if W.change then W.change()end
|
||||
elseif kb.isDown("lshift","lalt","lctrl")then
|
||||
--when hold [↑], control slider with left/right
|
||||
if i=="left"or i=="right"then
|
||||
local W=WIDGET.sel
|
||||
if W then
|
||||
if W.type=="slider"then
|
||||
local p=W.disp()
|
||||
local P=i=="left"and(p>0 and p-1)or p<W.unit and p+1
|
||||
if p==P or not P then return end
|
||||
W.code(P)
|
||||
if W.change then W.change()end
|
||||
end
|
||||
end
|
||||
end
|
||||
elseif i=="up"or i=="down"or i=="left"or i=="right"then
|
||||
if WIDGET.sel then
|
||||
local W=WIDGET.sel
|
||||
local WX,WY=W:getCenter()
|
||||
local dir=(i=="right"or i=="down")and 1 or -1
|
||||
local tar
|
||||
local minDist=1e99
|
||||
if i=="left"or i=="right"then
|
||||
for i=1,#WIDGET.active do
|
||||
local W1=WIDGET.active[i]
|
||||
if W~=W1 then
|
||||
local L=W1.resCtr
|
||||
for j=1,#L,2 do
|
||||
local x,y=L[j],L[j+1]
|
||||
local dist=(x-WX)*dir
|
||||
if dist>10 then
|
||||
dist=dist+abs(y-WY)*6.26
|
||||
if dist<minDist then
|
||||
minDist=dist
|
||||
tar=W1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
for i=1,#WIDGET.active do
|
||||
local W1=WIDGET.active[i]
|
||||
if W~=W1 then
|
||||
local L=W1.resCtr
|
||||
for j=1,#L,2 do
|
||||
local x,y=L[j],L[j+1]
|
||||
local dist=(y-WY)*dir
|
||||
if dist>10 then
|
||||
dist=dist+abs(x-WX)*6.26
|
||||
if dist<minDist then
|
||||
minDist=dist
|
||||
tar=W1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if tar then
|
||||
WIDGET.sel=tar
|
||||
end
|
||||
else
|
||||
WIDGET.sel=WIDGET.active[1]
|
||||
end
|
||||
end
|
||||
end
|
||||
function WIDGET.gamepadPressed(i)
|
||||
@@ -296,7 +422,9 @@ function WIDGET.gamepadPressed(i)
|
||||
end
|
||||
|
||||
function WIDGET.update()
|
||||
for _,W in next,WIDGET.active do W:update()end
|
||||
for _,W in next,WIDGET.active do
|
||||
W:update()
|
||||
end
|
||||
end
|
||||
function WIDGET.draw()
|
||||
for _,W in next,WIDGET.active do
|
||||
|
||||
Reference in New Issue
Block a user