Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

Commit 366f674

Browse files
committed
Add automatic scaling of Units used for bigger numbers
1 parent 8645bde commit 366f674

File tree

12 files changed

+510
-216
lines changed

12 files changed

+510
-216
lines changed

MicroEngineerProject/MicroEngineer/Entries/BaseEntry.cs

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using Newtonsoft.Json;
2-
using System.Xml;
3-
using UnityEngine;
42

53
namespace MicroMod
64
{
@@ -20,7 +18,15 @@ public class BaseEntry
2018
[JsonProperty]
2119
public bool HideWhenNoData;
2220
[JsonProperty]
23-
public string Unit;
21+
public string MiliUnit;
22+
[JsonProperty]
23+
public string BaseUnit;
24+
[JsonProperty]
25+
public string KiloUnit;
26+
[JsonProperty]
27+
public string MegaUnit;
28+
[JsonProperty]
29+
public string GigaUnit;
2430
[JsonProperty]
2531
public byte NumberOfDecimalDigits;
2632
[JsonProperty("Formatting")]
@@ -43,11 +49,81 @@ public virtual string ValueDisplay
4349
if (EntryValue == null)
4450
return "-";
4551

46-
return String.IsNullOrEmpty(this.Formatting) ? EntryValue.ToString() : String.Format(Formatting, EntryValue);
52+
if (String.IsNullOrEmpty(this.Formatting))
53+
return EntryValue.ToString();
54+
55+
if (!double.TryParse(EntryValue.ToString(), out double d))
56+
return EntryValue.ToString(); // This case shouldn't exist, but just to be sure
57+
58+
if (Math.Abs(d) < 1) // mili
59+
{
60+
return !String.IsNullOrEmpty(this.MiliUnit) ? String.Format(Formatting, d * 1000) :
61+
String.Format(Formatting, d);
62+
}
63+
else if (Math.Abs(d) < 1000000) // base
64+
{
65+
return String.Format(Formatting, d);
66+
}
67+
else if (Math.Abs(d) < 1000000000) // kilo
68+
{
69+
return !String.IsNullOrEmpty(this.KiloUnit) ? String.Format(Formatting, d / 1000) :
70+
String.Format(Formatting, d);
71+
72+
}
73+
else if (Math.Abs(d) < 1000000000000) // mega
74+
{
75+
return !String.IsNullOrEmpty(this.MegaUnit) ? String.Format(Formatting, d / 1000000) :
76+
!String.IsNullOrEmpty(this.KiloUnit) ? String.Format(Formatting, d / 1000) :
77+
String.Format(Formatting, d);
78+
79+
}
80+
else // giga
81+
{
82+
return !String.IsNullOrEmpty(this.GigaUnit) ? String.Format(Formatting, d / 1000000000) :
83+
!String.IsNullOrEmpty(this.MegaUnit) ? String.Format(Formatting, d / 1000000) :
84+
!String.IsNullOrEmpty(this.KiloUnit) ? String.Format(Formatting, d / 1000) :
85+
String.Format(Formatting, d);
86+
}
4787
}
4888
}
4989

50-
90+
public virtual string UnitDisplay
91+
{
92+
get
93+
{
94+
if (EntryValue == null)
95+
return "";
96+
97+
if (String.IsNullOrEmpty(this.Formatting))
98+
return this.BaseUnit ?? "";
99+
100+
if (!double.TryParse(EntryValue.ToString(), out double d))
101+
return this.BaseUnit ?? ""; // This case shouldn't exist, but just to be sure
102+
103+
if (d > 0.001 && d < 1) // mili
104+
{
105+
return this.MiliUnit ?? this.BaseUnit ?? "";
106+
}
107+
else if (Math.Abs(d) < 1000000) // base
108+
{
109+
return this.BaseUnit ?? "";
110+
}
111+
else if (Math.Abs(d) < 1000000000) // kilo
112+
{
113+
return this.KiloUnit ?? this.BaseUnit ?? "";
114+
115+
}
116+
else if (Math.Abs(d) < 1000000000000) // mega
117+
{
118+
return this.MegaUnit ?? this.KiloUnit ?? this.BaseUnit ?? "";
119+
120+
}
121+
else // giga
122+
{
123+
return this.GigaUnit ?? this.MegaUnit ?? this.KiloUnit ?? this.BaseUnit ?? "";
124+
}
125+
}
126+
}
51127

52128
public virtual void RefreshData() { }
53129
}

MicroEngineerProject/MicroEngineer/Entries/BodyEntries.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public Body()
1212
Description = "Shows the body that vessel is currently at.";
1313
Category = MicroEntryCategory.Body;
1414
IsDefault = false;
15-
Unit = null;
15+
BaseUnit = null;
1616
Formatting = null;
1717
}
1818

@@ -32,8 +32,12 @@ public ReferenceBodyConstants_Radius()
3232
Description = "Body's radius.";
3333
Category = MicroEntryCategory.Body;
3434
IsDefault = false;
35-
Unit = "m";
36-
NumberOfDecimalDigits = 0;
35+
MiliUnit = "mm";
36+
BaseUnit = "m";
37+
KiloUnit = "km";
38+
MegaUnit = "Mm";
39+
GigaUnit = "Gm";
40+
NumberOfDecimalDigits = 2;
3741
Formatting = "N";
3842
}
3943

@@ -53,7 +57,7 @@ public ReferenceBodyConstants_StandardGravitationParameter()
5357
Description = "Product of the gravitational constant G and the mass M of the body.";
5458
Category = MicroEntryCategory.Body;
5559
IsDefault = false;
56-
Unit = "μ";
60+
BaseUnit = "μ";
5761
NumberOfDecimalDigits = 4;
5862
Formatting = "e";
5963
}

MicroEngineerProject/MicroEngineer/Entries/FlightEntries.cs

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ public Speed()
1212
Description = "Shows the vessel's total velocity.";
1313
Category = MicroEntryCategory.Flight;
1414
IsDefault = true;
15-
Unit = "m/s";
16-
NumberOfDecimalDigits = 1;
15+
MiliUnit = "mm/s";
16+
BaseUnit = "m/s";
17+
KiloUnit = "km/s";
18+
MegaUnit = "Mm/s";
19+
GigaUnit = "Gm/s";
20+
NumberOfDecimalDigits = 2;
1721
Formatting = "N";
1822
}
1923

@@ -33,7 +37,7 @@ public MachNumber()
3337
Description = "Shows the ratio of vessel's speed and local speed of sound.";
3438
Category = MicroEntryCategory.Flight;
3539
IsDefault = true;
36-
Unit = null;
40+
BaseUnit = null;
3741
NumberOfDecimalDigits = 2;
3842
Formatting = "N";
3943
}
@@ -54,7 +58,7 @@ public GeeForce()
5458
Description = "Measurement of the type of force per unit mass – typically acceleration – that causes a perception of weight, with a g-force of 1 g equal to the conventional value of gravitational acceleration on Earth/Kerbin.";
5559
Category = MicroEntryCategory.Flight;
5660
IsDefault = true;
57-
Unit = "g";
61+
BaseUnit = "g";
5862
NumberOfDecimalDigits = 3;
5963
Formatting = "N";
6064
}
@@ -75,7 +79,7 @@ public AngleOfAttack()
7579
Description = "Angle of Attack specifies the angle between the chord line of the wing and the vector representing the relative motion between the aircraft and the atmosphere.";
7680
Category = MicroEntryCategory.Flight;
7781
IsDefault = true;
78-
Unit = "°";
82+
BaseUnit = "°";
7983
NumberOfDecimalDigits = 3;
8084
Formatting = "N";
8185
}
@@ -96,7 +100,7 @@ public SideSlip()
96100
Description = "A slip is an aerodynamic state where an aircraft is moving somewhat sideways as well as forward relative to the oncoming airflow or relative wind.";
97101
Category = MicroEntryCategory.Flight;
98102
IsDefault = false;
99-
Unit = "°";
103+
BaseUnit = "°";
100104
NumberOfDecimalDigits = 3;
101105
Formatting = "N";
102106
}
@@ -117,7 +121,7 @@ public Heading()
117121
Description = "Heading of a vessel is the compass direction in which the craft's nose is pointed.";
118122
Category = MicroEntryCategory.Flight;
119123
IsDefault = true;
120-
Unit = "°";
124+
BaseUnit = "°";
121125
NumberOfDecimalDigits = 2;
122126
Formatting = "N";
123127
}
@@ -138,7 +142,7 @@ public Pitch_HorizonRelative()
138142
Description = "Lateral axis passes through an aircraft from wingtip to wingtip. Rotation about this axis is called pitch (moving up-down).";
139143
Category = MicroEntryCategory.Flight;
140144
IsDefault = true;
141-
Unit = "°";
145+
BaseUnit = "°";
142146
NumberOfDecimalDigits = 2;
143147
Formatting = "N";
144148
}
@@ -159,7 +163,7 @@ public Roll_HorizonRelative()
159163
Description = "Longitudinal axis passes through the aircraft from nose to tail. Rotation about this axis is called roll (rotating left-right).";
160164
Category = MicroEntryCategory.Flight;
161165
IsDefault = true;
162-
Unit = "°";
166+
BaseUnit = "°";
163167
NumberOfDecimalDigits = 2;
164168
Formatting = "N";
165169
}
@@ -180,7 +184,7 @@ public Yaw_HorizonRelative()
180184
Description = "Vertical axis passes through an aircraft from top to bottom. Rotation about this axis is called yaw (moving left-right).";
181185
Category = MicroEntryCategory.Flight;
182186
IsDefault = true;
183-
Unit = "°";
187+
BaseUnit = "°";
184188
NumberOfDecimalDigits = 2;
185189
Formatting = "N";
186190
}
@@ -201,7 +205,7 @@ public Zenith()
201205
Description = "The zenith is an imaginary point directly above a particular location, on the celestial sphere. \"Above\" means in the vertical direction opposite to the gravity direction.";
202206
Category = MicroEntryCategory.Flight;
203207
IsDefault = false;
204-
Unit = "°";
208+
BaseUnit = "°";
205209
NumberOfDecimalDigits = 2;
206210
Formatting = "N";
207211
}
@@ -222,8 +226,12 @@ public TotalLift()
222226
Description = "Shows the total lift force produced by the vessel.";
223227
Category = MicroEntryCategory.Flight;
224228
IsDefault = true;
225-
Unit = "N";
226-
NumberOfDecimalDigits = 0;
229+
MiliUnit = "mN";
230+
BaseUnit = "N";
231+
KiloUnit = "kN";
232+
MegaUnit = "MN";
233+
GigaUnit = "GN";
234+
NumberOfDecimalDigits = 2;
227235
Formatting = "N";
228236
}
229237

@@ -253,8 +261,12 @@ public TotalDrag()
253261
Description = "Shows the total drag force exerted on the vessel.";
254262
Category = MicroEntryCategory.Flight;
255263
IsDefault = true;
256-
Unit = "N";
257-
NumberOfDecimalDigits = 0;
264+
MiliUnit = "mN";
265+
BaseUnit = "N";
266+
KiloUnit = "kN";
267+
MegaUnit = "MN";
268+
GigaUnit = "GN";
269+
NumberOfDecimalDigits = 2;
258270
Formatting = "N";
259271
}
260272

@@ -284,7 +296,7 @@ public LiftDivDrag()
284296
Description = "Shows the ratio of total lift and drag forces.";
285297
Category = MicroEntryCategory.Flight;
286298
IsDefault = true;
287-
Unit = null;
299+
BaseUnit = null;
288300
NumberOfDecimalDigits = 2;
289301
Formatting = "N";
290302
}
@@ -315,7 +327,7 @@ public DragCoefficient()
315327
Description = "Dimensionless quantity that is used to quantify the drag or resistance of an object in a fluid environment, such as air or water.";
316328
Category = MicroEntryCategory.Flight;
317329
IsDefault = false;
318-
Unit = null;
330+
BaseUnit = null;
319331
NumberOfDecimalDigits = 2;
320332
Formatting = "N";
321333
}
@@ -336,7 +348,7 @@ public ExposedArea()
336348
Description = "The surface area that interacts with the working fluid or gas.";
337349
Category = MicroEntryCategory.Flight;
338350
IsDefault = false;
339-
Unit = null; // TODO
351+
BaseUnit = null; // TODO
340352
NumberOfDecimalDigits = 2;
341353
Formatting = "N";
342354
}
@@ -357,8 +369,12 @@ public AtmosphericDensity()
357369
Description = "Shows the atmospheric density.";
358370
Category = MicroEntryCategory.Flight;
359371
IsDefault = true;
360-
Unit = "g/L";
361-
NumberOfDecimalDigits = 3;
372+
MiliUnit = "mg/L";
373+
BaseUnit = "g/L";
374+
KiloUnit = "kg/L";
375+
MegaUnit = "Mg/L";
376+
GigaUnit = "Gg/L";
377+
NumberOfDecimalDigits = 2;
362378
Formatting = "N";
363379
}
364380

@@ -378,8 +394,12 @@ public SoundSpeed()
378394
Description = "Distance travelled per unit of time by a sound wave as it propagates through the air.";
379395
Category = MicroEntryCategory.Flight;
380396
IsDefault = false;
381-
Unit = "m/s";
382-
NumberOfDecimalDigits = 1;
397+
MiliUnit = "mm/s";
398+
BaseUnit = "m/s";
399+
KiloUnit = "km/s";
400+
MegaUnit = "Mm/s";
401+
GigaUnit = "Gm/s";
402+
NumberOfDecimalDigits = 2;
383403
Formatting = "N";
384404
}
385405

0 commit comments

Comments
 (0)