优化滑条控件和列表框控件

This commit is contained in:
MrZ626
2021-12-26 02:38:25 +08:00
parent 6eeddba773
commit b07c4dc53a
12 changed files with 102 additions and 87 deletions

View File

@@ -529,7 +529,7 @@ function slider:isAbove(x,y)
return x>self.x-10 and x<self.x+self.w+10 and y>self.y-25 and y<self.y+25
end
function slider:getCenter()
return self.x+self.w*(self.pos/self.unit),self.y
return self.x+self.w*((self.pos-self.rangeL)/(self.rangeR-self.rangeL)),self.y
end
function slider:update(dt)
local ATV=self.ATV
@@ -556,8 +556,8 @@ function slider:draw()
--Units
if not self.smooth then
gc_setLineWidth(2)
for p=0,self.unit do
local X=x+(x2-x)*p/self.unit
for p=self.rangeL,self.rangeR,self.unit do
local X=x+(x2-x)*(p-self.rangeL)/(self.rangeR-self.rangeL)
gc_line(X,y+7,X,y-7)
end
end
@@ -567,7 +567,7 @@ function slider:draw()
gc_line(x,y,x2,y)
--Block
local cx=x+(x2-x)*self.pos/self.unit
local cx=x+(x2-x)*(self.pos-self.rangeL)/(self.rangeR-self.rangeL)
local bx,by,bw,bh=cx-10-ATV*.5,y-16-ATV,20+ATV,32+2*ATV
gc_setColor(.8,.8,.8)
gc_rectangle('fill',bx,by,bw,bh,3)
@@ -602,13 +602,16 @@ end
function slider:drag(x)
if not x then return end
x=x-self.x
local p=self.disp()
local P=x<0 and 0 or x>self.w and self.unit or x/self.w*self.unit
if not self.smooth then
P=int(P+.5)
local newPos=MATH.interval(x/self.w,0,1)
local newVal
if not self.unit then
newVal=(1-newPos)*self.rangeL+newPos*self.rangeR
else
newVal=newPos*(self.rangeR-self.rangeL)
newVal=self.rangeL+newVal-newVal%self.unit
end
if p~=P then
self.code(P)
if newVal~=self.disp()then
self.code(newVal)
end
if self.change and timer()-self.lastTime>.5 then
self.lastTime=timer()
@@ -621,8 +624,8 @@ function slider:release(x)
end
function slider:scroll(n)
local p=self.disp()
local u=self.smooth and .01 or 1
local P=n==-1 and max(p-u,0)or min(p+u,self.unit)
local u=self.unit or .01
local P=MATH.interval(p+u*n,self.rangeL,self.rangeR)
if p==P or not P then return end
self.code(P)
if self.change and timer()-self.lastTime>.18 then
@@ -633,7 +636,13 @@ end
function slider:arrowKey(k)
self:scroll((k=='left'or k=='up')and -1 or 1)
end
function WIDGET.newSlider(D)--name,x,y,w[,lim][,fText][,color][,unit][,smooth][,font=30][,fType][,change],disp[,show][,code],hide
function WIDGET.newSlider(D)--name,x,y,w[,lim][,fText][,color][,axis][,smooth][,font=30][,fType][,change],disp[,show][,code],hide
if not D.axis then
D.axis={0,1,false}
D.smooth=true
elseif not D.axis[3]then
D.smooth=true
end
local _={
name= D.name or"_",
@@ -652,8 +661,10 @@ function WIDGET.newSlider(D)--name,x,y,w[,lim][,fText][,color][,unit][,smooth][,
fText= D.fText,
color= D.color and(COLOR[D.color]or D.color)or COLOR.Z,
unit= D.unit or 1,
smooth=false,
rangeL=D.axis[1],
rangeR=D.axis[2],
unit= D.axis[3],
smooth=D.smooth,
font= D.font or 30,
fType= D.fType,
change=D.change,
@@ -663,22 +674,17 @@ function WIDGET.newSlider(D)--name,x,y,w[,lim][,fText][,color][,unit][,smooth][,
hide= D.hide,
show= false,
}
if D.smooth~=nil then
_.smooth=D.smooth
else
_.smooth=_.unit<=1
end
if D.show then
if type(D.show)=='function'then
_.show=D.show
else
_.show=sliderShowFunc[D.show]
end
elseif D.show~=false then
if _.unit<=1 then
_.show=sliderShowFunc.percent
else
elseif D.show~=false then--Use default if nil
if _.unit and _.unit%1==0 then
_.show=sliderShowFunc.int
else
_.show=sliderShowFunc.percent
end
end
for k,v in next,slider do _[k]=v end
@@ -1227,6 +1233,14 @@ function listBox:arrowKey(dir)
end
end
end
function listBox:select(i)
self.selected=i
if self.selected<int(self.scrollPos/self.lineH)+2 then
self:drag(nil,nil,nil,1e99)
elseif self.selected>int(self.scrollPos/self.lineH)+self.capacity-1 then
self:drag(nil,nil,nil,-1e99)
end
end
function listBox:draw()
local x,y,w,h=self.x,self.y,self.w,self.h
local list=self.list