particles/MainMenu/MainMenu.cs

70 lines
2.2 KiB
C#
Raw Normal View History

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-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 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 12:02:24 -05:00
// Random seed
GD.Randomize();
var randomSeed = (int)GD.Randi();
2022-01-25 12:33:18 -05:00
_main.Seed = randomSeed;
RefreshSeedText();
}
public void RefreshSeedText()
{
_seedText.Text = _main.Seed.ToString();
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
var nParticles = _particleCountText.Text.ToInt();
2022-01-25 12:33:18 -05:00
_main.Seed = _seedText.Text.ToInt();
_main.StartSimulation(nParticles);
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()
{
var randomSeed = (int) GD.Randi();
_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
}
}