Skip to content

Instantly share code, notes, and snippets.

@Shilo
Created December 9, 2024 09:13
Show Gist options
  • Save Shilo/5ab2dc75a9f2eb4a05b95803a4227989 to your computer and use it in GitHub Desktop.
Save Shilo/5ab2dc75a9f2eb4a05b95803a4227989 to your computer and use it in GitHub Desktop.
CollisionPolygon2D editor tool to copy-paste polygon global position
[GlobalClass, Tool]
public partial class Region : CollisionPolygon2D
{
private List<Vector2> _editorGlobalPolygon;
/// <summary>
/// When enabled, the polygon is stored as global space.
/// When disabled, the global polygon is restored as local space.
/// This allows workflow: enable property, move position, disable property.
/// </summary>
[ExportCategory("CollisionPolygon2D Editor")]
[Export]
private bool EditorCopyPastePolygon
{
set
{
if (!Engine.IsEditorHint() || value == (this._editorGlobalPolygon != null))
return;
if (value)
{
this._editorGlobalPolygon = this.Polygon.Select(this.ToGlobal).ToList();
}
else
{
this.Polygon = this._editorGlobalPolygon.Select(this.ToLocal).ToArray();
this._editorGlobalPolygon = null;
}
}
get => this._editorGlobalPolygon != null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment