aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/gui/WCenteringPanel.kt
blob: c6d1f565f3a664b9410aa06393864a0375663e3f (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
package moe.nea.firmament.gui

import io.github.cottonmc.cotton.gui.widget.WPanel
import io.github.cottonmc.cotton.gui.widget.WWidget
import io.github.cottonmc.cotton.gui.widget.data.Axis

data class WCenteringPanel(
    val child: WWidget,
    val axis: Axis,
) : WPanel() {
    init {
        child.parent = this
    }

    override fun setSize(x: Int, y: Int) {
        super.setSize(x, y)
        if (!child.canResize()) return
        if (axis == Axis.HORIZONTAL) {
            child.setSize(child.width, y)
        } else {
            child.setSize(x, child.height)
        }
    }

    override fun layout() {
        super.layout()
        child.setLocation(
            axis.choose((child.width + width) / 2, child.x),
            axis.choose(child.y, (child.height + height) / 2),
        )
    }


}