Skip to content

Commit 5c723cf

Browse files
committed
修复textBox滚动范围计算错误导致最后一行可能看不到
1 parent 06013be commit 5c723cf

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

widget.lua

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,8 +1472,8 @@ function Widgets.textBox:setTexts(t)
14721472
end
14731473
function Widgets.textBox:push(t)
14741474
ins(self._texts,t)
1475-
if self._scrollPos==(#self._texts-1-self._capacity)*self.lineHeight then-- minus 1 for the new message
1476-
self._scrollPos=min(self._scrollPos+self.lineHeight,(#self._texts-self._capacity)*self.lineHeight)
1475+
if self._scrollPos==(#self._texts-1)*self.lineHeight-self.h then-- minus 1 for the new message
1476+
self._scrollPos=min(self._scrollPos+self.lineHeight,#self._texts*self.lineHeight-self.h)
14771477
end
14781478
end
14791479
function Widgets.textBox:clear()
@@ -1503,10 +1503,10 @@ function Widgets.textBox:press(x,y)
15031503
end
15041504
end
15051505
function Widgets.textBox:drag(_,_,_,dy)
1506-
self._scrollPos=max(0,min(self._scrollPos-dy,(#self._texts-self._capacity)*self.lineHeight))
1506+
self._scrollPos=max(0,min(self._scrollPos-dy,#self._texts*self.lineHeight-self.h))
15071507
end
15081508
function Widgets.textBox:scroll(dx,dy)
1509-
self._scrollPos=max(0,min(self._scrollPos-(dx+dy)*self.lineHeight,(#self._texts-self._capacity)*self.lineHeight))
1509+
self._scrollPos=max(0,min(self._scrollPos-(dx+dy)*self.lineHeight,#self._texts*self.lineHeight-self.h))
15101510
end
15111511
function Widgets.textBox:arrowKey(k)
15121512
self:scroll(0,k =='up' and -1 or k=='down' and 1 or 0)
@@ -1521,8 +1521,9 @@ function Widgets.textBox:update(dt)
15211521
end
15221522
function Widgets.textBox:draw()
15231523
local x,y,w,h=self._x,self._y,self.w,self.h
1524-
local texts=self._texts
1524+
local list=self._texts
15251525
local lineH=self.lineHeight
1526+
local H=#list*lineH
15261527
local scroll=self._scrollPos1
15271528

15281529
-- Background
@@ -1541,12 +1542,12 @@ function Widgets.textBox:draw()
15411542

15421543
-- Slider
15431544
gc_setColor(COLOR.L)
1544-
if #texts>self._capacity then
1545-
local len=h*h/(#texts*lineH)
1545+
if #list>self._capacity then
1546+
local len=h*h/H
15461547
if self.scrollBarPos=='left' then
1547-
gc_rectangle('fill',-15,(h-len)*scroll/((#texts-self._capacity)*lineH),10,len,self.cornerR)
1548+
gc_rectangle('fill',-15,(h-len)*scroll/(H-h),10,len,self.cornerR)
15481549
elseif self.scrollBarPos=='right' then
1549-
gc_rectangle('fill',w+5,(h-len)*scroll/((#texts-self._capacity)*lineH),10,len,self.cornerR)
1550+
gc_rectangle('fill',w+5,(h-len)*scroll/(H-h),10,len,self.cornerR)
15501551
end
15511552
end
15521553

@@ -1571,8 +1572,8 @@ function Widgets.textBox:draw()
15711572
local pos=floor(scroll/lineH)
15721573
for i=1,self._capacity+1 do
15731574
i=pos+i
1574-
if texts[i] then
1575-
gc_printf(texts[i],10,self.yOffset,w-16)
1575+
if list[i] then
1576+
gc_printf(list[i],10,self.yOffset,w-16)
15761577
end
15771578
gc_translate(0,lineH)
15781579
end

0 commit comments

Comments
 (0)