diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/SkinElement.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/SkinElement.kt index 141bf1f08..bc5391d58 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/SkinElement.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/SkinElement.kt @@ -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 } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/Widgets18.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/Widgets18.kt index 0a1efde34..5455138e6 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/Widgets18.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/Widgets18.kt @@ -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() } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/Widgets8.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/Widgets8.kt index 6b2a93aaa..c80c0c379 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/Widgets8.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/Widgets8.kt @@ -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() } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/ButtonPanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/ButtonPanel.kt index b12b0c343..6a3c081a7 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/ButtonPanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/ButtonPanel.kt @@ -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>( y: Float = 0f, width: Float = 40f, height: Float = HEIGHT, - label: Component -) : MinecraftWidgetPanel(screen, parent, x, y, width, height) { + var label: Component +) : EditablePanel(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>( this.callback = onPress } - var label = label - set(value) { - field = value - widget?.message = value + protected var callback: Runnable? = null + protected var pressed = false + + override fun mouseClickedInner(x: Double, y: Double, button: Int): Boolean { + if (isDisabled || pressed) { + return true } - 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 - } - } + pressed = true + trapMouseInput = true + playGuiClickSound() + return true } - protected var callback: Runnable? = null + 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>( 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>( } 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>( 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, 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>( 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, 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 diff --git a/src/main/resources/assets/overdrive_that_matters/textures/gui/widgets_18.png b/src/main/resources/assets/overdrive_that_matters/textures/gui/widgets_18.png index 26bc6123b..330434324 100644 Binary files a/src/main/resources/assets/overdrive_that_matters/textures/gui/widgets_18.png and b/src/main/resources/assets/overdrive_that_matters/textures/gui/widgets_18.png differ diff --git a/src/main/resources/assets/overdrive_that_matters/textures/gui/widgets_18.xcf b/src/main/resources/assets/overdrive_that_matters/textures/gui/widgets_18.xcf index c10fe4fb0..71df56853 100644 --- a/src/main/resources/assets/overdrive_that_matters/textures/gui/widgets_18.xcf +++ b/src/main/resources/assets/overdrive_that_matters/textures/gui/widgets_18.xcf @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:52df530bc275818f5a9a620a43ead44c2d1ebcf6b3109e7ad1165a64208c42b5 -size 12808 +oid sha256:dd7743555cb274450628286c4ae447662cc3f0c87885649dbc943bc2bb60a22f +size 13498 diff --git a/src/main/resources/assets/overdrive_that_matters/textures/gui/widgets_8.png b/src/main/resources/assets/overdrive_that_matters/textures/gui/widgets_8.png index dc3266753..bb630b4da 100644 Binary files a/src/main/resources/assets/overdrive_that_matters/textures/gui/widgets_8.png and b/src/main/resources/assets/overdrive_that_matters/textures/gui/widgets_8.png differ diff --git a/src/main/resources/assets/overdrive_that_matters/textures/gui/widgets_8.xcf b/src/main/resources/assets/overdrive_that_matters/textures/gui/widgets_8.xcf index abafda640..1e8ebc96f 100644 --- a/src/main/resources/assets/overdrive_that_matters/textures/gui/widgets_8.xcf +++ b/src/main/resources/assets/overdrive_that_matters/textures/gui/widgets_8.xcf @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:b96852d05f9f9130d0ed105004968130b9f1b1e543fd1f54e91006723bb45119 -size 5141 +oid sha256:6192467185f2a175883d70c9e6abe83d422e98b0a63823fffd7a53ce5f7089ba +size 5597