Formatting without SI prefix while holding shift

This commit is contained in:
DBotThePony 2023-02-05 17:52:54 +07:00
parent 2d519356fa
commit e28867e913
Signed by: DBot
GPG Key ID: DCC23B5715498507
10 changed files with 105 additions and 42 deletions

View File

@ -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<Component>)
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<Compo
tooltips.add(
TranslatableComponent(
"otm.item.power.storage",
it.batteryLevel.formatPower(),
it.maxBatteryLevel.formatPower()
it.batteryLevel.formatPower(formatAsReadable = ShiftPressedCond),
it.maxBatteryLevel.formatPower(formatAsReadable = ShiftPressedCond)
).withStyle(ChatFormatting.GRAY))
if (it is IEnergyStorageImpl) {
@ -35,7 +36,7 @@ internal fun batteryLevel(it: IMatteryEnergyStorage, tooltips: MutableList<Compo
FlowDirection.INPUT -> {
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<Compo
FlowDirection.OUTPUT -> {
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<Compo
tooltips.add(
TranslatableComponent(
"otm.item.power.throughput",
maxInput.formatPower(),
maxOutput.formatPower()
maxInput.formatPower(formatAsReadable = ShiftPressedCond),
maxOutput.formatPower(formatAsReadable = ShiftPressedCond)
).withStyle(ChatFormatting.GRAY))
} else if (maxInput != null) {
tooltips.add(
TranslatableComponent(
"otm.item.power.throughput",
maxInput.formatPower(),
maxInput.formatPower(formatAsReadable = ShiftPressedCond),
TranslatableComponent("otm.item.power.infinity").withStyle(ChatFormatting.GRAY)
).withStyle(ChatFormatting.GRAY))
} else if (maxOutput != null) {
@ -85,7 +86,7 @@ internal fun batteryLevel(it: IMatteryEnergyStorage, tooltips: MutableList<Compo
TranslatableComponent(
"otm.item.power.throughput",
TranslatableComponent("otm.item.power.infinity").withStyle(ChatFormatting.GRAY),
maxOutput.formatPower(),
maxOutput.formatPower(formatAsReadable = ShiftPressedCond),
).withStyle(ChatFormatting.GRAY))
} else {
tooltips.add(

View File

@ -8,9 +8,11 @@ import net.minecraft.client.resources.sounds.SimpleSoundInstance
import net.minecraft.sounds.SoundEvents
import org.lwjgl.glfw.GLFW
import org.lwjgl.glfw.GLFW.GLFW_CURSOR
import ru.dbotthepony.mc.otm.isClient
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.DoubleBuffer
import java.util.function.BooleanSupplier
inline val minecraft: Minecraft get() = Minecraft.getInstance()
inline val font: Font get() = minecraft.font
@ -20,6 +22,25 @@ fun Window.isKeyDown(key: Int) = InputConstants.isKeyDown(window, key)
val Window.isShiftDown get() = isKeyDown(InputConstants.KEY_LSHIFT) || isKeyDown(InputConstants.KEY_RSHIFT)
val Window.isCtrlDown get() = isKeyDown(InputConstants.KEY_RCONTROL) || isKeyDown(InputConstants.KEY_LCONTROL)
object ShiftPressedCond : BooleanSupplier {
override fun getAsBoolean(): Boolean {
if (isClient) {
return impl()
}
return false
}
private fun impl(): Boolean {
return minecraft.window.isShiftDown
}
}
object ShiftDepressedCond : BooleanSupplier {
override fun getAsBoolean(): Boolean {
return !ShiftPressedCond.asBoolean
}
}
fun playGuiClickSound() {
minecraft.soundManager.play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1.0f))

View File

@ -15,6 +15,7 @@ import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.block.entity.tech.GravitationStabilizerBlockEntity
import ru.dbotthepony.mc.otm.block.entity.blackhole.BlackHoleBlockEntity
import ru.dbotthepony.mc.otm.capability.matteryPlayer
import ru.dbotthepony.mc.otm.client.ShiftPressedCond
import ru.dbotthepony.mc.otm.client.minecraft
import ru.dbotthepony.mc.otm.client.render.*
import ru.dbotthepony.mc.otm.core.math.RGBAColor
@ -180,7 +181,7 @@ class BlackHoleRenderer(private val context: BlockEntityRendererProvider.Context
poseStack.scale(scale, scale, scale)
val font = Minecraft.getInstance().font
val text1 = TranslatableComponent("otm.3d2d.gravitation_stabilizer.mass", tile.mass.formatMatter())
val text1 = TranslatableComponent("otm.3d2d.gravitation_stabilizer.mass", tile.mass.formatMatter(formatAsReadable = ShiftPressedCond))
val text2 = TranslatableComponent("otm.3d2d.gravitation_stabilizer.strength", "%.2f".format(tile.gravitationStrength))
val sorse = DynamicBufferSource.WORLD

View File

@ -16,6 +16,7 @@ import ru.dbotthepony.mc.otm.block.RotatableMatteryBlock
import ru.dbotthepony.mc.otm.block.entity.tech.GravitationStabilizerBlockEntity
import ru.dbotthepony.mc.otm.block.entity.blackhole.BlackHoleBlockEntity
import ru.dbotthepony.mc.otm.block.entity.WorkerState
import ru.dbotthepony.mc.otm.client.ShiftPressedCond
import ru.dbotthepony.mc.otm.client.minecraft
import ru.dbotthepony.mc.otm.client.render.*
import ru.dbotthepony.mc.otm.core.*
@ -156,11 +157,11 @@ class GravitationStabilizerRenderer(private val context: BlockEntityRendererProv
val sorse = DynamicBufferSource.WORLD
val font = minecraft.font
font.drawAligned(poseStack, sorse, TranslatableComponent("otm.3d2d.gravitation_stabilizer.mass", bhTile.mass.formatMatter()), TextAlign.TOP_CENTER, 1f, -font.lineHeight.toFloat() / 2f + 1f, 0)
font.drawAligned(poseStack, sorse, TranslatableComponent("otm.3d2d.gravitation_stabilizer.mass", bhTile.mass.formatMatter(formatAsReadable = ShiftPressedCond)), TextAlign.TOP_CENTER, 1f, -font.lineHeight.toFloat() / 2f + 1f, 0)
font.drawAligned(poseStack, sorse, TranslatableComponent("otm.3d2d.gravitation_stabilizer.strength", "%.2f".format(bhTile.gravitationStrength)), TextAlign.TOP_CENTER, 1f, font.lineHeight.toFloat() / 2f + 1f, 0)
poseStack.translate(0.2f, 0f, -0.5f)
font.drawAligned(poseStack, sorse, TranslatableComponent("otm.3d2d.gravitation_stabilizer.mass", bhTile.mass.formatMatter()), TextAlign.TOP_CENTER, 0f, -font.lineHeight.toFloat() / 2f, 0xFFFFFF)
font.drawAligned(poseStack, sorse, TranslatableComponent("otm.3d2d.gravitation_stabilizer.mass", bhTile.mass.formatMatter(formatAsReadable = ShiftPressedCond)), TextAlign.TOP_CENTER, 0f, -font.lineHeight.toFloat() / 2f, 0xFFFFFF)
font.drawAligned(poseStack, sorse, TranslatableComponent("otm.3d2d.gravitation_stabilizer.strength", "%.2f".format(bhTile.gravitationStrength)), TextAlign.TOP_CENTER, 0f, font.lineHeight.toFloat() / 2f, 0xFFFFFF)
poseStack.popPose()

View File

@ -2,6 +2,7 @@ package ru.dbotthepony.mc.otm.client.screen.tech
import net.minecraft.network.chat.Component
import net.minecraft.world.entity.player.Inventory
import ru.dbotthepony.mc.otm.client.ShiftPressedCond
import ru.dbotthepony.mc.otm.client.screen.MatteryScreen
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.client.screen.panels.*
@ -12,7 +13,7 @@ import ru.dbotthepony.mc.otm.core.util.formatPower
import ru.dbotthepony.mc.otm.menu.tech.EnergyCounterMenu
class EnergyCounterScreen(menu: EnergyCounterMenu, inventory: Inventory, title: Component) : MatteryScreen<EnergyCounterMenu>(menu, inventory, title) {
override fun makeMainFrame(): FramePanel<out MatteryScreen<*>> {
override fun makeMainFrame(): FramePanel<MatteryScreen<*>> {
val frame = super.makeMainFrame()!!
var label: Label<EnergyCounterScreen> = object : Label<EnergyCounterScreen>(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)
)
}
}

View File

@ -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<S : Screen> @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)
)
}

View File

@ -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<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())
formatPowerLevel(widget.level(), widget.maxLevel(), formatAsReadable = ShiftPressedCond)
)
}

View File

@ -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])
}
fun Int.formatSiComponent(suffix: Any = "", decimalPlaces: Int = 2): Component {
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, 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) {

View File

@ -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)
)
}

View File

@ -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) {