Use rectangle size instead of extents for Shape dimensions

This commit is contained in:
Marcel Admiraal 2020-12-07 19:58:47 +00:00
parent 0f6745b6aa
commit d5d99aaed6
5 changed files with 26 additions and 26 deletions

View file

@ -13,8 +13,8 @@
<methods>
</methods>
<members>
<member name="extents" type="Vector2" setter="set_extents" getter="get_extents" default="Vector2( 10, 10 )">
The rectangle's half extents. The width and height of this shape is twice the half extents.
<member name="size" type="Vector2" setter="set_size" getter="get_size" default="Vector2( 20, 20 )">
The rectangle's width and height.
</member>
</members>
<constants>

View file

@ -98,7 +98,7 @@ Variant CollisionShape2DEditor::get_handle_value(int idx) const {
Ref<RectangleShape2D> rect = node->get_shape();
if (idx < 3) {
return rect->get_extents().abs();
return rect->get_size().abs();
}
} break;
@ -179,13 +179,13 @@ void CollisionShape2DEditor::set_handle(int idx, Point2 &p_point) {
if (idx < 3) {
Ref<RectangleShape2D> rect = node->get_shape();
Vector2 extents = rect->get_extents();
Vector2 size = rect->get_size();
if (idx == 2) {
extents = p_point;
size = p_point * 2;
} else {
extents[idx] = p_point[idx];
size[idx] = p_point[idx] * 2;
}
rect->set_extents(extents.abs());
rect->set_size(size.abs());
canvas_item_editor->update_viewport();
}
@ -279,9 +279,9 @@ void CollisionShape2DEditor::commit_handle(int idx, Variant &p_org) {
case RECTANGLE_SHAPE: {
Ref<RectangleShape2D> rect = node->get_shape();
undo_redo->add_do_method(rect.ptr(), "set_extents", rect->get_extents());
undo_redo->add_do_method(rect.ptr(), "set_size", rect->get_size());
undo_redo->add_do_method(canvas_item_editor, "update_viewport");
undo_redo->add_undo_method(rect.ptr(), "set_extents", p_org);
undo_redo->add_undo_method(rect.ptr(), "set_size", p_org);
undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
} break;
@ -493,7 +493,7 @@ void CollisionShape2DEditor::forward_canvas_draw_over_viewport(Control *p_overla
Ref<RectangleShape2D> shape = node->get_shape();
handles.resize(3);
Vector2 ext = shape->get_extents();
Vector2 ext = shape->get_size() / 2;
handles.write[0] = Point2(ext.x, 0);
handles.write[1] = Point2(0, ext.y);
handles.write[2] = Point2(ext.x, ext.y);

View file

@ -405,5 +405,5 @@ TouchScreenButton::TouchScreenButton() {
shape_centered = true;
shape_visible = true;
unit_rect = Ref<RectangleShape2D>(memnew(RectangleShape2D));
unit_rect->set_extents(Vector2(0.5, 0.5));
unit_rect->set_size(Vector2(1, 1));
}

View file

@ -33,40 +33,40 @@
#include "servers/physics_server_2d.h"
#include "servers/rendering_server.h"
void RectangleShape2D::_update_shape() {
PhysicsServer2D::get_singleton()->shape_set_data(get_rid(), extents);
PhysicsServer2D::get_singleton()->shape_set_data(get_rid(), size / 2);
emit_changed();
}
void RectangleShape2D::set_extents(const Vector2 &p_extents) {
extents = p_extents;
void RectangleShape2D::set_size(const Vector2 &p_size) {
size = p_size;
_update_shape();
}
Vector2 RectangleShape2D::get_extents() const {
return extents;
Vector2 RectangleShape2D::get_size() const {
return size;
}
void RectangleShape2D::draw(const RID &p_to_rid, const Color &p_color) {
RenderingServer::get_singleton()->canvas_item_add_rect(p_to_rid, Rect2(-extents, extents * 2.0), p_color);
RenderingServer::get_singleton()->canvas_item_add_rect(p_to_rid, Rect2(-size / 2, size), p_color);
}
Rect2 RectangleShape2D::get_rect() const {
return Rect2(-extents, extents * 2.0);
return Rect2(-size / 2, size);
}
real_t RectangleShape2D::get_enclosing_radius() const {
return extents.length();
return size.length() / 2;
}
void RectangleShape2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_extents", "extents"), &RectangleShape2D::set_extents);
ClassDB::bind_method(D_METHOD("get_extents"), &RectangleShape2D::get_extents);
ClassDB::bind_method(D_METHOD("set_size", "size"), &RectangleShape2D::set_size);
ClassDB::bind_method(D_METHOD("get_size"), &RectangleShape2D::get_size);
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "extents"), "set_extents", "get_extents");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size"), "set_size", "get_size");
}
RectangleShape2D::RectangleShape2D() :
Shape2D(PhysicsServer2D::get_singleton()->rectangle_shape_create()) {
extents = Vector2(10, 10);
size = Vector2(20, 20);
_update_shape();
}

View file

@ -36,15 +36,15 @@
class RectangleShape2D : public Shape2D {
GDCLASS(RectangleShape2D, Shape2D);
Vector2 extents;
Vector2 size;
void _update_shape();
protected:
static void _bind_methods();
public:
void set_extents(const Vector2 &p_extents);
Vector2 get_extents() const;
void set_size(const Vector2 &p_size);
Vector2 get_size() const;
virtual void draw(const RID &p_to_rid, const Color &p_color) override;
virtual Rect2 get_rect() const override;