Inventory rows height test

This commit is contained in:
DBotThePony 2022-09-09 17:53:45 +07:00
parent d42974072b
commit d66f892d9e
Signed by: DBot
GPG Key ID: DCC23B5715498507
9 changed files with 260 additions and 51 deletions

View File

@ -4,6 +4,10 @@ import net.minecraft.client.Minecraft
import net.minecraft.client.gui.Font import net.minecraft.client.gui.Font
import net.minecraft.client.resources.sounds.SimpleSoundInstance import net.minecraft.client.resources.sounds.SimpleSoundInstance
import net.minecraft.sounds.SoundEvents import net.minecraft.sounds.SoundEvents
import org.lwjgl.glfw.GLFW
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.DoubleBuffer
inline val minecraft: Minecraft get() = Minecraft.getInstance() inline val minecraft: Minecraft get() = Minecraft.getInstance()
inline val font: Font get() = minecraft.font inline val font: Font get() = minecraft.font
@ -11,3 +15,64 @@ inline val font: Font get() = minecraft.font
fun playGuiClickSound() { fun playGuiClickSound() {
minecraft.soundManager.play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1.0f)) minecraft.soundManager.play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1.0f))
} }
fun setMousePos(x: Double, y: Double) {
GLFW.glfwSetCursorPos(minecraft.window.window, x, y)
}
private val cursorXPosBuf = ByteBuffer.allocateDirect(8).also { it.order(ByteOrder.LITTLE_ENDIAN) }.asDoubleBuffer()
private val cursorYPosBuf = ByteBuffer.allocateDirect(8).also { it.order(ByteOrder.LITTLE_ENDIAN) }.asDoubleBuffer()
data class MousePos(val x: Double, val y: Double) {
fun set() {
setMousePos(x, y)
}
fun move(x: Double = 0.0, y: Double = 0.0) {
GLFW.glfwSetCursorPos(minecraft.window.window, this.x + x, this.y + y)
}
fun move(x: Float = 0.0f, y: Float = 0.0f) {
GLFW.glfwSetCursorPos(minecraft.window.window, this.x + x, this.y + y)
}
fun moveScaled(x: Double = 0.0, y: Double = 0.0) {
GLFW.glfwSetCursorPos(minecraft.window.window, this.x + x * minecraft.window.guiScale, this.y + y * minecraft.window.guiScale)
}
fun moveScaled(x: Float = 0.0f, y: Float = 0.0f) {
GLFW.glfwSetCursorPos(minecraft.window.window, this.x + x * minecraft.window.guiScale, this.y + y * minecraft.window.guiScale)
}
}
val mousePos: MousePos get() {
cursorXPosBuf.position(0)
cursorYPosBuf.position(0)
GLFW.glfwGetCursorPos(minecraft.window.window, cursorXPosBuf, cursorYPosBuf)
cursorXPosBuf.position(0)
cursorYPosBuf.position(0)
return MousePos(cursorXPosBuf.get(), cursorYPosBuf.get())
}
fun moveMousePos(x: Double = 0.0, y: Double = 0.0) {
val (currentX, currentY) = mousePos
GLFW.glfwSetCursorPos(minecraft.window.window, currentX + x, currentY + y)
}
fun moveMousePos(x: Float = 0.0f, y: Float = 0.0f) {
val (currentX, currentY) = mousePos
GLFW.glfwSetCursorPos(minecraft.window.window, currentX + x, currentY + y)
}
fun moveMousePosScaled(x: Double = 0.0, y: Double = 0.0) {
val (currentX, currentY) = mousePos
GLFW.glfwSetCursorPos(minecraft.window.window, currentX + x * minecraft.window.guiScale, currentY + y * minecraft.window.guiScale)
}
fun moveMousePosScaled(x: Float = 0.0f, y: Float = 0.0f) {
val (currentX, currentY) = mousePos
GLFW.glfwSetCursorPos(minecraft.window.window, currentX + x * minecraft.window.guiScale, currentY + y * minecraft.window.guiScale)
}

View File

@ -6,7 +6,7 @@ import net.minecraft.resources.ResourceLocation
import ru.dbotthepony.mc.otm.client.screen.panels.DockProperty import ru.dbotthepony.mc.otm.client.screen.panels.DockProperty
@Suppress("unused") @Suppress("unused")
class SkinGrid( data class SkinGrid(
val texture: ResourceLocation, val texture: ResourceLocation,
val width: Float, val width: Float,
val height: Float, val height: Float,
@ -92,14 +92,15 @@ fun ResourceLocation.vLine(
) = SkinElement(this, x, y, 1f, height, textureWidth, textureHeight) ) = SkinElement(this, x, y, 1f, height, textureWidth, textureHeight)
@Suppress("unused") @Suppress("unused")
class SkinElement @JvmOverloads constructor( data class SkinElement @JvmOverloads constructor(
val texture: ResourceLocation, val texture: ResourceLocation,
val x: Float, val x: Float,
val y: Float, val y: Float,
val width: Float, val width: Float,
val height: Float, val height: Float,
val imageWidth: Float = 256f, val imageWidth: Float = 256f,
val imageHeight: Float = 256f val imageHeight: Float = 256f,
val winding: UVWindingOrder = UVWindingOrder.NORMAL
) { ) {
init { init {
require(x >= 0f) { "Invalid x $x" } require(x >= 0f) { "Invalid x $x" }
@ -117,7 +118,7 @@ class SkinElement @JvmOverloads constructor(
y: Float = 0f, y: Float = 0f,
width: Float = this.width, width: Float = this.width,
height: Float = this.height, height: Float = this.height,
winding: UVWindingOrder = UVWindingOrder.U0_V0_U1_V1 winding: UVWindingOrder = this.winding
) { ) {
RenderSystem.setShaderTexture(0, texture) RenderSystem.setShaderTexture(0, texture)
RenderSystem.enableBlend() RenderSystem.enableBlend()
@ -133,7 +134,7 @@ class SkinElement @JvmOverloads constructor(
y: Double = 0.0, y: Double = 0.0,
width: Double = this.width.toDouble(), width: Double = this.width.toDouble(),
height: Double = this.height.toDouble(), height: Double = this.height.toDouble(),
winding: UVWindingOrder = UVWindingOrder.U0_V0_U1_V1 winding: UVWindingOrder = this.winding
) = render(stack, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), winding) ) = render(stack, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), winding)
@JvmOverloads @JvmOverloads
@ -143,7 +144,7 @@ class SkinElement @JvmOverloads constructor(
y: Float = 0f, y: Float = 0f,
width: Float = this.width, width: Float = this.width,
height: Float = this.height, height: Float = this.height,
winding: UVWindingOrder = UVWindingOrder.U0_V0_U1_V1 winding: UVWindingOrder = this.winding
) { ) {
RenderSystem.setShaderTexture(0, texture) RenderSystem.setShaderTexture(0, texture)
RenderSystem.enableBlend() RenderSystem.enableBlend()
@ -159,7 +160,7 @@ class SkinElement @JvmOverloads constructor(
y: Double = 0.0, y: Double = 0.0,
width: Double = this.width.toDouble(), width: Double = this.width.toDouble(),
height: Double = this.height.toDouble(), height: Double = this.height.toDouble(),
winding: UVWindingOrder = UVWindingOrder.U0_V0_U1_V1 winding: UVWindingOrder = this.winding
) = renderPartial(stack, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), winding) ) = renderPartial(stack, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), winding)
@JvmOverloads @JvmOverloads
@ -168,7 +169,7 @@ class SkinElement @JvmOverloads constructor(
x: Float = 0f, x: Float = 0f,
y: Float = 0f, y: Float = 0f,
width: Float = this.width, width: Float = this.width,
winding: UVWindingOrder = UVWindingOrder.U0_V0_U1_V1, winding: UVWindingOrder = this.winding
) { ) {
render(stack, x, y, width = width, winding = winding) render(stack, x, y, width = width, winding = winding)
} }
@ -179,7 +180,7 @@ class SkinElement @JvmOverloads constructor(
x: Double, x: Double,
y: Double = 0.0, y: Double = 0.0,
width: Double = this.width.toDouble(), width: Double = this.width.toDouble(),
winding: UVWindingOrder = UVWindingOrder.U0_V0_U1_V1, winding: UVWindingOrder = this.winding,
) = renderWidth(stack, x.toFloat(), y.toFloat(), width.toFloat(), winding) ) = renderWidth(stack, x.toFloat(), y.toFloat(), width.toFloat(), winding)
@JvmOverloads @JvmOverloads
@ -188,7 +189,7 @@ class SkinElement @JvmOverloads constructor(
x: Float = 0f, x: Float = 0f,
y: Float = 0f, y: Float = 0f,
height: Float = this.height, height: Float = this.height,
winding: UVWindingOrder = UVWindingOrder.U0_V0_U1_V1, winding: UVWindingOrder = this.winding,
) { ) {
render(stack, x, y, height = height, winding = winding) render(stack, x, y, height = height, winding = winding)
} }
@ -199,7 +200,7 @@ class SkinElement @JvmOverloads constructor(
x: Double, x: Double,
y: Double = 0.0, y: Double = 0.0,
height: Double = this.height.toDouble(), height: Double = this.height.toDouble(),
winding: UVWindingOrder = UVWindingOrder.U0_V0_U1_V1, winding: UVWindingOrder = this.winding,
) = renderHieght(stack, x.toFloat(), y.toFloat(), height.toFloat(), winding) ) = renderHieght(stack, x.toFloat(), y.toFloat(), height.toFloat(), winding)
private val u0 = this.x / imageWidth private val u0 = this.x / imageWidth
@ -214,7 +215,7 @@ class SkinElement @JvmOverloads constructor(
y: Float = 0f, y: Float = 0f,
width: Float = this.width, width: Float = this.width,
height: Float = this.height, height: Float = this.height,
winding: UVWindingOrder = UVWindingOrder.U0_V0_U1_V1 winding: UVWindingOrder = UVWindingOrder.NORMAL
) { ) {
val winded = winding.translate(u0, v0, u1, v1) val winded = winding.translate(u0, v0, u1, v1)
@ -235,7 +236,7 @@ class SkinElement @JvmOverloads constructor(
y: Double = 0.0, y: Double = 0.0,
width: Double = this.width.toDouble(), width: Double = this.width.toDouble(),
height: Double = this.height.toDouble(), height: Double = this.height.toDouble(),
winding: UVWindingOrder = UVWindingOrder.U0_V0_U1_V1 winding: UVWindingOrder = UVWindingOrder.NORMAL
) = renderRaw(stack, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), winding) ) = renderRaw(stack, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), winding)
@JvmOverloads @JvmOverloads
@ -245,7 +246,7 @@ class SkinElement @JvmOverloads constructor(
y: Float = 0f, y: Float = 0f,
width: Float = this.width, width: Float = this.width,
height: Float = this.height, height: Float = this.height,
winding: UVWindingOrder = UVWindingOrder.U0_V0_U1_V1 winding: UVWindingOrder = UVWindingOrder.NORMAL
) { ) {
val u1 = (this.x + width) / imageWidth val u1 = (this.x + width) / imageWidth
val v1 = (this.y + height) / imageHeight val v1 = (this.y + height) / imageHeight
@ -269,11 +270,11 @@ class SkinElement @JvmOverloads constructor(
y: Double = 0.0, y: Double = 0.0,
width: Double = this.width.toDouble(), width: Double = this.width.toDouble(),
height: Double = this.height.toDouble(), height: Double = this.height.toDouble(),
winding: UVWindingOrder = UVWindingOrder.U0_V0_U1_V1 winding: UVWindingOrder = UVWindingOrder.NORMAL
) = renderRawPartial(stack, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), winding) ) = renderRawPartial(stack, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), winding)
} }
class StretchingRectangleElement( data class StretchingRectangleElement(
val topLeft: SkinElement, val topLeft: SkinElement,
val topRight: SkinElement, val topRight: SkinElement,
val bottomLeft: SkinElement, val bottomLeft: SkinElement,

View File

@ -0,0 +1,15 @@
package ru.dbotthepony.mc.otm.client.render
object Widgets {
val SLOT = WidgetLocation.WIDGETS.element(0f, 0f, 18f, 18f)
val ARROW_UP_BUTTON_IDLE = WidgetLocation.WIDGETS.element(0f, 18f, 18f, 6f)
val ARROW_UP_BUTTON_HOVERED = WidgetLocation.WIDGETS.element(0f, 18f + 6f, 18f, 6f)
val ARROW_UP_BUTTON_PRESSED = WidgetLocation.WIDGETS.element(0f, 18f + 6f * 2f, 18f, 6f)
val ARROW_UP_BUTTON_DISABLED = WidgetLocation.WIDGETS.element(0f, 18f + 6f * 3f, 18f, 6f)
val ARROW_DOWN_BUTTON_IDLE = ARROW_UP_BUTTON_IDLE.copy(winding = UVWindingOrder.FLIP)
val ARROW_DOWN_BUTTON_HOVERED = ARROW_UP_BUTTON_HOVERED.copy(winding = UVWindingOrder.FLIP)
val ARROW_DOWN_BUTTON_PRESSED = ARROW_UP_BUTTON_PRESSED.copy(winding = UVWindingOrder.FLIP)
val ARROW_DOWN_BUTTON_DISABLED = ARROW_UP_BUTTON_DISABLED.copy(winding = UVWindingOrder.FLIP)
}

View File

@ -3,10 +3,14 @@ package ru.dbotthepony.mc.otm.client.screen
import com.mojang.blaze3d.platform.InputConstants import com.mojang.blaze3d.platform.InputConstants
import com.mojang.blaze3d.vertex.PoseStack import com.mojang.blaze3d.vertex.PoseStack
import net.minecraft.client.gui.screens.inventory.InventoryScreen import net.minecraft.client.gui.screens.inventory.InventoryScreen
import ru.dbotthepony.mc.otm.client.mousePos
import ru.dbotthepony.mc.otm.client.moveMousePos
import ru.dbotthepony.mc.otm.client.moveMousePosScaled
import ru.dbotthepony.mc.otm.client.render.Widgets18 import ru.dbotthepony.mc.otm.client.render.Widgets18
import ru.dbotthepony.mc.otm.core.TranslatableComponent import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.client.render.element import ru.dbotthepony.mc.otm.client.render.element
import ru.dbotthepony.mc.otm.client.screen.panels.* import ru.dbotthepony.mc.otm.client.screen.panels.*
import ru.dbotthepony.mc.otm.client.setMousePos
import ru.dbotthepony.mc.otm.client.shouldOpenVanillaInventory import ru.dbotthepony.mc.otm.client.shouldOpenVanillaInventory
import ru.dbotthepony.mc.otm.menu.ExoSuitInventoryMenu import ru.dbotthepony.mc.otm.menu.ExoSuitInventoryMenu
import ru.dbotthepony.mc.otm.network.ExoSuitMenuOpen import ru.dbotthepony.mc.otm.network.ExoSuitMenuOpen
@ -15,25 +19,59 @@ import yalter.mousetweaks.api.MouseTweaksDisableWheelTweak
@MouseTweaksDisableWheelTweak @MouseTweaksDisableWheelTweak
class ExoSuitInventoryScreen(menu: ExoSuitInventoryMenu) : MatteryScreen<ExoSuitInventoryMenu>(menu, TranslatableComponent("otm.gui.exosuit")) { class ExoSuitInventoryScreen(menu: ExoSuitInventoryMenu) : MatteryScreen<ExoSuitInventoryMenu>(menu, TranslatableComponent("otm.gui.exosuit")) {
var inventoryRows = 3
set(value) {
val newValue = value.coerceAtLeast(3).coerceAtMost(6)
val old = field
if (field != newValue) {
field = newValue
if (mainFrame != null) {
updateInventoryRows(old)
}
}
}
private lateinit var mainInventoryLine: EditablePanel<ExoSuitInventoryScreen>
private lateinit var scrollPanel: DiscreteScrollBarPanel<ExoSuitInventoryScreen>
private fun updateInventoryRows(old: Int) {
mainFrame!!.height = FRAME_BASE_HEIGHT + inventoryRows * AbstractSlotPanel.SIZE
mainInventoryLine.height = AbstractSlotPanel.SIZE * inventoryRows
for (i in scrollPanel.scroll until scrollPanel.scroll + old) {
getInventorySlotsRow(i).visible = false
}
for (i in scrollPanel.scroll until scrollPanel.scroll + inventoryRows) {
getInventorySlotsRow(i).also {
it.parent = mainInventoryLine
it.y = AbstractSlotPanel.SIZE * (i - scrollPanel.scroll)
it.visible = true
}
}
scrollPanel.scroll = scrollPanel.scroll
}
override fun makeMainFrame(): FramePanel<MatteryScreen<*>> { override fun makeMainFrame(): FramePanel<MatteryScreen<*>> {
val frame = FramePanel(this, width = 200f, height = 180f, title = this.title) val frame = FramePanel(this, width = 200f, height = FRAME_BASE_HEIGHT + inventoryRows * AbstractSlotPanel.SIZE, title = this.title)
val toolbeltLine = EditablePanel(this, frame, height = 18f) val toolbeltLine = EditablePanel(this, frame, height = 18f)
toolbeltLine.dock = Dock.BOTTOM toolbeltLine.dock = Dock.BOTTOM
toolbeltLine.setDockMargin(top = 3f) toolbeltLine.setDockMargin(top = 3f)
var mainInventoryLine: EditablePanel<*>? = null scrollPanel = DiscreteScrollBarPanel(this, null, maxScroll = { ((menu.playerCombinedInventorySlots.size - inventoryRows * 9) + 8) / 9 },
val scrollPanel = DiscreteScrollBarPanel(this, null, maxScroll = { ((menu.playerCombinedInventorySlots.size - 27) + 8) / 9 },
scrollCallback = { scrollCallback = {
_, old, new -> _, old, new ->
for (i in old .. old + 2) { for (i in old until old + inventoryRows) {
getInventorySlotsRow(i).visible = false getInventorySlotsRow(i).visible = false
} }
for (i in new .. new + 2) { for (i in new until new + inventoryRows) {
val row = getInventorySlotsRow(i) val row = getInventorySlotsRow(i)
row.visible = true row.visible = true
row.y = (i - new) * AbstractSlotPanel.SIZE row.y = (i - new) * AbstractSlotPanel.SIZE
@ -43,14 +81,13 @@ class ExoSuitInventoryScreen(menu: ExoSuitInventoryMenu) : MatteryScreen<ExoSuit
menu.lastScroll = new menu.lastScroll = new
}) })
scrollPanel.scroll = menu.lastScroll mainInventoryLine = object : EditablePanel<ExoSuitInventoryScreen>(this@ExoSuitInventoryScreen, frame, height = AbstractSlotPanel.SIZE * inventoryRows) {
mainInventoryLine = object : EditablePanel<ExoSuitInventoryScreen>(this@ExoSuitInventoryScreen, frame, height = 18f * 3f) {
override fun mouseScrolledInner(x: Double, y: Double, scroll: Double): Boolean { override fun mouseScrolledInner(x: Double, y: Double, scroll: Double): Boolean {
return scrollPanel.mouseScrolledInner(x, y, scroll) return scrollPanel.mouseScrolledInner(x, y, scroll)
} }
} }
scrollPanel.scroll = menu.lastScroll
scrollPanel.parent = mainInventoryLine scrollPanel.parent = mainInventoryLine
mainInventoryLine.dock = Dock.BOTTOM mainInventoryLine.dock = Dock.BOTTOM
@ -63,7 +100,7 @@ class ExoSuitInventoryScreen(menu: ExoSuitInventoryMenu) : MatteryScreen<ExoSuit
val offhand = SlotPanel(this, toolbeltLine, menu.offhandSlot) val offhand = SlotPanel(this, toolbeltLine, menu.offhandSlot)
offhand.dock = Dock.RIGHT offhand.dock = Dock.RIGHT
for (i in scrollPanel.scroll .. scrollPanel.scroll + 2) { for (i in scrollPanel.scroll until scrollPanel.scroll + inventoryRows) {
getInventorySlotsRow(i).also { getInventorySlotsRow(i).also {
it.parent = mainInventoryLine it.parent = mainInventoryLine
it.y = AbstractSlotPanel.SIZE * (i - scrollPanel.scroll) it.y = AbstractSlotPanel.SIZE * (i - scrollPanel.scroll)
@ -113,7 +150,7 @@ class ExoSuitInventoryScreen(menu: ExoSuitInventoryMenu) : MatteryScreen<ExoSuit
val resultPanel = SlotPanel(this, craftingCanvas, menu.craftingResultSlot, x = craftingCanvas.width - 18f, y = topLine.height / 2f - 9f) val resultPanel = SlotPanel(this, craftingCanvas, menu.craftingResultSlot, x = craftingCanvas.width - 18f, y = topLine.height / 2f - 9f)
val arrowPanel = object : EditablePanel<ExoSuitInventoryScreen>( object : EditablePanel<ExoSuitInventoryScreen>(
this@ExoSuitInventoryScreen, this@ExoSuitInventoryScreen,
craftingCanvas, craftingCanvas,
x = craftingSlotsCanvas.width, x = craftingSlotsCanvas.width,
@ -132,15 +169,27 @@ class ExoSuitInventoryScreen(menu: ExoSuitInventoryMenu) : MatteryScreen<ExoSuit
shouldOpenVanillaInventory = true shouldOpenVanillaInventory = true
val minecraft = minecraft!! val minecraft = minecraft!!
val mouseX = minecraft.mouseHandler.xpos() val (mouseX, mouseY) = mousePos
val mouseY = minecraft.mouseHandler.ypos()
onClose() onClose()
minecraft.setScreen(InventoryScreen(minecraft.player!!)) minecraft.setScreen(InventoryScreen(minecraft.player!!))
InputConstants.grabOrReleaseMouse(minecraft.window.window, 212993, mouseX, mouseY) setMousePos(mouseX, mouseY)
}).also { it.tooltip = TranslatableComponent("otm.gui.exosuit.go_back") } }).also { it.tooltip = TranslatableComponent("otm.gui.exosuit.go_back") }
HeightControls(this, frame, frame.width + 2f, frame.height.coerceAtMost(95f)) {
inventoryRows += if (it) 1 else -1
val movePixels = if (it) -AbstractSlotPanel.SIZE / 2f else AbstractSlotPanel.SIZE / 2f
frame.y += movePixels
moveMousePosScaled(y = movePixels)
HeightControls.Status.of(inventoryRows < 6, inventoryRows > 3)
}.also {
it.controlStatus = HeightControls.Status.of(inventoryRows < 6, inventoryRows > 3)
}
return frame return frame
} }
@ -153,5 +202,7 @@ class ExoSuitInventoryScreen(menu: ExoSuitInventoryMenu) : MatteryScreen<ExoSuit
val ENTITY_RECTANGLE = INVENTORY_LOCATION.element(25f, 7f, 51f, 72f) val ENTITY_RECTANGLE = INVENTORY_LOCATION.element(25f, 7f, 51f, 72f)
val STATUS_EFFECT_BG = INVENTORY_LOCATION.element(0f, 166f, 120f, 32f) val STATUS_EFFECT_BG = INVENTORY_LOCATION.element(0f, 166f, 120f, 32f)
val CRAFT_ARROW = INVENTORY_LOCATION.element(135f, 29f, 16f, 13f) val CRAFT_ARROW = INVENTORY_LOCATION.element(135f, 29f, 16f, 13f)
const val FRAME_BASE_HEIGHT = 126f
} }
} }

View File

@ -3,7 +3,7 @@ package ru.dbotthepony.mc.otm.client.screen.panels
import com.mojang.blaze3d.platform.InputConstants import com.mojang.blaze3d.platform.InputConstants
import com.mojang.blaze3d.vertex.PoseStack import com.mojang.blaze3d.vertex.PoseStack
import net.minecraft.ChatFormatting import net.minecraft.ChatFormatting
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen import net.minecraft.client.gui.screens.Screen
import net.minecraft.network.chat.Component import net.minecraft.network.chat.Component
import ru.dbotthepony.mc.otm.client.playGuiClickSound import ru.dbotthepony.mc.otm.client.playGuiClickSound
import ru.dbotthepony.mc.otm.core.TextComponent import ru.dbotthepony.mc.otm.core.TextComponent
@ -15,7 +15,7 @@ import java.util.*
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
import kotlin.reflect.KMutableProperty0 import kotlin.reflect.KMutableProperty0
open class ButtonPanel<out S : AbstractContainerScreen<*>>( open class ButtonPanel<out S : Screen>(
screen: S, screen: S,
parent: EditablePanel<*>?, parent: EditablePanel<*>?,
x: Float = 0f, x: Float = 0f,
@ -42,6 +42,10 @@ open class ButtonPanel<out S : AbstractContainerScreen<*>>(
protected var callback: Runnable? = null protected var callback: Runnable? = null
protected var pressed = false protected var pressed = false
protected open fun onClick(button: Int) {
callback?.run()
}
override fun mouseClickedInner(x: Double, y: Double, button: Int): Boolean { override fun mouseClickedInner(x: Double, y: Double, button: Int): Boolean {
if (isDisabled || pressed) { if (isDisabled || pressed) {
return true return true
@ -62,7 +66,7 @@ open class ButtonPanel<out S : AbstractContainerScreen<*>>(
pressed = false pressed = false
if (isHovered) { if (isHovered) {
callback?.run() onClick(button)
} }
return true return true
@ -113,7 +117,7 @@ open class ButtonPanel<out S : AbstractContainerScreen<*>>(
} }
@Suppress("PropertyName") @Suppress("PropertyName")
abstract class RectangleButtonPanel<out S : AbstractContainerScreen<*>>( abstract class RectangleButtonPanel<out S : Screen>(
screen: S, screen: S,
parent: EditablePanel<*>?, parent: EditablePanel<*>?,
x: Float = 0f, x: Float = 0f,
@ -124,7 +128,7 @@ abstract class RectangleButtonPanel<out S : AbstractContainerScreen<*>>(
) : EditablePanel<S>(screen, parent, x, y, width, height) { ) : EditablePanel<S>(screen, parent, x, y, width, height) {
protected var pressed = false protected var pressed = false
protected open fun click(clickButton: Int) { protected open fun onClick(clickButton: Int) {
onPress?.invoke(clickButton) onPress?.invoke(clickButton)
} }
@ -175,7 +179,7 @@ abstract class RectangleButtonPanel<out S : AbstractContainerScreen<*>>(
pressed = false pressed = false
if (isHovered) { if (isHovered) {
click(button) onClick(button)
} }
} }
@ -185,7 +189,7 @@ abstract class RectangleButtonPanel<out S : AbstractContainerScreen<*>>(
} }
} }
abstract class EnumRectangleButtonPanel<out S : AbstractContainerScreen<*>, T : Enum<T>>( abstract class EnumRectangleButtonPanel<out S : Screen, T : Enum<T>>(
screen: S, screen: S,
parent: EditablePanel<*>?, parent: EditablePanel<*>?,
x: Float = 0f, x: Float = 0f,
@ -293,7 +297,7 @@ abstract class EnumRectangleButtonPanel<out S : AbstractContainerScreen<*>, T :
return super.mouseReleasedInner(x, y, button) return super.mouseReleasedInner(x, y, button)
} }
override fun click(clickButton: Int) { override fun onClick(clickButton: Int) {
when (clickButton) { when (clickButton) {
InputConstants.MOUSE_BUTTON_LEFT -> { InputConstants.MOUSE_BUTTON_LEFT -> {
prop.set(prop.get().next(enum.enumConstants)) prop.set(prop.get().next(enum.enumConstants))
@ -354,7 +358,7 @@ abstract class EnumRectangleButtonPanel<out S : AbstractContainerScreen<*>, T :
} }
} }
open class LargeRectangleButtonPanel<out S : AbstractContainerScreen<*>>( open class LargeRectangleButtonPanel<out S : Screen>(
screen: S, screen: S,
parent: EditablePanel<*>?, parent: EditablePanel<*>?,
x: Float = 0f, x: Float = 0f,
@ -385,7 +389,7 @@ open class LargeRectangleButtonPanel<out S : AbstractContainerScreen<*>>(
} }
} }
open class LargeEnumRectangleButtonPanel<out S : AbstractContainerScreen<*>, T : Enum<T>>( open class LargeEnumRectangleButtonPanel<out S : Screen, T : Enum<T>>(
screen: S, screen: S,
parent: EditablePanel<*>?, parent: EditablePanel<*>?,
x: Float = 0f, x: Float = 0f,
@ -407,7 +411,7 @@ open class LargeEnumRectangleButtonPanel<out S : AbstractContainerScreen<*>, T :
} }
} }
open class SmallRectangleButtonPanel<out S : AbstractContainerScreen<*>>( open class SmallRectangleButtonPanel<out S : Screen>(
screen: S, screen: S,
parent: EditablePanel<*>?, parent: EditablePanel<*>?,
x: Float = 0f, x: Float = 0f,
@ -438,7 +442,7 @@ open class SmallRectangleButtonPanel<out S : AbstractContainerScreen<*>>(
} }
} }
open class SmallEnumRectangleButtonPanel<out S : AbstractContainerScreen<*>, T : Enum<T>>( open class SmallEnumRectangleButtonPanel<out S : Screen, T : Enum<T>>(
screen: S, screen: S,
parent: EditablePanel<*>?, parent: EditablePanel<*>?,
x: Float = 0f, x: Float = 0f,

View File

@ -95,13 +95,7 @@ open class DiscreteScrollBarPanel<S : Screen> @JvmOverloads constructor(
return return
} }
val newValue = if (value < 0) { val newValue = value.coerceAtLeast(0).coerceAtMost(maxScroll.invoke(this))
0
} else if (value > maxScroll.invoke(this)) {
maxScroll.invoke(this)
} else {
value
}
if (newValue != field) { if (newValue != field) {
val old = field val old = field

View File

@ -0,0 +1,79 @@
package ru.dbotthepony.mc.otm.client.screen.panels
import com.mojang.blaze3d.platform.InputConstants
import net.minecraft.client.gui.screens.Screen
import ru.dbotthepony.mc.otm.client.render.SkinElement
import ru.dbotthepony.mc.otm.client.render.Widgets
open class HeightControls<out S : Screen>(
screen: S,
parent: EditablePanel<*>?,
x: Float = 0f,
y: Float = 0f,
val callback: (isIncrease: Boolean) -> Status
) : BackgroundPanel<S>(screen, parent, x, y, WIDTH, HEIGHT) {
enum class Status(val canIncrease: Boolean, val canDecrease: Boolean) {
NONE(false, false),
INCREASE(true, false),
DECREASE(false, true),
EITHER(true, true);
companion object {
fun of(canIncrease: Boolean, canDecrease: Boolean): Status {
return if (canIncrease && canDecrease) {
EITHER
} else if (canIncrease) {
INCREASE
} else if (canDecrease) {
DECREASE
} else {
NONE
}
}
}
}
var controlStatus: Status = Status.EITHER
set(value) {
if (field == value)
return
field = value
increase.isDisabled = !value.canIncrease
decrease.isDisabled = !value.canDecrease
}
open inner class Control(val isIncrease: Boolean) : RectangleButtonPanel<S>(screen, this@HeightControls, width = BUTTON_WIDTH, height = BUTTON_HEIGHT) {
override val PRESSED: SkinElement = if (isIncrease) Widgets.ARROW_DOWN_BUTTON_PRESSED else Widgets.ARROW_UP_BUTTON_PRESSED
override val HOVERED: SkinElement = if (isIncrease) Widgets.ARROW_DOWN_BUTTON_HOVERED else Widgets.ARROW_UP_BUTTON_HOVERED
override val IDLE: SkinElement = if (isIncrease) Widgets.ARROW_DOWN_BUTTON_IDLE else Widgets.ARROW_UP_BUTTON_IDLE
override val DISABLED: SkinElement = if (isIncrease) Widgets.ARROW_DOWN_BUTTON_DISABLED else Widgets.ARROW_UP_BUTTON_DISABLED
init {
dock = Dock.TOP
dockBottom = 2f
}
override fun onClick(clickButton: Int) {
if (clickButton == InputConstants.MOUSE_BUTTON_LEFT) {
this@HeightControls.onClick(isIncrease)
} else if (clickButton == InputConstants.MOUSE_BUTTON_RIGHT) {
this@HeightControls.onClick(!isIncrease)
}
}
}
val decrease = Control(false)
val increase = Control(true)
open fun onClick(isIncrease: Boolean) {
controlStatus = callback.invoke(isIncrease)
}
companion object {
const val BUTTON_WIDTH = 18f
const val BUTTON_HEIGHT = 6f
const val WIDTH = BUTTON_WIDTH + 4f
const val HEIGHT = BUTTON_HEIGHT * 2f + 8f
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 KiB

After

Width:  |  Height:  |  Size: 3.2 KiB