Buttons now have proper isDisabled, regular button is now native panel

Fixes #8
This commit is contained in:
DBotThePony 2022-09-09 15:22:11 +07:00
parent 66576fee94
commit 71f0bf3a62
Signed by: DBot
GPG Key ID: DCC23B5715498507
8 changed files with 108 additions and 29 deletions

View File

@ -19,6 +19,9 @@ class SkinGrid(
val imageWidth get() = width * columns
val imageHeight get() = height * rows
val currentX: Float get() = column * width
val currentY: Float get() = row * height
fun skip(): SkinGrid {
column++
@ -36,7 +39,7 @@ class SkinGrid(
}
fun next(): SkinElement {
val element = SkinElement(texture, column * width, row * height, width, height, imageWidth, imageHeight)
val element = SkinElement(texture, currentX, currentY, width, height, imageWidth, imageHeight)
skip()
return element
}

View File

@ -1,10 +1,30 @@
package ru.dbotthepony.mc.otm.client.render
private fun makeButton(grid: SkinGrid): StretchingRectangleElement {
val x = grid.currentX
val y = grid.currentY
return StretchingRectangleElement(
topLeft = WidgetLocation.WIDGETS_18.element(x, y, 2f, 2f, grid.imageWidth, grid.imageHeight),
topRight = WidgetLocation.WIDGETS_18.element(x + 16f, y, 2f, 2f, grid.imageWidth, grid.imageHeight),
bottomLeft = WidgetLocation.WIDGETS_18.element(x, y + 16f, 2f, 2f, grid.imageWidth, grid.imageHeight),
bottomRight = WidgetLocation.WIDGETS_18.element(x + 16f, y + 16f, 2f, 2f, grid.imageWidth, grid.imageHeight),
middle = WidgetLocation.WIDGETS_18.element(x + 2f, y + 2f, 14f, 14f, grid.imageWidth, grid.imageHeight),
top = WidgetLocation.WIDGETS_18.element(x + 2f, y, 14f, 2f, grid.imageWidth, grid.imageHeight),
left = WidgetLocation.WIDGETS_18.element(x, y + 2f, 2f, 14f, grid.imageWidth, grid.imageHeight),
right = WidgetLocation.WIDGETS_18.element(x + 16f, y + 2f, 2f, 14f, grid.imageWidth, grid.imageHeight),
bottom = WidgetLocation.WIDGETS_18.element(x + 2f, y + 16f, 14f, 2f, grid.imageWidth, grid.imageHeight),
)
}
object Widgets18 {
val GRID = SkinGrid(WidgetLocation.WIDGETS_18, 18f, 18f)
val BUTTON_IDLE_STRETCHABLE = makeButton(GRID)
val BUTTON_IDLE = GRID.next()
val BUTTON_HOVERED_STRETCHABLE = makeButton(GRID)
val BUTTON_HOVERED = GRID.next()
val BUTTON_PRESSED_STRETCHABLE = makeButton(GRID)
val BUTTON_PRESSED = GRID.next()
val ARROW_DOWN = GRID.next()
val AZ = GRID.next()
@ -18,4 +38,11 @@ object Widgets18 {
val EQUIPMENT_BATTERY_SLOT_BACKGROUND = GRID.next()
val MATTER_CAPACITOR_SLOT_BACKGROUND = GRID.next()
val RETURN_ARROW_LEFT = GRID.next()
init {
GRID.jump()
}
val BUTTON_DISABLED_STRETCHABLE = makeButton(GRID)
val BUTTON_DISABLED = GRID.next()
}

View File

@ -25,8 +25,9 @@ object Widgets8 {
val ARROW_PAINTED_UP = GRID.next()
init {
GRID.jump().skip()
GRID.jump()
}
val BUTTON_DISABLED = GRID.next()
val MINUS = GRID.next()
}

View File

@ -3,13 +3,12 @@ package ru.dbotthepony.mc.otm.client.screen.panels
import com.mojang.blaze3d.platform.InputConstants
import com.mojang.blaze3d.vertex.PoseStack
import net.minecraft.ChatFormatting
import net.minecraft.client.gui.components.Button
import net.minecraft.client.gui.components.Button.OnPress
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen
import net.minecraft.network.chat.Component
import ru.dbotthepony.mc.otm.client.playGuiClickSound
import ru.dbotthepony.mc.otm.core.TextComponent
import ru.dbotthepony.mc.otm.client.render.*
import ru.dbotthepony.mc.otm.core.RGBAColor
import ru.dbotthepony.mc.otm.core.next
import ru.dbotthepony.mc.otm.core.prev
import java.util.*
@ -23,9 +22,10 @@ open class ButtonPanel<out S : AbstractContainerScreen<*>>(
y: Float = 0f,
width: Float = 40f,
height: Float = HEIGHT,
label: Component
) : MinecraftWidgetPanel<S, Button>(screen, parent, x, y, width, height) {
var label: Component
) : EditablePanel<S>(screen, parent, x, y, width, height) {
constructor(screen: S, parent: EditablePanel<*>?, label: Component) : this(screen, parent, x = 0f, label = label)
constructor(
screen: S,
parent: EditablePanel<*>?,
@ -39,35 +39,76 @@ open class ButtonPanel<out S : AbstractContainerScreen<*>>(
this.callback = onPress
}
var label = label
set(value) {
field = value
widget?.message = value
}
override fun copyValues(new_widget: Button, old_widget: Button) {}
override fun configureNew(widget: Button, recreation: Boolean) {}
override fun makeNew(): Button {
return object : Button(0, 0, width.toInt(), height.toInt().coerceAtMost(20), label, OnPress { this@ButtonPanel.onPress() }) {
override fun isHoveredOrFocused(): Boolean {
return this@ButtonPanel.isHovered
}
}
}
protected var callback: Runnable? = null
protected var pressed = false
override fun mouseClickedInner(x: Double, y: Double, button: Int): Boolean {
if (isDisabled || pressed) {
return true
}
pressed = true
trapMouseInput = true
playGuiClickSound()
return true
}
override fun mouseReleasedInner(x: Double, y: Double, button: Int): Boolean {
if (!pressed) {
return true
}
trapMouseInput = false
pressed = false
if (isHovered) {
callback?.run()
}
return true
}
fun bind(runnable: Runnable) {
callback = runnable
}
protected open fun onPress() {
callback?.run()
var textColor = RGBAColor.WHITE
var isDisabled = false
set(value) {
if (field != value) {
if (!value) {
pressed = false
trapMouseInput = false
}
field = value
}
}
override fun innerRender(stack: PoseStack, mouseX: Float, mouseY: Float, partialTick: Float) {
if (isDisabled) {
Widgets18.BUTTON_DISABLED_STRETCHABLE.render(stack, width = width, height = height)
} else if (pressed) {
Widgets18.BUTTON_PRESSED_STRETCHABLE.render(stack, width = width, height = height)
} else if (isHovered) {
Widgets18.BUTTON_HOVERED_STRETCHABLE.render(stack, width = width, height = height)
} else {
Widgets18.BUTTON_IDLE_STRETCHABLE.render(stack, width = width, height = height)
}
font.drawAligned(stack, label, TextAlign.CENTER_CENTER, width / 2f, height / 2f, textColor.toInt())
}
override fun sizeToContents() {
super.sizeToContents()
height = height.coerceAtLeast(HEIGHT).coerceAtLeast(font.lineHeight.toFloat() + 2f)
width = width.coerceAtLeast(HEIGHT).coerceAtLeast(font.width(label) + 4f)
}
companion object {
const val HEIGHT = 20f
const val HEIGHT = 18f
}
}
@ -90,6 +131,7 @@ abstract class SquareButtonPanel<out S : AbstractContainerScreen<*>>(
abstract val PRESSED: SkinElement
abstract val HOVERED: SkinElement
abstract val IDLE: SkinElement
abstract val DISABLED: SkinElement
var isDisabled = false
set(value) {
@ -104,9 +146,11 @@ abstract class SquareButtonPanel<out S : AbstractContainerScreen<*>>(
}
override fun innerRender(stack: PoseStack, mouseX: Float, mouseY: Float, partialTick: Float) {
if (pressed) {
if (isDisabled) {
DISABLED.render(stack, 0f, 0f, width, height)
} else if (pressed) {
PRESSED.render(stack, 0f, 0f, width, height)
} else if (isHovered && !isDisabled) {
} else if (isHovered) {
HOVERED.render(stack, 0f, 0f, width, height)
} else {
IDLE.render(stack, 0f, 0f, width, height)
@ -324,6 +368,7 @@ open class LargeSquareButtonPanel<out S : AbstractContainerScreen<*>>(
final override val IDLE = Widgets18.BUTTON_IDLE
final override val HOVERED = Widgets18.BUTTON_HOVERED
final override val PRESSED = Widgets18.BUTTON_PRESSED
final override val DISABLED = Widgets18.BUTTON_DISABLED
override fun innerRender(stack: PoseStack, mouseX: Float, mouseY: Float, partialTick: Float) {
super.innerRender(stack, mouseX, mouseY, partialTick)
@ -355,6 +400,7 @@ open class LargeEnumSquareButtonPanel<out S : AbstractContainerScreen<*>, T : En
final override val IDLE = Widgets18.BUTTON_IDLE
final override val HOVERED = Widgets18.BUTTON_HOVERED
final override val PRESSED = Widgets18.BUTTON_PRESSED
final override val DISABLED = Widgets18.BUTTON_DISABLED
companion object {
const val SIZE = 18f
@ -375,6 +421,7 @@ open class SmallSquareButtonPanel<out S : AbstractContainerScreen<*>>(
final override val IDLE = Widgets8.BUTTON_IDLE
final override val HOVERED = Widgets8.BUTTON_HOVERED
final override val PRESSED = Widgets8.BUTTON_PRESSED
final override val DISABLED = Widgets8.BUTTON_DISABLED
override fun innerRender(stack: PoseStack, mouseX: Float, mouseY: Float, partialTick: Float) {
super.innerRender(stack, mouseX, mouseY, partialTick)
@ -406,6 +453,7 @@ open class SmallEnumSquareButtonPanel<out S : AbstractContainerScreen<*>, T : En
final override val IDLE = Widgets8.BUTTON_IDLE
final override val HOVERED = Widgets8.BUTTON_HOVERED
final override val PRESSED = Widgets8.BUTTON_PRESSED
final override val DISABLED = Widgets8.BUTTON_DISABLED
companion object {
const val SIZE = 8f

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.7 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 852 B

After

Width:  |  Height:  |  Size: 885 B