Fix speed label not disappearing on machine edit

This commit is contained in:
Norbi Peti 2022-02-24 00:43:05 +01:00
parent 1f94cf1608
commit 8223447319
2 changed files with 33 additions and 17 deletions

View file

@ -20,7 +20,7 @@ namespace BuildingTools
private readonly CommandUtils _commandUtils; private readonly CommandUtils _commandUtils;
private readonly BlockSelections _blockSelections; private readonly BlockSelections _blockSelections;
private readonly MirrorModeEngine _mirrorModeEngine = new MirrorModeEngine(); //private readonly MirrorModeEngine _mirrorModeEngine = new MirrorModeEngine();
public BuildingTools() public BuildingTools()
{ {
@ -250,7 +250,7 @@ namespace BuildingTools
} }
TweakPropertyInfo[] stats; TweakPropertyInfo[] stats;
if (stat is null) if (stat is null || stat.Length == 0)
{ {
stats = data.Stats; stats = data.Stats;
} }
@ -271,10 +271,10 @@ namespace BuildingTools
statInfo.max = value == 0 ? 1000 : value; statInfo.max = value == 0 ? 1000 : value;
} }
Logging.CommandLog($"{(stat is null ? "All stats" : $"Stat {stat}")} max changed to {(value == 0 ? 1000 : value)}"); Logging.CommandLog($"{(stat is null || stat.Length == 0 ? "All stats" : $"Stat {stat}")} max changed to {(value == 0 ? 1000 : value)} on {bl}");
}).Build(); }).Build();
_mirrorModeEngine.Init(); //_mirrorModeEngine.Init();
UI.Init(); UI.Init();
new Harmony("BuildTools").PatchAll(Assembly.GetExecutingAssembly()); new Harmony("BuildTools").PatchAll(Assembly.GetExecutingAssembly());
@ -335,7 +335,7 @@ namespace BuildingTools
$"- Rotation: {rot.x:F}° {rot.y:F}° {rot.z:F}°\n" + $"- Rotation: {rot.x:F}° {rot.y:F}° {rot.z:F}°\n" +
$"- Velocity: {vel.x:F} {vel.y:F} {vel.z:F}\n" + $"- Velocity: {vel.x:F} {vel.y:F} {vel.z:F}\n" +
$"- Angular velocity: {ave.x:F} {ave.y:F} {ave.z:F}\n" + $"- Angular velocity: {ave.x:F} {ave.y:F} {ave.z:F}\n" +
$"- {(body.Static ? "Static body" : $"Mass: {body.Mass:F} center: {com.x:F} {com.y:F} {com.z:F}")}\n" + $"- {(body.Static ? "Static body" : $"Center of mass: {com.x:F} {com.y:F} {com.z:F}")}\n" +
$"- Volume: {body.Volume:F}\n" + $"- Volume: {body.Volume:F}\n" +
$"- Chunk health: {body.CurrentHealth:F} / {body.InitialHealth:F} - Multiplier: {body.HealthMultiplier:F}\n" + $"- Chunk health: {body.CurrentHealth:F} / {body.InitialHealth:F} - Multiplier: {body.HealthMultiplier:F}\n" +
(cluster == null (cluster == null
@ -357,7 +357,6 @@ namespace BuildingTools
$"- Rotation: {rot.x:F}° {rot.y:F}° {rot.z:F}°\n" + $"- Rotation: {rot.x:F}° {rot.y:F}° {rot.z:F}°\n" +
$"- Velocity: {vel.x:F} {vel.y:F} {vel.z:F}\n" + $"- Velocity: {vel.x:F} {vel.y:F} {vel.z:F}\n" +
$"- Angular velocity: {ave.x:F} {ave.y:F} {ave.z:F}\n" + $"- Angular velocity: {ave.x:F} {ave.y:F} {ave.z:F}\n" +
$"- Mass: {player.Mass:F}\n" +
$"- Health: {player.CurrentHealth:F} / {player.InitialHealth:F}"; $"- Health: {player.CurrentHealth:F} / {player.InitialHealth:F}";
} }

View file

@ -9,20 +9,37 @@ namespace BuildingTools
{ {
public class UI public class UI
{ {
private static Label _speedLabel; private static Label _speedLabel = new Label(new Rect(Screen.width - 200, 0, 200, 20), "Speed: ", "SpeedLabel")
{
Enabled = false
};
public static void Init() public static void Init()
{ {
Game.Simulate += (sender, args) => Game.Simulate += OnGameOnSimulate;
{ Game.Edit += OnGameOnEditOrExit;
_speedLabel = new Label(new Rect(Screen.width - 200, 0, 200, 20), "Speed: ", "SpeedLabel"); Game.Exit += OnGameOnEditOrExit;
Scheduler.Schedule(new Repeatable( }
() => _speedLabel.Text = $"Speed: {math.length(Player.LocalPlayer?.Velocity ?? 0) * 3.6:F} km/h",
() => _speedLabel != null));
};
Game.Edit += (sender, args) => _speedLabel = null;
Game.Exit += (sender, args) => _speedLabel = null;
private static void OnGameOnSimulate(object sender, GameEventArgs args)
{
_speedLabel.Enabled = true;
Scheduler.Schedule(new Repeatable(UpdateTask, ShouldLabelBeUpdated));
}
private static void OnGameOnEditOrExit(object sender, GameEventArgs args)
{
_speedLabel.Enabled = false;
}
private static void UpdateTask()
{
_speedLabel.Text = $"Speed: {math.length(Player.LocalPlayer?.Velocity ?? 0) * 3.6:F} km/h";
}
private static bool ShouldLabelBeUpdated()
{
return _speedLabel.Enabled;
} }
} }
} }