Skip to content

Commit

Permalink
Mouse wheel support for scroll panes
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Aug 1, 2024
1 parent 19a770b commit ea1af01
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final case class ScrollOptions(
scrollMode == ScrollMode.None

object ScrollOptions:
val default: ScrollOptions = ScrollOptions(ScrollMode.Vertical, 1)
val default: ScrollOptions = ScrollOptions(ScrollMode.Vertical, 4)

enum ScrollMode:
case None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,21 @@ object ScrollPane:
context: UIContext[ReferenceData],
model: ScrollPane[A, ReferenceData]
): GlobalEvent => Outcome[ScrollPane[A, ReferenceData]] =
e =>
case MouseEvent.Wheel(pos, deltaY)
if model.scrollOptions.isEnabled && Bounds(context.bounds.coords, model.dimensions)
.contains(context.mouseCoords) =>
val scrollBy =
val coords =
if deltaY < 0 then -model.scrollOptions.scrollSpeed else model.scrollOptions.scrollSpeed
coords.toDouble / model.dimensions.height.toDouble

Outcome(
model.copy(
scrollAmount = Math.min(1.0d, Math.max(0.0d, model.scrollAmount + scrollBy))
)
)

case e =>
val scrollingActive =
model.scrollOptions.isEnabled && model.contentBounds.height > model.dimensions.height
val ctx = context.copy(bounds = Bounds(context.bounds.coords, model.dimensions))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import roguelikestarterkit.ui.datatypes.UIContext
Scrolling TODOs:
- scroll up/down/left/right arrow buttons (use scroll speed in options)
- mouse wheel events (use scroll speed in options?)
- ways to describe scrolling: Fixed amount, proportional, etc.
- scroll button size can be adjusted based on the content size.
- horizontal scrolling
Expand Down

0 comments on commit ea1af01

Please sign in to comment.