From 7195a672db58f99f69a1ed6149aceb2568df0f92 Mon Sep 17 00:00:00 2001 From: Maik Mursall Date: Wed, 15 Nov 2023 23:37:25 +0100 Subject: [PATCH 1/6] Improved UIButton.updatedUIButton logic to decide which background color to use --- korge/src/common/korlibs/korge/ui/UIButton.kt | 62 ++++--------------- 1 file changed, 13 insertions(+), 49 deletions(-) diff --git a/korge/src/common/korlibs/korge/ui/UIButton.kt b/korge/src/common/korlibs/korge/ui/UIButton.kt index 4371f473cb..c95be92a9c 100644 --- a/korge/src/common/korlibs/korge/ui/UIButton.kt +++ b/korge/src/common/korlibs/korge/ui/UIButton.kt @@ -257,59 +257,23 @@ open class UIButton( } open fun updatedUIButton(down: Boolean? = null, over: Boolean? = null, pos: Point = Point.ZERO, immediate: Boolean = false) { - val button = this - if (!button.enabled) { - //button.animStateManager.set( - // AnimState( - // button::bgcolor[button.bgColorDisabled] - //)) - //button.animatorEffects.cancel() - background.bgColor = button.bgColorDisabled - button.invalidateRender() - return + val bgColor = when { + !enabled -> bgColorDisabled + over ?: false -> bgColorOver + selected -> bgColorSelected + else -> bgColorOut } - //println("UPDATED: down=$down, over=$over, px=$px, py=$py") + + if (immediate) { + background.bgColor = bgColor + } else { + animator.tween(background::bgColor[bgColor], time = 0.25.seconds) + } + if (down == true) { - //button.animStateManager.set( - // AnimState( - // button::highlightRadius[0.0, 1.0], - // button::highlightAlpha[1.0], - // button::highlightPos[Point(px / button.width, py / button.height), Point(px / button.width, py / button.height)], - // )) background.addHighlight(pos) - /* - button.highlightPos.setTo(px / button.width, py / button.height) - button.animatorEffects.tween( - button::highlightRadius[0.0, 1.0], - button::highlightColor[Colors.WHITE.withAd(0.5), Colors.WHITE.withAd(0.5)], - time = 0.5.seconds, easing = Easing.EASE_IN - ) - */ - } - if (down == false) { - //button.animStateManager.set( - // AnimState(button::highlightAlpha[0.0]) - //) + } else { background.removeHighlights() - //button.animatorEffects.tween(button::highlightColor[Colors.TRANSPARENT_BLACK], time = 0.2.seconds) - } - if (over != null) { - val bgcolor = when { - !button.enabled -> button.bgColorDisabled - over -> button.bgColorOver - selected -> button.bgColorSelected - else -> button.bgColorOut - } - //button.animStateManager.set( - // AnimState( - // button::bgcolor[bgcolor] - // ) - //) - if (immediate) { - background.bgColor = bgcolor - } else { - button.animator.tween(background::bgColor[bgcolor], time = 0.25.seconds) - } } } From b0bc4633737e6373c20695a5a616417243cbe608 Mon Sep 17 00:00:00 2001 From: Maik Mursall Date: Thu, 16 Nov 2023 18:46:48 +0100 Subject: [PATCH 2/6] disabled background color now fades in + improved selection logic --- korge/src/common/korlibs/korge/ui/UIButton.kt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/korge/src/common/korlibs/korge/ui/UIButton.kt b/korge/src/common/korlibs/korge/ui/UIButton.kt index c95be92a9c..7da4f5400c 100644 --- a/korge/src/common/korlibs/korge/ui/UIButton.kt +++ b/korge/src/common/korlibs/korge/ui/UIButton.kt @@ -99,6 +99,13 @@ open class UIButton( setInitialState() } + override var enabled: Boolean + get() = super.enabled + set(value) { + mouseEnabled = value + updatedUIButton(enabled = value) + } + //protected val rect: NinePatchEx = ninePatch(null, width, height) //protected val background = roundRect( // width, height, radiusWidth(width), radiusHeight(height), bgColorOut) @@ -256,10 +263,10 @@ open class UIButton( touch.simulateTapAt(views, localToGlobal(Point(width * 0.5f, height * 0.5f))) } - open fun updatedUIButton(down: Boolean? = null, over: Boolean? = null, pos: Point = Point.ZERO, immediate: Boolean = false) { + open fun updatedUIButton(down: Boolean? = null, over: Boolean? = null, enabled: Boolean? = null, pos: Point = Point.ZERO, immediate: Boolean = false) { val bgColor = when { - !enabled -> bgColorDisabled - over ?: false -> bgColorOver + enabled == false -> bgColorDisabled + over == true -> bgColorOver selected -> bgColorSelected else -> bgColorOut } From 496e503a3e96b6129eb1d164b498b60d51398948 Mon Sep 17 00:00:00 2001 From: Maik Mursall Date: Thu, 16 Nov 2023 18:47:27 +0100 Subject: [PATCH 3/6] Highlight now persists when pressing down and stop hovering --- korge/src/common/korlibs/korge/ui/UIButton.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/korge/src/common/korlibs/korge/ui/UIButton.kt b/korge/src/common/korlibs/korge/ui/UIButton.kt index 7da4f5400c..49f1ceb208 100644 --- a/korge/src/common/korlibs/korge/ui/UIButton.kt +++ b/korge/src/common/korlibs/korge/ui/UIButton.kt @@ -279,7 +279,7 @@ open class UIButton( if (down == true) { background.addHighlight(pos) - } else { + } else if (down == false) { background.removeHighlights() } } From 63527d4920411b9636f0b3292024f27e15824419 Mon Sep 17 00:00:00 2001 From: Maik Mursall Date: Thu, 16 Nov 2023 18:56:29 +0100 Subject: [PATCH 4/6] Now only updates button if enabled get actually changed --- korge/src/common/korlibs/korge/ui/UIButton.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/korge/src/common/korlibs/korge/ui/UIButton.kt b/korge/src/common/korlibs/korge/ui/UIButton.kt index 49f1ceb208..73e9e71445 100644 --- a/korge/src/common/korlibs/korge/ui/UIButton.kt +++ b/korge/src/common/korlibs/korge/ui/UIButton.kt @@ -102,8 +102,10 @@ open class UIButton( override var enabled: Boolean get() = super.enabled set(value) { + if (value != mouseEnabled) + updatedUIButton(enabled = value) + mouseEnabled = value - updatedUIButton(enabled = value) } //protected val rect: NinePatchEx = ninePatch(null, width, height) From 907baa66d51baa23f3f975c2ec923f9e7bf0a889 Mon Sep 17 00:00:00 2001 From: Maik Mursall Date: Thu, 16 Nov 2023 20:09:03 +0100 Subject: [PATCH 5/6] Improved updatedUIButton logic some more --- korge/src/common/korlibs/korge/ui/UIButton.kt | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/korge/src/common/korlibs/korge/ui/UIButton.kt b/korge/src/common/korlibs/korge/ui/UIButton.kt index 73e9e71445..e6b7bb22bd 100644 --- a/korge/src/common/korlibs/korge/ui/UIButton.kt +++ b/korge/src/common/korlibs/korge/ui/UIButton.kt @@ -102,10 +102,11 @@ open class UIButton( override var enabled: Boolean get() = super.enabled set(value) { - if (value != mouseEnabled) - updatedUIButton(enabled = value) - + val valueChanged = value != mouseEnabled mouseEnabled = value + + if (valueChanged) + updatedUIButton(enable = value) } //protected val rect: NinePatchEx = ninePatch(null, width, height) @@ -265,9 +266,17 @@ open class UIButton( touch.simulateTapAt(views, localToGlobal(Point(width * 0.5f, height * 0.5f))) } - open fun updatedUIButton(down: Boolean? = null, over: Boolean? = null, enabled: Boolean? = null, pos: Point = Point.ZERO, immediate: Boolean = false) { + open fun updatedUIButton(down: Boolean? = null, over: Boolean? = null, enable: Boolean? = null, pos: Point = Point.ZERO, immediate: Boolean = false) { + if (enabled && down != null) { + if (down) { + background.addHighlight(pos) + } else { + background.removeHighlights() + } + } + val bgColor = when { - enabled == false -> bgColorDisabled + enable == false || !enabled -> bgColorDisabled over == true -> bgColorOver selected -> bgColorSelected else -> bgColorOut @@ -278,12 +287,6 @@ open class UIButton( } else { animator.tween(background::bgColor[bgColor], time = 0.25.seconds) } - - if (down == true) { - background.addHighlight(pos) - } else if (down == false) { - background.removeHighlights() - } } var selected: Boolean = false From 90754fb2bc7ea8d6778ba797c1f793e713d6540c Mon Sep 17 00:00:00 2001 From: Maik Mursall Date: Sun, 19 Nov 2023 14:30:18 +0100 Subject: [PATCH 6/6] Simplified updating behaviour for UIButton.enabled --- korge/src/common/korlibs/korge/ui/UIButton.kt | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/korge/src/common/korlibs/korge/ui/UIButton.kt b/korge/src/common/korlibs/korge/ui/UIButton.kt index e6b7bb22bd..4b14997560 100644 --- a/korge/src/common/korlibs/korge/ui/UIButton.kt +++ b/korge/src/common/korlibs/korge/ui/UIButton.kt @@ -102,11 +102,10 @@ open class UIButton( override var enabled: Boolean get() = super.enabled set(value) { - val valueChanged = value != mouseEnabled - mouseEnabled = value + if (value == mouseEnabled) return - if (valueChanged) - updatedUIButton(enable = value) + mouseEnabled = value + updatedUIButton(enable = value) } //protected val rect: NinePatchEx = ninePatch(null, width, height)