Add default button backgrounds and click sound
This commit is contained in:
parent
c79b93a4f9
commit
4070baaa63
|
@ -39,7 +39,6 @@ class TestCacaoScreen: CacaoScreen() {
|
||||||
handler = {
|
handler = {
|
||||||
println("$it clicked!")
|
println("$it clicked!")
|
||||||
}
|
}
|
||||||
background = NinePatchView(buttonNinePatch)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
solver.dsl {
|
solver.dsl {
|
||||||
|
|
|
@ -3,6 +3,8 @@ package net.shadowfacts.cacao.util
|
||||||
import com.mojang.blaze3d.platform.GlStateManager
|
import com.mojang.blaze3d.platform.GlStateManager
|
||||||
import net.minecraft.client.MinecraftClient
|
import net.minecraft.client.MinecraftClient
|
||||||
import net.minecraft.client.gui.DrawableHelper
|
import net.minecraft.client.gui.DrawableHelper
|
||||||
|
import net.minecraft.client.sound.PositionedSoundInstance
|
||||||
|
import net.minecraft.sound.SoundEvent
|
||||||
import net.shadowfacts.cacao.geometry.Rect
|
import net.shadowfacts.cacao.geometry.Rect
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -15,6 +17,12 @@ object RenderHelper {
|
||||||
|
|
||||||
private val disabled = (System.getProperty("cacao.drawing.disabled") ?: "false").toBoolean()
|
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].
|
* Draws a solid [rect] filled with the given [color].
|
||||||
*/
|
*/
|
||||||
|
@ -65,6 +73,9 @@ object RenderHelper {
|
||||||
GlStateManager.translated(x, y, z)
|
GlStateManager.translated(x, y, z)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see org.lwjgl.opengl.GL11.glColor4f
|
||||||
|
*/
|
||||||
fun color(r: Float, g: Float, b: Float, alpha: Float) {
|
fun color(r: Float, g: Float, b: Float, alpha: Float) {
|
||||||
if (disabled) return
|
if (disabled) return
|
||||||
GlStateManager.color4f(r, g, b, alpha)
|
GlStateManager.color4f(r, g, b, alpha)
|
||||||
|
|
|
@ -1,8 +1,13 @@
|
||||||
package net.shadowfacts.cacao.view.button
|
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.geometry.Point
|
||||||
import net.shadowfacts.cacao.util.MouseButton
|
import net.shadowfacts.cacao.util.MouseButton
|
||||||
|
import net.shadowfacts.cacao.util.NinePatchTexture
|
||||||
import net.shadowfacts.cacao.util.RenderHelper
|
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.cacao.view.View
|
||||||
import net.shadowfacts.kiwidsl.dsl
|
import net.shadowfacts.kiwidsl.dsl
|
||||||
|
|
||||||
|
@ -19,6 +24,12 @@ import net.shadowfacts.kiwidsl.dsl
|
||||||
*/
|
*/
|
||||||
abstract class AbstractButton<Impl: AbstractButton<Impl>>(val content: View, val padding: Double = 4.0): View() {
|
abstract class AbstractButton<Impl: AbstractButton<Impl>>(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 function that handles when this button is clicked.
|
||||||
* The parameter is the type of the concrete button implementation that was used.
|
* The parameter is the type of the concrete button implementation that was used.
|
||||||
|
@ -40,19 +51,24 @@ abstract class AbstractButton<Impl: AbstractButton<Impl>>(val content: View, val
|
||||||
* If a [backgroundColor] is specified, it will be drawn behind the background View and thus not visible
|
* 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.
|
* 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.
|
* 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? = null
|
var hoveredBackground: View? = NinePatchView(HOVERED_BG)
|
||||||
/**
|
/**
|
||||||
* 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? = 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() {
|
override fun wasAdded() {
|
||||||
solver.dsl {
|
solver.dsl {
|
||||||
|
@ -109,6 +125,10 @@ abstract class AbstractButton<Impl: AbstractButton<Impl>>(val content: View, val
|
||||||
// For example, an implementing class may be defined as such: `class Button: AbstractButton<Button>`
|
// For example, an implementing class may be defined as such: `class Button: AbstractButton<Button>`
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
handler(this as Impl)
|
handler(this as Impl)
|
||||||
|
|
||||||
|
if (clickSoundEnabled) {
|
||||||
|
RenderHelper.playSound(SoundEvents.UI_BUTTON_CLICK)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue