From ba11840e1cf37eaf7d0c84c167eef489f9f28a0d Mon Sep 17 00:00:00 2001 From: Firenoo <49818773+firenoo@users.noreply.github.com> Date: Wed, 10 May 2023 21:08:03 -0700 Subject: [PATCH] More cleanup + texture for type change Some more code cleanup (spacing and labels) --- .../client/gui/GuiAdvancedMemoryCard.kt | 195 ++++++++++++++---- .../betterp2p/client/gui/GuiHelper.kt | 33 +++ .../client/gui/widget/WidgetButton.kt | 31 +-- .../client/gui/widget/WidgetP2PDevice.kt | 27 +-- 4 files changed, 207 insertions(+), 79 deletions(-) diff --git a/src/main/java/com/projecturanus/betterp2p/client/gui/GuiAdvancedMemoryCard.kt b/src/main/java/com/projecturanus/betterp2p/client/gui/GuiAdvancedMemoryCard.kt index 9dd826d..ab271f8 100644 --- a/src/main/java/com/projecturanus/betterp2p/client/gui/GuiAdvancedMemoryCard.kt +++ b/src/main/java/com/projecturanus/betterp2p/client/gui/GuiAdvancedMemoryCard.kt @@ -1,6 +1,9 @@ package com.projecturanus.betterp2p.client.gui import appeng.client.gui.widgets.MEGuiTextField +import appeng.parts.p2p.PartP2PLiquids +import appeng.parts.p2p.PartP2PRedstone +import appeng.parts.p2p.PartP2PTunnelME import com.projecturanus.betterp2p.BetterP2P import com.projecturanus.betterp2p.MODID import com.projecturanus.betterp2p.capability.MemoryInfo @@ -11,11 +14,14 @@ import com.projecturanus.betterp2p.client.gui.widget.* import com.projecturanus.betterp2p.item.BetterMemoryCardModes import com.projecturanus.betterp2p.item.MAX_TOOLTIP_LENGTH import com.projecturanus.betterp2p.network.* +import com.projecturanus.betterp2p.util.p2p.ClientTunnelInfo import com.projecturanus.betterp2p.util.p2p.TunnelInfo +import net.minecraft.client.Minecraft import net.minecraft.client.gui.FontRenderer import net.minecraft.client.gui.GuiScreen import net.minecraft.client.renderer.Tessellator import net.minecraft.client.resources.I18n +import net.minecraft.init.Blocks import net.minecraft.util.ResourceLocation import org.lwjgl.input.Keyboard import org.lwjgl.input.Mouse @@ -55,7 +61,9 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound { private var mode = msg.memoryInfo.mode private val modeButton: WidgetButton - private var type: TunnelInfo? = BetterP2P.proxy.getP2PFromIndex(msg.memoryInfo.type) + private var type: ClientTunnelInfo? = BetterP2P.proxy.getP2PFromIndex(msg.memoryInfo.type) as? ClientTunnelInfo + private var typeCooldown: Long = System.currentTimeMillis() + private var typeUpdateReady: Boolean = true private val typeButton: WidgetButton private val scrollBar: WidgetScrollBar @@ -90,19 +98,29 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound { ::selectedInfo, ::mode, scrollBar) searchBar = MEGuiTextField(100, 10) resizeButton = object: WidgetButton(this, 0, 0, 32, 32) { - override fun mousePressed(mouseX: Int, mouseY: Int) { + override fun mousePressed(mouseX: Int, mouseY: Int, button: Int) { if (super.mousePressed(mc, mouseX, mouseY)) { - scale = when(scale) { - GuiScale.DYNAMIC -> GuiScale.SMALL - GuiScale.SMALL -> GuiScale.NORMAL - GuiScale.NORMAL -> GuiScale.LARGE - GuiScale.LARGE -> GuiScale.DYNAMIC + if (button == 0) { + scale = when (scale) { + GuiScale.DYNAMIC -> GuiScale.SMALL + GuiScale.SMALL -> GuiScale.NORMAL + GuiScale.NORMAL -> GuiScale.LARGE + GuiScale.LARGE -> GuiScale.DYNAMIC + } + } else if (button == 1) { + scale = when (scale) { + GuiScale.DYNAMIC -> GuiScale.LARGE + GuiScale.LARGE -> GuiScale.NORMAL + GuiScale.NORMAL -> GuiScale.SMALL + GuiScale.SMALL -> GuiScale.DYNAMIC + } } initGui() super.func_146113_a(mc.soundHandler) } } } + modeButton = object: WidgetButton(this, 0, 0, 32, 32) { val modeDescriptions: List> = listOf( fmtTooltips( @@ -126,7 +144,7 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound { hoverText = modeDescriptions[mode.ordinal] } - override fun mousePressed(mouseX: Int, mouseY: Int) { + override fun mousePressed(mouseX: Int, mouseY: Int, button: Int) { if (super.mousePressed(mc, mouseX, mouseY)) { mode = mode.next() hoverText = modeDescriptions[mode.ordinal] @@ -136,31 +154,89 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound { } } } + typeButton = object: WidgetButton(this, 0, 0, 32, 32) { val types = BetterP2P.proxy.getP2PTypeList() - private var index = 0 + private var index = + if (type != null) { + types.first { it.index == type?.index}.index + } else { + types.size + } + private val me = BetterP2P.proxy.getP2PFromClass(PartP2PTunnelME::class.java) as ClientTunnelInfo + private val fluid = BetterP2P.proxy.getP2PFromClass(PartP2PLiquids::class.java) as ClientTunnelInfo + private val redstone = BetterP2P.proxy.getP2PFromClass(PartP2PRedstone::class.java) as ClientTunnelInfo - private fun nextType(): TunnelInfo { - // range: [0, types.size - 1] - index = (index + 1).rem(types.size) - return types[index] + init { + hoverText = if (type == null) { + listOf("Any") + } else { + listOf(type!!.stack.displayName) + } } - override fun mousePressed(mouseX: Int, mouseY: Int) { + private fun nextType(reverse: Boolean): ClientTunnelInfo? { + // range: [0, types.size] + return if (reverse) { + index = (index - 1).rem(types.size + 1) + types.getOrNull(index) as? ClientTunnelInfo + } else { + index = (index + 1).rem(types.size + 1) + types.getOrNull(index) as? ClientTunnelInfo + } + } + + override fun mousePressed(mouseX: Int, mouseY: Int, button: Int) { if (super.mousePressed(mc, mouseX, mouseY)) { - type = nextType() - hoverText = listOf(type!!.stack.displayName) + if (button == 1) { + // open type selector? + } else { + type = nextType(false) + } + hoverText = + if (type == null) { + listOf("Any") + } else { + listOf(type!!.stack.displayName) + } requestRefresh() super.func_146113_a(mc.soundHandler) } -// setTexCoords((mode.ordinal + 3) * 32.0, 232.0) -// syncMemoryInfo() } + override fun draw(mc: Minecraft, mouseX: Int, mouseY: Int, partialTicks: Float) { + val tessellator = Tessellator.instance + drawBG(tessellator, mouseX, mouseY, partialTicks) + if (type != null) { + drawBlockIcon(mc, type!!.icon(), type!!.stack.iconIndex, + x = this.xPosition + 2, + y = this.yPosition + 2, + width = 28.0, + height = 28.0) + } else { + drawBlockIcon(mc, redstone.icon(), types[0].stack.iconIndex, + x = this.xPosition + 12, + y = this.yPosition + 12, + width = 18.0, + height = 18.0) + drawBlockIcon(mc, fluid.icon(), types[0].stack.iconIndex, + x = this.xPosition + 7, + y = this.yPosition + 7, + width = 18.0, + height = 18.0) + drawBlockIcon(mc, me.icon(), types[0].stack.iconIndex, + x = this.xPosition + 2, + y = this.yPosition + 2, + width = 18.0, + height = 18.0) + } + } } } - // Note this is called on resize too. + /** + * Called on resize. Initializes GUI elements. + */ override fun initGui() { super.initGui() checkInfo() @@ -187,11 +263,17 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound { col.resize(scale, h - 75) col.setPosition(guiLeft + tableX, guiTop + tableY) - modeButton.setPosition(guiLeft - 32, guiTop + 34) - modeButton.setTexCoords((mode.ordinal + 3) * 32.0, 232.0) - resizeButton.setPosition(guiLeft - 32, guiTop + 2) resizeButton.setTexCoords(scale.ordinal * 32.0, 200.0) + buttonList.add(resizeButton) + + modeButton.setPosition(guiLeft - 32, guiTop + 34) + modeButton.setTexCoords((mode.ordinal + 3) * 32.0, 232.0) + buttonList.add(modeButton) + + typeButton.setPosition(guiLeft - 32, guiTop + 66) + typeButton.setTexCoords(0.0, 200.0) + buttonList.add(typeButton) infos.refresh() checkInfo() @@ -213,6 +295,13 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound { * TODO: Implement */ fun requestRefresh() { + // Let's make sure the server only gets the last request in the last 250 ms, so we don't get spammed. + val time = System.currentTimeMillis() + typeCooldown = time + 250 + if (typeUpdateReady && time < typeCooldown) { + ModNetwork.channel.sendToServer(C2SRefreshP2PList(type?.index ?: TUNNEL_ANY)) + typeUpdateReady = false + } } fun refreshInfo(infos: List) { @@ -239,10 +328,11 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound { // Draw the stuff that resets GL state first fontRendererObj.drawString(I18n.format("item.advanced_memory_card.name"), guiLeft + tableX, guiTop + 6, 0) searchBar.drawTextBox() - resizeButton.draw(mc, mouseX, mouseY, partialTicks) - modeButton.draw(mc, mouseX, mouseY, partialTicks) + buttonList.forEach { it as WidgetButton + it.draw(mc, mouseX, mouseY, partialTicks) + } - // Now do our drawing + // Now draw our P2P entries GL11.glPushAttrib(GL11.GL_BLEND or GL11.GL_TEXTURE_2D or GL11.GL_COLOR) GL11.glEnable(GL11.GL_BLEND) GL11.glEnable(GL11.GL_TEXTURE_2D) @@ -252,16 +342,27 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound { col.render(this, mouseX, mouseY, partialTicks) // The GL state is already messed up here by string drawing but oh well GL11.glPopAttrib() - if (searchBar.isMouseIn(mouseX, mouseY)) { - drawHoveringText(sortRules, guiLeft, guiTop + ySize - 40, fontRendererObj) - } else if (resizeButton.isHovering(mouseX, mouseY)) { - drawHoveringText(resizeButton.hoverText, mouseX, mouseY, fontRendererObj) - } else if (modeButton.isHovering(mouseX, mouseY)) { - drawHoveringText(modeButton.hoverText, mouseX, mouseY, fontRendererObj) - } else { - col.mouseHovered(mouseX, mouseY) + var drewHoverText = false + run finished@ { + buttonList.forEach { it as WidgetButton + if (it.isHovering(mouseX, mouseY)) { + drawHoveringText(it.hoverText, mouseX, mouseY + 10, fontRendererObj) + drewHoverText = true + return@finished + } + } + } + if (!drewHoverText) { + if (searchBar.isMouseIn(mouseX, mouseY)) { + drawHoveringText(sortRules, guiLeft, guiTop + ySize - 40, fontRendererObj) + } else { + col.mouseHovered(mouseX, mouseY) + } + } + if (!typeUpdateReady && System.currentTimeMillis() > typeCooldown) { + typeUpdateReady = true + ModNetwork.channel.sendToServer(C2SRefreshP2PList(type?.index ?: TUNNEL_ANY)) } - super.drawScreen(mouseX, mouseY, partialTicks) } /** @@ -294,8 +395,9 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound { override fun mouseClicked(mouseX: Int, mouseY: Int, mouseButton: Int) { col.mouseClicked(mouseX, mouseY, mouseButton) scrollBar.click(mouseX, mouseY) - modeButton.mousePressed(mouseX, mouseY) - resizeButton.mousePressed(mouseX, mouseY) + modeButton.mousePressed(mouseX, mouseY, mouseButton) + resizeButton.mousePressed(mouseX, mouseY, mouseButton) + typeButton.mousePressed(mouseX, mouseY, mouseButton) searchBar.mouseClicked(mouseX, mouseY, mouseButton) if (mouseButton == 1 && searchBar.isMouseIn(mouseX, mouseY)) { this.searchBar.text = "" @@ -335,21 +437,30 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound { bindTexture(BACKGROUND) val tessellator = Tessellator.instance // Draw the top part - drawTexturedQuad(tessellator, guiLeft.toDouble(), guiTop.toDouble(), - (guiLeft + GUI_WIDTH).toDouble(), guiTop + 60.0, + drawTexturedQuad(tessellator, + x0 = guiLeft.toDouble(), + y0 = guiTop.toDouble(), + x1 = (guiLeft + GUI_WIDTH).toDouble(), + y1 = guiTop + 60.0, u0 = 0.0, v0 = 0.0, u1 = 1.0, v1 = 60.0 / GUI_TEX_HEIGHT) // Draw P2P segments val p2pHeight = P2PEntryConstants.HEIGHT + 1.0 for (i in 0 until scale.size(ySize - 75) - 2) { - drawTexturedQuad(tessellator, guiLeft.toDouble(), guiTop + 60.0 + p2pHeight * i, - (guiLeft + GUI_WIDTH).toDouble(), guiTop + 60.0 + p2pHeight * (i + 1), + drawTexturedQuad(tessellator, + x0 = guiLeft.toDouble(), + y0 = guiTop + 60.0 + p2pHeight * i, + x1 = (guiLeft + GUI_WIDTH).toDouble(), + y1 = guiTop + 60.0 + p2pHeight * (i + 1), u0 = 0.0, v0 = 60.0 / GUI_TEX_HEIGHT, u1 = 1.0, v1 = 102.0 / GUI_TEX_HEIGHT) } // Draw Bottom - drawTexturedQuad(tessellator, guiLeft.toDouble(), guiTop + ySize - 98.0, - (guiLeft + GUI_WIDTH).toDouble(), (guiTop + ySize).toDouble(), + drawTexturedQuad(tessellator, + x0 = guiLeft.toDouble(), + y0 = guiTop + ySize - 98.0, + x1 = (guiLeft + GUI_WIDTH).toDouble(), + y1 = (guiTop + ySize).toDouble(), u0 = 0.0, v0 = 102.0 / GUI_TEX_HEIGHT, u1 = 1.0, v1 = 200.0 / GUI_TEX_HEIGHT) } diff --git a/src/main/java/com/projecturanus/betterp2p/client/gui/GuiHelper.kt b/src/main/java/com/projecturanus/betterp2p/client/gui/GuiHelper.kt index 034f947..733e098 100644 --- a/src/main/java/com/projecturanus/betterp2p/client/gui/GuiHelper.kt +++ b/src/main/java/com/projecturanus/betterp2p/client/gui/GuiHelper.kt @@ -2,7 +2,11 @@ package com.projecturanus.betterp2p.client.gui import cpw.mods.fml.relauncher.Side import cpw.mods.fml.relauncher.SideOnly +import net.minecraft.client.Minecraft +import net.minecraft.client.renderer.OpenGlHelper import net.minecraft.client.renderer.Tessellator +import net.minecraft.util.IIcon +import org.lwjgl.opengl.GL11 /** * Draws a textured quad. @@ -23,3 +27,32 @@ fun drawTexturedQuad(tessellator: Tessellator, tessellator.addVertexWithUV(x0, y0, 0.0, u0, v0) tessellator.draw() } + +/** + * Draw the icon on the Block Texture Atlas + */ +fun drawBlockIcon(mc: Minecraft, icon: IIcon, overlay: IIcon, + x: Int, y: Int, width: Double = 16.0, height: Double = 16.0) { + val tessellator = Tessellator.instance + mc.textureManager.bindTexture(mc.renderEngine.getResourceLocation(0)) + GL11.glPushAttrib(GL11.GL_BLEND or GL11.GL_TEXTURE_2D or GL11.GL_COLOR) + GL11.glEnable(GL11.GL_BLEND) + GL11.glEnable(GL11.GL_TEXTURE_2D) + GL11.glColor3f(255f, 255f, 255f) + OpenGlHelper.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0) + drawTexturedQuad(tessellator, + x0 = x.toDouble() + 2, + y0 = y.toDouble() + 2, + x1 = x + width - 2, + y1 = y + height - 2, + u0 = icon.minU.toDouble(), v0 = icon.minV.toDouble(), + u1 = icon.maxU.toDouble(), v1 = icon.maxV.toDouble()) + drawTexturedQuad(tessellator, + x0 = x.toDouble(), + y0 = y.toDouble(), + x1 = x + width, + y1 = y + height, + u0 = overlay.minU.toDouble(), v0 = overlay.minV.toDouble(), + u1 = overlay.maxU.toDouble(), v1 = overlay.maxV.toDouble()) + GL11.glPopAttrib() +} diff --git a/src/main/java/com/projecturanus/betterp2p/client/gui/widget/WidgetButton.kt b/src/main/java/com/projecturanus/betterp2p/client/gui/widget/WidgetButton.kt index 0e089c3..516eb10 100644 --- a/src/main/java/com/projecturanus/betterp2p/client/gui/widget/WidgetButton.kt +++ b/src/main/java/com/projecturanus/betterp2p/client/gui/widget/WidgetButton.kt @@ -25,10 +25,19 @@ abstract class WidgetButton(val gui: GuiAdvancedMemoryCard, x: Int, y: Int, widt private var texY = 0.0 var hoverText: List = listOf() - abstract fun mousePressed(mouseX: Int, mouseY: Int) + abstract fun mousePressed(mouseX: Int, mouseY: Int, button: Int) - fun draw(mc: Minecraft?, mouseX: Int, mouseY: Int, partialTicks: Float) { + open fun draw(mc: Minecraft, mouseX: Int, mouseY: Int, partialTicks: Float) { val tessellator = Tessellator.instance + drawBG(tessellator, mouseX, mouseY, partialTicks) + // Draw button icon + drawTexturedQuad(tessellator, xPosition + 1.0, yPosition + 1.0, + xPosition + width - 1.0, yPosition + height - 1.0, + u0 = texX / GUI_WIDTH, v0 = texY / GUI_TEX_HEIGHT, + u1 = (texX + width) / GUI_WIDTH, v1 = (texY + height) / GUI_TEX_HEIGHT) + } + + open fun drawBG(tessellator: Tessellator, mouseX: Int, mouseY: Int, partialTicks: Float) { gui.bindTexture(gui.BACKGROUND) GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f) field_146123_n = mouseX >= xPosition && mouseY >= yPosition && mouseX < xPosition + width && mouseY < yPosition + height @@ -36,17 +45,15 @@ abstract class WidgetButton(val gui: GuiAdvancedMemoryCard, x: Int, y: Int, widt GL11.glEnable(GL11.GL_BLEND) OpenGlHelper.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0) // Draw button backdrop - drawTexturedQuad(tessellator, xPosition.toDouble(), yPosition.toDouble(), - (xPosition + width).toDouble(), (yPosition + height).toDouble(), - u0 = (32.0 * k) / GUI_WIDTH, v0 = (232.0) / GUI_TEX_HEIGHT, - u1 = (32.0 * (k + 1)) / GUI_WIDTH, v1 = (232.0 + height) / GUI_TEX_HEIGHT) - // Draw button icon - drawTexturedQuad(tessellator, xPosition + 1.0, yPosition + 1.0, - xPosition + width - 1.0, yPosition + height - 1.0, - u0 = texX / GUI_WIDTH, v0 = texY / GUI_TEX_HEIGHT, - u1 = (texX + width) / GUI_WIDTH, v1 = (texY + height) / GUI_TEX_HEIGHT) - + drawTexturedQuad(tessellator, + x0 = xPosition.toDouble(), + y0 = yPosition.toDouble(), + x1 = (xPosition + width).toDouble(), + y1 = (yPosition + height).toDouble(), + u0 = (32.0 * k) / GUI_WIDTH, v0 = (232.0) / GUI_TEX_HEIGHT, + u1 = (32.0 * (k + 1)) / GUI_WIDTH, v1 = (232.0 + height) / GUI_TEX_HEIGHT) } + fun setPosition(x: Int, y: Int) { this.xPosition = x this.yPosition = y diff --git a/src/main/java/com/projecturanus/betterp2p/client/gui/widget/WidgetP2PDevice.kt b/src/main/java/com/projecturanus/betterp2p/client/gui/widget/WidgetP2PDevice.kt index 8d5afe2..845a856 100644 --- a/src/main/java/com/projecturanus/betterp2p/client/gui/widget/WidgetP2PDevice.kt +++ b/src/main/java/com/projecturanus/betterp2p/client/gui/widget/WidgetP2PDevice.kt @@ -45,7 +45,7 @@ class WidgetP2PDevice(private val selectedInfoProperty: KProperty0 } GL11.glColor3f(255f, 255f, 255f) // Draw our icons... - drawIcon(gui, info.icon, info.overlay, x + 3, y + 3) + drawBlockIcon(gui.mc, info.icon, info.overlay, x + 3, y + 3) gui.bindTexture(gui.BACKGROUND) if (info.output) { drawTexturedQuad(Tessellator.instance, x.toDouble(), y + 4.0, x + 16.0, y + 20.0, @@ -107,28 +107,5 @@ class WidgetP2PDevice(private val selectedInfoProperty: KProperty0 } } - private fun drawIcon(gui: GuiAdvancedMemoryCard, icon: IIcon, overlay: IIcon, x: Int, y: Int) { - val tessellator = Tessellator.instance - gui.bindTexture(gui.mc.renderEngine.getResourceLocation(0)) - GL11.glPushAttrib(GL11.GL_BLEND or GL11.GL_TEXTURE_2D or GL11.GL_COLOR) - GL11.glEnable(GL11.GL_BLEND) - GL11.glEnable(GL11.GL_TEXTURE_2D) - GL11.glColor3f(255f, 255f, 255f) - OpenGlHelper.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0) - drawTexturedQuad(tessellator, - x0 = x.toDouble() + 1, - y0 = y.toDouble() + 1, - x1 = x + 15.0, - y1 = y + 15.0, - u0 = icon.minU.toDouble(), v0 = icon.minV.toDouble(), - u1 = icon.maxU.toDouble(), v1 = icon.maxV.toDouble()) - drawTexturedQuad(tessellator, - x0 = x.toDouble(), - y0 = y.toDouble(), - x1 = x + 16.0, - y1 = y + 16.0, - u0 = overlay.minU.toDouble(), v0 = overlay.minV.toDouble(), - u1 = overlay.maxU.toDouble(), v1 = overlay.maxV.toDouble()) - GL11.glPopAttrib() - } + }