Cacao: Fix changing button backgrounds after adding button to superview

This commit is contained in:
Shadowfacts 2021-03-28 13:40:56 -04:00
parent 4451dab503
commit 34b414aa00
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 29 additions and 8 deletions

View File

@ -46,18 +46,33 @@ abstract class AbstractButton<Impl: AbstractButton<Impl>>(val content: View, val
* unless the background view is not fully opaque. * unless the background view is not fully opaque.
*/ */
var background: View? = NinePatchView(NinePatchTexture.BUTTON_BG) var background: View? = NinePatchView(NinePatchTexture.BUTTON_BG)
set(value) {
field?.removeFromSuperview()
field = value
value?.also(::addBackground)
}
/** /**
* The background to draw when the button is hovered over by the mouse. * The background to draw when the button is hovered over by the mouse.
* If `null`, the normal [background] will be used. * If `null`, the normal [background] will be used.
* @see background * @see background
*/ */
var hoveredBackground: View? = NinePatchView(NinePatchTexture.BUTTON_HOVERED_BG) var hoveredBackground: View? = NinePatchView(NinePatchTexture.BUTTON_HOVERED_BG)
set(value) {
field?.removeFromSuperview()
field = value
value?.also(::addBackground)
}
/** /**
* The background to draw when the button is [disabled]. * The background to draw when the button is [disabled].
* If `null`, the normal [background] will be used. * If `null`, the normal [background] will be used.
* @see background * @see background
*/ */
var disabledBackground: View? = NinePatchView(NinePatchTexture.BUTTON_DISABLED_BG) var disabledBackground: View? = NinePatchView(NinePatchTexture.BUTTON_DISABLED_BG)
set(value) {
field?.removeFromSuperview()
field = value
value?.also(::addBackground)
}
/** /**
* The tooltip text shown when this button is hovered. * The tooltip text shown when this button is hovered.
@ -74,19 +89,25 @@ abstract class AbstractButton<Impl: AbstractButton<Impl>>(val content: View, val
content.rightAnchor.greaterThanOrEqualTo(rightAnchor - padding, WEAK) content.rightAnchor.greaterThanOrEqualTo(rightAnchor - padding, WEAK)
content.topAnchor.lessThanOrEqualTo(topAnchor + padding, WEAK) content.topAnchor.lessThanOrEqualTo(topAnchor + padding, WEAK)
content.bottomAnchor.greaterThanOrEqualTo(bottomAnchor - padding, WEAK) content.bottomAnchor.greaterThanOrEqualTo(bottomAnchor - padding, WEAK)
}
listOfNotNull(background, hoveredBackground, disabledBackground).forEach { listOfNotNull(background, hoveredBackground, disabledBackground).forEach(::addBackground)
addSubview(it)
it.leftAnchor equalTo leftAnchor
it.rightAnchor equalTo rightAnchor
it.topAnchor equalTo topAnchor
it.bottomAnchor equalTo bottomAnchor
}
}
super.wasAdded() super.wasAdded()
} }
private fun addBackground(view: View) {
if (superview != null && hasSolver) {
addSubview(view)
solver.dsl {
view.leftAnchor equalTo leftAnchor
view.rightAnchor equalTo rightAnchor
view.topAnchor equalTo topAnchor
view.bottomAnchor equalTo bottomAnchor
}
}
}
override fun draw(matrixStack: MatrixStack, mouse: Point, delta: Float) { override fun draw(matrixStack: MatrixStack, mouse: Point, delta: Float) {
RenderHelper.pushMatrix() RenderHelper.pushMatrix()
RenderHelper.translate(floor(frame.left), floor(frame.top)) RenderHelper.translate(floor(frame.left), floor(frame.top))