fix: Fix position of inventory buttons during preset importing
This commit is contained in:
@@ -29,7 +29,6 @@ public abstract class ReferenceCustomModelsPatch {
|
|||||||
private void addFirmamentReferencedModels(
|
private void addFirmamentReferencedModels(
|
||||||
BlockStatesLoader.BlockStateDefinition definition, CallbackInfo ci
|
BlockStatesLoader.BlockStateDefinition definition, CallbackInfo ci
|
||||||
) {
|
) {
|
||||||
inputs.keySet().stream().filter(it->it.toString().contains("firm")).forEach(System.out::println);
|
|
||||||
BakeExtraModelsEvent.Companion.publish(new BakeExtraModelsEvent(
|
BakeExtraModelsEvent.Companion.publish(new BakeExtraModelsEvent(
|
||||||
(modelIdentifier, identifier) -> addTopLevelModel(modelIdentifier, new ItemModel(identifier))));
|
(modelIdentifier, identifier) -> addTopLevelModel(modelIdentifier, new ItemModel(identifier))));
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
|
|
||||||
|
|
||||||
package moe.nea.firmament.features.inventory.buttons
|
package moe.nea.firmament.features.inventory.buttons
|
||||||
|
|
||||||
import io.github.notenoughupdates.moulconfig.common.IItemStack
|
import io.github.notenoughupdates.moulconfig.common.IItemStack
|
||||||
@@ -18,166 +16,203 @@ import moe.nea.firmament.util.ClipboardUtils
|
|||||||
import moe.nea.firmament.util.FragmentGuiScreen
|
import moe.nea.firmament.util.FragmentGuiScreen
|
||||||
import moe.nea.firmament.util.MC
|
import moe.nea.firmament.util.MC
|
||||||
import moe.nea.firmament.util.MoulConfigUtils
|
import moe.nea.firmament.util.MoulConfigUtils
|
||||||
|
import moe.nea.firmament.util.tr
|
||||||
|
|
||||||
class InventoryButtonEditor(
|
class InventoryButtonEditor(
|
||||||
val lastGuiRect: Rectangle,
|
val lastGuiRect: Rectangle,
|
||||||
) : FragmentGuiScreen() {
|
) : FragmentGuiScreen() {
|
||||||
inner class Editor(val originalButton: InventoryButton) {
|
inner class Editor(val originalButton: InventoryButton) {
|
||||||
@field:Bind
|
@field:Bind
|
||||||
var command: String = originalButton.command ?: ""
|
var command: String = originalButton.command ?: ""
|
||||||
|
|
||||||
@field:Bind
|
@field:Bind
|
||||||
var icon: String = originalButton.icon ?: ""
|
var icon: String = originalButton.icon ?: ""
|
||||||
|
|
||||||
@Bind
|
@Bind
|
||||||
fun getItemIcon(): IItemStack {
|
fun getItemIcon(): IItemStack {
|
||||||
save()
|
save()
|
||||||
return ModernItemStack.of(InventoryButton.getItemForName(icon))
|
return ModernItemStack.of(InventoryButton.getItemForName(icon))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bind
|
@Bind
|
||||||
fun delete() {
|
fun delete() {
|
||||||
buttons.removeIf { it === originalButton }
|
buttons.removeIf { it === originalButton }
|
||||||
popup = null
|
popup = null
|
||||||
}
|
}
|
||||||
|
|
||||||
fun save() {
|
fun save() {
|
||||||
originalButton.icon = icon
|
originalButton.icon = icon
|
||||||
originalButton.command = command
|
originalButton.command = command
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var buttons: MutableList<InventoryButton> =
|
var buttons: MutableList<InventoryButton> =
|
||||||
InventoryButtons.DConfig.data.buttons.map { it.copy() }.toMutableList()
|
InventoryButtons.DConfig.data.buttons.map { it.copy() }.toMutableList()
|
||||||
|
|
||||||
override fun close() {
|
override fun close() {
|
||||||
InventoryButtons.DConfig.data.buttons = buttons
|
InventoryButtons.DConfig.data.buttons = buttons
|
||||||
InventoryButtons.DConfig.markDirty()
|
InventoryButtons.DConfig.markDirty()
|
||||||
super.close()
|
super.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun init() {
|
override fun init() {
|
||||||
super.init()
|
super.init()
|
||||||
addDrawableChild(
|
addDrawableChild(
|
||||||
ButtonWidget.builder(Text.translatable("firmament.inventory-buttons.load-preset")) {
|
ButtonWidget.builder(Text.translatable("firmament.inventory-buttons.load-preset")) {
|
||||||
val t = ClipboardUtils.getTextContents()
|
val t = ClipboardUtils.getTextContents()
|
||||||
val newButtons = InventoryButtonTemplates.loadTemplate(t)
|
val newButtons = InventoryButtonTemplates.loadTemplate(t)
|
||||||
if (newButtons != null)
|
if (newButtons != null)
|
||||||
buttons = newButtons.toMutableList()
|
buttons = moveButtons(newButtons.map { it.copy(command = it.command?.removePrefix("/")) })
|
||||||
}
|
}
|
||||||
.position(lastGuiRect.minX + 10, lastGuiRect.minY + 35)
|
.position(lastGuiRect.minX + 10, lastGuiRect.minY + 35)
|
||||||
.width(lastGuiRect.width - 20)
|
.width(lastGuiRect.width - 20)
|
||||||
.build()
|
.build()
|
||||||
)
|
)
|
||||||
addDrawableChild(
|
addDrawableChild(
|
||||||
ButtonWidget.builder(Text.translatable("firmament.inventory-buttons.save-preset")) {
|
ButtonWidget.builder(Text.translatable("firmament.inventory-buttons.save-preset")) {
|
||||||
ClipboardUtils.setTextContent(InventoryButtonTemplates.saveTemplate(buttons))
|
ClipboardUtils.setTextContent(InventoryButtonTemplates.saveTemplate(buttons))
|
||||||
}
|
}
|
||||||
.position(lastGuiRect.minX + 10, lastGuiRect.minY + 60)
|
.position(lastGuiRect.minX + 10, lastGuiRect.minY + 60)
|
||||||
.width(lastGuiRect.width - 20)
|
.width(lastGuiRect.width - 20)
|
||||||
.build()
|
.build()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun render(context: DrawContext, mouseX: Int, mouseY: Int, delta: Float) {
|
private fun moveButtons(buttons: List<InventoryButton>): MutableList<InventoryButton> {
|
||||||
super.render(context, mouseX, mouseY, delta)
|
val newButtons: MutableList<InventoryButton> = ArrayList(buttons.size)
|
||||||
context.matrices.push()
|
val movedButtons = mutableListOf<InventoryButton>()
|
||||||
context.matrices.translate(0f, 0f, -10f)
|
for (button in buttons) {
|
||||||
context.fill(lastGuiRect.minX, lastGuiRect.minY, lastGuiRect.maxX, lastGuiRect.maxY, -1)
|
if ((!button.anchorBottom && !button.anchorRight && button.x > 0 && button.y > 0)) {
|
||||||
context.matrices.pop()
|
MC.sendChat(tr("firmament.inventory-buttons.button-moved",
|
||||||
for (button in buttons) {
|
"One of your imported buttons intersects with the inventory and has been moved to the top left."))
|
||||||
val buttonPosition = button.getBounds(lastGuiRect)
|
movedButtons.add(button.copy(
|
||||||
context.matrices.push()
|
x = 0,
|
||||||
context.matrices.translate(buttonPosition.minX.toFloat(), buttonPosition.minY.toFloat(), 0F)
|
y = -InventoryButton.dimensions.width,
|
||||||
button.render(context)
|
anchorRight = false,
|
||||||
context.matrices.pop()
|
anchorBottom = false
|
||||||
}
|
))
|
||||||
}
|
} else {
|
||||||
|
newButtons.add(button)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var i = 0
|
||||||
|
val zeroRect = Rectangle(0, 0, 1, 1)
|
||||||
|
for (movedButton in movedButtons) {
|
||||||
|
fun getPosition(button: InventoryButton, index: Int) =
|
||||||
|
button.copy(x = (index % 10) * InventoryButton.dimensions.width,
|
||||||
|
y = (index / 10) * -InventoryButton.dimensions.height,
|
||||||
|
anchorRight = false, anchorBottom = false)
|
||||||
|
while (true) {
|
||||||
|
val newPos = getPosition(movedButton, i++)
|
||||||
|
val newBounds = newPos.getBounds(zeroRect)
|
||||||
|
if (newButtons.none { it.getBounds(zeroRect).intersects(newBounds) }) {
|
||||||
|
newButtons.add(newPos)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newButtons
|
||||||
|
}
|
||||||
|
|
||||||
override fun keyPressed(keyCode: Int, scanCode: Int, modifiers: Int): Boolean {
|
override fun render(context: DrawContext, mouseX: Int, mouseY: Int, delta: Float) {
|
||||||
if (super.keyPressed(keyCode, scanCode, modifiers)) return true
|
super.render(context, mouseX, mouseY, delta)
|
||||||
if (keyCode == GLFW.GLFW_KEY_ESCAPE) {
|
context.matrices.push()
|
||||||
close()
|
context.matrices.translate(0f, 0f, -10f)
|
||||||
return true
|
context.fill(lastGuiRect.minX, lastGuiRect.minY, lastGuiRect.maxX, lastGuiRect.maxY, -1)
|
||||||
}
|
context.matrices.pop()
|
||||||
return false
|
for (button in buttons) {
|
||||||
}
|
val buttonPosition = button.getBounds(lastGuiRect)
|
||||||
|
context.matrices.push()
|
||||||
|
context.matrices.translate(buttonPosition.minX.toFloat(), buttonPosition.minY.toFloat(), 0F)
|
||||||
|
button.render(context)
|
||||||
|
context.matrices.pop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun mouseReleased(mouseX: Double, mouseY: Double, button: Int): Boolean {
|
override fun keyPressed(keyCode: Int, scanCode: Int, modifiers: Int): Boolean {
|
||||||
if (super.mouseReleased(mouseX, mouseY, button)) return true
|
if (super.keyPressed(keyCode, scanCode, modifiers)) return true
|
||||||
val clickedButton = buttons.firstOrNull { it.getBounds(lastGuiRect).contains(Point(mouseX, mouseY)) }
|
if (keyCode == GLFW.GLFW_KEY_ESCAPE) {
|
||||||
if (clickedButton != null && !justPerformedAClickAction) {
|
close()
|
||||||
createPopup(MoulConfigUtils.loadGui("button_editor_fragment", Editor(clickedButton)), Point(mouseX, mouseY))
|
return true
|
||||||
return true
|
}
|
||||||
}
|
return false
|
||||||
justPerformedAClickAction = false
|
}
|
||||||
lastDraggedButton = null
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun mouseDragged(mouseX: Double, mouseY: Double, button: Int, deltaX: Double, deltaY: Double): Boolean {
|
override fun mouseReleased(mouseX: Double, mouseY: Double, button: Int): Boolean {
|
||||||
if (super.mouseDragged(mouseX, mouseY, button, deltaX, deltaY)) return true
|
if (super.mouseReleased(mouseX, mouseY, button)) return true
|
||||||
|
val clickedButton = buttons.firstOrNull { it.getBounds(lastGuiRect).contains(Point(mouseX, mouseY)) }
|
||||||
|
if (clickedButton != null && !justPerformedAClickAction) {
|
||||||
|
createPopup(MoulConfigUtils.loadGui("button_editor_fragment", Editor(clickedButton)), Point(mouseX, mouseY))
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
justPerformedAClickAction = false
|
||||||
|
lastDraggedButton = null
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
if (initialDragMousePosition.distanceSquared(Vec2f(mouseX.toFloat(), mouseY.toFloat())) >= 4 * 4) {
|
override fun mouseDragged(mouseX: Double, mouseY: Double, button: Int, deltaX: Double, deltaY: Double): Boolean {
|
||||||
initialDragMousePosition = Vec2f(-10F, -10F)
|
if (super.mouseDragged(mouseX, mouseY, button, deltaX, deltaY)) return true
|
||||||
lastDraggedButton?.let { dragging ->
|
|
||||||
justPerformedAClickAction = true
|
|
||||||
val (anchorRight, anchorBottom, offsetX, offsetY) = getCoordsForMouse(mouseX.toInt(), mouseY.toInt())
|
|
||||||
?: return true
|
|
||||||
dragging.x = offsetX
|
|
||||||
dragging.y = offsetY
|
|
||||||
dragging.anchorRight = anchorRight
|
|
||||||
dragging.anchorBottom = anchorBottom
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
var lastDraggedButton: InventoryButton? = null
|
if (initialDragMousePosition.distanceSquared(Vec2f(mouseX.toFloat(), mouseY.toFloat())) >= 4 * 4) {
|
||||||
var justPerformedAClickAction = false
|
initialDragMousePosition = Vec2f(-10F, -10F)
|
||||||
var initialDragMousePosition = Vec2f(-10F, -10F)
|
lastDraggedButton?.let { dragging ->
|
||||||
|
justPerformedAClickAction = true
|
||||||
|
val (anchorRight, anchorBottom, offsetX, offsetY) = getCoordsForMouse(mouseX.toInt(), mouseY.toInt())
|
||||||
|
?: return true
|
||||||
|
dragging.x = offsetX
|
||||||
|
dragging.y = offsetY
|
||||||
|
dragging.anchorRight = anchorRight
|
||||||
|
dragging.anchorBottom = anchorBottom
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
data class AnchoredCoords(
|
var lastDraggedButton: InventoryButton? = null
|
||||||
val anchorRight: Boolean,
|
var justPerformedAClickAction = false
|
||||||
val anchorBottom: Boolean,
|
var initialDragMousePosition = Vec2f(-10F, -10F)
|
||||||
val offsetX: Int,
|
|
||||||
val offsetY: Int,
|
|
||||||
)
|
|
||||||
|
|
||||||
fun getCoordsForMouse(mx: Int, my: Int): AnchoredCoords? {
|
data class AnchoredCoords(
|
||||||
if (lastGuiRect.contains(mx, my) || lastGuiRect.contains(
|
val anchorRight: Boolean,
|
||||||
Point(
|
val anchorBottom: Boolean,
|
||||||
mx + InventoryButton.dimensions.width,
|
val offsetX: Int,
|
||||||
my + InventoryButton.dimensions.height,
|
val offsetY: Int,
|
||||||
)
|
)
|
||||||
)
|
|
||||||
) return null
|
|
||||||
|
|
||||||
val anchorRight = mx > lastGuiRect.maxX
|
fun getCoordsForMouse(mx: Int, my: Int): AnchoredCoords? {
|
||||||
val anchorBottom = my > lastGuiRect.maxY
|
if (lastGuiRect.contains(mx, my) || lastGuiRect.contains(
|
||||||
var offsetX = mx - if (anchorRight) lastGuiRect.maxX else lastGuiRect.minX
|
Point(
|
||||||
var offsetY = my - if (anchorBottom) lastGuiRect.maxY else lastGuiRect.minY
|
mx + InventoryButton.dimensions.width,
|
||||||
if (InputUtil.isKeyPressed(MC.window.handle, InputUtil.GLFW_KEY_LEFT_SHIFT)) {
|
my + InventoryButton.dimensions.height,
|
||||||
offsetX = MathHelper.floor(offsetX / 20F) * 20
|
)
|
||||||
offsetY = MathHelper.floor(offsetY / 20F) * 20
|
)
|
||||||
}
|
) return null
|
||||||
return AnchoredCoords(anchorRight, anchorBottom, offsetX, offsetY)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun mouseClicked(mouseX: Double, mouseY: Double, button: Int): Boolean {
|
val anchorRight = mx > lastGuiRect.maxX
|
||||||
if (super.mouseClicked(mouseX, mouseY, button)) return true
|
val anchorBottom = my > lastGuiRect.maxY
|
||||||
val clickedButton = buttons.firstOrNull { it.getBounds(lastGuiRect).contains(Point(mouseX, mouseY)) }
|
var offsetX = mx - if (anchorRight) lastGuiRect.maxX else lastGuiRect.minX
|
||||||
if (clickedButton != null) {
|
var offsetY = my - if (anchorBottom) lastGuiRect.maxY else lastGuiRect.minY
|
||||||
lastDraggedButton = clickedButton
|
if (InputUtil.isKeyPressed(MC.window.handle, InputUtil.GLFW_KEY_LEFT_SHIFT)) {
|
||||||
initialDragMousePosition = Vec2f(mouseX.toFloat(), mouseY.toFloat())
|
offsetX = MathHelper.floor(offsetX / 20F) * 20
|
||||||
return true
|
offsetY = MathHelper.floor(offsetY / 20F) * 20
|
||||||
}
|
}
|
||||||
val mx = mouseX.toInt()
|
return AnchoredCoords(anchorRight, anchorBottom, offsetX, offsetY)
|
||||||
val my = mouseY.toInt()
|
}
|
||||||
val (anchorRight, anchorBottom, offsetX, offsetY) = getCoordsForMouse(mx, my) ?: return true
|
|
||||||
buttons.add(InventoryButton(offsetX, offsetY, anchorRight, anchorBottom, null, null))
|
override fun mouseClicked(mouseX: Double, mouseY: Double, button: Int): Boolean {
|
||||||
justPerformedAClickAction = true
|
if (super.mouseClicked(mouseX, mouseY, button)) return true
|
||||||
return true
|
val clickedButton = buttons.firstOrNull { it.getBounds(lastGuiRect).contains(Point(mouseX, mouseY)) }
|
||||||
}
|
if (clickedButton != null) {
|
||||||
|
lastDraggedButton = clickedButton
|
||||||
|
initialDragMousePosition = Vec2f(mouseX.toFloat(), mouseY.toFloat())
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
val mx = mouseX.toInt()
|
||||||
|
val my = mouseY.toInt()
|
||||||
|
val (anchorRight, anchorBottom, offsetX, offsetY) = getCoordsForMouse(mx, my) ?: return true
|
||||||
|
buttons.add(InventoryButton(offsetX, offsetY, anchorRight, anchorBottom, null, null))
|
||||||
|
justPerformedAClickAction = true
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,35 +1,33 @@
|
|||||||
|
|
||||||
|
|
||||||
package moe.nea.firmament.features.inventory.buttons
|
package moe.nea.firmament.features.inventory.buttons
|
||||||
|
|
||||||
import kotlinx.serialization.decodeFromString
|
|
||||||
import kotlinx.serialization.encodeToString
|
import kotlinx.serialization.encodeToString
|
||||||
import net.minecraft.text.Text
|
import net.minecraft.text.Text
|
||||||
import moe.nea.firmament.Firmament
|
import moe.nea.firmament.Firmament
|
||||||
|
import moe.nea.firmament.util.ErrorUtil
|
||||||
import moe.nea.firmament.util.MC
|
import moe.nea.firmament.util.MC
|
||||||
import moe.nea.firmament.util.TemplateUtil
|
import moe.nea.firmament.util.TemplateUtil
|
||||||
|
|
||||||
object InventoryButtonTemplates {
|
object InventoryButtonTemplates {
|
||||||
|
|
||||||
val legacyPrefix = "NEUBUTTONS/"
|
val legacyPrefix = "NEUBUTTONS/"
|
||||||
val modernPrefix = "MAYBEONEDAYIWILLHAVEMYOWNFORMAT"
|
val modernPrefix = "MAYBEONEDAYIWILLHAVEMYOWNFORMAT"
|
||||||
|
|
||||||
fun loadTemplate(t: String): List<InventoryButton>? {
|
fun loadTemplate(t: String): List<InventoryButton>? {
|
||||||
val buttons = TemplateUtil.maybeDecodeTemplate<List<String>>(legacyPrefix, t) ?: return null
|
val buttons = TemplateUtil.maybeDecodeTemplate<List<String>>(legacyPrefix, t) ?: return null
|
||||||
return buttons.mapNotNull {
|
return buttons.mapNotNull {
|
||||||
try {
|
ErrorUtil.catch<InventoryButton?>("Could not import button") {
|
||||||
Firmament.json.decodeFromString<InventoryButton>(it).also {
|
Firmament.json.decodeFromString<InventoryButton>(it).also {
|
||||||
if (it.icon?.startsWith("extra:") == true || it.command?.any { it.isLowerCase() } == true) {
|
if (it.icon?.startsWith("extra:") == true) {
|
||||||
MC.sendChat(Text.translatable("firmament.inventory-buttons.import-failed"))
|
MC.sendChat(Text.translatable("firmament.inventory-buttons.import-failed"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
}.or {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun saveTemplate(buttons: List<InventoryButton>): String {
|
fun saveTemplate(buttons: List<InventoryButton>): String {
|
||||||
return TemplateUtil.encodeTemplate(legacyPrefix, buttons.map { Firmament.json.encodeToString(it) })
|
return TemplateUtil.encodeTemplate(legacyPrefix, buttons.map { Firmament.json.encodeToString(it) })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user