Cacao: Add button tooltip

This commit is contained in:
Shadowfacts 2021-03-28 13:40:23 -04:00
parent e8455bdb2f
commit 4451dab503
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 14 additions and 14 deletions

View File

@ -1,6 +1,7 @@
package net.shadowfacts.cacao.view.button
import net.minecraft.client.util.math.MatrixStack
import net.minecraft.text.Text
import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.util.MouseButton
import net.shadowfacts.cacao.util.texture.NinePatchTexture
@ -58,6 +59,11 @@ abstract class AbstractButton<Impl: AbstractButton<Impl>>(val content: View, val
*/
var disabledBackground: View? = NinePatchView(NinePatchTexture.BUTTON_DISABLED_BG)
/**
* The tooltip text shown when this button is hovered.
*/
var tooltip: Text? = null
override fun wasAdded() {
solver.dsl {
addSubview(content)
@ -97,6 +103,10 @@ abstract class AbstractButton<Impl: AbstractButton<Impl>>(val content: View, val
// don't draw subviews, otherwise all background views + content will get drawn
RenderHelper.popMatrix()
if (tooltip != null && mouse in bounds) {
window!!.drawTooltip(listOf(tooltip!!))
}
}
override fun mouseClicked(point: Point, mouseButton: MouseButton): Boolean {

View File

@ -1,6 +1,5 @@
package net.shadowfacts.phycon.block.terminal
import net.minecraft.client.util.math.MatrixStack
import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.geometry.Size
import net.shadowfacts.cacao.util.EnumHelper
@ -31,14 +30,15 @@ class SettingButton<E>(
get() = content as TextureView
init {
updateTexture()
update()
}
private fun updateTexture() {
private fun update() {
textureView.texture = textureCache.getOrPut(key.value) {
val uv = key.value.uv
Texture(key.value.iconTexture, uv[0], uv[1])
}
tooltip = key.value.tooltip
}
override fun mouseClicked(point: Point, mouseButton: MouseButton): Boolean {
@ -53,20 +53,10 @@ class SettingButton<E>(
PhysicalConnectivityClient.terminalSettings[key] = newValue
updateTexture()
update()
}
return super.mouseClicked(point, mouseButton)
}
override fun draw(matrixStack: MatrixStack, mouse: Point, delta: Float) {
super.draw(matrixStack, mouse, delta)
if (mouse in bounds) {
key.value.tooltip?.also {
window!!.drawTooltip(listOf(it))
}
}
}
}