From 45ebed376101656f947a1f7286a2cd940e8c9e77 Mon Sep 17 00:00:00 2001 From: Mitchell Katz Date: Thu, 26 Sep 2024 07:20:35 -0400 Subject: [PATCH 1/2] Window: Do not process input events when hasErrored is true Otherwise, it is possible that a Window allows clicks or key types to trigger when it is invisible GitHub: #96 Linear: EM-1157 Co-authored-by: Callum Bugajski <11320476+CallumBugajski@users.noreply.github.com> --- api/Elementa.api | 2 ++ .../gg/essential/elementa/ElementaVersion.kt | 8 +++++++ .../essential/elementa/components/Window.kt | 23 ++++++++++++++++--- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/api/Elementa.api b/api/Elementa.api index c079fd6d..6b82ab59 100644 --- a/api/Elementa.api +++ b/api/Elementa.api @@ -7,6 +7,7 @@ public final class gg/essential/elementa/ElementaVersion : java/lang/Enum { public static final field V4 Lgg/essential/elementa/ElementaVersion; public static final field V5 Lgg/essential/elementa/ElementaVersion; public static final field V6 Lgg/essential/elementa/ElementaVersion; + public static final field V7 Lgg/essential/elementa/ElementaVersion; public final fun enableFor (Lkotlin/jvm/functions/Function0;)Ljava/lang/Object; public static fun valueOf (Ljava/lang/String;)Lgg/essential/elementa/ElementaVersion; public static fun values ()[Lgg/essential/elementa/ElementaVersion; @@ -753,6 +754,7 @@ public final class gg/essential/elementa/components/Window : gg/essential/elemen public final fun getAnimationFPS ()I public fun getBottom ()F public final fun getFocusedComponent ()Lgg/essential/elementa/UIComponent; + public final fun getHasErrored ()Z public fun getHeight ()F public final fun getHoveredFloatingComponent ()Lgg/essential/elementa/UIComponent; public fun getLeft ()F diff --git a/src/main/kotlin/gg/essential/elementa/ElementaVersion.kt b/src/main/kotlin/gg/essential/elementa/ElementaVersion.kt index 3077ccef..e63c2687 100644 --- a/src/main/kotlin/gg/essential/elementa/ElementaVersion.kt +++ b/src/main/kotlin/gg/essential/elementa/ElementaVersion.kt @@ -90,8 +90,14 @@ enum class ElementaVersion { /** * [gg.essential.elementa.components.ScrollComponent] now has a minimum size for scrollbar grips. */ + @Deprecated(DEPRECATION_MESSAGE) V6, + /** + * [gg.essential.elementa.components.Window] now disables input events if an error has occurred during drawing. + */ + V7, + ; /** @@ -134,7 +140,9 @@ Be sure to read through all the changes between your current version and your ne internal val v4 = V4 @Suppress("DEPRECATION") internal val v5 = V5 + @Suppress("DEPRECATION") internal val v6 = V6 + internal val v7 = V7 @PublishedApi diff --git a/src/main/kotlin/gg/essential/elementa/components/Window.kt b/src/main/kotlin/gg/essential/elementa/components/Window.kt index 5dc9a471..a71b8a9b 100644 --- a/src/main/kotlin/gg/essential/elementa/components/Window.kt +++ b/src/main/kotlin/gg/essential/elementa/components/Window.kt @@ -32,7 +32,8 @@ class Window @JvmOverloads constructor( private set private var componentRequestingFocus: UIComponent? = null - private var cancelDrawing = false + var hasErrored = false + private set internal var clickInterceptor: ((mouseX: Double, mouseY: Double, button: Int) -> Boolean)? = null @@ -58,7 +59,7 @@ class Window @JvmOverloads constructor( version.enableFor { doDraw(matrixStack) } private fun doDraw(matrixStack: UMatrixStack) { - if (cancelDrawing) + if (hasErrored) return requireMainThread() @@ -108,7 +109,7 @@ class Window @JvmOverloads constructor( beforeDraw(matrixStack) super.draw(matrixStack) } catch (e: Throwable) { - cancelDrawing = true + hasErrored = true val guiName = UMinecraft.currentScreenObj?.javaClass?.simpleName ?: "" when (e) { @@ -170,6 +171,10 @@ class Window @JvmOverloads constructor( } override fun mouseScroll(delta: Double) { + if (hasErrored && version >= ElementaVersion.v7) { + return + } + requireMainThread() val (mouseX, mouseY) = getMousePosition() @@ -184,6 +189,10 @@ class Window @JvmOverloads constructor( } override fun mouseClick(mouseX: Double, mouseY: Double, button: Int) { + if (hasErrored && version >= ElementaVersion.v7) { + return + } + requireMainThread() // Override mouse positions to be in the center of the pixel on Elementa versions @@ -229,6 +238,10 @@ class Window @JvmOverloads constructor( } override fun mouseRelease() { + if (hasErrored && version >= ElementaVersion.v7) { + return + } + requireMainThread() super.mouseRelease() @@ -237,6 +250,10 @@ class Window @JvmOverloads constructor( } override fun keyType(typedChar: Char, keyCode: Int) { + if (hasErrored && version >= ElementaVersion.v7) { + return + } + requireMainThread() // If the typed character is in a PUA (https://en.wikipedia.org/wiki/Private_Use_Areas), we don't want to From 9308d17016b9297c25a5dd5679b2a1325af70232 Mon Sep 17 00:00:00 2001 From: Marius Metzger Date: Wed, 20 Nov 2024 14:31:25 +0100 Subject: [PATCH 2/2] Fix link to Example class in README (#152) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 436d2604..7dcd3394 100644 --- a/README.md +++ b/README.md @@ -247,7 +247,7 @@ you MUST call Window#mouseClick. This is also all handled by Elementa's WindowSc ## All together This is a basic excerpt of code from an Elementa GUI. To see a more fleshed out -example, look to the [ExampleGui class](src/main/java/com/example/examplemod/ExampleGui.kt). +example, look to the [ExampleGui class](example/common/src/main/kotlin/com/example/examplemod/ExampleGui.kt). ```kotlin val window = Window()