Skip to content

Instantly share code, notes, and snippets.

@DoctorGester
Created October 13, 2018 19:21
Show Gist options
  • Select an option

  • Save DoctorGester/a169596381e1dc8f6dc99adc33f5ac85 to your computer and use it in GitHub Desktop.

Select an option

Save DoctorGester/a169596381e1dc8f6dc99adc33f5ac85 to your computer and use it in GitHub Desktop.
function frame_scrollbar(content_height)
local frame = get_current_frame()
local frame_origin = get_frame_origin()
local frame_size = get_frame_size()
local frame_height = frame_size.y
local scrollbar_height_relative = frame_height / content_height
if scrollbar_height_relative >= 1 then
return
end
local scrollbar_offset_relative = frame.scroll_position / content_height
local scrollbar_height = scrollbar_height_relative * frame_height
local scrollbar_size = vector(10, scrollbar_height)
local scrollbar_position = frame_origin + vector(frame_size.x - scrollbar_size.x, scrollbar_offset_relative * frame_height)
local id = "frame_scrollbar_" .. tostring(#frame_stack)
local was_active = is_active(id)
local scrollbar = button_behavior(id, make_rectangle(scrollbar_position, scrollbar_size))
local became_active = scrollbar.active and not was_active
if became_active then
frame.scroll_drag_offset = ui_state.mouse_position.y - scrollbar_position.y
end
local function clamp_scroll_space(scroll_position_content_space)
return math.max(0, math.min(scroll_position_content_space, content_height - frame_height))
end
if scrollbar.active then
local scroll_position_screen_space = ui_state.mouse_position.y - frame_origin.y - frame.scroll_drag_offset
local scroll_position_content_space = scroll_position_screen_space / frame_height * content_height
frame.scroll_position = clamp_scroll_space(scroll_position_content_space)
love.graphics.setColor(0.7, 0.7, 0.7)
elseif scrollbar.hovered then
love.graphics.setColor(0.9, 0.9, 0.9)
else
love.graphics.setColor(1, 1, 1)
end
love.graphics.rectangle("fill", scrollbar_position.x, scrollbar_position.y, scrollbar_size.x, scrollbar_size.y)
local mouse_scroll = ui_state.wheel_input.y * 50
if mouse_scroll ~= 0 and rectangle_contains(frame.rectangle, ui_state.mouse_position) then
frame.scroll_position = clamp_scroll_space(frame.scroll_position - mouse_scroll)
end
end
@karungokihara
Copy link
Copy Markdown

i like it although i have not yet tested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment