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

Fix UIButton background color not updating properly #2032

Merged
merged 7 commits into from
Nov 24, 2023
79 changes: 27 additions & 52 deletions korge/src/common/korlibs/korge/ui/UIButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ open class UIButton(
setInitialState()
}

override var enabled: Boolean
get() = super.enabled
set(value) {
if (value == mouseEnabled) return

mouseEnabled = value
updatedUIButton(enable = value)
}

//protected val rect: NinePatchEx = ninePatch(null, width, height)
//protected val background = roundRect(
// width, height, radiusWidth(width), radiusHeight(height), bgColorOut)
Expand Down Expand Up @@ -256,61 +265,27 @@ 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) {
val button = this
if (!button.enabled) {
//button.animStateManager.set(
// AnimState(
// button::bgcolor[button.bgColorDisabled]
//))
//button.animatorEffects.cancel()
background.bgColor = button.bgColorDisabled
button.invalidateRender()
return
}
//println("UPDATED: down=$down, over=$over, px=$px, py=$py")
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])
//)
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
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 {
button.animator.tween(background::bgColor[bgcolor], time = 0.25.seconds)
background.removeHighlights()
}
}

val bgColor = when {
enable == false || !enabled -> bgColorDisabled
over == true -> bgColorOver
selected -> bgColorSelected
else -> bgColorOut
}

if (immediate) {
background.bgColor = bgColor
} else {
animator.tween(background::bgColor[bgColor], time = 0.25.seconds)
}
}

var selected: Boolean = false
Expand Down