Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default to opaque white color in more CanvasItem::draw_* methods #79666

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kleonc
Copy link
Member

@kleonc kleonc commented Jul 19, 2023

Makes the color parameter default to opaque white (Color(1, 1, 1, 1)) for these methods:

  • CanvasItem::draw_dashed_line,
  • CanvasItem::draw_line,
  • CanvasItem::draw_polyline,
  • CanvasItem::draw_arc,
  • CanvasItem::draw_multiline,
  • CanvasItem::draw_rect,
  • CanvasItem::draw_circle,
  • CanvasItem::draw_colored_polygon,
  • RenderingServer::canvas_item_add_line,
  • RenderingServer::canvas_item_add_rect,
  • RenderingServer::canvas_item_add_circle.

Resolves godotengine/godot-proposals#7310.


Notes:

  • In the code there were used Color(1, 1, 1, 1), Color(1, 1, 1), Color(1.0, 1.0, 1.0). They are equivalent, I've changed all of them to Color(1, 1, 1, 1) which seemed to be used the most. Could be changed to some other variant if preferred. Edit: it was done in canvas_item.h/cpp. But e.g. in RenderingServer.h/cpp Color(1, 1, 1) seems to be used instead, haven't touched these.

@Calinou
Copy link
Member

Calinou commented Jul 19, 2023

I haven't added defaults for the RenderingServer::canvas_item_add_* methods.

I think we should do that as well for consistency.

@clayjohn
Copy link
Member

Looks like this will require compatibility methods for GDExtension and C#. Take a look at the "files changed" tab for more info

@kleonc kleonc force-pushed the canvas-item-draw-opaque-white-by-default branch from c7ddf07 to e823d10 Compare July 19, 2023 23:06
@kleonc kleonc requested a review from a team as a code owner July 19, 2023 23:06
@kleonc
Copy link
Member Author

kleonc commented Jul 19, 2023

I haven't added defaults for the RenderingServer::canvas_item_add_* methods.

I think we should do that as well for consistency.

Changed some.

Looks like this will require compatibility methods for GDExtension and C#. Take a look at the "files changed" tab for more info

Added compat methods, based on #76577. Kinda no idea what I'm doing though. 🙃

Comment on lines 75 to 77
void canvas_item_add_line_79666(RID p_item, const Point2 &p_from, const Point2 &p_to, const Color &p_color, float p_width = -1.0, bool p_antialiased = false);
void canvas_item_add_rect_79666(RID p_item, const Rect2 &p_rect, const Color &p_color);
void canvas_item_add_circle_79666(RID p_item, const Point2 &p_pos, float p_radius, const Color &p_color);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note these are for pure virtual methods. Is it fine like this? 🤔

@kleonc kleonc force-pushed the canvas-item-draw-opaque-white-by-default branch 2 times, most recently from 1fdcc88 to 3c94a4d Compare July 20, 2023 00:40
@raulsntos
Copy link
Member

We currently don't generate compat methods for C#, so they have to be added manually:

diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Compat.cs b/modules/mono/glue/GodotSharp/GodotSharp/Compat.cs
index 41cea031ab..b8f8ff7927 100644
--- a/modules/mono/glue/GodotSharp/GodotSharp/Compat.cs
+++ b/modules/mono/glue/GodotSharp/GodotSharp/Compat.cs
@@ -24,6 +24,65 @@ partial class AnimationNode
     }
 }
 
+partial class CanvasItem
+{
+    /// <inheritdoc cref="DrawArc(Vector2, float, float, float, int, Nullable{Color}, float, bool)"/>
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public void DrawArc(Vector2 center, float radius, float startAngle, float endAngle, int pointCount, Color color, float width, bool antialiased)
+    {
+        DrawArc(center, radius, startAngle, endAngle, pointCount, new Nullable<Color>(color), width, antialiased);
+    }
+
+    /// <inheritdoc cref="DrawCircle(Vector2, float, Nullable{Color})"/>
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public void DrawCircle(Vector2 position, float radius, Color color)
+    {
+        DrawCircle(position, radius, new Nullable<Color>(color));
+    }
+
+    /// <inheritdoc cref="DrawColoredPolygon(Vector2[], Nullable{Color}, Vector2[], Texture2D)"/>
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public void DrawColoredPolygon(Vector2[] points, Color color, Vector2[] uvs, Texture2D texture)
+    {
+        DrawColoredPolygon(points, new Nullable<Color>(color), uvs, texture);
+    }
+
+    /// <inheritdoc cref="DrawDashedLine(Vector2, Vector2, Nullable{Color}, float, float, bool)"/>
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public void DrawDashedLine(Vector2 from, Vector2 to, Color color, float width, float dash, bool aligned)
+    {
+        DrawDashedLine(from, to, new Nullable<Color>(color), width, dash, aligned);
+    }
+
+    /// <inheritdoc cref="DrawLine(Vector2, Vector2, Nullable{Color}, float, bool)"/>
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public void DrawLine(Vector2 from, Vector2 to, Color color, float width, bool antialiased)
+    {
+        DrawLine(from, to, new Nullable<Color>(color), width, antialiased);
+    }
+
+    /// <inheritdoc cref="DrawMultiline(Vector2[], Nullable{Color}, float)"/>
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public void DrawMultiline(Vector2[] points, Color color, float width)
+    {
+        DrawMultiline(points, new Nullable<Color>(color), width);
+    }
+
+    /// <inheritdoc cref="DrawPolyline(Vector2[], Nullable{Color}, float, bool)"/>
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public void DrawPolyline(Vector2[] points, Color color, float width, bool antialiased)
+    {
+        DrawPolyline(points, new Nullable<Color>(color), width, antialiased);
+    }
+
+    /// <inheritdoc cref="DrawRect(Rect2, Nullable{Color}, bool, float)"/>
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public void DrawRect(Rect2 rect, Color color, bool filled, float width)
+    {
+        DrawRect(rect, new Nullable<Color>(color), filled, width);
+    }
+}
+
 partial class CodeEdit
 {
     /// <inheritdoc cref="AddCodeCompletionOption(CodeCompletionKind, string, string, Nullable{Color}, Resource, Nullable{Variant})"/>
@@ -81,6 +140,30 @@ partial class RenderingDevice
     }
 }
 
+partial class RenderingServer
+{
+    /// <inheritdoc cref="CanvasItemAddCircle(Rid, Vector2, float, Nullable{Color})"/>
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public static void CanvasItemAddCircle(Rid item, Vector2 pos, float radius, Color color)
+    {
+        CanvasItemAddCircle(item, pos, radius, new Nullable<Color>(color));
+    }
+
+    /// <inheritdoc cref="CanvasItemAddLine(Rid, Vector2, Vector2, Nullable{Color}, float, bool)"/>
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public static void CanvasItemAddLine(Rid item, Vector2 from, Vector2 to, Color color, float width, bool antialiased)
+    {
+        CanvasItemAddLine(item, from, to, new Nullable<Color>(color), width, antialiased);
+    }
+
+    /// <inheritdoc cref="CanvasItemAddRect(Rid, Rect2, Nullable{Color})"/>
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public static void CanvasItemAddRect(Rid item, Rect2 rect, Color color)
+    {
+        CanvasItemAddRect(item, rect, new Nullable<Color>(color));
+    }
+}
+
 partial class RichTextLabel
 {
     /// <inheritdoc cref="PushList(int, ListType, bool, string)"/>

@kleonc kleonc force-pushed the canvas-item-draw-opaque-white-by-default branch from 3c94a4d to 023e66f Compare July 20, 2023 01:00
@kleonc kleonc requested a review from a team as a code owner July 20, 2023 01:00
@kleonc kleonc force-pushed the canvas-item-draw-opaque-white-by-default branch from 023e66f to 77c8967 Compare July 20, 2023 09:48
@dsnopek
Copy link
Contributor

dsnopek commented Jul 20, 2023

Added compat methods, based on #76577. Kinda no idea what I'm doing though.

Thanks! I've skimmed them and they look good to me. :-)

Unfortunately, they won't actually work until after PR #79702 is merged. I think it'd be good for that to be merged first, so that CI can double check that you've gotten them all correct.

@kleonc kleonc force-pushed the canvas-item-draw-opaque-white-by-default branch from 77c8967 to 5793f99 Compare July 20, 2023 17:39
@kleonc
Copy link
Member Author

kleonc commented Jul 20, 2023

Unfortunately, they won't actually work until after PR #79702 is merged. I think it'd be good for that to be merged first, so that CI can double check that you've gotten them all correct.

Rebased, seeing #79702 was merged. Hopefully they are correct, will see.

Anyway, thanks all of you for the help with this! 🙂

@dsnopek
Copy link
Contributor

dsnopek commented Jul 20, 2023

Unfortunately, they won't actually work until after PR #79702 is merged. I think it'd be good for that to be merged first, so that CI can double check that you've gotten them all correct.

Rebased, seeing #79702 was merged. Hopefully they are correct, will see.

Unfortunately, CI is still not happy:

Validate extension JSON: Error: Hash changed for 'classes/CanvasItem/methods/draw_arc', from CFD4FBAB to 6031513E. This means that the function has changed and no compatibility function was provided.
Validate extension JSON: Error: Hash changed for 'classes/CanvasItem/methods/draw_colored_polygon', from 62E3D9E1 to B5F97BEA. This means that the function has changed and no compatibility function was provided.
Validate extension JSON: Error: Hash changed for 'classes/CanvasItem/methods/draw_dashed_line', from 81A7290C to EEC0FBE7. This means that the function has changed and no compatibility function was provided.
Validate extension JSON: Error: Hash changed for 'classes/CanvasItem/methods/draw_line', from 96057C42 to 89E57661. This means that the function has changed and no compatibility function was provided.
Validate extension JSON: Error: Hash changed for 'classes/CanvasItem/methods/draw_multiline', from FC2AB533 to 3BFB36E2. This means that the function has changed and no compatibility function was provided.
Validate extension JSON: Error: Hash changed for 'classes/CanvasItem/methods/draw_polyline', from F8E6DB22 to C0C2AA6D. This means that the function has changed and no compatibility function was provided.
Validate extension JSON: Error: Hash changed for 'classes/CanvasItem/methods/draw_rect', from 0507B53D to B5FC4F88. This means that the function has changed and no compatibility function was provided.

Looking at the code, perhaps it's because all the orignal DEFVAL()'s aren't provided when registering the compatibility methods?

For example, for draw_arc() I think it maybe should be:

ClassDB::bind_compatibility_method(D_METHOD("draw_arc", "center", "radius", "start_angle", "end_angle", "point_count", "color", "width", "antialiased"), &CanvasItem::draw_arc_compat_79666, DEFVAL(-1.0), DEFVAL(false));

@kleonc kleonc force-pushed the canvas-item-draw-opaque-white-by-default branch from 5793f99 to 54301e8 Compare July 20, 2023 19:10
@kleonc
Copy link
Member Author

kleonc commented Jul 20, 2023

Looking at the code, perhaps it's because all the orignal DEFVAL()'s aren't provided when registering the compatibility methods?

I was looking at this for some time and everything looked the same to me. Now that you've pointed it out it's so obvious, definitely needed another pair of eyes. Thanks again!

I wonder why it wasn't complaining about RenderingServer::canvas_item_add_line though, I've missed default values in compat binding for it too. 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make Color(1,1,1,1) default value of color arguments of custom drawing functions whenever applicable.
5 participants