aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/util/FragmentGuiScreen.kt
blob: 5e13d516f1e2837945ab367c39d7c4a3b1ff8953 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package moe.nea.firmament.util

import io.github.notenoughupdates.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, horizontalAmount, verticalAmount)
        }
    }
}