CurvedPolygon2D.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /// Description Pending
  2. using Godot;
  3. // Description pending.
  4. [Tool]
  5. /// Descriptpepepl
  6. public partial class CurvedPolygon2D: Path2D {
  7. [Export(PropertyHint.Range, "1,10")]
  8. public int MaxStages {
  9. get { return _maxStages; }
  10. set { _maxStages = value; QueueRedraw(); }
  11. }
  12. private int _maxStages = 5;
  13. [Export(PropertyHint.Range, "1.0,30.0,0.1,degrees")]
  14. public float ToleranceDegrees {
  15. get { return _toleranceDegrees; }
  16. set { _toleranceDegrees = value; QueueRedraw(); }
  17. }
  18. private float _toleranceDegrees = 4.0f;
  19. [Export]
  20. private NodePath _collisionPolygon = null;
  21. [ExportGroup("Texture")]
  22. [Export]
  23. private Texture2D Texture {
  24. get { return _texture; }
  25. set {
  26. _texture = value;
  27. if(Engine.IsEditorHint() && Polygon != null) {
  28. Polygon.Texture = value;
  29. }
  30. }
  31. }
  32. private Texture2D _texture = null;
  33. [Export(PropertyHint.Link)]
  34. private Vector2 TextureScale {
  35. get { return _scale; }
  36. set {
  37. _scale = value;
  38. if(Engine.IsEditorHint() && Polygon != null) {
  39. Polygon.TextureScale = _scale.Inverse();
  40. }
  41. }
  42. }
  43. private Vector2 _scale = new Vector2(1.0f, 1.0f);
  44. [Export(PropertyHint.Range, "-360.0,360.0,0.1,or_less,or_greater,radians")]
  45. private float TextureRotation {
  46. get { return _rotation; }
  47. set {
  48. _rotation = value;
  49. if(Engine.IsEditorHint() && Polygon != null) {
  50. Polygon.TextureRotation = value;
  51. }
  52. }
  53. }
  54. private float _rotation = 0.0f;
  55. private Polygon2D _polygon = null;
  56. public Polygon2D Polygon {
  57. get {
  58. if(_polygon == null) {
  59. _polygon = (Polygon2D)FindChild("Polygon", false, false);
  60. }
  61. return _polygon;
  62. }
  63. }
  64. public Vector2[] RawVertices {
  65. get {
  66. return Curve.Tessellate(1, ToleranceDegrees);
  67. }
  68. }
  69. public override void _Ready() {
  70. if(Curve == null) {
  71. Curve = new Curve2D();
  72. }
  73. _polygon = (Polygon2D)FindChild("Polygon", false, true);
  74. if(_polygon == null) {
  75. _polygon = new Polygon2D();
  76. _polygon.Texture = Texture;
  77. _polygon.TextureScale = TextureScale.Inverse();
  78. _polygon.TextureFilter = TextureFilter;
  79. _polygon.UseParentMaterial = true;
  80. _polygon.Name = "Polygon";
  81. AddChild(_polygon);
  82. }
  83. }
  84. public override void _Draw() {
  85. if(Curve != null) {
  86. Vector2[] points = Curve.Tessellate(MaxStages, ToleranceDegrees);
  87. if(Polygon == null) {
  88. QueueRedraw();
  89. } else {
  90. Polygon.Polygon = points;
  91. if(_collisionPolygon != null) {
  92. try {
  93. GetNode<CollisionPolygon2D>(_collisionPolygon).Polygon = points;
  94. } catch(System.InvalidCastException) {
  95. GD.PushError("Collision Polygon attribute has to be a CollisionPolygon2D.");
  96. } catch(System.NullReferenceException) {
  97. GD.PushError("Invalid reference to the Collision Polygon.");
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }