Make checkboxes accept GetterSetter as checking property

This commit is contained in:
DBotThePony 2023-07-18 00:57:24 +07:00
parent 687b5c9726
commit 0e9a480dd8
Signed by: DBot
GPG Key ID: DCC23B5715498507
4 changed files with 17 additions and 32 deletions

View File

@ -8,7 +8,7 @@ import ru.dbotthepony.mc.otm.core.GetterSetter
import ru.dbotthepony.mc.otm.core.value import ru.dbotthepony.mc.otm.core.value
import ru.dbotthepony.mc.otm.menu.input.IPlayerInputWithFeedback import ru.dbotthepony.mc.otm.menu.input.IPlayerInputWithFeedback
open class CheckBoxInputPanel<out S : Screen> @JvmOverloads constructor( open class CheckBoxInputPanel<out S : Screen>(
screen: S, screen: S,
parent: EditablePanel<*>?, parent: EditablePanel<*>?,
val widget: IPlayerInputWithFeedback<Boolean>, val widget: IPlayerInputWithFeedback<Boolean>,
@ -16,16 +16,8 @@ open class CheckBoxInputPanel<out S : Screen> @JvmOverloads constructor(
y: Float = 0f, y: Float = 0f,
width: Float = REGULAR_DIMENSIONS + 120f, width: Float = REGULAR_DIMENSIONS + 120f,
height: Float = REGULAR_DIMENSIONS height: Float = REGULAR_DIMENSIONS
) : CheckBoxPanel<S>(screen, parent, x, y, width, height) { ) : CheckBoxPanel<S>(screen, parent, x, y, width, height, isChecked = widget) {
override var isChecked: Boolean
get() = widget.value
set(value) {}
override var isDisabled: Boolean override var isDisabled: Boolean
get() = !widget.test(minecraft.player) get() = !widget.test(minecraft.player)
set(value) {} set(value) {}
override fun onClick(mouseButton: Int) {
widget.accept(!isChecked)
}
} }

View File

@ -9,17 +9,16 @@ import ru.dbotthepony.mc.otm.menu.input.BooleanInputWithFeedback
import ru.dbotthepony.mc.otm.menu.input.IPlayerInputWithFeedback import ru.dbotthepony.mc.otm.menu.input.IPlayerInputWithFeedback
import kotlin.math.roundToInt import kotlin.math.roundToInt
open class CheckBoxLabelInputPanel<out S : Screen> @JvmOverloads constructor( open class CheckBoxLabelInputPanel<out S : Screen>(
screen: S, screen: S,
parent: EditablePanel<*>?, parent: EditablePanel<*>?,
widget: IPlayerInputWithFeedback<Boolean>, val widget: IPlayerInputWithFeedback<Boolean>,
text: Component, text: Component,
x: Float = 0f, x: Float = 0f,
y: Float = 0f, y: Float = 0f,
width: Float = CheckBoxPanel.REGULAR_DIMENSIONS + 120f, width: Float = CheckBoxPanel.REGULAR_DIMENSIONS + 120f,
height: Float = CheckBoxPanel.REGULAR_DIMENSIONS height: Float = CheckBoxPanel.REGULAR_DIMENSIONS
) : AbstractCheckBoxLabelPanel<S>(screen, parent, x, y, width, height) { ) : AbstractCheckBoxLabelPanel<S>(screen, parent, x, y, width, height) {
val widget get() = checkbox.widget
override val checkbox = CheckBoxInputPanel(screen = screen, parent = this, widget = widget, x = 0f, y = 0f, width = CheckBoxPanel.REGULAR_DIMENSIONS, height = CheckBoxPanel.REGULAR_DIMENSIONS) override val checkbox = CheckBoxInputPanel(screen = screen, parent = this, widget = widget, x = 0f, y = 0f, width = CheckBoxPanel.REGULAR_DIMENSIONS, height = CheckBoxPanel.REGULAR_DIMENSIONS)
override val label = Label(screen, this, CheckBoxPanel.REGULAR_DIMENSIONS + 4f, 4f, text = text) override val label = Label(screen, this, CheckBoxPanel.REGULAR_DIMENSIONS + 4f, 4f, text = text)
} }

View File

@ -4,17 +4,18 @@ import net.minecraft.client.gui.screens.Screen
import net.minecraft.network.chat.Component import net.minecraft.network.chat.Component
import ru.dbotthepony.mc.otm.client.screen.panels.EditablePanel import ru.dbotthepony.mc.otm.client.screen.panels.EditablePanel
import ru.dbotthepony.mc.otm.client.screen.panels.Label import ru.dbotthepony.mc.otm.client.screen.panels.Label
import kotlin.math.roundToInt import ru.dbotthepony.mc.otm.core.GetterSetter
open class CheckBoxLabelPanel<out S : Screen> @JvmOverloads constructor( open class CheckBoxLabelPanel<out S : Screen>(
screen: S, screen: S,
parent: EditablePanel<*>?, parent: EditablePanel<*>?,
text: Component, text: Component,
x: Float = 0f, x: Float = 0f,
y: Float = 0f, y: Float = 0f,
width: Float = CheckBoxPanel.REGULAR_DIMENSIONS + 120f, width: Float = CheckBoxPanel.REGULAR_DIMENSIONS + 120f,
height: Float = CheckBoxPanel.REGULAR_DIMENSIONS height: Float = CheckBoxPanel.REGULAR_DIMENSIONS,
isChecked: GetterSetter<Boolean> = GetterSetter.box(false)
) : AbstractCheckBoxLabelPanel<S>(screen, parent, x, y, width, height) { ) : AbstractCheckBoxLabelPanel<S>(screen, parent, x, y, width, height) {
override val checkbox = CheckBoxPanel(screen, this, 0f, 0f, CheckBoxPanel.REGULAR_DIMENSIONS, CheckBoxPanel.REGULAR_DIMENSIONS) override val checkbox = CheckBoxPanel(screen, this, 0f, 0f, isChecked = isChecked)
override val label = Label(screen, this, CheckBoxPanel.REGULAR_DIMENSIONS + 4f, 4f, text = text) override val label = Label(screen, this, CheckBoxPanel.REGULAR_DIMENSIONS + 4f, 4f, text = text)
} }

View File

@ -1,27 +1,21 @@
package ru.dbotthepony.mc.otm.client.screen.panels.button package ru.dbotthepony.mc.otm.client.screen.panels.button
import com.mojang.blaze3d.platform.InputConstants
import com.mojang.blaze3d.vertex.PoseStack
import it.unimi.dsi.fastutil.booleans.BooleanConsumer
import net.minecraft.client.gui.GuiGraphics import net.minecraft.client.gui.GuiGraphics
import net.minecraft.client.gui.screens.Screen import net.minecraft.client.gui.screens.Screen
import ru.dbotthepony.mc.otm.client.playGuiClickSound
import ru.dbotthepony.mc.otm.client.render.AbstractMatterySprite import ru.dbotthepony.mc.otm.client.render.AbstractMatterySprite
import ru.dbotthepony.mc.otm.client.render.WidgetLocation import ru.dbotthepony.mc.otm.client.render.WidgetLocation
import ru.dbotthepony.mc.otm.client.screen.panels.EditablePanel import ru.dbotthepony.mc.otm.client.screen.panels.EditablePanel
import ru.dbotthepony.mc.otm.core.TextComponent import ru.dbotthepony.mc.otm.core.GetterSetter
open class CheckBoxPanel<out S : Screen> @JvmOverloads constructor( open class CheckBoxPanel<out S : Screen>(
screen: S, screen: S,
parent: EditablePanel<*>?, parent: EditablePanel<*>?,
x: Float = 0f, x: Float = 0f,
y: Float = 0f, y: Float = 0f,
width: Float = REGULAR_DIMENSIONS, width: Float = REGULAR_DIMENSIONS,
height: Float = REGULAR_DIMENSIONS, height: Float = REGULAR_DIMENSIONS,
var onPress: BooleanConsumer? = null open val isChecked: GetterSetter<Boolean> = GetterSetter.box(false)
) : AbstractButtonPanel<S>(screen, parent, x, y, width, height) { ) : AbstractButtonPanel<S>(screen, parent, x, y, width, height) {
open var isChecked = false
open val IDLE_UNCHECKED: AbstractMatterySprite = Companion.IDLE_UNCHECKED open val IDLE_UNCHECKED: AbstractMatterySprite = Companion.IDLE_UNCHECKED
open val IDLE_CHECKED: AbstractMatterySprite = Companion.IDLE_CHECKED open val IDLE_CHECKED: AbstractMatterySprite = Companion.IDLE_CHECKED
open val HOVERED_UNCHECKED: AbstractMatterySprite = Companion.HOVERED_UNCHECKED open val HOVERED_UNCHECKED: AbstractMatterySprite = Companion.HOVERED_UNCHECKED
@ -33,26 +27,26 @@ open class CheckBoxPanel<out S : Screen> @JvmOverloads constructor(
protected fun renderCheckboxBackground(graphics: GuiGraphics, mouseX: Float, mouseY: Float, partialTick: Float) { protected fun renderCheckboxBackground(graphics: GuiGraphics, mouseX: Float, mouseY: Float, partialTick: Float) {
if (isDisabled) { if (isDisabled) {
if (isChecked) { if (isChecked.get()) {
DISABLED_CHECKED.render(graphics, width = width, height = height) DISABLED_CHECKED.render(graphics, width = width, height = height)
} else { } else {
DISABLED_UNCHECKED.render(graphics, width = width, height = height) DISABLED_UNCHECKED.render(graphics, width = width, height = height)
} }
} else { } else {
if (isPressed) { if (isPressed) {
if (isChecked) { if (isChecked.get()) {
PRESSED_CHECKED.render(graphics, width = width, height = height) PRESSED_CHECKED.render(graphics, width = width, height = height)
} else { } else {
PRESSED_UNCHECKED.render(graphics, width = width, height = height) PRESSED_UNCHECKED.render(graphics, width = width, height = height)
} }
} else if (isHovered) { } else if (isHovered) {
if (isChecked) { if (isChecked.get()) {
HOVERED_CHECKED.render(graphics, width = width, height = height) HOVERED_CHECKED.render(graphics, width = width, height = height)
} else { } else {
HOVERED_UNCHECKED.render(graphics, width = width, height = height) HOVERED_UNCHECKED.render(graphics, width = width, height = height)
} }
} else { } else {
if (isChecked) { if (isChecked.get()) {
IDLE_CHECKED.render(graphics, width = width, height = height) IDLE_CHECKED.render(graphics, width = width, height = height)
} else { } else {
IDLE_UNCHECKED.render(graphics, width = width, height = height) IDLE_UNCHECKED.render(graphics, width = width, height = height)
@ -66,8 +60,7 @@ open class CheckBoxPanel<out S : Screen> @JvmOverloads constructor(
} }
override fun onClick(mouseButton: Int) { override fun onClick(mouseButton: Int) {
isChecked = !isChecked isChecked.accept(!isChecked.get())
onPress?.accept(isChecked)
} }
companion object { companion object {