Update scrollable sizes once per `View:update`

This reduces the number of `get_[h_]scrollable_size` calls by around
three times.
This commit is contained in:
Guldoman 2021-09-04 18:26:52 +02:00
parent 9e5d404b29
commit 73affe215f
No known key found for this signature in database
GPG Key ID: C08A498EC7F1AFDD
1 changed files with 10 additions and 6 deletions

View File

@ -19,6 +19,8 @@ function View:new()
self.scroll = { x = 0, y = 0, to = { x = 0, y = 0 } } self.scroll = { x = 0, y = 0, to = { x = 0, y = 0 } }
self.cursor = "arrow" self.cursor = "arrow"
self.scrollable = false self.scrollable = false
self.h_scrollable_size = 0
self.scrollable_size = math.huge
end end
function View:move_towards(t, k, dest, rate) function View:move_towards(t, k, dest, rate)
@ -63,7 +65,7 @@ end
function View:get_scrollbar_rect() function View:get_scrollbar_rect()
local sz = self:get_scrollable_size() local sz = self.scrollable_size
if sz <= self.size.y or sz == math.huge then if sz <= self.size.y or sz == math.huge then
return 0, 0, 0, 0 return 0, 0, 0, 0
end end
@ -77,7 +79,7 @@ end
function View:get_h_scrollbar_rect() function View:get_h_scrollbar_rect()
local sz = self:get_h_scrollable_size() local sz = self.h_scrollable_size
if sz <= self.size.x or sz == math.huge then if sz <= self.size.x or sz == math.huge then
return 0, 0, 0, 0 return 0, 0, 0, 0
end end
@ -121,10 +123,10 @@ end
function View:on_mouse_moved(x, y, dx, dy) function View:on_mouse_moved(x, y, dx, dy)
if self.dragging_scrollbar then if self.dragging_scrollbar then
local delta = self:get_scrollable_size() / self.size.y * dy local delta = self.scrollable_size / self.size.y * dy
self.scroll.to.y = self.scroll.to.y + delta self.scroll.to.y = self.scroll.to.y + delta
elseif self.dragging_h_scrollbar then elseif self.dragging_h_scrollbar then
local delta = self:get_h_scrollable_size() / self.size.x * dx local delta = self.h_scrollable_size / self.size.x * dx
self.scroll.to.x = self.scroll.to.x + delta self.scroll.to.x = self.scroll.to.x + delta
end end
self.hovered_scrollbar = self:scrollbar_overlaps_point(x, y) self.hovered_scrollbar = self:scrollbar_overlaps_point(x, y)
@ -163,14 +165,16 @@ end
function View:clamp_scroll_position() function View:clamp_scroll_position()
local max_x = self:get_h_scrollable_size() - self.size.x local max_x = self.h_scrollable_size - self.size.x
local max_y = self:get_scrollable_size() - self.size.y local max_y = self.scrollable_size - self.size.y
self.scroll.to.x = common.clamp(self.scroll.to.x, 0, max_x) self.scroll.to.x = common.clamp(self.scroll.to.x, 0, max_x)
self.scroll.to.y = common.clamp(self.scroll.to.y, 0, max_y) self.scroll.to.y = common.clamp(self.scroll.to.y, 0, max_y)
end end
function View:update() function View:update()
self.scrollable_size = self:get_scrollable_size()
self.h_scrollable_size = self:get_h_scrollable_size()
self:clamp_scroll_position() self:clamp_scroll_position()
self:move_towards(self.scroll, "x", self.scroll.to.x, 0.3) self:move_towards(self.scroll, "x", self.scroll.to.x, 0.3)
self:move_towards(self.scroll, "y", self.scroll.to.y, 0.3) self:move_towards(self.scroll, "y", self.scroll.to.y, 0.3)