2022-01-25 23:06:48 -05:00
|
|
|
using System.Globalization;
|
2022-01-17 23:52:42 -05:00
|
|
|
using Godot;
|
|
|
|
|
|
|
|
public class MainMenu : Control
|
|
|
|
{
|
|
|
|
|
2022-01-25 12:02:24 -05:00
|
|
|
// Nodes for menu items
|
2022-01-17 23:52:42 -05:00
|
|
|
private Main _main;
|
2022-01-25 12:02:24 -05:00
|
|
|
private Button _simulateButton;
|
|
|
|
private Button _toggleFullScreenButton;
|
|
|
|
private TextEdit _seedText;
|
|
|
|
private TextEdit _particleCountText;
|
2022-01-25 12:33:18 -05:00
|
|
|
private Button _randomizeButton;
|
2022-01-25 23:06:48 -05:00
|
|
|
private Label _zoomValue;
|
|
|
|
private HSlider _zoomSlider;
|
|
|
|
|
|
|
|
private Label _invalidLabel;
|
2022-01-17 23:52:42 -05:00
|
|
|
|
2022-01-27 17:49:56 -05:00
|
|
|
private string _prevSeedText;
|
|
|
|
private string _prevParticleCountText;
|
|
|
|
|
2022-01-17 23:52:42 -05:00
|
|
|
public override void _Ready()
|
|
|
|
{
|
2022-01-25 12:02:24 -05:00
|
|
|
// Init nodes
|
2022-01-17 23:52:42 -05:00
|
|
|
_main = GetTree().Root.GetNode<Main>("Main");
|
2022-01-25 12:02:24 -05:00
|
|
|
_simulateButton = GetNode("MenuButtons").GetNode<Button>("SimulateButton");
|
|
|
|
_toggleFullScreenButton = GetNode("MenuButtons").GetNode<Button>("ToggleFullscreenButton");
|
2022-01-25 12:33:18 -05:00
|
|
|
_seedText = GetNode("MenuButtons").GetNode("Inputs").GetNode("Seed").GetNode<TextEdit>("SeedText");
|
|
|
|
_particleCountText = GetNode("MenuButtons").GetNode("Inputs").GetNode("ParticleCount").GetNode<TextEdit>("ParticleCountText");
|
|
|
|
_randomizeButton = GetNode("MenuButtons").GetNode("Inputs").GetNode("Seed").GetNode<Button>("RandomizeButton");
|
2022-01-25 23:06:48 -05:00
|
|
|
_zoomValue = GetNode("MenuButtons").GetNode("Inputs").GetNode("Zoom").GetNode<Label>("ZoomValue");
|
|
|
|
_zoomSlider = GetNode("MenuButtons").GetNode("Inputs").GetNode("Zoom").GetNode<HSlider>("ZoomSlider");
|
|
|
|
_invalidLabel = GetNode<Label>("InvalidLabel");
|
2022-01-25 12:02:24 -05:00
|
|
|
|
|
|
|
// Connect signals
|
|
|
|
_simulateButton.Connect("pressed", this, nameof(_OnSimulatePressed));
|
|
|
|
_toggleFullScreenButton.Connect("pressed", this, nameof(_OnToggleFullscreenPressed));
|
2022-01-25 12:33:18 -05:00
|
|
|
_randomizeButton.Connect("pressed", this, nameof(_OnRandomizePressed));
|
2022-01-25 23:06:48 -05:00
|
|
|
_zoomSlider.Connect("value_changed", this, nameof(_OnZoomSliderChange));
|
2022-01-27 17:49:56 -05:00
|
|
|
_seedText.Connect("text_changed", this, nameof(_OnSeedTextChange));
|
|
|
|
_particleCountText.Connect("text_changed", this, nameof(_OnParticleCountTextChange));
|
2022-01-25 12:02:24 -05:00
|
|
|
|
|
|
|
// Random seed
|
|
|
|
GD.Randomize();
|
2022-01-25 23:06:48 -05:00
|
|
|
var randomSeed = Mathf.Abs((int)GD.Randi());
|
2022-01-25 12:33:18 -05:00
|
|
|
_main.Seed = randomSeed;
|
2022-01-25 23:06:48 -05:00
|
|
|
RefreshSeedText();
|
2022-01-27 17:49:56 -05:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2022-01-25 23:06:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void _OnZoomSliderChange(float value)
|
|
|
|
{
|
|
|
|
_zoomValue.Text = value.ToString(CultureInfo.InvariantCulture);
|
|
|
|
_main.Zoom = value;
|
2022-01-25 12:33:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void RefreshSeedText()
|
|
|
|
{
|
|
|
|
_seedText.Text = _main.Seed.ToString();
|
2022-01-27 17:49:56 -05:00
|
|
|
_prevSeedText = _seedText.Text;
|
2022-01-17 23:52:42 -05:00
|
|
|
}
|
|
|
|
|
2022-01-25 12:02:24 -05:00
|
|
|
public void _OnSimulatePressed()
|
2022-01-17 23:52:42 -05:00
|
|
|
{
|
2022-01-25 12:02:24 -05:00
|
|
|
// Start simulation with seed and number of particles
|
2022-01-27 17:49:56 -05:00
|
|
|
if (_seedText.Text.Empty() || _particleCountText.Text.Empty())
|
2022-01-25 23:06:48 -05:00
|
|
|
{
|
|
|
|
ShowInvalid();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_main.Zoom = (float)_zoomSlider.Value;
|
2022-01-27 17:49:56 -05:00
|
|
|
_main.Seed = int.Parse(_seedText.Text);
|
|
|
|
_main.StartSimulation(int.Parse(_particleCountText.Text));
|
2022-01-17 23:52:42 -05:00
|
|
|
}
|
|
|
|
|
2022-01-25 12:02:24 -05:00
|
|
|
public void _OnToggleFullscreenPressed()
|
2022-01-17 23:52:42 -05:00
|
|
|
{
|
2022-01-25 12:02:24 -05:00
|
|
|
OS.WindowFullscreen = !OS.WindowFullscreen;
|
|
|
|
}
|
|
|
|
|
2022-01-25 12:33:18 -05:00
|
|
|
public void _OnRandomizePressed()
|
|
|
|
{
|
2022-01-25 23:06:48 -05:00
|
|
|
var randomSeed = Mathf.Abs((int)GD.Randi());
|
2022-01-25 12:33:18 -05:00
|
|
|
_main.Seed = randomSeed;
|
|
|
|
RefreshSeedText();
|
|
|
|
}
|
|
|
|
|
2022-01-25 12:02:24 -05:00
|
|
|
public override void _Input(InputEvent @event)
|
|
|
|
{
|
|
|
|
// Quit only if menu is visible
|
|
|
|
if (Visible && @event.IsActionPressed("quit"))
|
|
|
|
{
|
|
|
|
GetTree().Quit();
|
|
|
|
}
|
2022-01-17 23:52:42 -05:00
|
|
|
}
|
2022-01-25 23:06:48 -05:00
|
|
|
|
|
|
|
private async void ShowInvalid()
|
|
|
|
{
|
|
|
|
GetNode<Tween>("InvalidTween").ResetAll();
|
|
|
|
GetNode<Timer>("InvalidTimer").Stop();
|
|
|
|
GetNode<Timer>("InvalidTimer").WaitTime = 3.0f;
|
|
|
|
_invalidLabel.Modulate = new Color(1, 1, 1, 1);
|
|
|
|
var invalidTimer = GetNode<Timer>("InvalidTimer");
|
|
|
|
invalidTimer.Start();
|
|
|
|
await ToSignal(invalidTimer, "timeout");
|
|
|
|
var invalidTween = GetNode<Tween>("InvalidTween");
|
|
|
|
invalidTween.InterpolateProperty(_invalidLabel, "modulate:a", 1, 0, 1);
|
|
|
|
invalidTween.Start();
|
|
|
|
await ToSignal(invalidTween, "tween_all_completed");
|
|
|
|
}
|
2022-01-17 23:52:42 -05:00
|
|
|
}
|