diff --git a/src/main/kotlin/net/shadowfacts/cacao/view/button/AbstractButton.kt b/src/main/kotlin/net/shadowfacts/cacao/view/button/AbstractButton.kt index 73875fc..dfaae55 100644 --- a/src/main/kotlin/net/shadowfacts/cacao/view/button/AbstractButton.kt +++ b/src/main/kotlin/net/shadowfacts/cacao/view/button/AbstractButton.kt @@ -46,18 +46,33 @@ abstract class AbstractButton>(val content: View, val * unless the background view is not fully opaque. */ 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. * If `null`, the normal [background] will be used. * @see background */ 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]. * If `null`, the normal [background] will be used. * @see background */ 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. @@ -74,19 +89,25 @@ abstract class AbstractButton>(val content: View, val content.rightAnchor.greaterThanOrEqualTo(rightAnchor - padding, WEAK) content.topAnchor.lessThanOrEqualTo(topAnchor + padding, WEAK) content.bottomAnchor.greaterThanOrEqualTo(bottomAnchor - padding, WEAK) - - listOfNotNull(background, hoveredBackground, disabledBackground).forEach { - addSubview(it) - it.leftAnchor equalTo leftAnchor - it.rightAnchor equalTo rightAnchor - it.topAnchor equalTo topAnchor - it.bottomAnchor equalTo bottomAnchor - } } + listOfNotNull(background, hoveredBackground, disabledBackground).forEach(::addBackground) + 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) { RenderHelper.pushMatrix() RenderHelper.translate(floor(frame.left), floor(frame.top))