aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/features
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/features')
-rw-r--r--src/main/kotlin/moe/nea/firmament/features/inventory/buttons/FragmentGuiScreen.kt88
-rw-r--r--src/main/kotlin/moe/nea/firmament/features/inventory/buttons/InventoryButtonEditor.kt38
-rw-r--r--src/main/kotlin/moe/nea/firmament/features/inventory/buttons/MoulConfigFragment.kt43
3 files changed, 159 insertions, 10 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/features/inventory/buttons/FragmentGuiScreen.kt b/src/main/kotlin/moe/nea/firmament/features/inventory/buttons/FragmentGuiScreen.kt
new file mode 100644
index 0000000..4f38f99
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/features/inventory/buttons/FragmentGuiScreen.kt
@@ -0,0 +1,88 @@
+package moe.nea.firmament.features.inventory.buttons
+
+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)
+ popup?.render(context, mouseX, mouseY, delta)
+ }
+
+ 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)
+ }
+ }
+ }
+
+ 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)
+ }
+ }
+}
diff --git a/src/main/kotlin/moe/nea/firmament/features/inventory/buttons/InventoryButtonEditor.kt b/src/main/kotlin/moe/nea/firmament/features/inventory/buttons/InventoryButtonEditor.kt
index ff3469d..982bf31 100644
--- a/src/main/kotlin/moe/nea/firmament/features/inventory/buttons/InventoryButtonEditor.kt
+++ b/src/main/kotlin/moe/nea/firmament/features/inventory/buttons/InventoryButtonEditor.kt
@@ -4,16 +4,19 @@ import io.github.moulberry.moulconfig.xml.Bind
import me.shedaniel.math.Dimension
import me.shedaniel.math.Point
import me.shedaniel.math.Rectangle
+import org.lwjgl.glfw.GLFW
import net.minecraft.client.gui.DrawContext
-import net.minecraft.client.gui.screen.Screen
-import net.minecraft.text.Text
+import moe.nea.firmament.util.MoulConfigUtils
class InventoryButtonEditor(
val lastGuiRect: Rectangle,
-) : Screen(Text.literal("")) {
+) : FragmentGuiScreen() {
class Editor {
@field:Bind
- var
+ var command: String = ""
+
+ @field:Bind
+ var icon: String = ""
}
data class Button(
@@ -40,22 +43,37 @@ class InventoryButtonEditor(
val buttons = mutableListOf<Button>()
override fun render(context: DrawContext, mouseX: Int, mouseY: Int, delta: Float) {
- super.render(context, mouseX, mouseY, delta)
-
context.fill(lastGuiRect.minX, lastGuiRect.minY, lastGuiRect.maxX, lastGuiRect.maxY, -1)
for (button in buttons) {
- val buttonPosition = button.getPosition(lastGuiRect)
+ val buttonPosition = button.getBounds(lastGuiRect)
context.fill(
- buttonPosition.x, buttonPosition.y,
- buttonPosition.x + 18, buttonPosition.y + 18,
+ buttonPosition.minX, buttonPosition.minY,
+ buttonPosition.maxX, buttonPosition.maxY,
0xFF00FF00.toInt()
)
}
+
+ super.render(context, mouseX, mouseY, delta)
+ }
+
+ override fun keyPressed(keyCode: Int, scanCode: Int, modifiers: Int): Boolean {
+ if (super.keyPressed(keyCode, scanCode, modifiers)) return true
+ if (keyCode == GLFW.GLFW_KEY_ESCAPE) {
+ close()
+ return true
+ }
+ return false
}
override fun mouseClicked(mouseX: Double, mouseY: Double, button: Int): Boolean {
- if (lastGuiRect.contains(mouseX, mouseY)) return true
+ if (super.mouseClicked(mouseX, mouseY, button)) return true
+ val clickedButton = buttons.firstOrNull { it.getBounds(lastGuiRect).contains(Point(mouseX, mouseY)) }
+ if (clickedButton != null) {
+ createPopup(MoulConfigUtils.loadGui("button_editor_fragment", Editor()), Point(mouseX, mouseY))
+ return true
+ }
+ if (lastGuiRect.contains(mouseX, mouseY) || lastGuiRect.contains(Point(mouseX + 18, mouseY + 18))) return true
val mx = mouseX.toInt()
val my = mouseY.toInt()
val anchorRight = mx > lastGuiRect.maxX
diff --git a/src/main/kotlin/moe/nea/firmament/features/inventory/buttons/MoulConfigFragment.kt b/src/main/kotlin/moe/nea/firmament/features/inventory/buttons/MoulConfigFragment.kt
new file mode 100644
index 0000000..273dd20
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/features/inventory/buttons/MoulConfigFragment.kt
@@ -0,0 +1,43 @@
+package moe.nea.firmament.features.inventory.buttons
+
+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()
+ }
+}