Skip to content

Commit f87fc2b

Browse files
committed
Gui window skrilling
1 parent 72f88ad commit f87fc2b

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

common/src/main/kotlin/com/lambda/gui/api/GuiEvent.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.lambda.event.Event
44
import com.lambda.util.KeyCode
55
import com.lambda.util.Mouse
66
import com.lambda.util.math.Vec2d
7+
import net.minecraft.world.gen.feature.DeltaFeature
78

89
abstract class GuiEvent : Event {
910
class Show : GuiEvent()
@@ -14,4 +15,5 @@ abstract class GuiEvent : Event {
1415
class CharTyped(val char: Char) : GuiEvent()
1516
class MouseClick(val button: Mouse.Button, val action: Mouse.Action, val mouse: Vec2d) : GuiEvent()
1617
class MouseMove(val mouse: Vec2d) : GuiEvent()
18+
class MouseScroll(val mouse: Vec2d, val delta: Double) : GuiEvent()
1719
}

common/src/main/kotlin/com/lambda/gui/api/LambdaGui.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ abstract class LambdaGui(
131131
onEvent(GuiEvent.MouseMove(rescaleMouse(mouseX, mouseY)))
132132
}
133133

134+
override fun mouseScrolled(
135+
mouseX: Double,
136+
mouseY: Double,
137+
horizontalAmount: Double,
138+
verticalAmount: Double
139+
): Boolean {
140+
onEvent(GuiEvent.MouseScroll(rescaleMouse(mouseX, mouseY), verticalAmount))
141+
return true
142+
}
143+
134144
final override fun shouldPause() = false
135145

136146
private fun rescaleMouse(mouseX: Double, mouseY: Double): Vec2d {

common/src/main/kotlin/com/lambda/gui/api/component/ListWindow.kt

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,39 @@ package com.lambda.gui.api.component
33
import com.lambda.gui.api.GuiEvent
44
import com.lambda.gui.api.component.button.ListButton
55
import com.lambda.gui.impl.AbstractClickGui
6+
import com.lambda.module.modules.client.ClickGui
67

78
abstract class ListWindow<T : ListButton>(
89
owner: AbstractClickGui,
910
) : WindowComponent<T>(owner) {
11+
private var scrollOffset: Double = 0.0
12+
private var rubberbandRequest = 0.0
13+
private var rubberbandDelta = 0.0
14+
1015
override fun onEvent(e: GuiEvent) {
11-
if (e is GuiEvent.Tick) {
12-
var y = 0.0
13-
contentComponents.children.forEach { button ->
14-
button.heightOffset = y
15-
y += button.size.y + button.listStep
16+
when (e) {
17+
is GuiEvent.Tick -> {
18+
rubberbandDelta += rubberbandRequest
19+
rubberbandRequest = 0.0
20+
21+
rubberbandDelta *= 0.5
22+
if (rubberbandDelta < 0.05) rubberbandDelta = 0.0
23+
24+
var y = scrollOffset + rubberbandDelta
25+
contentComponents.children.forEach { button ->
26+
button.heightOffset = y
27+
y += button.size.y + button.listStep
28+
}
29+
}
30+
is GuiEvent.MouseScroll -> {
31+
val delta = e.delta * 10.0 * ClickGui.scrollSpeed
32+
scrollOffset += delta
33+
34+
val prevOffset = scrollOffset
35+
val range = -contentComponents.children.sumOf { it.size.y + it.listStep }
36+
scrollOffset = scrollOffset.coerceAtLeast(range).coerceAtMost(0.0)
37+
38+
rubberbandRequest += prevOffset - scrollOffset
1639
}
1740
}
1841

common/src/main/kotlin/com/lambda/gui/api/component/core/list/ChildLayer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ open class ChildLayer<T : ChildComponent, R : IComponent>(
2828
childAccessible(child) && child.rect in rect && ownerAccessible && ownerComponent.isActive
2929
}
3030

31-
is GuiEvent.KeyPress, is GuiEvent.CharTyped -> {
31+
is GuiEvent.KeyPress, is GuiEvent.CharTyped, is GuiEvent.MouseScroll -> {
3232
if (!child.accessible) return@forEach
3333
}
3434

common/src/main/kotlin/com/lambda/module/modules/client/ClickGui.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ object ClickGui : Module(
2626
// Animation
2727
val openSpeed by setting("Open Speed", 0.5, 0.1..1.0, 0.01)
2828
val closeSpeed by setting("Close Speed", 0.5, 0.1..1.0, 0.01)
29+
val scrollSpeed by setting("Scroll Speed", 1.0, 0.1..10.0, 0.01)
2930

3031
// Alignment
3132
val allowHAlign by setting("Allow H Docking", false)

0 commit comments

Comments
 (0)