Created
October 13, 2018 19:21
-
-
Save DoctorGester/a169596381e1dc8f6dc99adc33f5ac85 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i like it although i have not yet tested