Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Window: Avoid processing input events if cancelDrawing=true #96

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions api/Elementa.api
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/gg/essential/elementa/ElementaVersion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,

;

/**
Expand Down Expand Up @@ -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
Expand Down
23 changes: 20 additions & 3 deletions src/main/kotlin/gg/essential/elementa/components/Window.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -58,7 +59,7 @@ class Window @JvmOverloads constructor(
version.enableFor { doDraw(matrixStack) }

private fun doDraw(matrixStack: UMatrixStack) {
if (cancelDrawing)
if (hasErrored)
return

requireMainThread()
Expand Down Expand Up @@ -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 ?: "<unknown>"
when (e) {
Expand Down Expand Up @@ -170,6 +171,10 @@ class Window @JvmOverloads constructor(
}

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

requireMainThread()

val (mouseX, mouseY) = getMousePosition()
Expand All @@ -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
Expand Down Expand Up @@ -229,6 +238,10 @@ class Window @JvmOverloads constructor(
}

override fun mouseRelease() {
if (hasErrored && version >= ElementaVersion.v7) {
return
}

requireMainThread()

super.mouseRelease()
Expand All @@ -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
Expand Down