Add panel background nine patch

This commit is contained in:
Shadowfacts 2019-06-26 19:32:06 -04:00
parent 260b1eafc3
commit 0defbdeffd
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
9 changed files with 29 additions and 27 deletions

View File

@ -8,8 +8,8 @@ import net.shadowfacts.cacao.Window
import net.shadowfacts.cacao.geometry.Axis
import net.shadowfacts.cacao.geometry.Size
import net.shadowfacts.cacao.util.Color
import net.shadowfacts.cacao.util.NinePatchTexture
import net.shadowfacts.cacao.util.Texture
import net.shadowfacts.cacao.util.texture.NinePatchTexture
import net.shadowfacts.cacao.util.texture.Texture
import net.shadowfacts.cacao.view.*
import net.shadowfacts.cacao.view.button.DropdownButton
@ -26,8 +26,7 @@ class TestCacaoScreen: CacaoScreen() {
val red = stack.addArrangedSubview(TextureView(Texture(Identifier("textures/block/birch_log_top.png"), 0, 0, 16, 16)).apply {
intrinsicContentSize = Size(50.0, 50.0)
})
val buttonNinePatch = NinePatchTexture(Texture(Identifier("textures/gui/widgets.png"), 0, 66), 3, 3, 194, 14)
val green = stack.addArrangedSubview(NinePatchView(buttonNinePatch).apply {
val green = stack.addArrangedSubview(NinePatchView(NinePatchTexture.PANEL_BG).apply {
intrinsicContentSize = Size(75.0, 100.0)
})
val blue = stack.addArrangedSubview(View().apply {

View File

@ -8,6 +8,7 @@ import net.minecraft.client.render.VertexFormats
import net.minecraft.client.sound.PositionedSoundInstance
import net.minecraft.sound.SoundEvent
import net.shadowfacts.cacao.geometry.Rect
import net.shadowfacts.cacao.util.texture.Texture
import org.lwjgl.opengl.GL11
/**

View File

@ -1,4 +1,6 @@
package net.shadowfacts.cacao.util
package net.shadowfacts.cacao.util.texture
import net.minecraft.util.Identifier
/**
* Helper class that represents a texture that can be divided into nine pieces (4 corners, 4 edges, and the center)
@ -15,12 +17,20 @@ package net.shadowfacts.cacao.util
*/
data class NinePatchTexture(val texture: Texture, val cornerWidth: Int, val cornerHeight: Int, val centerWidth: Int, val centerHeight: Int) {
companion object {
val PANEL_BG = NinePatchTexture(Texture(Identifier("textures/gui/demo_background.png"), 0, 0), 5, 5, 238, 156)
val BUTTON_BG = NinePatchTexture(Texture(Identifier("textures/gui/widgets.png"), 0, 66), 3, 3, 194, 14)
val BUTTON_HOVERED_BG = NinePatchTexture(Texture(Identifier("textures/gui/widgets.png"), 0, 86), 3, 3, 194, 14)
val BUTTON_DISABLED_BG = NinePatchTexture(Texture(Identifier("textures/gui/widgets.png"), 0, 46), 3, 3, 194, 14)
}
// Corners
val topLeft by lazy {
texture
}
val topRight by lazy {
Texture(texture.location, texture.u + cornerWidth + centerWidth, texture.v, texture.width, texture.height)
Texture(texture.location, texture.u + cornerWidth + centerWidth, texture.v, texture.width, texture.height)
}
val bottomLeft by lazy {
Texture(texture.location, texture.u, texture.v + cornerHeight + centerHeight, texture.width, texture.height)

View File

@ -1,4 +1,4 @@
package net.shadowfacts.cacao.util
package net.shadowfacts.cacao.util.texture
import net.minecraft.util.Identifier

View File

@ -2,7 +2,7 @@ package net.shadowfacts.cacao.view
import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.geometry.Rect
import net.shadowfacts.cacao.util.NinePatchTexture
import net.shadowfacts.cacao.util.texture.NinePatchTexture
import net.shadowfacts.cacao.util.RenderHelper
import net.shadowfacts.cacao.util.properties.ResettableLazyProperty

View File

@ -2,7 +2,7 @@ package net.shadowfacts.cacao.view
import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.util.RenderHelper
import net.shadowfacts.cacao.util.Texture
import net.shadowfacts.cacao.util.texture.Texture
/**
* A helper class for drawing a [Texture] in a view.

View File

@ -1,11 +1,9 @@
package net.shadowfacts.cacao.view.button
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.texture.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
@ -23,12 +21,6 @@ import net.shadowfacts.kiwidsl.dsl
*/
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 parameter is the type of the concrete button implementation that was used.
@ -50,19 +42,19 @@ 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
* unless the background view is not fully opaque.
*/
var background: View? = NinePatchView(DEFAULT_BG)
var background: View? = NinePatchView(NinePatchTexture.BUTTON_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? = NinePatchView(HOVERED_BG)
var hoveredBackground: View? = NinePatchView(NinePatchTexture.BUTTON_HOVERED_BG)
/**
* The background to draw when the button is [disabled].
* If `null`, the normal [background] will be used.
* @see background
*/
var disabledBackground: View? = NinePatchView(DISABLED_BG)
var disabledBackground: View? = NinePatchView(NinePatchTexture.BUTTON_DISABLED_BG)
override fun wasAdded() {
solver.dsl {

View File

@ -6,8 +6,8 @@ import net.shadowfacts.cacao.geometry.Axis
import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.geometry.Rect
import net.shadowfacts.cacao.util.MouseButton
import net.shadowfacts.cacao.util.NinePatchTexture
import net.shadowfacts.cacao.util.Texture
import net.shadowfacts.cacao.util.texture.NinePatchTexture
import net.shadowfacts.cacao.util.texture.Texture
import net.shadowfacts.cacao.util.properties.ResettableLazyProperty
import net.shadowfacts.cacao.view.NinePatchView
import net.shadowfacts.cacao.view.StackView
@ -89,7 +89,7 @@ class DropdownButton<Value, ContentView: View>(
private fun showDropdown() {
val dropdownWindow = window.screen.addWindow(Window())
val dropdownBackground = dropdownWindow.addView(NinePatchView(DEFAULT_BG).apply {
val dropdownBackground = dropdownWindow.addView(NinePatchView(NinePatchTexture.BUTTON_BG).apply {
zIndex = -1.0
})
val stack = dropdownWindow.addView(StackView(Axis.VERTICAL, StackView.Distribution.LEADING))
@ -100,8 +100,8 @@ class DropdownButton<Value, ContentView: View>(
val contentView = createView(value)
val button = stack.addArrangedSubview(Button(contentView, padding).apply {
background = null
hoveredBackground = DropdownItemBackgroundView(index == 0, index == last, HOVERED_BG)
disabledBackground = DropdownItemBackgroundView(index == 0, index == last, DISABLED_BG)
hoveredBackground = DropdownItemBackgroundView(index == 0, index == last, NinePatchTexture.BUTTON_HOVERED_BG)
disabledBackground = DropdownItemBackgroundView(index == 0, index == last, NinePatchTexture.BUTTON_DISABLED_BG)
disabled = value == this@DropdownButton.value
handler = {
dropdownWindow.removeFromScreen()

View File

@ -4,7 +4,7 @@ import net.minecraft.util.Identifier
import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.geometry.Size
import net.shadowfacts.cacao.util.MouseButton
import net.shadowfacts.cacao.util.Texture
import net.shadowfacts.cacao.util.texture.Texture
import net.shadowfacts.cacao.view.TextureView
/**