Skip to content

Commit

Permalink
Merge pull request #40 from graycat27/digit#38
Browse files Browse the repository at this point in the history
小数の桁数を見直して、見た目を改善
  • Loading branch information
graycat27 authored Mar 13, 2021
2 parents 2241efc + 7703627 commit b3774bb
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
*/
public class GuiTextFormat {

/** 1,234.5 from Float */
public static final String floatStr1f = "%,.1f";
/** 1,234.568 from Float */
public static final String floatStr3f = "%,.3f";

/** 12° from int */
public static final String pitchStr = "%+d"+ DEGREES;
/** 12.3° from float */
public static final String pitchStrDecimal = "%+.1f" + DEGREES;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;

import static com.github.graycat27.flightHUDmod.consts.GuiTextFormat.pitchStr;
import static com.github.graycat27.flightHUDmod.consts.GuiTextFormat.pitchStrDecimal;
import static com.github.graycat27.flightHUDmod.FlightHUDMod.modSettings;

/**
Expand Down Expand Up @@ -49,7 +50,7 @@ private void initDisplayComponent(){
int centerY = windowHeight / 2;

//center display
int pitchWidth = mc.fontRenderer.getStringWidth(String.format(Line.angleText, getDgrString(Pitch.UP)));
int pitchWidth = mc.fontRenderer.getStringWidth(String.format(Line.angleText, getDgrStringDecimal1(Pitch.UP)));
int markWidth = mc.fontRenderer.getStringWidth(Line.mark);
int height = mc.fontRenderer.FONT_HEIGHT;
boolean isVisible = this.isDisplayed();
Expand Down Expand Up @@ -102,16 +103,16 @@ private void initDisplayComponent(){
//movement marker
Speed speed = FlightHUDMod.getGuiController().getSpeed();
if(speed != null){
int flightDegrees;
float flightDegrees;
double levelY;
if(speed.getHorizonSpeed() != 0) {
double actualSpeedAngleRad = Math.atan(speed.getVerticalSpeed() / speed.getHorizonSpeed());
flightDegrees = (int) Math.toDegrees(actualSpeedAngleRad);
flightDegrees = (float) Math.toDegrees(actualSpeedAngleRad);
}else {
flightDegrees = speed.getVerticalSpeed() == 0 ? Pitch.LEVEL : (speed.getVerticalSpeed() > 0) ? Pitch.UP : Pitch.DOWN;
}

int deltaDgr = flightDegrees - pitch.value();
float deltaDgr = flightDegrees - pitch.value();
if(deltaDgr > Pitch.UP){
deltaDgr = Pitch.UP;
}
Expand All @@ -128,7 +129,8 @@ private void initDisplayComponent(){
levelY = windowHeight / 2.0 - height;
}

String flightPitch = getDgrString(flightDegrees);
String flightPitch = (-0.1f < flightDegrees && flightDegrees < 0.1f) ?
getDgrString((int)flightDegrees) : getDgrStringDecimal1(flightDegrees);
String flightPitchText = String.format("> %s <", flightPitch);
int width = mc.fontRenderer.getStringWidth(flightPitchText);
speedPitchTextDisplay = new TextDisplay(posX, (int)(centerY - levelY),
Expand All @@ -144,6 +146,12 @@ public static String getDgrString(int pitchValue){
}
return " "+ pitchValue + Pitch.DEGREES;
}
public static String getDgrStringDecimal1(float pitchValue){
if(pitchValue != Pitch.LEVEL) {
return String.format(pitchStrDecimal, pitchValue);
}
return " "+ pitchValue + Pitch.DEGREES;
}


@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ private static class DefaultValue{
static boolean showHUD = true;
static int interval = 15;
static int positionCompass = 25;
static int positionHeight = 73;
static int positionHeight = 67;
static int positionPitch = 50;
static int positionSpeed = 27;
static int positionSpeed = 33;
}

/** FlightHUDの表示ON/OFF */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.minecraft.client.entity.player.ClientPlayerEntity;

import static com.github.graycat27.flightHUDmod.consts.GuiTextFormat.floatStr3f;
import static com.github.graycat27.flightHUDmod.consts.GuiTextFormat.floatStr1f;

public class Height implements IUnit {

Expand Down Expand Up @@ -36,9 +36,9 @@ public double getHeight(){
return (double)heightVal/DIGIT;
}

/** 精度は小数第2位まで保証(3桁取得) */
/** 精度は整数桁のみ保証(小数第2位で四捨五入した値を取得) */
public String valToString(){
return String.format(floatStr3f, getHeight());
return String.format(floatStr1f, getHeight());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.minecraft.client.entity.player.ClientPlayerEntity;

import static com.github.graycat27.flightHUDmod.guiComponent.PitchMeter.getDgrString;
import static com.github.graycat27.flightHUDmod.guiComponent.PitchMeter.getDgrStringDecimal1;

public class Pitch implements IUnit {

Expand All @@ -15,7 +15,7 @@ public class Pitch implements IUnit {
public static final int UP = 90;

/** 内部管理変数。仰俯角。下向きが正 */
private int pitch;
private float pitch;

/** コンストラクタ<br>
* @throws IllegalArgumentException player must not be null. pitch must in -90 to 90 */
Expand All @@ -26,26 +26,25 @@ public Pitch(final ClientPlayerEntity player){

//プレイヤーの向いている仰俯角を算出
/* playerPitch = LEVELを0として、下を正とした回転角度 */
float playerPitch = player.rotationPitch;
int intFlightPitch = Math.round((-1)*playerPitch);
if( intFlightPitch < DOWN || UP < intFlightPitch){
float playerPitch = (-1)*player.rotationPitch;
if( playerPitch < DOWN || UP < playerPitch){
//must between -90to90
throw new IllegalArgumentException("direction must in -90to90 but was "+ pitch);
}
this.pitch = intFlightPitch;
this.pitch = playerPitch;
}

private Pitch(int pitch){
private Pitch(float pitch){
this.pitch = pitch;
}

public int value(){
public float value(){
return pitch;
}

@Override
public String valToString(){
return getDgrString(pitch);
return getDgrStringDecimal1(pitch);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.minecraft.client.entity.player.ClientPlayerEntity;

import static com.github.graycat27.flightHUDmod.consts.GuiTextFormat.floatStr3f;
import static com.github.graycat27.flightHUDmod.consts.GuiTextFormat.floatStr1f;

public class Speed implements IUnit {

Expand Down Expand Up @@ -84,23 +84,23 @@ public double getActualSpeed(){
return actualSpeed;
}
public String getActualSpeedValStr(){
return String.format(floatStr3f, actualSpeed);
return String.format(floatStr1f, actualSpeed);
}

/** 水平速度を返す */
public double getHorizonSpeed(){
return horizonSpeed;
}
public String getHorizonSpeedValStr(){
return String.format(floatStr3f, horizonSpeed);
return String.format(floatStr1f, horizonSpeed);
}

/** 鉛直速度を返す */
public double getVerticalSpeed(){
return verticalSpeed;
}
public String getVerticalSpeedValStr(){
return String.format(floatStr3f, verticalSpeed);
return String.format(floatStr1f, verticalSpeed);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@

public class GuiTextFormatTest {

@Test
public void testFloatStr1f(){
float f = 1234.56f;
String s = String.format(GuiTextFormat.floatStr1f, f);
assertEquals("1,234.6", s); //rounded number
}
@Test
public void testFloatStr1fRoundDown(){
float f = 1234.32f;
String s = String.format(GuiTextFormat.floatStr1f, f);
assertEquals("1,234.3", s); //rounded number
}

@Test
public void testFloatStr3f() {
float f = 1234.5678f;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.github.graycat27.flightHUDmod.guiComponent;

import org.junit.Test;

import static org.junit.jupiter.api.Assertions.*;

public class PitchMeterTest {

private final float f1 = 45.678f;
private final float f0 = 0.0f;
private final float fn1 = -45.678f;

/* getDgrString */
@Test
public void testGetDgrString(){
String s = PitchMeter.getDgrString((int)f0);
assertEquals(" 0°", s);
}
@Test
public void testGetDgrString1(){
String s = PitchMeter.getDgrString((int)f1);
assertEquals("+45°", s); //intキャストで切り捨て
}
@Test
public void testGetDgrString2(){
String s = PitchMeter.getDgrString((int)fn1);
assertEquals("-45°", s);
}

/* getDgrStringDecimal1 */
@Test
public void testGetDgrStringDecimal1(){
String s = PitchMeter.getDgrStringDecimal1(f0);
assertEquals(" 0.0°", s);
}
@Test
public void testGetDgrStringDecimal11(){
String s = PitchMeter.getDgrStringDecimal1(f1);
assertEquals("+45.7°", s); //formatで四捨五入
}
@Test
public void testGetDgrStringDecimal12(){
String s = PitchMeter.getDgrStringDecimal1(fn1);
assertEquals("-45.7°", s);
}

}

0 comments on commit b3774bb

Please sign in to comment.