Fix .NET unloading issue

main
copygirl 2 months ago
parent a2cca8165b
commit 3fa91b5954
  1. 2
      addons/terrain-editing/TerrainEditingControls+Editing.cs
  2. 26
      addons/terrain-editing/TerrainEditingControls.cs
  3. 19
      addons/terrain-editing/TerrainEditingPlugin.cs

@ -6,7 +6,7 @@ public partial class TerrainEditingControls
(TilePos, TilePos)? _selection = null;
Material _editToolMaterial;
void OnEditingReady()
public override void _EnterTree()
{
_editToolMaterial = new StandardMaterial3D {
VertexColorUseAsAlbedo = true,

@ -5,12 +5,11 @@ public partial class TerrainEditingControls
public (ToolMode , Button)[] ToolModeButtons { get; private set; }
public (ToolShape, Button)[] ToolShapeButtons { get; private set; }
public Button[] PaintTextureButtons { get; private set; }
public Slider DrawSizeSlider { get; private set; }
public Slider DrawSizeSlider { get; private set; }
ToolMode _toolMode;
ToolShape _toolShape;
int _texture;
ToolMode _toolMode = ToolMode.Height;
ToolShape _toolShape = ToolShape.Circle;
int _texture = 1;
public ToolMode ToolMode { get => _toolMode ; set => SetToolMode (value); }
public ToolShape ToolShape { get => _toolShape; set => SetToolShape(value); }
@ -52,16 +51,17 @@ public partial class TerrainEditingControls
DrawSizeSlider = GetNode<Slider>("SizeSlider");
DrawSizeSlider.ValueChanged += (_) => drawSizeLabel.Text = $"{DrawSize}";
SetToolMode(ToolMode.Height);
SetToolShape(ToolShape.Circle);
SetTexture(1);
OnEditingReady();
SetToolMode(_toolMode);
SetToolShape(_toolShape);
SetTexture(_texture);
}
void SetToolMode(ToolMode value)
{
// Ignore if _Ready hasn't been called yet.
if (ToolModeButtons == null) return;
foreach (var (mode, button) in ToolModeButtons)
button.Flat = button.ButtonPressed = value != mode;
foreach (var button in PaintTextureButtons)
@ -71,6 +71,9 @@ public partial class TerrainEditingControls
void SetToolShape(ToolShape value)
{
// Ignore if _Ready hasn't been called yet.
if (ToolShapeButtons == null) return;
foreach (var (shape, button) in ToolShapeButtons)
button.Flat = button.ButtonPressed = value != shape;
DrawSizeSlider.Editable = value != ToolShape.Corner;
@ -79,6 +82,9 @@ public partial class TerrainEditingControls
void SetTexture(int value)
{
// Ignore if _Ready hasn't been called yet.
if (PaintTextureButtons == null) return;
if ((value < 1) || (value > PaintTextureButtons.Length))
throw new ArgumentOutOfRangeException(nameof(value));
foreach (var (i, button) in PaintTextureButtons.Select((b, i) => (i, b)))

@ -1,9 +1,13 @@
#if TOOLS
[Tool]
public partial class TerrainEditingPlugin
: EditorPlugin
{
const string ScenePath = "res://addons/terrain-editing/TerrainEditingControls.tscn";
// The container to which the editing controls get added to when Terrain is selected.
const CustomControlContainer Container = CustomControlContainer.SpatialEditorSideRight;
TerrainEditingControls _controls;
public override bool _Handles(GodotObject obj)
@ -11,24 +15,25 @@ public partial class TerrainEditingPlugin
public override void _EnterTree()
{
var scene = GD.Load<PackedScene>("res://addons/terrain-editing/TerrainEditingControls.tscn");
var scene = GD.Load<PackedScene>(ScenePath);
_controls = scene.Instantiate<TerrainEditingControls>();
}
public override void _ExitTree()
{
if (_controls == null) return;
_controls.QueueFree();
if (_controls.GetParent() != null)
RemoveControlFromContainer(Container, _controls);
_controls.Free();
_controls = null;
}
public override void _MakeVisible(bool visible)
{
var container = CustomControlContainer.SpatialEditorSideRight;
if (visible)
AddControlToContainer(container, _controls);
else if (_controls != null)
RemoveControlFromContainer(container, _controls);
AddControlToContainer(Container, _controls);
else if (_controls.GetParent() != null)
RemoveControlFromContainer(Container, _controls);
}
}
#endif

Loading…
Cancel
Save