132 lines
3.8 KiB
Kotlin
132 lines
3.8 KiB
Kotlin
package net.shadowfacts.phycon.network.block.terminal
|
|
|
|
import com.mojang.blaze3d.platform.GlStateManager
|
|
import net.minecraft.client.gui.DrawableHelper
|
|
import net.minecraft.client.gui.screen.ingame.HandledScreen
|
|
import net.minecraft.client.gui.widget.TextFieldWidget
|
|
import net.minecraft.client.util.math.MatrixStack
|
|
import net.minecraft.entity.player.PlayerInventory
|
|
import net.minecraft.screen.slot.Slot
|
|
import net.minecraft.screen.slot.SlotActionType
|
|
import net.minecraft.text.LiteralText
|
|
import net.minecraft.text.Text
|
|
import net.minecraft.util.Identifier
|
|
import net.shadowfacts.phycon.PhysicalConnectivity
|
|
import org.lwjgl.glfw.GLFW
|
|
|
|
/**
|
|
* @author shadowfacts
|
|
*/
|
|
// todo: translate title
|
|
class TerminalScreen(handler: TerminalScreenHandler, playerInv: PlayerInventory, title: Text): HandledScreen<TerminalScreenHandler>(handler, playerInv, title) {
|
|
companion object {
|
|
val BACKGROUND = Identifier(PhysicalConnectivity.MODID, "textures/gui/terminal.png")
|
|
}
|
|
|
|
private lateinit var searchBox: TextFieldWidget
|
|
|
|
init {
|
|
backgroundWidth = 252
|
|
backgroundHeight = 222
|
|
}
|
|
|
|
override fun init() {
|
|
super.init()
|
|
|
|
client!!.keyboard.setRepeatEvents(true)
|
|
|
|
searchBox = TextFieldWidget(textRenderer, x + 138, y + 6, 80, 9, LiteralText("Search"))
|
|
searchBox.setMaxLength(50)
|
|
// setHasBorder is actually setDrawsBackground
|
|
searchBox.setHasBorder(false)
|
|
searchBox.isVisible = true
|
|
searchBox.setSelected(true)
|
|
searchBox.setEditableColor(0xffffff)
|
|
children.add(searchBox)
|
|
}
|
|
|
|
override fun tick() {
|
|
super.tick()
|
|
searchBox.tick()
|
|
}
|
|
|
|
override fun drawForeground(matrixStack: MatrixStack, mouseX: Int, mouseY: Int) {
|
|
textRenderer.draw(matrixStack, title, 65f, 6f, 0x404040)
|
|
textRenderer.draw(matrixStack, playerInventory.displayName, 65f, backgroundHeight - 94f, 0x404040)
|
|
// todo: translate this
|
|
textRenderer.draw(matrixStack, "Buffer", 7f, 6f, 0x404040)
|
|
}
|
|
|
|
override fun drawBackground(matrixStack: MatrixStack, delta: Float, mouseX: Int, mouseY: Int) {
|
|
renderBackground(matrixStack)
|
|
|
|
GlStateManager.color4f(1f, 1f, 1f, 1f)
|
|
client!!.textureManager.bindTexture(BACKGROUND)
|
|
val x = (width - backgroundWidth) / 2
|
|
val y = (height - backgroundHeight) / 2
|
|
drawTexture(matrixStack, x, y, 0, 0, backgroundWidth, backgroundHeight)
|
|
}
|
|
|
|
override fun render(matrixStack: MatrixStack, mouseX: Int, mouseY: Int, delta: Float) {
|
|
super.render(matrixStack, mouseX, mouseY, delta)
|
|
|
|
searchBox.render(matrixStack, mouseX, mouseY, delta)
|
|
|
|
drawMouseoverTooltip(matrixStack, mouseX, mouseY)
|
|
}
|
|
|
|
@ExperimentalUnsignedTypes
|
|
fun drawSlotUnderlay(matrixStack: MatrixStack, slot: Slot) {
|
|
if (!handler.isBufferSlot(slot.id)) {
|
|
return
|
|
}
|
|
|
|
val mode = handler.terminal.internalBuffer.getMode(slot.id - handler.bufferSlotsStart)
|
|
val color: UInt = when (mode) {
|
|
TerminalBufferInventory.Mode.TO_NETWORK -> 0xFFFF0000u
|
|
TerminalBufferInventory.Mode.FROM_NETWORK -> 0xFF00FF00u
|
|
else -> return
|
|
}
|
|
DrawableHelper.fill(matrixStack, slot.x, slot.y, slot.x + 16, slot.y + 16, color.toInt())
|
|
}
|
|
|
|
override fun onMouseClick(slot: Slot?, i: Int, j: Int, slotActionType: SlotActionType?) {
|
|
super.onMouseClick(slot, i, j, slotActionType)
|
|
|
|
// don't unfocus the search box on mouse click
|
|
searchBox.setSelected(true)
|
|
}
|
|
|
|
override fun charTyped(c: Char, i: Int): Boolean {
|
|
val oldText = searchBox.text
|
|
if (searchBox.charTyped(c, i)) {
|
|
if (searchBox.text != oldText) {
|
|
search()
|
|
}
|
|
return true
|
|
}
|
|
|
|
return super.charTyped(c, i)
|
|
}
|
|
|
|
override fun keyPressed(key: Int, j: Int, k: Int): Boolean {
|
|
val oldText = searchBox.text
|
|
if (searchBox.keyPressed(key, j, k)) {
|
|
if (searchBox.text != oldText) {
|
|
search()
|
|
}
|
|
return true
|
|
}
|
|
return if (searchBox.isFocused && searchBox.isVisible && key != GLFW.GLFW_KEY_ESCAPE) {
|
|
true
|
|
} else {
|
|
super.keyPressed(key, j, k)
|
|
}
|
|
}
|
|
|
|
private fun search() {
|
|
screenHandler.search(searchBox.text)
|
|
}
|
|
|
|
}
|