Add horizontal scrollbar
Classes and plugins that relied on having only the vertical scrollbar should continue working.
This commit is contained in:
parent
f106993d0e
commit
5c7b133e0b
|
@ -57,45 +57,71 @@ function View:get_scrollable_size()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function View:get_scrollbar_rect()
|
function View:get_scrollbar_rect() -- TODO: make plugins use `View:get_scrollbars_rect`
|
||||||
local sz = self:get_scrollable_size()
|
local rect = self:get_get_scrollbars_rect()
|
||||||
if sz <= self.size.y or sz == math.huge then
|
return rect.x, rect.y, rect.w, rect.h
|
||||||
return 0, 0, 0, 0
|
end
|
||||||
|
|
||||||
|
function View:get_scrollbars_rect()
|
||||||
|
local v_sizes = { x = 0, y = 0, w = 0, h = 0 }
|
||||||
|
local h_sizes = { x = 0, y = 0, w = 0, h = 0 }
|
||||||
|
local v_sz, h_sz = self:get_scrollable_size()
|
||||||
|
h_sz = h_sz or 0 -- FIXME: not every subclass returns the second value
|
||||||
|
|
||||||
|
if v_sz > self.size.y and v_sz ~= math.huge then
|
||||||
|
local h = math.max(20, self.size.y * self.size.y / v_sz)
|
||||||
|
v_sizes.x = self.position.x + self.size.x - style.scrollbar_size
|
||||||
|
v_sizes.y = self.position.y + self.scroll.y * (self.size.y - h) / (v_sz - self.size.y)
|
||||||
|
v_sizes.w = style.scrollbar_size
|
||||||
|
v_sizes.h = h
|
||||||
end
|
end
|
||||||
local h = math.max(20, self.size.y * self.size.y / sz)
|
|
||||||
return
|
if h_sz > self.size.x and h_sz ~= math.huge then
|
||||||
self.position.x + self.size.x - style.scrollbar_size,
|
local w = math.max(20, self.size.x * self.size.x / h_sz)
|
||||||
self.position.y + self.scroll.y * (self.size.y - h) / (sz - self.size.y),
|
h_sizes.x = self.position.x + self.scroll.x * (self.size.x - w) / (h_sz - self.size.x)
|
||||||
style.scrollbar_size,
|
h_sizes.y = self.position.y + self.size.y - style.scrollbar_size
|
||||||
h
|
h_sizes.w = w
|
||||||
|
h_sizes.h = style.scrollbar_size
|
||||||
|
end
|
||||||
|
|
||||||
|
return v_sizes, h_sizes
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function View:scrollbar_overlaps_point(x, y)
|
function View:scrollbar_overlaps_point(x, y)
|
||||||
local sx, sy, sw, sh = self:get_scrollbar_rect()
|
local v, h = self:get_scrollbars_rect()
|
||||||
return x >= sx - sw * 3 and x < sx + sw and y >= sy and y < sy + sh
|
local v_overlap = x >= v.x - v.w * 3 and x < v.x + v.w and y >= v.y and y < v.y + v.h
|
||||||
|
local h_overlap = x >= h.x and x < h.x + h.w and y >= h.y - h.h * 3 and y <= h.y + h.h
|
||||||
|
return v_overlap, not v_overlap and h_overlap -- precedence to vertical scrollbar
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function View:on_mouse_pressed(button, x, y, clicks)
|
function View:on_mouse_pressed(button, x, y, clicks)
|
||||||
if self:scrollbar_overlaps_point(x, y) then
|
self.dragging_v_scrollbar, self.dragging_h_scrollbar = self:scrollbar_overlaps_point(x, y)
|
||||||
self.dragging_scrollbar = true
|
self.dragging_scrollbar = self.dragging_v_scrollbar -- TODO: make plugins use `self.dragging_v_scrollbar`
|
||||||
return true
|
return self.dragging_v_scrollbar or self.dragging_h_scrollbar
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function View:on_mouse_released(button, x, y)
|
function View:on_mouse_released(button, x, y)
|
||||||
self.dragging_scrollbar = false
|
self.dragging_v_scrollbar = false
|
||||||
|
self.dragging_h_scrollbar = false
|
||||||
|
self.dragging_scrollbar = self.dragging_v_scrollbar -- TODO: make plugins use `self.dragging_v_scrollbar`
|
||||||
end
|
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_v_scrollbar then
|
||||||
local delta = self:get_scrollable_size() / self.size.y * dy
|
local v_sz,_ = self:get_scrollable_size()
|
||||||
|
local delta = v_sz / 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
|
||||||
|
local _,h_sz = self:get_scrollable_size()
|
||||||
|
local delta = h_sz / self.size.x * dx
|
||||||
|
self.scroll.to.x = self.scroll.to.x + delta
|
||||||
end
|
end
|
||||||
self.hovered_scrollbar = self:scrollbar_overlaps_point(x, y)
|
self.hovered_v_scrollbar, self.hovered_h_scrollbar = self:scrollbar_overlaps_point(x, y)
|
||||||
|
self.hovered_scrollbar = self.hovered_v_scrollbar -- TODO: make plugins use `self.hovered_v_scrollbar`
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -154,10 +180,13 @@ end
|
||||||
|
|
||||||
|
|
||||||
function View:draw_scrollbar()
|
function View:draw_scrollbar()
|
||||||
local x, y, w, h = self:get_scrollbar_rect()
|
local v, h = self:get_scrollbars_rect()
|
||||||
local highlight = self.hovered_scrollbar or self.dragging_scrollbar
|
local v_highlight = self.hovered_v_scrollbar or self.dragging_v_scrollbar
|
||||||
local color = highlight and style.scrollbar2 or style.scrollbar
|
local h_highlight = self.hovered_h_scrollbar or self.dragging_h_scrollbar
|
||||||
renderer.draw_rect(x, y, w, h, color)
|
local v_color = v_highlight and style.scrollbar2 or style.scrollbar
|
||||||
|
local h_color = h_highlight and style.scrollbar2 or style.scrollbar
|
||||||
|
renderer.draw_rect(v.x, v.y, v.w, v.h, v_color)
|
||||||
|
renderer.draw_rect(h.x, h.y, h.w, h.h, h_color)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue