Skip to content

Commit

Permalink
Allow extending CanvasItem in GDExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronfranke committed Feb 22, 2024
1 parent 16d6142 commit 110d852
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
6 changes: 6 additions & 0 deletions doc/classes/CanvasItem.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
Corresponds to the [constant NOTIFICATION_DRAW] notification in [method Object._notification].
</description>
</method>
<method name="_get_transform" qualifiers="virtual const">
<return type="Transform2D" />
<description>
Callback used to implement custom [CanvasItem] nodes in GDExtension. This method is called by [method get_transform]. This callback method is not used with [Control] and [Node2D], which implement their own [method get_transform].
</description>
</method>
<method name="draw_animation_slice">
<return type="void" />
<param index="0" name="animation_length" type="float" />
Expand Down
7 changes: 7 additions & 0 deletions scene/main/canvas_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,7 @@ void CanvasItem::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_clip_children_mode"), &CanvasItem::get_clip_children_mode);

GDVIRTUAL_BIND(_draw);
GDVIRTUAL_BIND(_get_transform);

ADD_GROUP("Visibility", "");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
Expand Down Expand Up @@ -1264,6 +1265,12 @@ void CanvasItem::_bind_methods() {
BIND_ENUM_CONSTANT(CLIP_CHILDREN_MAX);
}

Transform2D CanvasItem::get_transform() const {
Transform2D ret = Transform2D();
GDVIRTUAL_CALL(_get_transform, ret);
return ret;
}

Transform2D CanvasItem::get_canvas_transform() const {
ERR_READ_THREAD_GUARD_V(Transform2D());
ERR_FAIL_COND_V(!is_inside_tree(), Transform2D());
Expand Down
11 changes: 6 additions & 5 deletions scene/main/canvas_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ class CanvasItem : public Node {
_FORCE_INLINE_ void set_hide_clip_children(bool p_value) { hide_clip_children = p_value; }

GDVIRTUAL0(_draw)
GDVIRTUAL0RC(Transform2D, _get_transform)

public:
enum {
Expand All @@ -188,12 +189,12 @@ class CanvasItem : public Node {
virtual Dictionary _edit_get_state() const { return Dictionary(); }

// Used to move the node
virtual void _edit_set_position(const Point2 &p_position) = 0;
virtual Point2 _edit_get_position() const = 0;
virtual void _edit_set_position(const Point2 &p_position) {}
virtual Point2 _edit_get_position() const { return get_transform().get_origin(); }

// Used to scale the node
virtual void _edit_set_scale(const Size2 &p_scale) = 0;
virtual Size2 _edit_get_scale() const = 0;
virtual void _edit_set_scale(const Size2 &p_scale) {}
virtual Size2 _edit_get_scale() const { return get_transform().get_scale(); }

// Used to rotate the node
virtual bool _edit_use_rotation() const { return false; };
Expand Down Expand Up @@ -308,7 +309,7 @@ class CanvasItem : public Node {

CanvasItem *get_parent_item() const;

virtual Transform2D get_transform() const = 0;
virtual Transform2D get_transform() const;

virtual Transform2D get_global_transform() const;
virtual Transform2D get_global_transform_with_canvas() const;
Expand Down
2 changes: 1 addition & 1 deletion scene/register_scene_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ void register_scene_types() {
GDREGISTER_VIRTUAL_CLASS(Material);
GDREGISTER_CLASS(PlaceholderMaterial);
GDREGISTER_CLASS(ShaderMaterial);
GDREGISTER_ABSTRACT_CLASS(CanvasItem);
GDREGISTER_VIRTUAL_CLASS(CanvasItem);
GDREGISTER_CLASS(CanvasTexture);
GDREGISTER_CLASS(CanvasItemMaterial);
SceneTree::add_idle_callback(CanvasItemMaterial::flush_changes);
Expand Down

0 comments on commit 110d852

Please sign in to comment.