From e28867e9136d10e2a1c0bdfb0274de89aa954d6b Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sun, 5 Feb 2023 17:52:54 +0700 Subject: [PATCH] Formatting without SI prefix while holding shift --- .../capability/energy/IEnergyStorageImpl.kt | 21 +++--- .../ru/dbotthepony/mc/otm/client/Ext.kt | 21 ++++++ .../render/blockentity/BlackHoleRenderer.kt | 3 +- .../GravitationStabilizerRenderer.kt | 5 +- .../client/screen/tech/EnergyCounterScreen.kt | 11 +-- .../client/screen/widget/MatterGaugePanel.kt | 3 +- .../client/screen/widget/PowerGaugePanel.kt | 3 +- .../mc/otm/core/util/Formatting.kt | 70 ++++++++++++++----- .../mc/otm/item/MatterCapacitorItem.kt | 5 +- .../mc/otm/matter/MatterManager.kt | 5 +- 10 files changed, 105 insertions(+), 42 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/energy/IEnergyStorageImpl.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/energy/IEnergyStorageImpl.kt index edd032081..a290fcb05 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/energy/IEnergyStorageImpl.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/energy/IEnergyStorageImpl.kt @@ -4,6 +4,7 @@ import net.minecraft.ChatFormatting import net.minecraft.network.chat.Component import net.minecraftforge.energy.IEnergyStorage import ru.dbotthepony.mc.otm.capability.FlowDirection +import ru.dbotthepony.mc.otm.client.ShiftPressedCond import ru.dbotthepony.mc.otm.core.math.Decimal import ru.dbotthepony.mc.otm.core.TranslatableComponent import ru.dbotthepony.mc.otm.core.util.formatPower @@ -17,8 +18,8 @@ internal fun batteryLevel(it: IEnergyStorage, tooltips: MutableList) tooltips.add( TranslatableComponent( "otm.item.power.storage", - it.energyStored.formatPower(), - it.maxEnergyStored.formatPower() + it.energyStored.formatPower(formatAsReadable = ShiftPressedCond), + it.maxEnergyStored.formatPower(formatAsReadable = ShiftPressedCond) ).withStyle(ChatFormatting.GRAY)) } @@ -26,8 +27,8 @@ internal fun batteryLevel(it: IMatteryEnergyStorage, tooltips: MutableList { if (it.maxInput != null) { tooltips.add( - TranslatableComponent("otm.item.power.throughput_mono", it.maxInput!!.formatPower()).withStyle( + TranslatableComponent("otm.item.power.throughput_mono", it.maxInput!!.formatPower(formatAsReadable = ShiftPressedCond)).withStyle( ChatFormatting.GRAY )) } else { @@ -50,7 +51,7 @@ internal fun batteryLevel(it: IMatteryEnergyStorage, tooltips: MutableList { if (it.maxOutput != null) { tooltips.add( - TranslatableComponent("otm.item.power.throughput_mono", it.maxOutput!!.formatPower()).withStyle( + TranslatableComponent("otm.item.power.throughput_mono", it.maxOutput!!.formatPower(formatAsReadable = ShiftPressedCond)).withStyle( ChatFormatting.GRAY )) } else { @@ -70,14 +71,14 @@ internal fun batteryLevel(it: IMatteryEnergyStorage, tooltips: MutableList(menu, inventory, title) { - override fun makeMainFrame(): FramePanel> { + override fun makeMainFrame(): FramePanel> { val frame = super.makeMainFrame()!! var label: Label = object : Label(this@EnergyCounterScreen, frame) { @@ -20,7 +21,7 @@ class EnergyCounterScreen(menu: EnergyCounterMenu, inventory: Inventory, title: super.tick() text = TranslatableComponent( "otm.item.power.passed", - menu.passed.formatPower() + menu.passed.formatPower(formatAsReadable = ShiftPressedCond) ) } } @@ -33,7 +34,7 @@ class EnergyCounterScreen(menu: EnergyCounterMenu, inventory: Inventory, title: super.tick() text = TranslatableComponent( "otm.item.power.average", - menu.average.formatPower() + menu.average.formatPower(formatAsReadable = ShiftPressedCond) ) } } @@ -46,7 +47,7 @@ class EnergyCounterScreen(menu: EnergyCounterMenu, inventory: Inventory, title: super.tick() text = TranslatableComponent( "otm.item.power.last_20_ticks", - menu.last20Ticks.formatPower() + menu.last20Ticks.formatPower(formatAsReadable = ShiftPressedCond) ) } } @@ -59,7 +60,7 @@ class EnergyCounterScreen(menu: EnergyCounterMenu, inventory: Inventory, title: super.tick() text = TranslatableComponent( "otm.item.power.last_tick", - menu.lastTick.formatPower() + menu.lastTick.formatPower(formatAsReadable = ShiftPressedCond) ) } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/MatterGaugePanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/MatterGaugePanel.kt index a34012c68..2e8eed2b5 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/MatterGaugePanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/MatterGaugePanel.kt @@ -9,6 +9,7 @@ import net.minecraft.client.gui.screens.Screen import net.minecraft.client.renderer.GameRenderer import net.minecraft.network.chat.Component import org.lwjgl.opengl.GL11 +import ru.dbotthepony.mc.otm.client.ShiftPressedCond import ru.dbotthepony.mc.otm.client.render.WidgetLocation import ru.dbotthepony.mc.otm.client.render.tesselator import ru.dbotthepony.mc.otm.client.screen.panels.EditablePanel @@ -44,7 +45,7 @@ open class MatterGaugePanel @JvmOverloads constructor( "otm.gui.matter.percentage_level", String.format("%.2f", widget.percentage() * 100.0) ), - formatMatterLevel(widget.level(), widget.maxLevel()) + formatMatterLevel(widget.level(), widget.maxLevel(), formatAsReadable = ShiftPressedCond) ) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/PowerGaugePanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/PowerGaugePanel.kt index 194ef0004..1f248a14c 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/PowerGaugePanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/PowerGaugePanel.kt @@ -3,6 +3,7 @@ package ru.dbotthepony.mc.otm.client.screen.widget import com.mojang.blaze3d.vertex.PoseStack import net.minecraft.client.gui.screens.Screen import net.minecraft.network.chat.Component +import ru.dbotthepony.mc.otm.client.ShiftPressedCond import ru.dbotthepony.mc.otm.core.TranslatableComponent import ru.dbotthepony.mc.otm.client.render.* import ru.dbotthepony.mc.otm.client.screen.panels.EditablePanel @@ -25,7 +26,7 @@ open class PowerGaugePanel @JvmOverloads constructor( protected open fun makeTooltip(): MutableList { return mutableListOf( TranslatableComponent("otm.gui.power.percentage_level", String.format("%.2f", widget.percentage() * 100.0)), - formatPowerLevel(widget.level(), widget.maxLevel()) + formatPowerLevel(widget.level(), widget.maxLevel(), formatAsReadable = ShiftPressedCond) ) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/Formatting.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/Formatting.kt index ad2dad48b..ac8d94713 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/Formatting.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/Formatting.kt @@ -1,5 +1,6 @@ package ru.dbotthepony.mc.otm.core.util +import it.unimi.dsi.fastutil.chars.CharArrayList import net.minecraft.network.chat.Component import ru.dbotthepony.mc.otm.core.TextComponent import ru.dbotthepony.mc.otm.core.TranslatableComponent @@ -8,7 +9,9 @@ import ru.dbotthepony.mc.otm.core.math.isNegative import ru.dbotthepony.mc.otm.core.math.isZero import java.math.BigDecimal import java.math.BigInteger +import java.util.function.BooleanSupplier import kotlin.math.absoluteValue +import kotlin.math.roundToInt private fun concat(numbers: String, suffix: Any): Component { if (suffix == "") @@ -75,7 +78,7 @@ fun BigInteger.determineSiPrefix(): SiPrefix? { return SiPrefix.MULTIPLIES.lastOrNull { it.integer!! <= num } } -fun BigInteger.formatSiComponent(suffix: Any = "", decimalPlaces: Int = 2): Component { +fun BigInteger.formatSiComponent(suffix: Any = "", decimalPlaces: Int = 2, formatAsReadable: BooleanSupplier = never): Component { require(decimalPlaces >= 0) { "Invalid amount of decimal places required: $decimalPlaces" } val prefix = determineSiPrefix() ?: return concat(toString(decimalPlaces), suffix) val isNegative = isNegative @@ -148,41 +151,74 @@ fun Double.determineSiPrefix(): SiPrefix? { } } -fun Decimal.formatSi(decimalPlaces: Int = 2): String { - require(decimalPlaces >= 0) { "Invalid amount of decimal places required: $decimalPlaces" } - val prefix = determineSiPrefix() ?: return toString(decimalPlaces) - return (this / prefix.impreciseFraction).toString(decimalPlaces) + prefix.symbol +private val never = BooleanSupplier { false } + +private fun reformat(numbers: String): String { + if (numbers.length <= 4) + return numbers + + val result = CharArrayList((numbers.length * 1.6).roundToInt()) + + var dot = numbers.lastIndexOf('.') + + if (dot != -1) { + for (i in numbers.length - 1 downTo dot) { + result.add(numbers[i]) + } + + dot-- + } else { + dot = numbers.length - 1 + } + + var c = 0 + + for (i in dot downTo 0) { + if (++c == 4) { + c = 1 + result.add(' ') + } + + result.add(numbers[i]) + } + + return String(CharArray(result.size) { + result.getChar(result.size - it - 1) + }) } -fun Int.formatSiComponent(suffix: Any = "", decimalPlaces: Int = 2): Component { +fun Int.formatSiComponent(suffix: Any = "", decimalPlaces: Int = 2, formatAsReadable: BooleanSupplier = never): Component { require(decimalPlaces >= 0) { "Invalid amount of decimal places required: $decimalPlaces" } + if (formatAsReadable.asBoolean) return concat(reformat(toString()), suffix) val prefix = determineSiPrefix() ?: return concat(toString(), suffix) return TranslatableComponent(prefix.formatLocaleKey, "%.${decimalPlaces}f".format(this.toFloat() / prefix.int!!.toFloat()), suffix) } -fun Double.formatSiComponent(suffix: Any = "", decimalPlaces: Int = 2): Component { +fun Double.formatSiComponent(suffix: Any = "", decimalPlaces: Int = 2, formatAsReadable: BooleanSupplier = never): Component { require(decimalPlaces >= 0) { "Invalid amount of decimal places required: $decimalPlaces" } + if (formatAsReadable.asBoolean) return concat(reformat("%.${decimalPlaces}f".format(this)), suffix) val prefix = determineSiPrefix() ?: return concat("%.${decimalPlaces}f".format(this), suffix) return TranslatableComponent(prefix.formatLocaleKey, "%.${decimalPlaces}f".format(this / prefix.double), suffix) } -fun Decimal.formatSiComponent(suffix: Any = "", decimalPlaces: Int = 2): Component { +fun Decimal.formatSiComponent(suffix: Any = "", decimalPlaces: Int = 2, formatAsReadable: BooleanSupplier = never): Component { require(decimalPlaces >= 0) { "Invalid amount of decimal places required: $decimalPlaces" } + if (formatAsReadable.asBoolean) return concat(reformat(toString(decimalPlaces)), suffix) val prefix = determineSiPrefix() ?: return concat(toString(decimalPlaces), suffix) return TranslatableComponent(prefix.formatLocaleKey, (this / prefix.impreciseFraction).toString(decimalPlaces), suffix) } -fun Int.formatPower(decimalPlaces: Int = 2) = formatSiComponent(TranslatableComponent("otm.gui.power.name"), decimalPlaces) -fun Decimal.formatPower(decimalPlaces: Int = 2) = formatSiComponent(TranslatableComponent("otm.gui.power.name"), decimalPlaces) -fun Decimal.formatMatter(decimalPlaces: Int = 2) = formatSiComponent(TranslatableComponent("otm.gui.matter.name"), decimalPlaces) -fun Decimal.formatMatterFull(decimalPlaces: Int = 2) = TranslatableComponent("otm.gui.matter.format", formatSiComponent(TranslatableComponent("otm.gui.matter.name"), decimalPlaces)) +fun Int.formatPower(decimalPlaces: Int = 2, formatAsReadable: BooleanSupplier = never) = formatSiComponent(TranslatableComponent("otm.gui.power.name"), decimalPlaces, formatAsReadable = formatAsReadable) +fun Decimal.formatPower(decimalPlaces: Int = 2, formatAsReadable: BooleanSupplier = never) = formatSiComponent(TranslatableComponent("otm.gui.power.name"), decimalPlaces, formatAsReadable = formatAsReadable) +fun Decimal.formatMatter(decimalPlaces: Int = 2, formatAsReadable: BooleanSupplier = never) = formatSiComponent(TranslatableComponent("otm.gui.matter.name"), decimalPlaces, formatAsReadable = formatAsReadable) +fun Decimal.formatMatterFull(decimalPlaces: Int = 2, formatAsReadable: BooleanSupplier = never) = TranslatableComponent("otm.gui.matter.format", formatSiComponent(TranslatableComponent("otm.gui.matter.name"), decimalPlaces, formatAsReadable = formatAsReadable)) -fun BigInteger.formatPower(decimalPlaces: Int = 2) = formatSiComponent(TranslatableComponent("otm.gui.power.name"), decimalPlaces) -fun BigInteger.formatMatter(decimalPlaces: Int = 2) = formatSiComponent(TranslatableComponent("otm.gui.matter.name"), decimalPlaces) -fun BigInteger.formatMatterFull(decimalPlaces: Int = 2) = TranslatableComponent("otm.gui.matter.format", formatSiComponent(TranslatableComponent("otm.gui.matter.name"), decimalPlaces)) +fun BigInteger.formatPower(decimalPlaces: Int = 2, formatAsReadable: BooleanSupplier = never) = formatSiComponent(TranslatableComponent("otm.gui.power.name"), decimalPlaces, formatAsReadable = formatAsReadable) +fun BigInteger.formatMatter(decimalPlaces: Int = 2, formatAsReadable: BooleanSupplier = never) = formatSiComponent(TranslatableComponent("otm.gui.matter.name"), decimalPlaces, formatAsReadable = formatAsReadable) +fun BigInteger.formatMatterFull(decimalPlaces: Int = 2, formatAsReadable: BooleanSupplier = never) = TranslatableComponent("otm.gui.matter.format", formatSiComponent(TranslatableComponent("otm.gui.matter.name"), decimalPlaces, formatAsReadable = formatAsReadable)) -fun formatPowerLevel(a: Decimal, b: Decimal, decimalPlaces: Int = 2) = TranslatableComponent("otm.gui.level", a.formatPower(decimalPlaces), b.formatPower(decimalPlaces)) -fun formatMatterLevel(a: Decimal, b: Decimal, decimalPlaces: Int = 2) = TranslatableComponent("otm.gui.level", a.formatMatter(decimalPlaces), b.formatMatter(decimalPlaces)) +fun formatPowerLevel(a: Decimal, b: Decimal, decimalPlaces: Int = 2, formatAsReadable: BooleanSupplier = never) = TranslatableComponent("otm.gui.level", a.formatPower(decimalPlaces, formatAsReadable = formatAsReadable), b.formatPower(decimalPlaces, formatAsReadable = formatAsReadable)) +fun formatMatterLevel(a: Decimal, b: Decimal, decimalPlaces: Int = 2, formatAsReadable: BooleanSupplier = never) = TranslatableComponent("otm.gui.level", a.formatMatter(decimalPlaces, formatAsReadable = formatAsReadable), b.formatMatter(decimalPlaces, formatAsReadable = formatAsReadable)) private fun padded(num: Int): String { if (num in 0 .. 9) { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/MatterCapacitorItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/MatterCapacitorItem.kt index 78a446aa8..b58b41715 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/MatterCapacitorItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/MatterCapacitorItem.kt @@ -17,6 +17,7 @@ import ru.dbotthepony.mc.otm.capability.FlowDirection import ru.dbotthepony.mc.otm.core.TranslatableComponent import ru.dbotthepony.mc.otm.capability.MatteryCapability import ru.dbotthepony.mc.otm.capability.matter.* +import ru.dbotthepony.mc.otm.client.ShiftPressedCond import ru.dbotthepony.mc.otm.core.math.Decimal import ru.dbotthepony.mc.otm.core.util.formatMatter import ru.dbotthepony.mc.otm.core.ifPresentK @@ -136,8 +137,8 @@ class MatterCapacitorItem : Item { p_41423_.add( TranslatableComponent( "otm.item.matter.normal", - it.storedMatter.formatMatter(), - capacity.formatMatter() + it.storedMatter.formatMatter(formatAsReadable = ShiftPressedCond), + capacity.formatMatter(formatAsReadable = ShiftPressedCond) ).withStyle(ChatFormatting.GRAY) ) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/MatterManager.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/MatterManager.kt index 83d058abd..15851efe9 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/MatterManager.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/MatterManager.kt @@ -60,6 +60,7 @@ import ru.dbotthepony.mc.otm.SERVER_IS_LIVE import ru.dbotthepony.mc.otm.SystemTime import ru.dbotthepony.mc.otm.capability.MatteryCapability import ru.dbotthepony.mc.otm.capability.drive.IMatteryDrive +import ru.dbotthepony.mc.otm.client.isShiftDown import ru.dbotthepony.mc.otm.client.minecraft import ru.dbotthepony.mc.otm.container.stream import ru.dbotthepony.mc.otm.core.math.Decimal @@ -1085,9 +1086,7 @@ object MatterManager { } fun tooltipEvent(event: ItemTooltipEvent) { - val window = minecraft.window.window - - if (InputConstants.isKeyDown(window, GLFW.GLFW_KEY_LEFT_SHIFT) || InputConstants.isKeyDown(window, GLFW.GLFW_KEY_RIGHT_SHIFT)) { + if (minecraft.window.isShiftDown) { val matter = get(event.itemStack, accountForStackSize = false) if (matter.hasMatterValue) {