Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions api/Elementa.api
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ public final class gg/essential/elementa/ElementaVersion : java/lang/Enum {
public static final field V0 Lgg/essential/elementa/ElementaVersion;
public static final field V1 Lgg/essential/elementa/ElementaVersion;
public static final field V10 Lgg/essential/elementa/ElementaVersion;
public static final field V11 Lgg/essential/elementa/ElementaVersion;
public static final field V2 Lgg/essential/elementa/ElementaVersion;
public static final field V3 Lgg/essential/elementa/ElementaVersion;
public static final field V4 Lgg/essential/elementa/ElementaVersion;
Expand Down Expand Up @@ -118,6 +119,7 @@ public abstract class gg/essential/elementa/UIComponent : java/util/Observable,
public fun mouseMove (Lgg/essential/elementa/components/Window;)V
public fun mouseRelease ()V
public fun mouseScroll (D)V
public fun mouseScroll (DD)V
public final fun onFocus (Lkotlin/jvm/functions/Function1;)Lgg/essential/elementa/UIComponent;
public final fun onFocusLost (Lkotlin/jvm/functions/Function1;)Lgg/essential/elementa/UIComponent;
public final fun onKeyType (Lkotlin/jvm/functions/Function3;)Lgg/essential/elementa/UIComponent;
Expand Down Expand Up @@ -273,6 +275,7 @@ public abstract class gg/essential/elementa/WindowScreen : gg/essential/universa
public fun onMouseClicked (DDI)V
public fun onMouseReleased (DDI)V
public fun onMouseScrolled (D)V
public fun onMouseScrolled (DDDD)V
public fun onScreenClose ()V
public final fun stopAnimating (Lkotlin/reflect/KMutableProperty0;)V
}
Expand Down Expand Up @@ -776,6 +779,7 @@ public final class gg/essential/elementa/components/Window : gg/essential/elemen
public fun mouseClick (DDI)V
public fun mouseRelease ()V
public fun mouseScroll (D)V
public fun mouseScroll (DD)V
public final fun removeFloatingComponent (Lgg/essential/elementa/UIComponent;)V
public final fun setHoveredFloatingComponent (Lgg/essential/elementa/UIComponent;)V
public final fun unfocus ()V
Expand Down Expand Up @@ -2747,15 +2751,22 @@ public abstract class gg/essential/elementa/events/UIEvent {
}

public final class gg/essential/elementa/events/UIScrollEvent : gg/essential/elementa/events/UIEvent {
public fun <init> (DDLgg/essential/elementa/UIComponent;Lgg/essential/elementa/UIComponent;)V
public fun <init> (DLgg/essential/elementa/UIComponent;Lgg/essential/elementa/UIComponent;)V
public fun <init> (DLgg/essential/elementa/UIComponent;Lgg/essential/elementa/UIComponent;D)V
public final fun component1 ()D
public final fun component2 ()Lgg/essential/elementa/UIComponent;
public final fun component3 ()Lgg/essential/elementa/UIComponent;
public final fun component4 ()D
public final fun copy (DLgg/essential/elementa/UIComponent;Lgg/essential/elementa/UIComponent;)Lgg/essential/elementa/events/UIScrollEvent;
public final fun copy (DLgg/essential/elementa/UIComponent;Lgg/essential/elementa/UIComponent;D)Lgg/essential/elementa/events/UIScrollEvent;
public static synthetic fun copy$default (Lgg/essential/elementa/events/UIScrollEvent;DLgg/essential/elementa/UIComponent;Lgg/essential/elementa/UIComponent;DILjava/lang/Object;)Lgg/essential/elementa/events/UIScrollEvent;
public static synthetic fun copy$default (Lgg/essential/elementa/events/UIScrollEvent;DLgg/essential/elementa/UIComponent;Lgg/essential/elementa/UIComponent;ILjava/lang/Object;)Lgg/essential/elementa/events/UIScrollEvent;
public fun equals (Ljava/lang/Object;)Z
public final fun getCurrentTarget ()Lgg/essential/elementa/UIComponent;
public final fun getDelta ()D
public final fun getScrollX ()D
public final fun getScrollY ()D
public final fun getTarget ()Lgg/essential/elementa/UIComponent;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
kotlin = "1.9.25"
kotlinx-coroutines = "1.5.2"
jetbrains-annotations = "23.0.0"
universalcraft = "431"
universalcraft = "466"
commonmark = "0.17.1"
dom4j = "2.1.1"

Expand Down
21 changes: 20 additions & 1 deletion src/main/kotlin/gg/essential/elementa/ElementaVersion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,23 @@ enum class ElementaVersion {
* This fixes the alpha channel of the render result, allowing it to be correctly composited with other textures.
* See [UniversalCraft#105](https://github.com/EssentialGG/UniversalCraft/pull/105) for more details.
*/
@Deprecated(DEPRECATION_MESSAGE)
V10,

/**
* Improvements for vertical scrolling and support for horizontal scrolling.
*
* Adds new mouseScroll() function to UIComponent that also supports horizontal scrolling.
* The old function is no longer supported and will no longer be called.
* Previously, deltas were always coerced between -1.0 and 1.0 in WindowScreen.
* This was wrong, and is no longer the case for the new function.
* UIScrollEvent now includes also the horizontal delta.
Comment thread
Johni0702 marked this conversation as resolved.
* ScrollComponent now also properly supports new horizontal scrolling.
*
* See [UniversalCraft#128](https://github.com/SparkUniverse/UniversalCraft/pull/128) for the underlying changes.
*/
V11,

;

/**
Expand Down Expand Up @@ -241,14 +256,18 @@ Be sure to read through all the changes between your current version and your ne
internal val v8 = V8
@Suppress("DEPRECATION")
internal val v9 = V9
@Suppress("DEPRECATION")
internal val v10 = V10
internal val v11 = V11

internal val atLeastV9Active: Boolean
get() = active >= v9
internal val atLeastV10Active: Boolean
get() = active >= v10
internal val atLeastV11Active: Boolean
get() = active >= v11

@PublishedApi
internal var active: ElementaVersion = v0
}
}
}
24 changes: 20 additions & 4 deletions src/main/kotlin/gg/essential/elementa/UIComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -730,23 +730,39 @@ abstract class UIComponent : Observable(), ReferenceHolder {
this.forEachChild { it.mouseRelease() }
}

@Suppress("DEPRECATION")
@Deprecated("Not called by WindowScreen in elementa v11 and above", ReplaceWith("mouseScroll(deltaHorizontal, deltaVertical)"))
open fun mouseScroll(delta: Double) {
if (delta == 0.0) return

for (i in children.lastIndex downTo 0) {
val child = children[i]

if (child.isHovered()) {
return child.mouseScroll(delta)
}
}

fireScrollEvent(UIScrollEvent(delta, this, this))
}

/**
* Runs the set [onMouseScroll] method for the component and it's children.
* Use this in the proper mouse scroll event to cascade all component's mouse scroll events.
* Most common use is on the [Window] object.
*/
open fun mouseScroll(delta: Double) {
if (delta == 0.0) return
open fun mouseScroll(scrollX: Double, scrollY: Double) {
if (scrollX == 0.0 && scrollY == 0.0) return

for (i in children.lastIndex downTo 0) {
val child = children[i]

if (child.isHovered()) {
return child.mouseScroll(delta)
return child.mouseScroll(scrollX, scrollY)
}
}

fireScrollEvent(UIScrollEvent(delta, this, this))
fireScrollEvent(UIScrollEvent(scrollX, scrollY, this, this))
}

open fun onWindowResize() {
Expand Down
19 changes: 17 additions & 2 deletions src/main/kotlin/gg/essential/elementa/WindowScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,26 @@ abstract class WindowScreen @JvmOverloads constructor(
window.mouseRelease()
}

@Suppress("DEPRECATION")
@Deprecated(
"Provided `delta` values have different units depending on Minecraft versions. See ElementaVersion.V11 for details.",
replaceWith = ReplaceWith("onMouseScrolled(mouseX, mouseY, deltaHorizontal, deltaVertical)")
)
override fun onMouseScrolled(delta: Double) {
super.onMouseScrolled(delta)

// We also need to pass along scrolling
window.mouseScroll(delta.coerceIn(-1.0, 1.0))
if (version < ElementaVersion.v11) {
// We also need to pass along scrolling
window.mouseScroll(delta.coerceIn(-1.0, 1.0))
}
}

override fun onMouseScrolled(mouseX: Double, mouseY: Double, deltaHorizontal: Double, deltaVertical: Double) {
super.onMouseScrolled(mouseX, mouseY, deltaHorizontal, deltaVertical)

if (version >= ElementaVersion.v11) {
window.mouseScroll(deltaHorizontal, deltaVertical)
}
}

override fun onKeyPressed(keyCode: Int, typedChar: Char, modifiers: UKeyboard.Modifiers?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gg.essential.elementa.components

import gg.essential.elementa.ElementaVersion
import gg.essential.elementa.UIComponent
import gg.essential.elementa.components.UpdateFunc
import gg.essential.elementa.constraints.*
import gg.essential.elementa.constraints.animation.Animations
import gg.essential.elementa.constraints.resolution.ConstraintVisitor
Expand Down Expand Up @@ -151,6 +150,11 @@ class ScrollComponent constructor(
it.stopPropagation()
}
}
if(horizontalScrollEnabled) {
if (onScroll(it.scrollX.toFloat(), isHorizontal = true) || !passthroughScroll) {
it.stopPropagation()
}
}
} else {
// old behavior
if (UKeyboard.isShiftKeyDown() && horizontalScrollEnabled) {
Expand Down Expand Up @@ -425,6 +429,7 @@ class ScrollComponent constructor(
* @return whether the offset changed
*/
private fun onScroll(delta: Float, isHorizontal: Boolean): Boolean {
if (delta == 0f) return false
var changed = false
val offset = if (isHorizontal) ::horizontalOffset else ::verticalOffset
val range = calculateOffsetRange(isHorizontal)
Expand Down
20 changes: 20 additions & 0 deletions src/main/kotlin/gg/essential/elementa/components/Window.kt
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ class Window @JvmOverloads constructor(
}
}

@Suppress("DEPRECATION")
@Deprecated("Not called in elementa v11 and above", ReplaceWith("mouseScroll(0.0, delta)"))
override fun mouseScroll(delta: Double) {
if (hasErrored && version >= ElementaVersion.v7) {
return
Expand All @@ -242,6 +244,24 @@ class Window @JvmOverloads constructor(
super.mouseScroll(delta)
}

override fun mouseScroll(scrollX: Double, scrollY: Double) {
if (hasErrored && version >= ElementaVersion.v7) {
return
}

requireMainThread()

val (mouseX, mouseY) = getMousePosition()
for (floatingComponent in allFloatingComponentsInReverseOrder()) {
if (floatingComponent.isPointInside(mouseX, mouseY)) {
floatingComponent.mouseScroll(scrollX, scrollY)
return
}
}

super.mouseScroll(scrollX, scrollY)
}

override fun mouseClick(mouseX: Double, mouseY: Double, button: Int) {
if (hasErrored && version >= ElementaVersion.v7) {
return
Expand Down
24 changes: 21 additions & 3 deletions src/main/kotlin/gg/essential/elementa/events/UIMouseEvents.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,25 @@ data class UIClickEvent(
}

data class UIScrollEvent(
val delta: Double,
val scrollY: Double,
val target: UIComponent,
val currentTarget: UIComponent
) : UIEvent()
val currentTarget: UIComponent,
val scrollX: Double, // only on Minecraft 1.20.2+ and ElementaVersion.V11+
) : UIEvent() {
constructor(scrollX: Double, scrollY: Double, target: UIComponent, currentTarget: UIComponent)
: this(scrollY, target, currentTarget, scrollX)

// Added to ensure backwards binary compatibility
constructor(delta: Double, target: UIComponent, currentTarget: UIComponent) : this(delta, target, currentTarget, 0.0)

// Added to ensure backwards binary compatibility
fun copy(
delta: Double = this.scrollY,
target: UIComponent = this.target,
currentTarget: UIComponent = this.currentTarget,
) = copy(scrollY = delta, target = target, currentTarget = currentTarget, scrollX = scrollX)

// Added to ensure backwards binary compatibility
val delta: Double
get() = scrollY
}
Loading