mouse events for scrollbar

You can either drag the scrollbar or click anywhere in the scrollbar
area.

It has this weird funkiness that the scrollbar shrinks as you get near
the bottom. But it feels.. weirdly satisfying?
This commit is contained in:
Kartik K. Agaram 2023-11-18 05:26:28 -08:00
parent 3717d7868a
commit fe8a21bea3
9 changed files with 50 additions and 5 deletions

View File

@ -1,3 +1,6 @@
on.update = function()
refresh_debug_animations()
end
if Editor_scrollbar_drag then
adjust_scrollbar(App.mouse_y())
end
end

View File

@ -2,5 +2,11 @@ on.mouse_press = function(x,y, mouse_button)
if mouse_press_consumed_by_any_button_handler(Editor_state, x,y, mouse_button) then
return
end
edit.mouse_press(Editor_state, x,y, mouse_button)
if on_editor_scrollbar(x,y) then
Editor_scrollbar_drag = true
elseif on_editor_scrollbar_area(x,y) then
-- nothing
else
edit.mouse_press(Editor_state, x,y, mouse_button)
end
end

View File

@ -1,3 +1,10 @@
on.mouse_release = function(x,y, mouse_button)
edit.mouse_release(Editor_state, x,y, mouse_button)
if Editor_scrollbar_drag then
adjust_scrollbar(y)
Editor_scrollbar_drag = nil
elseif on_editor_scrollbar_area(x,y) then
adjust_scrollbar(y)
else
edit.mouse_release(Editor_state, x,y, mouse_button)
end
end

View File

@ -8,4 +8,6 @@ draw_scrollbar = function(Editor_state)
local boty = Editor_state.top +sbbot*(Editor_state.bottom - Editor_state.top)
App.color{r=0.6, g=0.6, b=0.8, a=0.5}
love.graphics.rectangle('fill', Editor_state.right+20, topy, 20, boty-topy)
Editor_scrollbar_top = topy
Editor_scrollbar_bottom = boty
end

13
0036-adjust_scrollbar Normal file
View File

@ -0,0 +1,13 @@
adjust_scrollbar = function(y)
local s = (y-Editor_state.top) / (Editor_state.bottom-Editor_state.top)
local screen_line = s*Editor_state.screen_line_count
local line = 1
for i=1,#Editor_state.lines do
if Editor_state.line_cache[i].start_screen_line_index > screen_line then
break
end
line = i
end
Editor_state.screen_top1 = {line=line, pos=1}
Editor_state.cursor1 = {line=line, pos=1}
end

View File

@ -0,0 +1,2 @@
Editor_scrollbar_top = 0 -- in px
Editor_scrollbar_bottom = 0

7
0038-on_editor_scrollbar Normal file
View File

@ -0,0 +1,7 @@
on_editor_scrollbar = function(x,y)
if x < Editor_state.right+20 then return end
if x > Editor_state.right+40 then return end
if y < Editor_scrollbar_top then return end
if y > Editor_scrollbar_bottom then return end
return true
end

View File

@ -0,0 +1,7 @@
on_editor_scrollbar_area = function(x,y)
if x < Editor_state.right+20 then return end
if x > Editor_state.right+40 then return end
if y < Editor_state.top then return end
if y > Editor_state.bottom then return end
return true
end

View File

@ -65,8 +65,6 @@ found anything amiss: http://akkartik.name/contact
* Can't scroll while selecting text with mouse.
* No scrollbars yet. That stuff is hard.
## Mirrors and Forks
This repo is a fork of [lines.love](http://akkartik.name/lines.html), an