Add Inventory Buttons

This commit is contained in:
nea
2023-10-28 03:36:11 +02:00
committed by Linnea Gräf
parent 47fbb25ab2
commit e15406e22e
24 changed files with 683 additions and 9 deletions

View File

@@ -0,0 +1,97 @@
/*
* SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package moe.nea.firmament.util
import io.github.moulberry.moulconfig.gui.GuiContext
import me.shedaniel.math.Dimension
import me.shedaniel.math.Point
import me.shedaniel.math.Rectangle
import net.minecraft.client.gui.DrawContext
import net.minecraft.client.gui.screen.Screen
import net.minecraft.text.Text
abstract class FragmentGuiScreen(
val dismissOnOutOfBounds: Boolean = true
) : Screen(Text.literal("")) {
var popup: MoulConfigFragment? = null
fun createPopup(context: GuiContext, position: Point) {
popup = MoulConfigFragment(context, position) { popup = null }
}
override fun render(context: DrawContext, mouseX: Int, mouseY: Int, delta: Float) {
super.render(context, mouseX, mouseY, delta)
context.matrices.push()
context.matrices.translate(0f, 0f, 1000f)
popup?.render(context, mouseX, mouseY, delta)
context.matrices.pop()
}
private inline fun ifPopup(ifYes: (MoulConfigFragment) -> Unit): Boolean {
val p = popup ?: return false
ifYes(p)
return true
}
override fun keyPressed(keyCode: Int, scanCode: Int, modifiers: Int): Boolean {
return ifPopup {
it.keyPressed(keyCode, scanCode, modifiers)
}
}
override fun keyReleased(keyCode: Int, scanCode: Int, modifiers: Int): Boolean {
return ifPopup {
it.keyReleased(keyCode, scanCode, modifiers)
}
}
override fun mouseMoved(mouseX: Double, mouseY: Double) {
ifPopup { it.mouseMoved(mouseX, mouseY) }
}
override fun mouseReleased(mouseX: Double, mouseY: Double, button: Int): Boolean {
return ifPopup {
it.mouseReleased(mouseX, mouseY, button)
}
}
override fun mouseDragged(mouseX: Double, mouseY: Double, button: Int, deltaX: Double, deltaY: Double): Boolean {
return ifPopup {
it.mouseDragged(mouseX, mouseY, button, deltaX, deltaY)
}
}
override fun mouseClicked(mouseX: Double, mouseY: Double, button: Int): Boolean {
return ifPopup {
if (!Rectangle(
it.position,
Dimension(it.context.root.width, it.context.root.height)
).contains(Point(mouseX, mouseY))
&& dismissOnOutOfBounds
) {
popup = null
} else {
it.mouseClicked(mouseX, mouseY, button)
}
}|| super.mouseClicked(mouseX, mouseY, button)
}
override fun charTyped(chr: Char, modifiers: Int): Boolean {
return ifPopup { it.charTyped(chr, modifiers) }
}
override fun mouseScrolled(
mouseX: Double,
mouseY: Double,
horizontalAmount: Double,
verticalAmount: Double
): Boolean {
return ifPopup {
it.mouseScrolled(mouseX, mouseY, verticalAmount)
}
}
}

View File

@@ -0,0 +1,21 @@
/*
* SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package moe.nea.firmament.util
import me.shedaniel.math.Rectangle
import moe.nea.firmament.mixins.accessor.AccessorHandledScreen
import net.minecraft.client.gui.screen.ingame.HandledScreen
fun HandledScreen<*>.getRectangle(): Rectangle {
this as AccessorHandledScreen
return Rectangle(
getX_Firmament(),
getY_Firmament(),
getBackgroundWidth_Firmament(),
getBackgroundHeight_Firmament()
)
}

View File

@@ -0,0 +1,49 @@
/*
* SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package moe.nea.firmament.util
import io.github.moulberry.moulconfig.gui.GuiContext
import io.github.moulberry.moulconfig.gui.GuiImmediateContext
import io.github.notenoughupdates.moulconfig.gui.GuiComponentWrapper
import me.shedaniel.math.Point
import net.minecraft.client.gui.DrawContext
import moe.nea.firmament.util.MC
class MoulConfigFragment(
context: GuiContext,
val position: Point,
val dismiss: () -> Unit
) : GuiComponentWrapper(context) {
init {
this.init(MC.instance, MC.screen!!.width, MC.screen!!.height)
}
override fun createContext(drawContext: DrawContext?): GuiImmediateContext {
val oldContext = super.createContext(drawContext)
return oldContext.translated(
position.x,
position.y,
context.root.width,
context.root.height,
)
}
override fun render(drawContext: DrawContext?, i: Int, j: Int, f: Float) {
val ctx = createContext(drawContext)
val m = drawContext!!.matrices
m.push()
m.translate(position.x.toFloat(), position.y.toFloat(), 0F)
context.root.render(ctx)
m.pop()
ctx.renderContext.doDrawTooltip()
}
override fun close() {
dismiss()
}
}

View File

@@ -0,0 +1,18 @@
/*
* SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package moe.nea.firmament.util
import io.github.moulberry.moulconfig.common.MyResourceLocation
import io.github.moulberry.moulconfig.gui.GuiContext
import io.github.moulberry.moulconfig.xml.XMLUniverse
object MoulConfigUtils {
val universe = XMLUniverse.getDefaultUniverse()
fun loadGui(name: String, bindTo: Any): GuiContext {
return GuiContext(universe.load(bindTo, MyResourceLocation("firmament", "gui/$name.xml")))
}
}

View File

@@ -0,0 +1,89 @@
/*
* SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package moe.nea.firmament.util
import java.util.*
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.serializer
import moe.nea.firmament.Firmament
object TemplateUtil {
@JvmStatic
fun getTemplatePrefix(data: String): String? {
val decoded = maybeFromBase64Encoded(data) ?: return null
return decoded.replaceAfter("/", "", "").ifBlank { null }
}
@JvmStatic
fun intoBase64Encoded(raw: String): String {
return Base64.getEncoder().encodeToString(raw.encodeToByteArray())
}
private val base64Alphabet = charArrayOf(
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '='
)
@JvmStatic
fun maybeFromBase64Encoded(raw: String): String? {
val raw = raw.trim()
if (raw.any { it !in base64Alphabet }) {
return null
}
return try {
Base64.getDecoder().decode(raw).decodeToString()
} catch (ex: Exception) {
null
}
}
/**
* Returns a base64 encoded string, truncated such that for all `x`, `x.startsWith(prefix)` implies
* `base64Encoded(x).startsWith(getPrefixComparisonSafeBase64Encoding(prefix))`
* (however, the inverse may not always be true).
*/
@JvmStatic
fun getPrefixComparisonSafeBase64Encoding(prefix: String): String {
val rawEncoded =
Base64.getEncoder().encodeToString(prefix.encodeToByteArray())
.replace("=", "")
return rawEncoded.substring(0, rawEncoded.length - rawEncoded.length % 4)
}
inline fun <reified T> encodeTemplate(sharePrefix: String, data: T): String =
encodeTemplate(sharePrefix, data, serializer())
fun <T> encodeTemplate(sharePrefix: String, data: T, serializer: SerializationStrategy<T>): String {
require(sharePrefix.endsWith("/"))
return intoBase64Encoded(sharePrefix + Firmament.json.encodeToString(serializer, data))
}
inline fun <reified T : Any> maybeDecodeTemplate(sharePrefix: String, data: String): T? =
maybeDecodeTemplate(sharePrefix, data, serializer())
fun <T : Any> maybeDecodeTemplate(sharePrefix: String, data: String, serializer: DeserializationStrategy<T>): T? {
require(sharePrefix.endsWith("/"))
val data = data.trim()
if (!data.startsWith(getPrefixComparisonSafeBase64Encoding(sharePrefix)))
return null
val decoded = maybeFromBase64Encoded(data) ?: return null
if (!decoded.startsWith(sharePrefix))
return null
return try {
Firmament.json.decodeFromString<T>(serializer, decoded.substring(sharePrefix.length))
} catch (e: Exception) {
null
}
}
}