mirror of
https://github.com/GTNewHorizons/BetterP2P.git
synced 2026-11-24 22:04:32 +00:00
Adv. Mem Card: Add a "button" to resize the GUI.
Click on the black box to cycle through GUI scales. Will need to add a texture in the future. Also fixed some janky scrollbar behavior.
This commit is contained in:
1 parent
0809143b8e
commit
887e8b2593
9 files changed
+95
-19
No files matched your search
@@ -1,8 +1,10 @@
|
||||
package com.projecturanus.betterp2p.capability
|
||||
|
||||
import com.projecturanus.betterp2p.client.gui.widget.GuiScale
|
||||
import com.projecturanus.betterp2p.item.BetterMemoryCardModes
|
||||
import com.projecturanus.betterp2p.network.NONE
|
||||
|
||||
data class MemoryInfo(var selectedEntry: Long = NONE,
|
||||
var frequency: Long = 0,
|
||||
var mode: BetterMemoryCardModes = BetterMemoryCardModes.OUTPUT)
|
||||
var mode: BetterMemoryCardModes = BetterMemoryCardModes.OUTPUT,
|
||||
var gui: GuiScale = GuiScale.DYNAMIC)
|
||||
@@ -37,7 +37,8 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
|
||||
private val tableX = 9
|
||||
private val tableY = 19
|
||||
|
||||
private val scale = GuiScale.DYNAMIC
|
||||
private var scale = msg.memoryInfo.gui
|
||||
private val resizeButton: WidgetButton
|
||||
|
||||
private var _ySize: Lazy<Int> = lazy { 242 }
|
||||
private var ySize: Int
|
||||
@@ -46,8 +47,8 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
|
||||
_ySize = lazy { value }
|
||||
}
|
||||
|
||||
private var scrollBar: WidgetScrollBar
|
||||
private lateinit var searchBar: MEGuiTextField
|
||||
private val scrollBar: WidgetScrollBar
|
||||
private val searchBar: MEGuiTextField
|
||||
|
||||
private val infos = InfoList(msg.infos.map(::InfoWrapper), ::searchText)
|
||||
|
||||
@@ -79,6 +80,21 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
|
||||
scrollBar = WidgetScrollBar(0, 0)
|
||||
col = WidgetP2PColumn(this, infos,0, 0,
|
||||
::selectedInfo, ::mode, scrollBar)
|
||||
searchBar = MEGuiTextField(100, 10)
|
||||
resizeButton = object: WidgetButton(0, 0, 32, 32, { "gui.advanced_memory_card.gui_scale.resize" }) {
|
||||
override fun mousePressed(mouseX: Int, mouseY: 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
|
||||
}
|
||||
initGui()
|
||||
super.func_146113_a(mc.soundHandler)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Note this is called on resize too.
|
||||
@@ -86,17 +102,19 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
|
||||
super.initGui()
|
||||
checkInfo()
|
||||
val h = height.coerceAtLeast(256)
|
||||
val numEntries = scale.size(h - 75)
|
||||
if (scale.minHeight > h) {
|
||||
scale = GuiScale.DYNAMIC
|
||||
}
|
||||
val numEntries = scale.size(height - 75)
|
||||
ySize = (numEntries * P2P_ENTRY_HEIGHT) + 75 + (numEntries - 1)
|
||||
guiTop = (h - ySize) / 2
|
||||
guiLeft = (width - GUI_WIDTH) / 2
|
||||
|
||||
scrollBar.displayX = guiLeft + 268
|
||||
scrollBar.displayY = guiTop + 19
|
||||
scrollBar.height = numEntries * P2P_ENTRY_HEIGHT + (numEntries - 1)
|
||||
scrollBar.height = numEntries * P2P_ENTRY_HEIGHT + (numEntries - 1) - 7
|
||||
scrollBar.setRange(0, infos.size.coerceIn(0, (infos.size - numEntries).coerceAtLeast(0)), 23)
|
||||
|
||||
searchBar = MEGuiTextField(100, 10)
|
||||
searchBar.setMaxStringLength(40)
|
||||
searchBar.x = guiLeft + 163
|
||||
searchBar.y = guiTop + 5
|
||||
@@ -108,6 +126,7 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
|
||||
modeButton.xPosition = guiLeft + 8
|
||||
modeButton.yPosition = guiTop + ySize - 52
|
||||
|
||||
resizeButton.setPosition(guiLeft - 32, guiTop + 2)
|
||||
infos.refresh()
|
||||
checkInfo()
|
||||
refreshOverlay()
|
||||
@@ -128,7 +147,7 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
|
||||
}
|
||||
|
||||
private fun syncMemoryInfo() {
|
||||
ModNetwork.channel.sendToServer(C2SUpdateInfo(MemoryInfo(infos.selectedEntry, selectedInfo?.frequency ?: 0, mode)))
|
||||
ModNetwork.channel.sendToServer(C2SUpdateInfo(MemoryInfo(infos.selectedEntry, selectedInfo?.frequency ?: 0, mode, scale)))
|
||||
}
|
||||
|
||||
private fun drawInformation() {
|
||||
@@ -153,6 +172,7 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
|
||||
searchBar.drawTextBox()
|
||||
col.render(this, mouseX, mouseY, partialTicks)
|
||||
modeButton.drawButton(mc, mouseX, mouseY)
|
||||
resizeButton.draw(mouseX, mouseY, partialTicks)
|
||||
fontRendererObj.drawString(I18n.format("item.advanced_memory_card.name"), guiLeft + tableX, guiTop + 6, 0)
|
||||
if (modeButton.func_146115_a()) {
|
||||
descriptionLines.clear()
|
||||
@@ -163,6 +183,8 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
|
||||
drawInformation()
|
||||
if (searchBar.isMouseIn(mouseX, mouseY)) {
|
||||
drawHoveringText(sortRules, guiLeft, guiTop + ySize - 40, fontRendererObj)
|
||||
} else if (resizeButton.isHovering(mouseX, mouseY)) {
|
||||
drawHoveringText(listOf(I18n.format(resizeButton.hoverText())), mouseX, mouseY, fontRendererObj)
|
||||
}
|
||||
super.drawScreen(mouseX, mouseY, partialTicks)
|
||||
}
|
||||
@@ -208,6 +230,7 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
|
||||
switchMode()
|
||||
}
|
||||
scrollBar.click(mouseX, mouseY)
|
||||
resizeButton.mousePressed(mouseX, mouseY)
|
||||
searchBar.mouseClicked(mouseX, mouseY, mouseButton)
|
||||
if (mouseButton == 1 && searchBar.isMouseIn(mouseX, mouseY)) {
|
||||
this.searchBar.text = ""
|
||||
@@ -216,6 +239,10 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
|
||||
super.mouseClicked(mouseX, mouseY, mouseButton)
|
||||
}
|
||||
|
||||
override fun mouseMovedOrUp(mouseX: Int, mouseY: Int, state: Int) {
|
||||
super.mouseMovedOrUp(mouseX, mouseY, state)
|
||||
if (state != -1) scrollBar.moving = false
|
||||
}
|
||||
override fun mouseClickMove(mouseX: Int, mouseY: Int, clickedMouseButton: Int, timeSinceLastClick: Long) {
|
||||
scrollBar.click(mouseX, mouseY)
|
||||
super.mouseClickMove(mouseX, mouseY, clickedMouseButton, timeSinceLastClick)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.projecturanus.betterp2p.client.gui.widget
|
||||
|
||||
enum class GuiScale(inline val size: (Int) -> Int) {
|
||||
SMALL({ 4 }),
|
||||
NORMAL({ 6 }),
|
||||
LARGE({ 10 }),
|
||||
DYNAMIC({ availableHeight -> availableHeight / P2P_ENTRY_HEIGHT})
|
||||
enum class GuiScale(inline val size: (Int) -> Int, val minHeight: Int) {
|
||||
SMALL({ 4 }, 0),
|
||||
NORMAL({ 6 }, 326),
|
||||
LARGE({ 8 }, 409),
|
||||
DYNAMIC({ availableHeight -> availableHeight / P2P_ENTRY_HEIGHT }, 0)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.projecturanus.betterp2p.client.gui.widget
|
||||
|
||||
import net.minecraft.client.gui.GuiButton
|
||||
|
||||
/**
|
||||
* Widget button for stuff.
|
||||
* @param x
|
||||
* @param y
|
||||
* @param width
|
||||
* @param height
|
||||
* @param hoverText - unlocalized text to display when mouse hovering
|
||||
*/
|
||||
abstract class WidgetButton(x: Int, y: Int, private var width: Int, private var height: Int,
|
||||
val hoverText: () -> String): GuiButton(0, x, y, width, height, "") {
|
||||
|
||||
abstract fun mousePressed(mouseX: Int, mouseY: Int)
|
||||
|
||||
fun draw(mouseX: Int, mouseY: Int, partial: Float) {
|
||||
drawRect(xPosition, yPosition, xPosition + width, yPosition + height, 0xFF shl 24)
|
||||
}
|
||||
|
||||
fun setPosition(x: Int, y: Int) {
|
||||
this.xPosition = x
|
||||
this.yPosition = y
|
||||
}
|
||||
|
||||
fun setSize(width: Int, height: Int) {
|
||||
this.width = width
|
||||
this.height = height
|
||||
}
|
||||
|
||||
fun isHovering(mouseX: Int, mouseY: Int): Boolean {
|
||||
return mouseX > xPosition && mouseX < xPosition + width && mouseY > yPosition && mouseY < yPosition + height
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,6 @@ class WidgetP2PColumn(private val gui: GuiAdvancedMemoryCard,
|
||||
x, y + i * (P2P_ENTRY_HEIGHT + 1))
|
||||
entries.add(widget)
|
||||
}
|
||||
scrollBar.setRange(0, entries.size, 23)
|
||||
}
|
||||
|
||||
fun setPosition(x: Int, y: Int) {
|
||||
|
||||
@@ -14,7 +14,7 @@ class WidgetScrollBar(var displayX: Int, var displayY: Int) {
|
||||
var onScroll: () -> Unit = {}
|
||||
|
||||
var currentScroll = 0
|
||||
|
||||
var moving = false
|
||||
fun <T> draw(g: T) where T: TextureBound, T: Gui {
|
||||
g.bindTexture("minecraft", "textures/gui/container/creative_inventory/tabs.png")
|
||||
if (getRange() == 0) {
|
||||
@@ -48,12 +48,13 @@ class WidgetScrollBar(var displayX: Int, var displayY: Int) {
|
||||
if (getRange() == 0) {
|
||||
return
|
||||
}
|
||||
if (x > displayX && x <= displayX + width) {
|
||||
if (y > displayY && y <= displayY + height) {
|
||||
if (y > displayY && y <= displayY + height) {
|
||||
if (moving || (x > displayX && x <= displayX + width)) {
|
||||
currentScroll = y - displayY
|
||||
currentScroll = minScroll + currentScroll * 2 * getRange() / height
|
||||
currentScroll = currentScroll + 1 shr 1
|
||||
applyRange()
|
||||
moving = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import appeng.parts.p2p.PartP2PTunnel
|
||||
import com.projecturanus.betterp2p.MODID
|
||||
import com.projecturanus.betterp2p.capability.MemoryInfo
|
||||
import com.projecturanus.betterp2p.client.ClientCache
|
||||
import com.projecturanus.betterp2p.client.gui.widget.GuiScale
|
||||
import com.projecturanus.betterp2p.network.ModNetwork
|
||||
import com.projecturanus.betterp2p.network.NONE
|
||||
import com.projecturanus.betterp2p.network.S2CListP2P
|
||||
@@ -107,8 +108,9 @@ object ItemAdvancedMemoryCard : Item() {
|
||||
|
||||
if (stack.tagCompound == null) stack.tagCompound = NBTTagCompound()
|
||||
val compound = stack.tagCompound!!
|
||||
if (!compound.hasKey("gui")) compound.setByte("gui", GuiScale.DYNAMIC.ordinal.toByte())
|
||||
if (!compound.hasKey("selectedIndex", Constants.NBT.TAG_LONG)) compound.setLong("selectedIndex", NONE)
|
||||
return MemoryInfo(compound.getLong("selectedIndex"), compound.getLong("frequency"), BetterMemoryCardModes.values()[compound.getInteger("mode")])
|
||||
return MemoryInfo(compound.getLong("selectedIndex"), compound.getLong("frequency"), BetterMemoryCardModes.values()[compound.getInteger("mode")], GuiScale.values()[compound.getByte("gui").toInt()])
|
||||
}
|
||||
|
||||
fun writeInfo(stack: ItemStack, info: MemoryInfo) {
|
||||
@@ -119,5 +121,6 @@ object ItemAdvancedMemoryCard : Item() {
|
||||
compound.setLong("selectedIndex", info.selectedEntry)
|
||||
compound.setLong("frequency", info.frequency)
|
||||
compound.setInteger("mode", info.mode.ordinal)
|
||||
compound.setByte("gui", info.gui.ordinal.toByte())
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.projecturanus.betterp2p.network
|
||||
|
||||
import com.projecturanus.betterp2p.capability.MemoryInfo
|
||||
import com.projecturanus.betterp2p.client.gui.widget.GuiScale
|
||||
import com.projecturanus.betterp2p.item.BetterMemoryCardModes
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessage
|
||||
import io.netty.buffer.ByteBuf
|
||||
@@ -9,10 +10,13 @@ fun writeMemoryInfo(buf: ByteBuf, info: MemoryInfo) {
|
||||
buf.writeLong(info.selectedEntry)
|
||||
buf.writeLong(info.frequency)
|
||||
buf.writeInt(info.mode.ordinal)
|
||||
buf.writeByte(info.gui.ordinal)
|
||||
}
|
||||
|
||||
fun readMemoryInfo(buf: ByteBuf): MemoryInfo {
|
||||
return MemoryInfo(buf.readLong(), buf.readLong(), BetterMemoryCardModes.values()[buf.readInt()])
|
||||
return MemoryInfo(buf.readLong(), buf.readLong(),
|
||||
BetterMemoryCardModes.values()[buf.readInt()],
|
||||
GuiScale.values()[buf.readByte().toInt()])
|
||||
}
|
||||
|
||||
class C2SUpdateInfo(var info: MemoryInfo = MemoryInfo()) : IMessage {
|
||||
|
||||
Reference in new issue
Block a user