From 005e34498e8ffed76ed57e060018eb20581476f5 Mon Sep 17 00:00:00 2001 From: orosmatthew Date: Thu, 27 Jan 2022 17:49:56 -0500 Subject: [PATCH] fixed issue #1 --- MainMenu/MainMenu.cs | 47 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/MainMenu/MainMenu.cs b/MainMenu/MainMenu.cs index aed85c2..8f40b25 100644 --- a/MainMenu/MainMenu.cs +++ b/MainMenu/MainMenu.cs @@ -16,6 +16,9 @@ public class MainMenu : Control private Label _invalidLabel; + private string _prevSeedText; + private string _prevParticleCountText; + public override void _Ready() { // Init nodes @@ -34,12 +37,46 @@ public class MainMenu : Control _toggleFullScreenButton.Connect("pressed", this, nameof(_OnToggleFullscreenPressed)); _randomizeButton.Connect("pressed", this, nameof(_OnRandomizePressed)); _zoomSlider.Connect("value_changed", this, nameof(_OnZoomSliderChange)); + _seedText.Connect("text_changed", this, nameof(_OnSeedTextChange)); + _particleCountText.Connect("text_changed", this, nameof(_OnParticleCountTextChange)); // Random seed GD.Randomize(); var randomSeed = Mathf.Abs((int)GD.Randi()); _main.Seed = randomSeed; RefreshSeedText(); + + // Set default previous values + _prevSeedText = _seedText.Text; + _prevParticleCountText = _particleCountText.Text; + } + + public void _OnParticleCountTextChange() + { + var particleCountCheck = int.TryParse(_particleCountText.Text, out var nParticles); + if (!particleCountCheck && !_particleCountText.Text.Empty()) + { + ShowInvalid(); + _particleCountText.Text = _prevParticleCountText; + } + else if (!_particleCountText.Text.Empty()) + { + _prevParticleCountText = _particleCountText.Text; + } + } + + public void _OnSeedTextChange() + { + var seedCheck = int.TryParse(_seedText.Text, out var seed); + if (!seedCheck && !_seedText.Text.Empty()) + { + ShowInvalid(); + _seedText.Text = _prevSeedText; + } + else if (!_seedText.Text.Empty()) + { + _prevSeedText = _seedText.Text; + } } public void _OnZoomSliderChange(float value) @@ -51,22 +88,20 @@ public class MainMenu : Control public void RefreshSeedText() { _seedText.Text = _main.Seed.ToString(); + _prevSeedText = _seedText.Text; } public void _OnSimulatePressed() { // Start simulation with seed and number of particles - var particleCountCheck = int.TryParse(_particleCountText.Text, out var nParticles); - var seedCheck = int.TryParse(_seedText.Text, out var seed); - if (!particleCountCheck || !seedCheck) + if (_seedText.Text.Empty() || _particleCountText.Text.Empty()) { ShowInvalid(); return; } - _main.Zoom = (float)_zoomSlider.Value; - _main.Seed = seed; - _main.StartSimulation(nParticles); + _main.Seed = int.Parse(_seedText.Text); + _main.StartSimulation(int.Parse(_particleCountText.Text)); } public void _OnToggleFullscreenPressed()