Skip to content

Commit

Permalink
Patch resource panel. Use units, use SI, flip negative sign, account …
Browse files Browse the repository at this point in the history
…for warp
  • Loading branch information
NathanKell committed Oct 23, 2023
1 parent 70e8da0 commit e64253a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
51 changes: 51 additions & 0 deletions Source/Harmony/ResourceItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using HarmonyLib;
using KSP.Localization;
using KSP.UI.Screens;
using System.Reflection.Emit;
using System.Reflection;
using System.Collections.Generic;

namespace RealismOverhaul.Harmony
{
[HarmonyPatch(typeof(ResourceItem))]
internal class PatchResourceItem
{
[HarmonyTranspiler]
[HarmonyPatch("FixedUpdate")]
internal static IEnumerable<CodeInstruction> Transpiler_FixedUpdate(IEnumerable<CodeInstruction> instructions)
{
List<CodeInstruction> code = new List<CodeInstruction>(instructions);
for (int i = 1; i < code.Count; ++i)
{
if (code[i].opcode == OpCodes.Call && (MethodInfo)code[i].operand == AccessTools.Method(typeof(UnityEngine.Time), "get_fixedDeltaTime"))
{
code[i].operand = AccessTools.Method(typeof(TimeWarp), "get_fixedDeltaTime");
break; // let the smoothing still occur in realtime
}
}

return code;
}

[HarmonyPrefix]
[HarmonyPatch("Update")]
internal static bool Prefix_Update(ResourceItem __instance)
{
int resID = __instance.resourceID;

__instance.resourceBar.value = (float)__instance.resourceValue;

__instance.amountText.text = "<color=#000000>" + ResourceUnits.PrintAmount(__instance.vesselResourceCurrent, resID, 5, __instance.vesselResourceCurrent > 100 ? "F0" : "F2") + "</color>";
__instance.maxText.text = "<color=#000000>" + ResourceUnits.PrintAmount(__instance.vesselResourceTotal, resID, 5, __instance.vesselResourceCurrent > 100 ? "F0" : "F2") + "</color>";

if (__instance.deltaSmoothed == 0f)
__instance.deltaText.text = "<color=#000000>(0)</color>";
else if (__instance.deltaSmoothed > 0f)
__instance.deltaText.text = "<color=#110000>(-" + ResourceUnits.PrintRatePerSecBare(-__instance.deltaSmoothed, resID, 3, "F2") + ")</color>";
else
__instance.deltaText.text = "<color=#000011>(" + ResourceUnits.PrintRatePerSecBare(-__instance.deltaSmoothed, resID, 3, "F2") + ")</color>";

return false;
}
}
}
1 change: 1 addition & 0 deletions Source/RealismOverhaul.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<Compile Include="DebugTools\DebugDrawer.cs" />
<Compile Include="DebugTools\DrawTools.cs" />
<Compile Include="Harmony\ModuleEngines.cs" />
<Compile Include="Harmony\ResourceItem.cs" />
<Compile Include="Harmony\ModuleRCS.cs" />
<Compile Include="Harmony\ModuleResource.cs" />
<Compile Include="ResourceUnitInfo.cs" />
Expand Down
28 changes: 25 additions & 3 deletions Source/ResourceUnitInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,29 @@ public static string PrintMassRate(double massFlow)
{
return massFlow < 1d ? KSPUtil.PrintSI(massFlow * 1000d * 1000d, "g") : KSPUtil.PrintSI(massFlow, "t");
}

public static string PrintRatePerSecBare(double rate, int resID, int sigFigs = 3, string precision = "G2", bool longPrefix = false)
{
string unitRate = string.Empty;
bool useSI = false;
if (GetResourceUnitInfo(resID) is ResourceUnitInfo rui)
{
unitRate = rui.RateUnit;
rate *= rui.MultiplierToUnit;
if (rui.UseHuman)
{
useSI = false;
}
else
{
useSI = true;
}
}
if (useSI)
return KSPUtil.PrintSI(rate, unitRate, sigFigs, longPrefix);
else
return rate.ToString(precision) + unitRate;
}

public static string PrintRate(double rate, int resID, bool showFlowMode, ModuleResource res = null, Propellant p = null, bool showMass = false, int sigFigs = 3, bool longPrefix = false)
{
Expand Down Expand Up @@ -168,13 +191,12 @@ public static string PrintRate(double rate, int resID, bool showFlowMode, Module
return output;
}

public static string PrintAmount(double amount, int resID, int sigFigs = 3, string precision = "G2", bool longPrefix = false)
public static string PrintAmount(double amount, int resID, int sigFigs = 3, string precision = "G2", bool preventSI = false, bool longPrefix = false)
{
string unit = string.Empty;
if (GetResourceUnitInfo(resID) is ResourceUnitInfo rui)
{

if (rui.UseHuman)
if (rui.UseHuman || preventSI)
unit = rui.AmountUnit;
else
return PrintSIAmount(amount, rui, sigFigs, longPrefix);
Expand Down

0 comments on commit e64253a

Please sign in to comment.