aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/features/inventory/storageoverlay/StorageOverlayCustom.kt
blob: d0d91140a8bda23b379e1174204cd7b58bb82166 (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
94
95
96
97
98
package moe.nea.firmament.features.inventory.storageoverlay

import me.shedaniel.math.Point
import me.shedaniel.math.Rectangle
import net.minecraft.client.MinecraftClient
import net.minecraft.client.gui.DrawContext
import net.minecraft.client.gui.screen.ingame.GenericContainerScreen
import net.minecraft.entity.player.PlayerInventory
import net.minecraft.screen.slot.Slot
import moe.nea.firmament.mixins.accessor.AccessorHandledScreen
import moe.nea.firmament.util.customgui.CustomGui

class StorageOverlayCustom(
    val handler: StorageBackingHandle,
    val screen: GenericContainerScreen,
    val overview: StorageOverlayScreen,
) : CustomGui() {
    override fun onVoluntaryExit(): Boolean {
        overview.isExiting = true
        return super.onVoluntaryExit()
    }

    override fun getBounds(): List<Rectangle> {
        return overview.getBounds()
    }

    override fun afterSlotRender(context: DrawContext, slot: Slot) {
        if (slot.inventory !is PlayerInventory)
            context.disableScissor()
    }

    override fun beforeSlotRender(context: DrawContext, slot: Slot) {
        if (slot.inventory !is PlayerInventory)
            overview.createScissors(context)
    }

    override fun onInit() {
        overview.init(MinecraftClient.getInstance(), screen.width, screen.height)
        overview.init()
        screen as AccessorHandledScreen
        screen.x_Firmament = overview.measurements.x
        screen.y_Firmament = overview.measurements.y
        screen.backgroundWidth_Firmament = overview.measurements.totalWidth
        screen.backgroundHeight_Firmament = overview.measurements.totalHeight
    }

    override fun isPointOverSlot(slot: Slot, xOffset: Int, yOffset: Int, pointX: Double, pointY: Double): Boolean {
        if (!super.isPointOverSlot(slot, xOffset, yOffset, pointX, pointY))
            return false
        if (slot.inventory !is PlayerInventory) {
            if (!overview.getScrollPanelInner().contains(pointX, pointY))
                return false
        }
        return true
    }

    override fun shouldDrawForeground(): Boolean {
        return false
    }

    override fun mouseClick(mouseX: Double, mouseY: Double, button: Int): Boolean {
        return overview.mouseClicked(mouseX, mouseY, button, (handler as? StorageBackingHandle.Page)?.storagePageSlot)
    }

    override fun render(drawContext: DrawContext, delta: Float, mouseX: Int, mouseY: Int) {
        overview.drawBackgrounds(drawContext)
        overview.drawPages(drawContext,
                           mouseX,
                           mouseY,
                           delta,
                           (handler as? StorageBackingHandle.Page)?.storagePageSlot,
                           screen.screenHandler.slots.take(screen.screenHandler.rows * 9).drop(9),
                           Point((screen as AccessorHandledScreen).x_Firmament, screen.y_Firmament))
        overview.drawScrollBar(drawContext)
    }

    override fun moveSlot(slot: Slot) {
        val index = slot.index
        if (index in 0..<36) {
            val (x, y) = overview.getPlayerInventorySlotPosition(index)
            slot.x = x - (screen as AccessorHandledScreen).x_Firmament
            slot.y = y - screen.y_Firmament
        } else {
            slot.x = -100000
            slot.y = -100000
        }
    }

    override fun mouseScrolled(
        mouseX: Double,
        mouseY: Double,
        horizontalAmount: Double,
        verticalAmount: Double
    ): Boolean {
        return overview.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount)
    }
}