Skip to content

Commit e560ea4

Browse files
committed
Merge pull request #70646 from KoBeWi/relatable_offset
Add `pivot_offset_ratio` property to Control
2 parents 094eb99 + 618afcf commit e560ea4

File tree

5 files changed

+57
-11
lines changed

5 files changed

+57
-11
lines changed

doc/classes/Control.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,12 @@
409409
Returns combined minimum size from [member custom_minimum_size] and [method get_minimum_size].
410410
</description>
411411
</method>
412+
<method name="get_combined_pivot_offset" qualifiers="const">
413+
<return type="Vector2" />
414+
<description>
415+
Returns the combined value of [member pivot_offset] and [member pivot_offset_ratio], in pixels. The ratio is multiplied by the control's size.
416+
</description>
417+
</method>
412418
<method name="get_cursor_shape" qualifiers="const">
413419
<return type="int" enum="Control.CursorShape" />
414420
<param index="0" name="position" type="Vector2" default="Vector2(0, 0)" />
@@ -1090,7 +1096,12 @@
10901096
</member>
10911097
<member name="physics_interpolation_mode" type="int" setter="set_physics_interpolation_mode" getter="get_physics_interpolation_mode" overrides="Node" enum="Node.PhysicsInterpolationMode" default="2" />
10921098
<member name="pivot_offset" type="Vector2" setter="set_pivot_offset" getter="get_pivot_offset" default="Vector2(0, 0)">
1093-
By default, the node's pivot is its top-left corner. When you change its [member rotation] or [member scale], it will rotate or scale around this pivot. Set this property to [member size] / 2 to pivot around the Control's center.
1099+
By default, the node's pivot is its top-left corner. When you change its [member rotation] or [member scale], it will rotate or scale around this pivot.
1100+
The actual offset is the combined value of this property and [member pivot_offset_ratio].
1101+
</member>
1102+
<member name="pivot_offset_ratio" type="Vector2" setter="set_pivot_offset_ratio" getter="get_pivot_offset_ratio" default="Vector2(0, 0)">
1103+
Same as [member pivot_offset], but expressed as uniform vector, where [code]Vector2(0, 0)[/code] is the top-left corner of this control, and [code]Vector2(1, 1)[/code] is its bottom-right corner. Set this property to [code]Vector2(0.5, 0.5)[/code] to pivot around this control's center.
1104+
The actual offset is the combined value of this property and [member pivot_offset].
10941105
</member>
10951106
<member name="position" type="Vector2" setter="_set_position" getter="get_position" default="Vector2(0, 0)">
10961107
The node's position, relative to its containing node. It corresponds to the rectangle's top-left corner. The property is not affected by [member pivot_offset].

editor/scene/canvas_item_editor_plugin.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4205,17 +4205,18 @@ void CanvasItemEditor::_notification(int p_what) {
42054205

42064206
Control *control = Object::cast_to<Control>(ci);
42074207
if (control) {
4208-
real_t anchors[4];
4209-
Vector2 pivot;
4208+
Vector2 pivot = control->get_pivot_offset();
4209+
Vector2 pivot_ratio = control->get_pivot_offset_ratio();
42104210

4211-
pivot = control->get_pivot_offset();
4211+
real_t anchors[4];
42124212
anchors[SIDE_LEFT] = control->get_anchor(SIDE_LEFT);
42134213
anchors[SIDE_RIGHT] = control->get_anchor(SIDE_RIGHT);
42144214
anchors[SIDE_TOP] = control->get_anchor(SIDE_TOP);
42154215
anchors[SIDE_BOTTOM] = control->get_anchor(SIDE_BOTTOM);
42164216

4217-
if (pivot != se->prev_pivot || anchors[SIDE_LEFT] != se->prev_anchors[SIDE_LEFT] || anchors[SIDE_RIGHT] != se->prev_anchors[SIDE_RIGHT] || anchors[SIDE_TOP] != se->prev_anchors[SIDE_TOP] || anchors[SIDE_BOTTOM] != se->prev_anchors[SIDE_BOTTOM]) {
4217+
if (pivot != se->prev_pivot || pivot_ratio != se->prev_pivot_ratio || anchors[SIDE_LEFT] != se->prev_anchors[SIDE_LEFT] || anchors[SIDE_RIGHT] != se->prev_anchors[SIDE_RIGHT] || anchors[SIDE_TOP] != se->prev_anchors[SIDE_TOP] || anchors[SIDE_BOTTOM] != se->prev_anchors[SIDE_BOTTOM]) {
42184218
se->prev_pivot = pivot;
4219+
se->prev_pivot_ratio = pivot_ratio;
42194220
se->prev_anchors[SIDE_LEFT] = anchors[SIDE_LEFT];
42204221
se->prev_anchors[SIDE_RIGHT] = anchors[SIDE_RIGHT];
42214222
se->prev_anchors[SIDE_TOP] = anchors[SIDE_TOP];

editor/scene/canvas_item_editor_plugin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class CanvasItemEditorSelectedItem : public Object {
5858
Transform2D prev_xform;
5959
Rect2 prev_rect;
6060
Vector2 prev_pivot;
61+
Vector2 prev_pivot_ratio;
6162
real_t prev_anchors[4] = { (real_t)0.0 };
6263

6364
Transform2D pre_drag_xform;

scene/gui/control.cpp

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Dictionary Control::_edit_get_state() const {
5858
s["rotation"] = get_rotation();
5959
s["scale"] = get_scale();
6060
s["pivot"] = get_pivot_offset();
61+
s["pivot_ratio"] = get_pivot_offset_ratio();
6162

6263
Array anchors = { get_anchor(SIDE_LEFT), get_anchor(SIDE_TOP), get_anchor(SIDE_RIGHT), get_anchor(SIDE_BOTTOM) };
6364
s["anchors"] = anchors;
@@ -74,13 +75,14 @@ Dictionary Control::_edit_get_state() const {
7475
void Control::_edit_set_state(const Dictionary &p_state) {
7576
ERR_FAIL_COND(p_state.is_empty() ||
7677
!p_state.has("rotation") || !p_state.has("scale") ||
77-
!p_state.has("pivot") || !p_state.has("anchors") || !p_state.has("offsets") ||
78+
!p_state.has("pivot") || !p_state.has("pivot_ratio") || !p_state.has("anchors") || !p_state.has("offsets") ||
7879
!p_state.has("layout_mode") || !p_state.has("anchors_layout_preset"));
7980
Dictionary state = p_state;
8081

8182
set_rotation(state["rotation"]);
8283
set_scale(state["scale"]);
8384
set_pivot_offset(state["pivot"]);
85+
set_pivot_offset_ratio(state["pivot_ratio"]);
8486

8587
Array anchors = state["anchors"];
8688

@@ -152,10 +154,11 @@ void Control::_edit_set_pivot(const Point2 &p_pivot) {
152154
Vector2 move = Vector2((std::cos(data.rotation) - 1.0) * delta_pivot.x - std::sin(data.rotation) * delta_pivot.y, std::sin(data.rotation) * delta_pivot.x + (std::cos(data.rotation) - 1.0) * delta_pivot.y);
153155
set_position(get_position() + move);
154156
set_pivot_offset(p_pivot);
157+
set_pivot_offset_ratio(Vector2());
155158
}
156159

157160
Point2 Control::_edit_get_pivot() const {
158-
return get_pivot_offset();
161+
return get_combined_pivot_offset();
159162
}
160163

161164
bool Control::_edit_use_pivot() const {
@@ -536,7 +539,7 @@ void Control::_validate_property(PropertyInfo &p_property) const {
536539
// If the parent is a container, display only container-related properties.
537540
if (p_property.name.begins_with("anchor_") || p_property.name.begins_with("offset_") || p_property.name.begins_with("grow_") || p_property.name == "anchors_preset") {
538541
p_property.usage ^= PROPERTY_USAGE_DEFAULT;
539-
} else if (p_property.name == "position" || p_property.name == "rotation" || p_property.name == "scale" || p_property.name == "size" || p_property.name == "pivot_offset") {
542+
} else if (p_property.name == "position" || p_property.name == "rotation" || p_property.name == "scale" || p_property.name == "size" || p_property.name == "pivot_offset" || p_property.name == "pivot_offset_ratio") {
540543
p_property.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_READ_ONLY;
541544
} else if (Engine::get_singleton()->is_editor_hint() && p_property.name == "layout_mode") {
542545
// Set the layout mode to be disabled with the proper value.
@@ -709,8 +712,8 @@ Size2 Control::get_parent_area_size() const {
709712

710713
Transform2D Control::_get_internal_transform() const {
711714
// T(pivot_offset) * R(rotation) * S(scale) * T(-pivot_offset)
712-
Transform2D xform(data.rotation, data.scale, 0.0f, data.pivot_offset);
713-
xform.translate_local(-data.pivot_offset);
715+
Transform2D xform(data.rotation, data.scale, 0.0f, get_combined_pivot_offset());
716+
xform.translate_local(-get_combined_pivot_offset());
714717
return xform;
715718
}
716719

@@ -1606,6 +1609,23 @@ real_t Control::get_rotation_degrees() const {
16061609
return Math::rad_to_deg(get_rotation());
16071610
}
16081611

1612+
void Control::set_pivot_offset_ratio(const Vector2 &p_ratio) {
1613+
ERR_MAIN_THREAD_GUARD;
1614+
if (data.pivot_offset_ratio == p_ratio) {
1615+
return;
1616+
}
1617+
1618+
data.pivot_offset_ratio = p_ratio;
1619+
queue_redraw();
1620+
_notify_transform();
1621+
queue_accessibility_update();
1622+
}
1623+
1624+
Vector2 Control::get_pivot_offset_ratio() const {
1625+
ERR_READ_THREAD_GUARD_V(Vector2());
1626+
return data.pivot_offset_ratio;
1627+
}
1628+
16091629
void Control::set_pivot_offset(const Vector2 &p_pivot) {
16101630
ERR_MAIN_THREAD_GUARD;
16111631
if (data.pivot_offset == p_pivot) {
@@ -1623,6 +1643,11 @@ Vector2 Control::get_pivot_offset() const {
16231643
return data.pivot_offset;
16241644
}
16251645

1646+
Vector2 Control::get_combined_pivot_offset() const {
1647+
ERR_READ_THREAD_GUARD_V(Vector2());
1648+
return data.pivot_offset + data.pivot_offset_ratio * get_size();
1649+
}
1650+
16261651
/// Sizes.
16271652

16281653
void Control::_update_minimum_size() {
@@ -3990,6 +4015,7 @@ void Control::_bind_methods() {
39904015
ClassDB::bind_method(D_METHOD("set_rotation_degrees", "degrees"), &Control::set_rotation_degrees);
39914016
ClassDB::bind_method(D_METHOD("set_scale", "scale"), &Control::set_scale);
39924017
ClassDB::bind_method(D_METHOD("set_pivot_offset", "pivot_offset"), &Control::set_pivot_offset);
4018+
ClassDB::bind_method(D_METHOD("set_pivot_offset_ratio", "ratio"), &Control::set_pivot_offset_ratio);
39934019
ClassDB::bind_method(D_METHOD("get_begin"), &Control::get_begin);
39944020
ClassDB::bind_method(D_METHOD("get_end"), &Control::get_end);
39954021
ClassDB::bind_method(D_METHOD("get_position"), &Control::get_position);
@@ -3998,6 +4024,8 @@ void Control::_bind_methods() {
39984024
ClassDB::bind_method(D_METHOD("get_rotation_degrees"), &Control::get_rotation_degrees);
39994025
ClassDB::bind_method(D_METHOD("get_scale"), &Control::get_scale);
40004026
ClassDB::bind_method(D_METHOD("get_pivot_offset"), &Control::get_pivot_offset);
4027+
ClassDB::bind_method(D_METHOD("get_pivot_offset_ratio"), &Control::get_pivot_offset_ratio);
4028+
ClassDB::bind_method(D_METHOD("get_combined_pivot_offset"), &Control::get_combined_pivot_offset);
40014029
ClassDB::bind_method(D_METHOD("get_custom_minimum_size"), &Control::get_custom_minimum_size);
40024030
ClassDB::bind_method(D_METHOD("get_parent_area_size"), &Control::get_parent_area_size);
40034031
ClassDB::bind_method(D_METHOD("get_global_position"), &Control::get_global_position);
@@ -4224,7 +4252,8 @@ void Control::_bind_methods() {
42244252
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation", PROPERTY_HINT_RANGE, "-360,360,0.1,or_less,or_greater,radians_as_degrees"), "set_rotation", "get_rotation");
42254253
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation_degrees", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_rotation_degrees", "get_rotation_degrees");
42264254
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scale"), "set_scale", "get_scale");
4227-
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "pivot_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_pivot_offset", "get_pivot_offset");
4255+
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "pivot_offset", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_EDITOR), "set_pivot_offset", "get_pivot_offset");
4256+
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "pivot_offset_ratio"), "set_pivot_offset_ratio", "get_pivot_offset_ratio");
42284257

42294258
ADD_SUBGROUP("Container Sizing", "size_flags_");
42304259
ADD_PROPERTY(PropertyInfo(Variant::INT, "size_flags_horizontal", PROPERTY_HINT_FLAGS, "Fill:1,Expand:2,Shrink Center:4,Shrink End:8"), "set_h_size_flags", "get_h_size_flags");

scene/gui/control.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ class Control : public CanvasItem {
212212
real_t rotation = 0.0;
213213
Vector2 scale = Vector2(1, 1);
214214
Vector2 pivot_offset;
215+
Vector2 pivot_offset_ratio;
215216

216217
Point2 pos_cache;
217218
Size2 size_cache;
@@ -535,8 +536,11 @@ class Control : public CanvasItem {
535536
void set_rotation_degrees(real_t p_degrees);
536537
real_t get_rotation() const;
537538
real_t get_rotation_degrees() const;
539+
void set_pivot_offset_ratio(const Vector2 &p_ratio);
540+
Vector2 get_pivot_offset_ratio() const;
538541
void set_pivot_offset(const Vector2 &p_pivot);
539542
Vector2 get_pivot_offset() const;
543+
Vector2 get_combined_pivot_offset() const;
540544

541545
void update_minimum_size();
542546

0 commit comments

Comments
 (0)