Merge pull request #1145 from thatsIch/e-number-converter
Using a unified number converter to display short versions of numbers
This commit is contained in:
commit
6bd4472229
5 changed files with 346 additions and 135 deletions
|
@ -29,31 +29,17 @@ import net.minecraft.item.ItemStack;
|
|||
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.core.AEConfig;
|
||||
import appeng.util.ReadableNumberConverter;
|
||||
|
||||
|
||||
public class AppEngRenderItem extends RenderItem
|
||||
{
|
||||
private static final ReadableNumberConverter NUMBER_CONVERTER = ReadableNumberConverter.INSTANCE;
|
||||
|
||||
public IAEItemStack aeStack;
|
||||
|
||||
private void renderQuad(Tessellator par1Tessellator, int par2, int par3, int par4, int par5, int par6)
|
||||
{
|
||||
par1Tessellator.startDrawingQuads();
|
||||
par1Tessellator.setColorOpaque_I( par6 );
|
||||
par1Tessellator.addVertex( par2, par3, 0.0D );
|
||||
par1Tessellator.addVertex( par2, par3 + par5, 0.0D );
|
||||
par1Tessellator.addVertex( par2 + par4, par3 + par5, 0.0D );
|
||||
par1Tessellator.addVertex( par2 + par4, par3, 0.0D );
|
||||
par1Tessellator.draw();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void renderItemOverlayIntoGUI(FontRenderer par1FontRenderer, TextureManager par2RenderEngine, ItemStack par3ItemStack, int par4, int par5)
|
||||
{
|
||||
this.renderItemOverlayIntoGUI( par1FontRenderer, par2RenderEngine, par3ItemStack, par4, par5, null );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void renderItemOverlayIntoGUI(FontRenderer par1FontRenderer, TextureManager par2RenderEngine, ItemStack is, int par4, int par5, String par6Str)
|
||||
public void renderItemOverlayIntoGUI( FontRenderer fontRenderer, TextureManager textureManager, ItemStack is, int par4, int par5, String par6Str )
|
||||
{
|
||||
if( is != null )
|
||||
{
|
||||
|
@ -61,8 +47,8 @@ public class AppEngRenderItem extends RenderItem
|
|||
float inverseScaleFactor = 1.0f / scaleFactor;
|
||||
int offset = AEConfig.instance.useTerminalUseLargeFont() ? 0 : -1;
|
||||
|
||||
boolean unicodeFlag = par1FontRenderer.getUnicodeFlag();
|
||||
par1FontRenderer.setUnicodeFlag( false );
|
||||
boolean unicodeFlag = fontRenderer.getUnicodeFlag();
|
||||
fontRenderer.setUnicodeFlag( false );
|
||||
|
||||
if( is.getItem().showDurabilityBar( is ) )
|
||||
{
|
||||
|
@ -89,82 +75,60 @@ public class AppEngRenderItem extends RenderItem
|
|||
|
||||
if( is.stackSize == 0 )
|
||||
{
|
||||
String var6 = AEConfig.instance.useTerminalUseLargeFont() ? "+" : "Craft";
|
||||
String craftLabelText = AEConfig.instance.useTerminalUseLargeFont() ? "+" : "Craft";
|
||||
GL11.glDisable( GL11.GL_LIGHTING );
|
||||
GL11.glDisable( GL11.GL_DEPTH_TEST );
|
||||
GL11.glPushMatrix();
|
||||
GL11.glScaled( scaleFactor, scaleFactor, scaleFactor );
|
||||
int X = (int) (((float) par4 + offset + 16.0f - par1FontRenderer.getStringWidth( var6 ) * scaleFactor) * inverseScaleFactor);
|
||||
int X = (int) ( ( (float) par4 + offset + 16.0f - fontRenderer.getStringWidth( craftLabelText ) * scaleFactor ) * inverseScaleFactor );
|
||||
int Y = (int) ( ( (float) par5 + offset + 16.0f - 7.0f * scaleFactor ) * inverseScaleFactor );
|
||||
par1FontRenderer.drawStringWithShadow( var6, X, Y, 16777215 );
|
||||
fontRenderer.drawStringWithShadow( craftLabelText, X, Y, 16777215 );
|
||||
GL11.glPopMatrix();
|
||||
GL11.glEnable( GL11.GL_LIGHTING );
|
||||
GL11.glEnable( GL11.GL_DEPTH_TEST );
|
||||
}
|
||||
|
||||
long amount = this.aeStack != null ? this.aeStack.getStackSize() : is.stackSize;
|
||||
amount = Math.max( amount, 999999999999L );
|
||||
|
||||
if( amount != 0 )
|
||||
{
|
||||
String var6 = String.valueOf( Math.abs( amount ) );
|
||||
|
||||
if ( AEConfig.instance.useTerminalUseLargeFont() )
|
||||
{
|
||||
if ( amount > 999999999 )
|
||||
{
|
||||
var6 = String.valueOf( ( int ) Math.floor( amount / 1000000000.0 ) ) + 'B';
|
||||
}
|
||||
else if ( amount > 99999999 )
|
||||
{
|
||||
var6 = "." + (int) Math.floor( amount / 100000000.0 ) + 'B';
|
||||
}
|
||||
else if ( amount > 999999 )
|
||||
{
|
||||
var6 = String.valueOf( ( int ) Math.floor( amount / 1000000.0 ) ) + 'M';
|
||||
}
|
||||
else if ( amount > 99999 )
|
||||
{
|
||||
var6 = "." + (int) Math.floor( amount / 100000.0 ) + 'M';
|
||||
}
|
||||
else if ( amount > 999 )
|
||||
{
|
||||
var6 = String.valueOf( ( int ) Math.floor( amount / 1000.0 ) ) + 'K';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( amount > 999999999 )
|
||||
{
|
||||
var6 = String.valueOf( ( int ) Math.floor( amount / 1000000000.0 ) ) + 'B';
|
||||
}
|
||||
else if ( amount > 999999999 )
|
||||
{
|
||||
var6 = String.valueOf( ( int ) Math.floor( amount / 1000000000.0 ) ) + 'B';
|
||||
}
|
||||
else if ( amount > 999999 )
|
||||
{
|
||||
var6 = String.valueOf( ( int ) Math.floor( amount / 1000000.0 ) ) + 'M';
|
||||
}
|
||||
else if ( amount > 9999 )
|
||||
{
|
||||
var6 = String.valueOf( ( int ) Math.floor( amount / 1000.0 ) ) + 'K';
|
||||
}
|
||||
}
|
||||
final String stackSize = this.getToBeRenderedStackSize( amount );
|
||||
|
||||
GL11.glDisable( GL11.GL_LIGHTING );
|
||||
GL11.glDisable( GL11.GL_DEPTH_TEST );
|
||||
GL11.glPushMatrix();
|
||||
GL11.glScaled( scaleFactor, scaleFactor, scaleFactor );
|
||||
int X = (int) (((float) par4 + offset + 16.0f - par1FontRenderer.getStringWidth( var6 ) * scaleFactor) * inverseScaleFactor);
|
||||
int X = (int) ( ( (float) par4 + offset + 16.0f - fontRenderer.getStringWidth( stackSize ) * scaleFactor ) * inverseScaleFactor );
|
||||
int Y = (int) ( ( (float) par5 + offset + 16.0f - 7.0f * scaleFactor ) * inverseScaleFactor );
|
||||
par1FontRenderer.drawStringWithShadow( var6, X, Y, 16777215 );
|
||||
fontRenderer.drawStringWithShadow( stackSize, X, Y, 16777215 );
|
||||
GL11.glPopMatrix();
|
||||
GL11.glEnable( GL11.GL_LIGHTING );
|
||||
GL11.glEnable( GL11.GL_DEPTH_TEST );
|
||||
}
|
||||
|
||||
par1FontRenderer.setUnicodeFlag( unicodeFlag );
|
||||
fontRenderer.setUnicodeFlag( unicodeFlag );
|
||||
}
|
||||
}
|
||||
|
||||
private String getToBeRenderedStackSize( long originalSize )
|
||||
{
|
||||
if( AEConfig.instance.useTerminalUseLargeFont() )
|
||||
{
|
||||
return NUMBER_CONVERTER.toShortHumanReadableForm( originalSize );
|
||||
}
|
||||
else
|
||||
{
|
||||
return NUMBER_CONVERTER.toHumanReadableForm( originalSize );
|
||||
}
|
||||
}
|
||||
|
||||
private void renderQuad( Tessellator par1Tessellator, int par2, int par3, int par4, int par5, int par6 )
|
||||
{
|
||||
par1Tessellator.startDrawingQuads();
|
||||
par1Tessellator.setColorOpaque_I( par6 );
|
||||
par1Tessellator.addVertex( par2, par3, 0.0D );
|
||||
par1Tessellator.addVertex( par2, par3 + par5, 0.0D );
|
||||
par1Tessellator.addVertex( par2 + par4, par3 + par5, 0.0D );
|
||||
par1Tessellator.addVertex( par2 + par4, par3, 0.0D );
|
||||
par1Tessellator.draw();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,9 +38,12 @@ import appeng.core.AELog;
|
|||
import appeng.tile.AEBaseTile;
|
||||
import appeng.tile.crafting.TileCraftingMonitorTile;
|
||||
import appeng.util.Platform;
|
||||
import appeng.util.ReadableNumberConverter;
|
||||
|
||||
|
||||
public class RenderBlockCraftingCPUMonitor extends RenderBlockCraftingCPU
|
||||
{
|
||||
private static final ReadableNumberConverter NUMBER_CONVERTER = ReadableNumberConverter.INSTANCE;
|
||||
|
||||
public RenderBlockCraftingCPUMonitor() {
|
||||
super( true, 20 );
|
||||
|
@ -173,22 +176,13 @@ public class RenderBlockCraftingCPUMonitor extends RenderBlockCraftingCPU
|
|||
GL11.glTranslatef( 0.0f, 0.14f, -0.24f );
|
||||
GL11.glScalef( 1.0f / 62.0f, 1.0f / 62.0f, 1.0f / 62.0f );
|
||||
|
||||
long qty = ais.getStackSize();
|
||||
if ( qty > 999999999999L )
|
||||
qty = 999999999999L;
|
||||
|
||||
String msg = Long.toString( qty );
|
||||
if ( qty > 1000000000 )
|
||||
msg = Long.toString( qty / 1000000000 ) + 'B';
|
||||
else if ( qty > 1000000 )
|
||||
msg = Long.toString( qty / 1000000 ) + 'M';
|
||||
else if ( qty > 9999 )
|
||||
msg = Long.toString( qty / 1000 ) + 'K';
|
||||
final long stackSize = ais.getStackSize();
|
||||
final String renderedStackSize = NUMBER_CONVERTER.toHumanReadableForm( stackSize );
|
||||
|
||||
FontRenderer fr = Minecraft.getMinecraft().fontRenderer;
|
||||
int width = fr.getStringWidth( msg );
|
||||
int width = fr.getStringWidth( renderedStackSize );
|
||||
GL11.glTranslatef( -0.5f * width, 0.0f, -1.0f );
|
||||
fr.drawString( msg, 0, 0, 0 );
|
||||
fr.drawString( renderedStackSize, 0, 0, 0 );
|
||||
|
||||
GL11.glPopAttrib();
|
||||
}
|
||||
|
|
|
@ -60,12 +60,13 @@ import appeng.core.localization.PlayerMessages;
|
|||
import appeng.helpers.Reflected;
|
||||
import appeng.me.GridAccessException;
|
||||
import appeng.util.Platform;
|
||||
import appeng.util.ReadableNumberConverter;
|
||||
import appeng.util.item.AEItemStack;
|
||||
|
||||
|
||||
public class PartStorageMonitor extends PartMonitor implements IPartStorageMonitor, IStackWatcherHost
|
||||
{
|
||||
|
||||
private static final ReadableNumberConverter NUMBER_CONVERTER = ReadableNumberConverter.INSTANCE;
|
||||
IAEItemStack configuredItem;
|
||||
boolean isLocked;
|
||||
IStackWatcher myWatcher;
|
||||
|
@ -339,22 +340,13 @@ public class PartStorageMonitor extends PartMonitor implements IPartStorageMonit
|
|||
GL11.glTranslatef( 0.0f, 0.14f, -0.24f );
|
||||
GL11.glScalef( 1.0f / 62.0f, 1.0f / 62.0f, 1.0f / 62.0f );
|
||||
|
||||
long qty = ais.getStackSize();
|
||||
if ( qty > 999999999999L )
|
||||
qty = 999999999999L;
|
||||
|
||||
String msg = Long.toString( qty );
|
||||
if ( qty > 1000000000 )
|
||||
msg = Long.toString( qty / 1000000000 ) + 'B';
|
||||
else if ( qty > 1000000 )
|
||||
msg = Long.toString( qty / 1000000 ) + 'M';
|
||||
else if ( qty > 9999 )
|
||||
msg = Long.toString( qty / 1000 ) + 'K';
|
||||
final long stackSize = ais.getStackSize();
|
||||
final String renderedStackSize = NUMBER_CONVERTER.toHumanReadableForm( stackSize );
|
||||
|
||||
FontRenderer fr = Minecraft.getMinecraft().fontRenderer;
|
||||
int width = fr.getStringWidth( msg );
|
||||
int width = fr.getStringWidth( renderedStackSize );
|
||||
GL11.glTranslatef( -0.5f * width, 0.0f, -1.0f );
|
||||
fr.drawString( msg, 0, 0, 0 );
|
||||
fr.drawString( renderedStackSize, 0, 0, 0 );
|
||||
|
||||
GL11.glPopAttrib();
|
||||
}
|
||||
|
|
114
src/main/java/appeng/util/ReadableNumberConverter.java
Normal file
114
src/main/java/appeng/util/ReadableNumberConverter.java
Normal file
|
@ -0,0 +1,114 @@
|
|||
package appeng.util;
|
||||
|
||||
|
||||
/**
|
||||
* Converter class to convert a large number into a SI system.
|
||||
*
|
||||
* @author thatsIch
|
||||
* @version rv2
|
||||
* @since rv2
|
||||
*/
|
||||
public enum ReadableNumberConverter
|
||||
{
|
||||
INSTANCE;
|
||||
|
||||
/**
|
||||
* Defines the base for a division, non-si standard could be 1024 for kilobytes
|
||||
*/
|
||||
private static final int DIVISION_BASE = 1000;
|
||||
|
||||
/**
|
||||
* for lg(1000) = 3, just saves some calculation
|
||||
*/
|
||||
private static final double LOG_DIVISION_BASE = Math.log( DIVISION_BASE );
|
||||
|
||||
/**
|
||||
* String representation of the sorted postfixes
|
||||
*/
|
||||
private static final char[] ENCODED_POSTFIXES = "KMGTPE".toCharArray();
|
||||
|
||||
/**
|
||||
* if a result would be higher than this threshold,
|
||||
* it is pushed into the next bigger group,
|
||||
* so the display string is shorter
|
||||
*/
|
||||
private static final int SHORT_THRESHOLD = 100;
|
||||
|
||||
/**
|
||||
* Converts a number into a human readable form. It will not round the number, but floor it.
|
||||
*
|
||||
* Example: 15555L -> 15.5K
|
||||
*
|
||||
* @param number to be converted number
|
||||
*
|
||||
* @return String in SI format cut down as far as possible
|
||||
*/
|
||||
public String toHumanReadableForm( long number )
|
||||
{
|
||||
final String sign = this.getSign( number );
|
||||
final long absNumber = Math.abs( number );
|
||||
|
||||
if( absNumber < DIVISION_BASE )
|
||||
return Long.toString( absNumber );
|
||||
|
||||
final int exp = (int) ( Math.log( absNumber ) / LOG_DIVISION_BASE );
|
||||
final char postFix = ENCODED_POSTFIXES[exp - 1];
|
||||
final int result = (int) ( absNumber / Math.pow( DIVISION_BASE, exp ) );
|
||||
|
||||
return String.format( "%s%d%s", sign, result, postFix );
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a number into a human readable form. It will not round the number, but floor it.
|
||||
* Will try to cut the number down 1 decimal earlier. This will limit the String size to 3 chars.
|
||||
*
|
||||
* Example: 900L -> 0.9K
|
||||
*
|
||||
* @param number to be converted number
|
||||
*
|
||||
* @return String in SI format cut down as far as possible
|
||||
*/
|
||||
public String toShortHumanReadableForm( long number )
|
||||
{
|
||||
final String sign = this.getSign( number );
|
||||
final long absNumber = Math.abs( number );
|
||||
|
||||
if( absNumber < DIVISION_BASE )
|
||||
return Long.toString( absNumber );
|
||||
|
||||
final int exp = (int) ( Math.log( absNumber ) / LOG_DIVISION_BASE );
|
||||
final int result = (int) ( absNumber / Math.pow( DIVISION_BASE, exp ) );
|
||||
if( result >= SHORT_THRESHOLD )
|
||||
{
|
||||
final int shortResult = result / SHORT_THRESHOLD;
|
||||
final char postFix = ENCODED_POSTFIXES[exp];
|
||||
|
||||
return String.format( "%s.%d%s", sign, shortResult, postFix );
|
||||
}
|
||||
else
|
||||
{
|
||||
final char postFix = ENCODED_POSTFIXES[exp - 1];
|
||||
|
||||
return String.format( "%s%d%s", sign, result, postFix );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets character representation of the sign of a number
|
||||
*
|
||||
* @param number maybe signed number
|
||||
*
|
||||
* @return '-' if the number is signed, else an empty character
|
||||
*/
|
||||
private String getSign( long number )
|
||||
{
|
||||
if ( number < 0 )
|
||||
{
|
||||
return "-";
|
||||
}
|
||||
else
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
147
src/test/java/appeng/util/ReadableNumberConverterTest.java
Normal file
147
src/test/java/appeng/util/ReadableNumberConverterTest.java
Normal file
|
@ -0,0 +1,147 @@
|
|||
package appeng.util;
|
||||
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
||||
/**
|
||||
* Tests for {@link appeng.util.ReadableNumberConverter}
|
||||
*
|
||||
* @author thatsIch
|
||||
* @version rv2
|
||||
* @since rv2
|
||||
*/
|
||||
public final class ReadableNumberConverterTest
|
||||
{
|
||||
private static final long NUMBER_NORMAL = 55L;
|
||||
private static final long NUMBER_KILO = 155555L;
|
||||
private static final long NUMBER_MEGA = 155555555L;
|
||||
private static final long NUMBER_GIGA = 155555555555L;
|
||||
private static final long NUMBER_TERA = 155555555555555L;
|
||||
private static final long NUMBER_PETA = 155555555555555555L;
|
||||
private static final long NUMBER_EXA = 1555555555555555555L;
|
||||
private static final long NUMBER_NEGATIVE_GIGA = -155555555555L;
|
||||
|
||||
private static final String RESULT_NORMAL = "55";
|
||||
private static final String RESULT_KILO = "155K";
|
||||
private static final String RESULT_MEGA = "155M";
|
||||
private static final String RESULT_GIGA = "155G";
|
||||
private static final String RESULT_TERA = "155T";
|
||||
private static final String RESULT_PETA = "155P";
|
||||
private static final String RESULT_EXA = "1E";
|
||||
private static final String RESULT_NEGATIVE_GIGA = "-155G";
|
||||
|
||||
private static final String RESULT_SHORT_NORMAL = "55";
|
||||
private static final String RESULT_SHORT_KILO = ".1M";
|
||||
private static final String RESULT_SHORT_MEGA = ".1G";
|
||||
private static final String RESULT_SHORT_GIGA = ".1T";
|
||||
private static final String RESULT_SHORT_TERA = ".1P";
|
||||
private static final String RESULT_SHORT_PETA = ".1E";
|
||||
private static final String RESULT_SHORT_EXA = "1E";
|
||||
private static final String RESULT_SHORT_NEGATIVE_GIGA = "-.1T";
|
||||
|
||||
private final ReadableNumberConverter converter;
|
||||
|
||||
public ReadableNumberConverterTest()
|
||||
{
|
||||
this.converter = ReadableNumberConverter.INSTANCE;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertNormal()
|
||||
{
|
||||
assertEquals( RESULT_NORMAL, this.converter.toHumanReadableForm( NUMBER_NORMAL ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertKilo()
|
||||
{
|
||||
assertEquals( RESULT_KILO, this.converter.toHumanReadableForm( NUMBER_KILO ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertMega()
|
||||
{
|
||||
assertEquals( RESULT_MEGA, this.converter.toHumanReadableForm( NUMBER_MEGA ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertGiga()
|
||||
{
|
||||
assertEquals( RESULT_GIGA, this.converter.toHumanReadableForm( NUMBER_GIGA ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertTera()
|
||||
{
|
||||
assertEquals( RESULT_TERA, this.converter.toHumanReadableForm( NUMBER_TERA ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertPeta()
|
||||
{
|
||||
assertEquals( RESULT_PETA, this.converter.toHumanReadableForm( NUMBER_PETA ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertExa()
|
||||
{
|
||||
assertEquals( RESULT_EXA, this.converter.toHumanReadableForm( NUMBER_EXA ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertNegativeGiga()
|
||||
{
|
||||
assertEquals( RESULT_NEGATIVE_GIGA, this.converter.toHumanReadableForm( NUMBER_NEGATIVE_GIGA ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertShortNormal()
|
||||
{
|
||||
assertEquals( RESULT_SHORT_NORMAL, this.converter.toShortHumanReadableForm( NUMBER_NORMAL ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertShortKilo()
|
||||
{
|
||||
assertEquals( RESULT_SHORT_KILO, this.converter.toShortHumanReadableForm( NUMBER_KILO ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertShortMega()
|
||||
{
|
||||
assertEquals( RESULT_SHORT_MEGA, this.converter.toShortHumanReadableForm( NUMBER_MEGA ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertShortGiga()
|
||||
{
|
||||
assertEquals( RESULT_SHORT_GIGA, this.converter.toShortHumanReadableForm( NUMBER_GIGA ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertShortTera()
|
||||
{
|
||||
assertEquals( RESULT_SHORT_TERA, this.converter.toShortHumanReadableForm( NUMBER_TERA ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertShortPeta()
|
||||
{
|
||||
assertEquals( RESULT_SHORT_PETA, this.converter.toShortHumanReadableForm( NUMBER_PETA ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertShortExa()
|
||||
{
|
||||
assertEquals( RESULT_SHORT_EXA, this.converter.toShortHumanReadableForm( NUMBER_EXA ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertShortNegativeGiga()
|
||||
{
|
||||
assertEquals( RESULT_SHORT_NEGATIVE_GIGA, this.converter.toShortHumanReadableForm( NUMBER_NEGATIVE_GIGA ) );
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue