Make menu widgets be just containers for synchronized fields

This commit is contained in:
DBotThePony 2023-03-25 11:33:27 +07:00
parent 0902c445f6
commit 6cc5659c43
Signed by: DBot
GPG Key ID: DCC23B5715498507
21 changed files with 136 additions and 185 deletions

View File

@ -22,21 +22,21 @@ open class HorizontalPowerGaugePanel<out S : Screen>(
if (height >= 18f) {
if (flop) {
GAUGE_BACKGROUND_TALL.render(stack, height = height, width = this.width, winding = UVWindingOrder.U1_V0_U0_V1)
val width = this.width * widget.percentage()
val width = this.width * widget.percentage
GAUGE_FOREGROUND_TALL.renderPartial(stack, x = this.width - width, height = height, width = width, winding = UVWindingOrder.U1_V0_U0_V1)
} else {
GAUGE_BACKGROUND_TALL.render(stack, height = height, width = this.width)
val width = this.width * widget.percentage()
val width = this.width * widget.percentage
GAUGE_FOREGROUND_TALL.renderPartial(stack, height = height, width = width)
}
} else {
if (flop) {
GAUGE_BACKGROUND.render(stack, height = height, width = this.width, winding = UVWindingOrder.U1_V0_U0_V1)
val width = this.width * widget.percentage()
val width = this.width * widget.percentage
GAUGE_FOREGROUND.renderPartial(stack, x = this.width - width, height = height, width = width, winding = UVWindingOrder.U1_V0_U0_V1)
} else {
GAUGE_BACKGROUND.render(stack, height = height, width = this.width)
val width = this.width * widget.percentage()
val width = this.width * widget.percentage
GAUGE_FOREGROUND.renderPartial(stack, height = height, width = width)
}
}

View File

@ -43,9 +43,9 @@ open class MatterGaugePanel<S : Screen> @JvmOverloads constructor(
return mutableListOf(
TranslatableComponent(
"otm.gui.matter.percentage_level",
String.format("%.2f", widget.percentage() * 100.0)
String.format("%.2f", widget.percentage * 100.0)
),
formatMatterLevel(widget.level(), widget.maxLevel(), formatAsReadable = ShiftPressedCond)
formatMatterLevel(widget.level, widget.maxLevel, formatAsReadable = ShiftPressedCond)
)
}
@ -54,22 +54,22 @@ open class MatterGaugePanel<S : Screen> @JvmOverloads constructor(
lastAbsoluteX = absoluteX
lastAbsoluteY = absoluteY
lastDraw = nanoTime
lastLevel = widget.percentage()
lastLevel = widget.percentage
} else {
val diff = ((lastAbsoluteX - absoluteX).pow(2f) + (lastAbsoluteY - absoluteY).pow(2f)).pow(0.5f) / 10f + (lastLevel - widget.percentage()).absoluteValue * 1.5f
val diff = ((lastAbsoluteX - absoluteX).pow(2f) + (lastAbsoluteY - absoluteY).pow(2f)).pow(0.5f) / 10f + (lastLevel - widget.percentage).absoluteValue * 1.5f
lastAbsoluteX = absoluteX
lastAbsoluteY = absoluteY
wavesStrength = (wavesStrength + diff - (nanoTime - lastDraw) / 400_000_000f).coerceAtLeast(0.5f).coerceAtMost(16f)
lastDraw = nanoTime
lastLevel = widget.percentage()
lastLevel = widget.percentage
}
GAUGE_BACKGROUND.render(stack)
val height = this.height * (1f - widget.percentage())
val height = this.height * (1f - widget.percentage)
if (widget.percentage() > 0.01f) {
if (widget.percentage > 0.01f) {
RenderSystem.setShader(GameRenderer::getPositionTexShader)
RenderSystem.enableTexture()
RenderSystem.enableBlend()

View File

@ -23,15 +23,15 @@ open class PatternGaugePanel<S : Screen> @JvmOverloads constructor(
return mutableListOf(
TranslatableComponent(
"otm.gui.pattern.percentage_level",
String.format("%.2f", widget.percentage() * 100.0)
String.format("%.2f", widget.percentage * 100.0)
),
TranslatableComponent("otm.gui.pattern.format", widget.level().toString(0), widget.maxLevel().toString(0))
TranslatableComponent("otm.gui.pattern.format", widget.level.toString(0), widget.maxLevel.toString(0))
)
}
override fun innerRender(stack: PoseStack, mouseX: Float, mouseY: Float, partialTick: Float) {
GAUGE_BACKGROUND.render(stack)
val height = this.height * widget.percentage()
val height = this.height * widget.percentage
GAUGE_FOREGROUND.renderPartial(stack, y = this.height - height, height = height)
}

View File

@ -25,13 +25,13 @@ open class PowerGaugePanel<out S : Screen> @JvmOverloads constructor(
protected open fun makeTooltip(): MutableList<Component> {
return mutableListOf(
TranslatableComponent("otm.gui.power.percentage_level", String.format("%.2f", widget.percentage() * 100.0)),
formatPowerLevel(widget.level(), widget.maxLevel(), formatAsReadable = ShiftPressedCond)
TranslatableComponent("otm.gui.power.percentage_level", String.format("%.2f", widget.percentage * 100.0)),
formatPowerLevel(widget.level, widget.maxLevel, formatAsReadable = ShiftPressedCond)
)
}
override fun innerRender(stack: PoseStack, mouseX: Float, mouseY: Float, partialTick: Float) {
val height = this.height * widget.percentage()
val height = this.height * widget.percentage
if (width >= 18f) {
GAUGE_BACKGROUND_WIDE.render(stack, width = width, height = this.height)

View File

@ -28,11 +28,11 @@ open class ProgressGaugePanel<S : Screen> @JvmOverloads constructor(
protected open fun makeTooltip(): MutableList<Component> {
val tooltip: MutableList<Component>
if (widget.isStuck()) {
if (widget.isStuck) {
tooltip = mutableListOf(
TranslatableComponent(
"otm.gui.progress_widget",
String.format("%.2f", widget.percentage() * 100f)
String.format("%.2f", widget.percentage * 100f)
),
TranslatableComponent("otm.gui.progress_widget_stuck").withStyle(ChatFormatting.DARK_RED)
)
@ -40,7 +40,7 @@ open class ProgressGaugePanel<S : Screen> @JvmOverloads constructor(
tooltip = mutableListOf(
TranslatableComponent(
"otm.gui.progress_widget",
String.format("%.2f", widget.percentage() * 100f)
String.format("%.2f", widget.percentage * 100f)
)
)
}
@ -49,21 +49,21 @@ open class ProgressGaugePanel<S : Screen> @JvmOverloads constructor(
}
override fun innerRender(stack: PoseStack, mouseX: Float, mouseY: Float, partialTick: Float) {
if (widget.isStuck() && tickCount % 40 <= 20) {
if (widget.isStuck && tickCount % 40 <= 20) {
RenderSystem.setShaderColor(0.75f, 0.4f, 0.4f, 1f)
}
if (flop) {
GAUGE_BACKGROUND.render(stack, height = height, width = this.width, winding = UVWindingOrder.U1_V0_U0_V1)
val width = (this.width * widget.percentage()).roundToInt().toFloat()
val width = (this.width * widget.percentage).roundToInt().toFloat()
GAUGE_FOREGROUND.renderPartial(stack, x = this.width - width, height = height, width = width, winding = UVWindingOrder.U1_V0_U0_V1)
} else {
GAUGE_BACKGROUND.render(stack, height = height, width = this.width)
val width = (this.width * widget.percentage()).roundToInt().toFloat()
val width = (this.width * widget.percentage).roundToInt().toFloat()
GAUGE_FOREGROUND.renderPartial(stack, height = height, width = width)
}
if (widget.isStuck() && tickCount % 40 <= 20) {
if (widget.isStuck && tickCount % 40 <= 20) {
RenderSystem.setShaderColor(1f, 1f, 1f, 1f)
}
}

View File

@ -36,7 +36,6 @@ import ru.dbotthepony.mc.otm.core.util.IStreamCodec
import ru.dbotthepony.mc.otm.core.util.ItemValueCodec
import ru.dbotthepony.mc.otm.core.util.NullValueCodec
import ru.dbotthepony.mc.otm.core.util.VarIntValueCodec
import ru.dbotthepony.mc.otm.menu.widget.AbstractWidget
import ru.dbotthepony.mc.otm.network.synchronizer.FieldSynchronizer
import ru.dbotthepony.mc.otm.network.synchronizer.IField
import ru.dbotthepony.mc.otm.network.MatteryPacket
@ -74,9 +73,6 @@ abstract class MatteryMenu @JvmOverloads protected constructor(
val mSynchronizer = FieldSynchronizer()
val ply: Player get() = inventory.player
private val _matteryWidgets = ArrayList<AbstractWidget>()
val matteryWidgets: List<AbstractWidget> = Collections.unmodifiableList(_matteryWidgets)
private val _playerInventorySlots = ArrayList<InventorySlot>()
private val _playerInventorySlots2 = ArrayList<InventorySlot>()
private val _playerHotbarSlots = ArrayList<InventorySlot>()
@ -223,25 +219,6 @@ abstract class MatteryMenu @JvmOverloads protected constructor(
return result
}
fun addWidget(widget: AbstractWidget): Int {
val indexOf = _matteryWidgets.indexOf(widget)
if (indexOf == -1) {
_matteryWidgets.add(widget)
return _matteryWidgets.size - 1
}
return indexOf
}
fun getWidget(index: Int): AbstractWidget? {
if (index !in _matteryWidgets.indices) {
return null
}
return _matteryWidgets[index]
}
open inner class InventorySlot(container: Container, index: Int, x: Int = 0, y: Int = 0) : UserFilteredSlot(container, index, x, y) {
override fun mayPlace(itemStack: ItemStack): Boolean {
return !isInventorySlotLocked(index) && super.mayPlace(itemStack)
@ -351,10 +328,6 @@ abstract class MatteryMenu @JvmOverloads protected constructor(
}
override fun broadcastChanges() {
for (widget in _matteryWidgets) {
widget.updateServer()
}
val payload = mSynchronizer.collectNetworkPayload()
if (payload != null) {
@ -375,10 +348,6 @@ abstract class MatteryMenu @JvmOverloads protected constructor(
}
override fun broadcastFullState() {
for (widget in _matteryWidgets) {
widget.updateServer()
}
mSynchronizer.invalidate()
val payload = mSynchronizer.collectNetworkPayload()

View File

@ -18,7 +18,7 @@ abstract class MatteryPoweredMenu protected constructor(
) : MatteryMenu(menuType, containerID, inventory, tile) {
val powerWidget = LevelGaugeWidget(this, tile?.matteryEnergy)
val batterySlot = BatterySlot(tile?.batteryContainer ?: SimpleContainer(1), 0)
val redstone = EnumInputWithFeedback(this, RedstoneSetting::class.java)
val redstone = EnumInputWithFeedback<RedstoneSetting>(this)
init {
if (tile != null) {

View File

@ -23,24 +23,18 @@ class MatterBottlerMenu @JvmOverloads constructor(
) : MatteryPoweredMenu(
MMenus.MATTER_BOTTLER, p_38852_, inventory, tile
) {
val workFlow: BooleanInputWithFeedback
val progressWidget: ProgressGaugeWidget
val matterWidget: LevelGaugeWidget
val workFlow = BooleanInputWithFeedback(this)
val progressWidget = ProgressGaugeWidget(this)
val matterWidget = LevelGaugeWidget(this)
val storageSlots: List<MatterySlot>
init {
val container = tile?.container ?: SimpleContainer(6)
if (tile == null) {
progressWidget = ProgressGaugeWidget(this)
matterWidget = LevelGaugeWidget(this)
workFlow = BooleanInputWithFeedback(this)
} else {
progressWidget = ProgressGaugeWidget(this) { tile.getWorkProgress() }
matterWidget = LevelGaugeWidget(this, tile.matter)
workFlow = BooleanInputWithFeedback(this, tile::isBottling)
if (tile != null) {
progressWidget.with(tile::getWorkProgress)
matterWidget.with(tile.matter)
workFlow.with(tile::isBottling)
}
storageSlots = immutableList(6) { index ->

View File

@ -17,18 +17,15 @@ class MatterCapacitorBankMenu @JvmOverloads constructor(
) : MatteryMenu(
MMenus.MATTER_CAPACITOR_BANK, p_38852_, inventory, tile
) {
val matterGauge: LevelGaugeWidget
val totalMatterGauge: LevelGaugeWidget
val matterGauge = LevelGaugeWidget(this)
val totalMatterGauge = LevelGaugeWidget(this)
val storageSlots: List<MatterContainerInputSlot>
init {
if (tile == null) {
matterGauge = LevelGaugeWidget(this)
totalMatterGauge = LevelGaugeWidget(this)
} else {
matterGauge = LevelGaugeWidget(this, tile)
totalMatterGauge = LevelGaugeWidget(this, {
if (tile != null) {
matterGauge.with(tile)
totalMatterGauge.with({
tile.matterNode.graph.getMatterStorageLevel()
}, {
tile.matterNode.graph.getMatterStorageMaxLevel()

View File

@ -25,8 +25,8 @@ class MatterDecomposerMenu @JvmOverloads constructor(
val input: MatterySlot
val outputMain: MachineOutputSlot
val outputStacking: MachineOutputSlot
val progressWidget: ProgressGaugeWidget
val matterWidget: LevelGaugeWidget
val progressWidget = ProgressGaugeWidget(this)
val matterWidget = LevelGaugeWidget(this, tile?.getCapability(MatteryCapability.MATTER)?.orNull())
init {
val container = tile?.container ?: SimpleContainer(3)
@ -40,12 +40,8 @@ class MatterDecomposerMenu @JvmOverloads constructor(
outputMain = MachineOutputSlot(container, 1)
outputStacking = MachineOutputSlot(container, 2)
matterWidget = LevelGaugeWidget(this, tile?.getCapability(MatteryCapability.MATTER)?.orNull())
if (tile == null) {
progressWidget = ProgressGaugeWidget(this)
} else {
progressWidget = ProgressGaugeWidget(this, tile::workProgress, tile::isUnableToProcess)
if (tile != null) {
progressWidget.with(tile::workProgress, tile::isUnableToProcess)
}
addStorageSlot(outputMain)

View File

@ -27,7 +27,7 @@ class MatterReconstructorMenu(
}
val equipment = makeEquipmentSlots(mapMoveToExternal = true)
val progress: ProgressGaugeWidget
val progress = ProgressGaugeWidget(this)
val redstoneControl = EnumInputWithFeedback<RedstoneSetting>(this)
val energyConfig = EnergyPlayerInput(this)
@ -43,10 +43,8 @@ class MatterReconstructorMenu(
energyConfig.configure(tile.energyConfig)
}
if (tile == null) {
progress = ProgressGaugeWidget(this)
} else {
progress = ProgressGaugeWidget(this, tile::visualProgress, tile::isUnableToProcess)
if (tile != null) {
progress.with(tile::visualProgress, tile::isUnableToProcess)
}
}
}

View File

@ -16,23 +16,18 @@ class MatterRecyclerMenu @JvmOverloads constructor(
inventory: Inventory,
tile: MatterRecyclerBlockEntity? = null
) : MatteryPoweredMenu(MMenus.MATTER_RECYCLER, containerID, inventory, tile) {
val input: MatterySlot
val progress: ProgressGaugeWidget
val matter = LevelGaugeWidget(this, tile?.matter)
init {
val container = tile?.container ?: SimpleContainer(1)
input = object : MatterySlot(container, 0) {
val input = object : MatterySlot(tile?.container ?: SimpleContainer(1), 0) {
override fun mayPlace(itemStack: ItemStack): Boolean {
return itemStack.item is MatterDustItem && (itemStack.item as MatterDustItem).getMatterValue(itemStack) != null
}
}
if (tile == null) {
progress = ProgressGaugeWidget(this)
} else {
progress = ProgressGaugeWidget(this, tile::workProgress, tile::isUnableToProcess)
val progress = ProgressGaugeWidget(this)
val matter = LevelGaugeWidget(this, tile?.matter)
init {
if (tile != null) {
progress.with(tile::workProgress, tile::isUnableToProcess)
}
addStorageSlot(input)

View File

@ -18,8 +18,8 @@ class MatterReplicatorMenu @JvmOverloads constructor(
) : MatteryPoweredMenu(
MMenus.MATTER_REPLICATOR, p_38852_, inventory, tile
) {
val matter: LevelGaugeWidget
val progress: ProgressGaugeWidget
val matter = LevelGaugeWidget(this, tile?.matter)
val progress = ProgressGaugeWidget(this)
val storageSlots: List<MachineOutputSlot>
init {
@ -30,11 +30,7 @@ class MatterReplicatorMenu @JvmOverloads constructor(
}
if (tile != null) {
matter = LevelGaugeWidget(this, tile.matter)
progress = ProgressGaugeWidget(this, tile::workProgress, tile::isUnableToProcess)
} else {
matter = LevelGaugeWidget(this)
progress = ProgressGaugeWidget(this)
progress.with(tile::workProgress, tile::isUnableToProcess)
}
addInventorySlots()

View File

@ -21,8 +21,8 @@ class MatterScannerMenu @JvmOverloads constructor(
MMenus.MATTER_SCANNER, p_38852_, inventory, tile
) {
val input: MatterySlot
val progress: ProgressGaugeWidget
val patterns: LevelGaugeWidget
val progress = ProgressGaugeWidget(this)
val patterns = LevelGaugeWidget(this)
init {
val container = tile?.container ?: SimpleContainer(1)
@ -34,13 +34,10 @@ class MatterScannerMenu @JvmOverloads constructor(
addStorageSlot(input)
if (tile != null) {
progress = ProgressGaugeWidget(this, tile::workProgress, tile::isUnableToProcess)
patterns = LevelGaugeWidget(this,
progress.with(tile::workProgress, tile::isUnableToProcess)
patterns.with(
{ Decimal(tile.matterNode.graph.patternCount) },
{ Decimal(tile.matterNode.graph.patternCapacity) })
} else {
progress = ProgressGaugeWidget(this)
patterns = LevelGaugeWidget(this)
}
addInventorySlots()

View File

@ -17,18 +17,15 @@ class PatternStorageMenu @JvmOverloads constructor(
) : MatteryMenu(
MMenus.PATTERN_STORAGE, p_38852_, inventory, tile
) {
val storedThis: LevelGaugeWidget
val storedGrid: LevelGaugeWidget
val storedThis = LevelGaugeWidget(this)
val storedGrid = LevelGaugeWidget(this)
val storageSlots: List<PatternSlot>
init {
if (tile == null) {
storedThis = LevelGaugeWidget(this)
storedGrid = LevelGaugeWidget(this)
} else {
storedThis = LevelGaugeWidget(this, tile)
storedGrid = LevelGaugeWidget(this, {
if (tile != null) {
storedThis.with(tile)
storedGrid.with({
Decimal(tile.matterNode.graph.patternCount)
}, {
Decimal(tile.matterNode.graph.patternCapacity)

View File

@ -54,14 +54,18 @@ class ChemicalGeneratorMenu @JvmOverloads constructor(id: Int, inv: Inventory, t
}
}
val progress = ProgressGaugeWidget(this) { 1f - tile!!.workTicks.toFloat() / tile.workTicksTotal }
val progress = ProgressGaugeWidget(this)
val energy = LevelGaugeWidget(this, tile?.energy)
var burnTime by mSynchronizer.int()
var burnTime by mSynchronizer.int().property
init {
addStorageSlot(fuelSlot)
addStorageSlot(batterySlot)
addStorageSlot(residueSlot)
if (tile != null) {
progress.with { 1f - tile.workTicks.toFloat() / tile.workTicksTotal }
}
}
init {
@ -70,7 +74,6 @@ class ChemicalGeneratorMenu @JvmOverloads constructor(id: Int, inv: Inventory, t
override fun broadcastChanges() {
super.broadcastChanges()
progress.updateServer()
burnTime = (tile as ChemicalGeneratorBlockEntity).workTicks
}
}

View File

@ -19,15 +19,13 @@ class CobblerMenu @JvmOverloads constructor(
) : MatteryMenu(MMenus.COBBLESTONE_GENERATOR, p_38852_, inventory, tile) {
val storageSlots = (tile?.container ?: SimpleContainer(CobblerBlockEntity.CONTAINER_SIZE)).let { c -> immutableList(c.containerSize) { addStorageSlot(MachineOutputSlot(c, it)) } }
val redstone = EnumInputWithFeedback<RedstoneSetting>(this)
val itemConfig = ItemHandlerPlayerInput(this, false, false)
val itemConfig = ItemHandlerPlayerInput(this)
val progress: ProgressGaugeWidget
val progress = ProgressGaugeWidget(this)
init {
if (tile == null)
progress = ProgressGaugeWidget(this)
else {
progress = ProgressGaugeWidget(this, tile::workProgress, tile::isUnableToProcess)
if (tile != null) {
progress.with(tile::workProgress, tile::isUnableToProcess)
redstone.with(tile.redstoneControl::redstoneSetting)
itemConfig.configure(tile.itemConfig)
}

View File

@ -20,7 +20,7 @@ class PlatePressMenu @JvmOverloads constructor(
val inputSlot = MatterySlot(tile?.inputContainer ?: SimpleContainer(1), 0)
val outputSlot = MachineOutputSlot(tile?.outputContainer ?: SimpleContainer(1), 0) { tile?.popExperience(ply as ServerPlayer) }
val progressGauge = if (tile != null) ProgressGaugeWidget(this, tile::workProgress, tile::isUnableToProcess) else ProgressGaugeWidget(this)
val progressGauge = ProgressGaugeWidget(this)
val itemConfig = ItemHandlerPlayerInput(this, allowPush = true)
val energyConfig = EnergyPlayerInput(this, allowPull = true)
@ -31,6 +31,7 @@ class PlatePressMenu @JvmOverloads constructor(
addInventorySlots()
if (tile != null) {
progressGauge.with(tile::workProgress, tile::isUnableToProcess)
itemConfig.configure(tile.itemConfig)
energyConfig.configure(tile.energyConfig)
}

View File

@ -1,10 +0,0 @@
package ru.dbotthepony.mc.otm.menu.widget
import net.minecraft.world.inventory.ContainerData
import ru.dbotthepony.mc.otm.menu.MatteryMenu
abstract class AbstractWidget(val menu: MatteryMenu,) {
val slotID: Int = menu.addWidget(this)
abstract fun updateServer()
}

View File

@ -4,46 +4,42 @@ import ru.dbotthepony.mc.otm.capability.energy.IMatteryEnergyStorage
import ru.dbotthepony.mc.otm.capability.matter.IMatterStorage
import ru.dbotthepony.mc.otm.capability.matter.IPatternStorage
import ru.dbotthepony.mc.otm.core.math.Decimal
import ru.dbotthepony.mc.otm.core.util.DecimalValueCodec
import ru.dbotthepony.mc.otm.menu.MatteryMenu
@Suppress("unused")
class LevelGaugeWidget(menu: MatteryMenu) : AbstractWidget(menu) {
class LevelGaugeWidget(menu: MatteryMenu) {
var levelProvider = { Decimal.ONE }
var maxLevelProvider = { Decimal.ONE }
var level by menu.mSynchronizer.fraction()
private set
var maxLevel by menu.mSynchronizer.fraction()
private set
val level by menu.mSynchronizer.ComputedField(getter = { levelProvider.invoke() }, codec = DecimalValueCodec)
val maxLevel by menu.mSynchronizer.ComputedField(getter = { maxLevelProvider.invoke() }, codec = DecimalValueCodec)
constructor(
menu: MatteryMenu,
power: IMatteryEnergyStorage?
) : this(menu) {
if (power == null) return
this.levelProvider = power::batteryLevel
this.maxLevelProvider = power::maxBatteryLevel
if (power != null) {
with(power)
}
}
constructor(
menu: MatteryMenu,
matter: IMatterStorage?
) : this(menu) {
if (matter == null) return
this.levelProvider = matter::storedMatter
this.maxLevelProvider = matter::maxStoredMatter
if (matter != null) {
with(matter)
}
}
constructor(
menu: MatteryMenu,
patterns: IPatternStorage?
) : this(menu) {
if (patterns == null) return
this.levelProvider = { Decimal(patterns.storedPatterns) }
this.maxLevelProvider = { Decimal(patterns.patternCapacity) }
if (patterns != null) {
with(patterns)
}
}
constructor(
@ -55,12 +51,29 @@ class LevelGaugeWidget(menu: MatteryMenu) : AbstractWidget(menu) {
this.maxLevelProvider = maxLevel
}
override fun updateServer() {
level = levelProvider.invoke()
maxLevel = maxLevelProvider.invoke()
val percentage: Float get() = level.percentage(maxLevel)
fun with(level: () -> Decimal, maxLevel: () -> Decimal): LevelGaugeWidget {
this.levelProvider = level
this.maxLevelProvider = maxLevel
return this
}
fun level(): Decimal = level
fun maxLevel(): Decimal = maxLevel
fun percentage(): Float = level.percentage(maxLevel)
fun with(patterns: IPatternStorage): LevelGaugeWidget {
this.levelProvider = { Decimal(patterns.storedPatterns) }
this.maxLevelProvider = { Decimal(patterns.patternCapacity) }
return this
}
fun with(power: IMatteryEnergyStorage): LevelGaugeWidget {
this.levelProvider = power::batteryLevel
this.maxLevelProvider = power::maxBatteryLevel
return this
}
fun with(matter: IMatterStorage): LevelGaugeWidget {
this.levelProvider = matter::storedMatter
this.maxLevelProvider = matter::maxStoredMatter
return this
}
}

View File

@ -1,35 +1,42 @@
package ru.dbotthepony.mc.otm.menu.widget
import mekanism.api.functions.FloatSupplier
import ru.dbotthepony.mc.otm.menu.MatteryMenu
import java.util.function.BooleanSupplier
@Suppress("unused")
class ProgressGaugeWidget(menu: MatteryMenu) : AbstractWidget(menu) {
var progress = {0f}
var stuck = {false}
var progressContainer by menu.mSynchronizer.short()
var stuckContainer by menu.mSynchronizer.bool().property
class ProgressGaugeWidget(menu: MatteryMenu) {
var progressSupplier: FloatSupplier = FloatSupplier { 0f }
var stuckSupplier: BooleanSupplier = BooleanSupplier { false }
val percentage by menu.mSynchronizer.ComputedFloatField(getter = { progressSupplier.asFloat })
val isStuck by menu.mSynchronizer.ComputedBooleanField(getter = { stuckSupplier.asBoolean })
constructor(
menu: MatteryMenu,
progress: () -> Float
progress: FloatSupplier
) : this(menu) {
this.progress = progress
this.progressSupplier = progress
}
constructor(
menu: MatteryMenu,
progress: () -> Float,
stuck: () -> Boolean,
progress: FloatSupplier,
stuck: BooleanSupplier,
) : this(menu) {
this.progress = progress
this.stuck = stuck
this.progressSupplier = progress
this.stuckSupplier = stuck
}
override fun updateServer() {
progressContainer = (Math.max(Math.min(progress.invoke(), 1f), 0f) * 10_000f).toInt().toShort()
stuckContainer = stuck.invoke()
fun with(progress: FloatSupplier): ProgressGaugeWidget {
this.progressSupplier = progress
this.stuckSupplier = BooleanSupplier { false }
return this
}
fun isStuck(): Boolean = stuckContainer
fun percentage(): Float = progressContainer / 10_000f
fun with(progress: FloatSupplier, stuck: BooleanSupplier): ProgressGaugeWidget {
this.progressSupplier = progress
this.stuckSupplier = stuck
return this
}
}