GodotComponentTest/scenes/tests/EditorUI.cs

155 lines
5.2 KiB
C#
Raw Normal View History

using Godot;
2023-11-18 22:32:57 +01:00
using Godot.Collections;
2023-11-18 22:32:57 +01:00
public class EditorUI : Control {
public enum InputMode {
None,
Grass,
Sand,
Water,
Obstacle,
Navigate
}
private CheckBox _gameGeometryCheckBox;
private Button _grassButton;
private Button _navigateButton;
private CheckBox _navigationGeometryCheckBox;
private Button _obstacleButton;
private CheckBox _physicsGeometryCheckBox;
// private members
private Button _resetButton;
private Button _sandButton;
private StreamContainer _streamContainer;
private ShaderMaterial _tileMaterial;
private TileWorld _tileWorld;
private Button _waterButton;
public InputMode CurrentInputMode = InputMode.None;
// exported members
// public members
public Vector2 currentTileOffset = Vector2.Zero;
// Called when the node enters the scene tree for the first time.
2023-11-18 22:32:57 +01:00
public override void _Ready() {
_tileMaterial = GD.Load<ShaderMaterial>("materials/HexTileTextureLookup.tres");
// signals
_resetButton = (Button)FindNode("ResetButton");
_resetButton.Connect("pressed", this, nameof(OnResetButton));
_grassButton = (Button)FindNode("GrassButton");
_grassButton.Connect("pressed", this, nameof(OnGrassButton));
_sandButton = (Button)FindNode("SandButton");
_sandButton.Connect("pressed", this, nameof(OnSandButton));
_waterButton = (Button)FindNode("WaterButton");
_waterButton.Connect("pressed", this, nameof(OnWaterButton));
_obstacleButton = (Button)FindNode("ObstacleButton");
_obstacleButton.Connect("pressed", this, nameof(OnObstacleButton));
_navigateButton = (Button)FindNode("NavigateButton");
_navigateButton.Connect("pressed", this, nameof(OnNavigateButton));
_gameGeometryCheckBox = (CheckBox)FindNode("GameGeometryCheckBox");
_gameGeometryCheckBox.Connect("toggled", this, nameof(OnGameGeometryCheckBoxToggled));
_physicsGeometryCheckBox = (CheckBox)FindNode("PhysicsGeometryCheckBox");
_physicsGeometryCheckBox.Connect("toggled", this, nameof(OnPhysicsGeometryCheckBoxToggled));
_navigationGeometryCheckBox = (CheckBox)FindNode("NavigationGeometryCheckBox");
_navigationGeometryCheckBox.Connect("toggled", this, nameof(OnNavigationGeometryCheckBoxToggled));
}
2023-11-18 22:32:57 +01:00
public void OnResetButton() {
GD.Print("Resetting Map");
_tileWorld.Seed = _tileWorld.Seed + 1;
_tileWorld.Generate(24);
}
2023-11-18 22:32:57 +01:00
public void OnGrassButton() {
CurrentInputMode = InputMode.Grass;
}
2023-11-18 22:32:57 +01:00
public void OnSandButton() {
CurrentInputMode = InputMode.Sand;
}
2023-11-18 22:32:57 +01:00
public void OnWaterButton() {
CurrentInputMode = InputMode.Water;
}
2023-11-18 22:32:57 +01:00
public void OnObstacleButton() {
CurrentInputMode = InputMode.Obstacle;
}
2023-11-18 22:32:57 +01:00
public void OnNavigateButton() {
CurrentInputMode = InputMode.Navigate;
}
2023-11-18 22:32:57 +01:00
public void OnGameGeometryCheckBoxToggled(bool pressed) {
Array gameGeometries = GetTree().GetNodesInGroup("GameGeometry");
foreach (Spatial mesh in gameGeometries) {
if (mesh != null) {
mesh.Visible = pressed;
2023-11-18 22:32:57 +01:00
}
}
}
2023-11-18 22:32:57 +01:00
public void OnPhysicsGeometryCheckBoxToggled(bool pressed) {
Array physicsGeometries = GetTree().GetNodesInGroup("PhysicsGeometry");
foreach (Spatial mesh in physicsGeometries) {
if (mesh != null) {
mesh.Visible = pressed;
2023-11-18 22:32:57 +01:00
}
}
}
2023-11-18 22:32:57 +01:00
public void OnNavigationGeometryCheckBoxToggled(bool pressed) {
UpdateTileMaterial();
}
2023-11-18 22:32:57 +01:00
public void UpdateTileMaterial() {
if (_navigationGeometryCheckBox.Pressed) {
ImageTexture newWorldTexture = new();
newWorldTexture.CreateFromImage(_tileWorld.NavigationmapImage,
(uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
_tileMaterial.SetShaderParam("MapAlbedoTexture", newWorldTexture);
_tileMaterial.SetShaderParam("TextureSize", (int)_tileWorld.NavigationmapImage.GetSize().x);
2023-11-18 22:32:57 +01:00
} else {
ImageTexture newWorldTexture = new();
newWorldTexture.CreateFromImage(_tileWorld.ColormapImage,
(uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
_tileMaterial.SetShaderParam("MapAlbedoTexture", newWorldTexture);
_tileMaterial.SetShaderParam("TextureSize", (int)_tileWorld.ColormapImage.GetSize().x);
}
}
2023-11-18 22:32:57 +01:00
public void OnTileClicked(Vector2 offsetCoord) {
switch (CurrentInputMode) {
case InputMode.Grass:
_tileWorld.SetTileColorAtOffset(currentTileOffset, Colors.Green);
break;
case InputMode.Water:
_tileWorld.SetTileColorAtOffset(currentTileOffset, Colors.Blue);
break;
case InputMode.Sand:
_tileWorld.SetTileColorAtOffset(currentTileOffset, Colors.Yellow);
break;
case InputMode.Obstacle:
_tileWorld.MarkCellUnwalkable(HexCell.FromOffsetCoords(offsetCoord));
break;
}
UpdateTileMaterial();
}
}