From 4070baaa637e1accfe6117de857dc337400d0b91 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 23 Jun 2019 21:22:54 -0400 Subject: [PATCH] Add default button backgrounds and click sound --- .../net/shadowfacts/asmr/TestCacaoScreen.kt | 1 - .../shadowfacts/cacao/util/RenderHelper.kt | 11 ++++++++ .../cacao/view/button/AbstractButton.kt | 26 ++++++++++++++++--- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/net/shadowfacts/asmr/TestCacaoScreen.kt b/src/main/kotlin/net/shadowfacts/asmr/TestCacaoScreen.kt index 3db231c..6f9ae7a 100644 --- a/src/main/kotlin/net/shadowfacts/asmr/TestCacaoScreen.kt +++ b/src/main/kotlin/net/shadowfacts/asmr/TestCacaoScreen.kt @@ -39,7 +39,6 @@ class TestCacaoScreen: CacaoScreen() { handler = { println("$it clicked!") } - background = NinePatchView(buttonNinePatch) }) solver.dsl { diff --git a/src/main/kotlin/net/shadowfacts/cacao/util/RenderHelper.kt b/src/main/kotlin/net/shadowfacts/cacao/util/RenderHelper.kt index 83e13a0..c2c77c9 100644 --- a/src/main/kotlin/net/shadowfacts/cacao/util/RenderHelper.kt +++ b/src/main/kotlin/net/shadowfacts/cacao/util/RenderHelper.kt @@ -3,6 +3,8 @@ package net.shadowfacts.cacao.util import com.mojang.blaze3d.platform.GlStateManager import net.minecraft.client.MinecraftClient import net.minecraft.client.gui.DrawableHelper +import net.minecraft.client.sound.PositionedSoundInstance +import net.minecraft.sound.SoundEvent import net.shadowfacts.cacao.geometry.Rect /** @@ -15,6 +17,12 @@ object RenderHelper { private val disabled = (System.getProperty("cacao.drawing.disabled") ?: "false").toBoolean() + // TODO: find a better place for this + fun playSound(event: SoundEvent) { + if (disabled) return + MinecraftClient.getInstance().soundManager.play(PositionedSoundInstance.master(event, 1f)) + } + /** * Draws a solid [rect] filled with the given [color]. */ @@ -65,6 +73,9 @@ object RenderHelper { GlStateManager.translated(x, y, z) } + /** + * @see org.lwjgl.opengl.GL11.glColor4f + */ fun color(r: Float, g: Float, b: Float, alpha: Float) { if (disabled) return GlStateManager.color4f(r, g, b, alpha) 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 0a0f102..6abab16 100644 --- a/src/main/kotlin/net/shadowfacts/cacao/view/button/AbstractButton.kt +++ b/src/main/kotlin/net/shadowfacts/cacao/view/button/AbstractButton.kt @@ -1,8 +1,13 @@ package net.shadowfacts.cacao.view.button +import net.minecraft.sound.SoundEvents +import net.minecraft.util.Identifier import net.shadowfacts.cacao.geometry.Point import net.shadowfacts.cacao.util.MouseButton +import net.shadowfacts.cacao.util.NinePatchTexture import net.shadowfacts.cacao.util.RenderHelper +import net.shadowfacts.cacao.util.Texture +import net.shadowfacts.cacao.view.NinePatchView import net.shadowfacts.cacao.view.View import net.shadowfacts.kiwidsl.dsl @@ -19,6 +24,12 @@ import net.shadowfacts.kiwidsl.dsl */ abstract class AbstractButton>(val content: View, val padding: Double = 4.0): View() { + companion object { + val DEFAULT_BG = NinePatchTexture(Texture(Identifier("textures/gui/widgets.png"), 0, 66), 3, 3, 194, 14) + val HOVERED_BG = NinePatchTexture(Texture(Identifier("textures/gui/widgets.png"), 0, 86), 3, 3, 194, 14) + val DISABLED_BG = NinePatchTexture(Texture(Identifier("textures/gui/widgets.png"), 0, 46), 3, 3, 194, 14) + } + /** * The function that handles when this button is clicked. * The parameter is the type of the concrete button implementation that was used. @@ -40,19 +51,24 @@ abstract class AbstractButton>(val content: View, val * If a [backgroundColor] is specified, it will be drawn behind the background View and thus not visible * unless the background view is not fully opaque. */ - var background: View? = null + var background: View? = NinePatchView(DEFAULT_BG) /** * 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? = null + var hoveredBackground: View? = NinePatchView(HOVERED_BG) /** * The background to draw when the button is [disabled]. * If `null`, the normal [background] will be used. * @see background */ - var disabledBackground: View? = null + var disabledBackground: View? = NinePatchView(DISABLED_BG) + + /** + * If the button will play the Minecraft button click sound when clicked. + */ + var clickSoundEnabled = true override fun wasAdded() { solver.dsl { @@ -109,6 +125,10 @@ abstract class AbstractButton>(val content: View, val // For example, an implementing class may be defined as such: `class Button: AbstractButton