Merge pull request #70646 from KoBeWi/relatable_offset

Add `pivot_offset_ratio` property to Control
This commit is contained in:
Thaddeus Crews
2025-10-13 12:30:10 -05:00
5 changed files with 57 additions and 11 deletions

View File

@ -58,6 +58,7 @@ Dictionary Control::_edit_get_state() const {
s["rotation"] = get_rotation();
s["scale"] = get_scale();
s["pivot"] = get_pivot_offset();
s["pivot_ratio"] = get_pivot_offset_ratio();
Array anchors = { get_anchor(SIDE_LEFT), get_anchor(SIDE_TOP), get_anchor(SIDE_RIGHT), get_anchor(SIDE_BOTTOM) };
s["anchors"] = anchors;
@ -74,13 +75,14 @@ Dictionary Control::_edit_get_state() const {
void Control::_edit_set_state(const Dictionary &p_state) {
ERR_FAIL_COND(p_state.is_empty() ||
!p_state.has("rotation") || !p_state.has("scale") ||
!p_state.has("pivot") || !p_state.has("anchors") || !p_state.has("offsets") ||
!p_state.has("pivot") || !p_state.has("pivot_ratio") || !p_state.has("anchors") || !p_state.has("offsets") ||
!p_state.has("layout_mode") || !p_state.has("anchors_layout_preset"));
Dictionary state = p_state;
set_rotation(state["rotation"]);
set_scale(state["scale"]);
set_pivot_offset(state["pivot"]);
set_pivot_offset_ratio(state["pivot_ratio"]);
Array anchors = state["anchors"];
@ -152,10 +154,11 @@ void Control::_edit_set_pivot(const Point2 &p_pivot) {
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);
set_position(get_position() + move);
set_pivot_offset(p_pivot);
set_pivot_offset_ratio(Vector2());
}
Point2 Control::_edit_get_pivot() const {
return get_pivot_offset();
return get_combined_pivot_offset();
}
bool Control::_edit_use_pivot() const {
@ -536,7 +539,7 @@ void Control::_validate_property(PropertyInfo &p_property) const {
// If the parent is a container, display only container-related properties.
if (p_property.name.begins_with("anchor_") || p_property.name.begins_with("offset_") || p_property.name.begins_with("grow_") || p_property.name == "anchors_preset") {
p_property.usage ^= PROPERTY_USAGE_DEFAULT;
} else if (p_property.name == "position" || p_property.name == "rotation" || p_property.name == "scale" || p_property.name == "size" || p_property.name == "pivot_offset") {
} 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") {
p_property.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_READ_ONLY;
} else if (Engine::get_singleton()->is_editor_hint() && p_property.name == "layout_mode") {
// Set the layout mode to be disabled with the proper value.
@ -709,8 +712,8 @@ Size2 Control::get_parent_area_size() const {
Transform2D Control::_get_internal_transform() const {
// T(pivot_offset) * R(rotation) * S(scale) * T(-pivot_offset)
Transform2D xform(data.rotation, data.scale, 0.0f, data.pivot_offset);
xform.translate_local(-data.pivot_offset);
Transform2D xform(data.rotation, data.scale, 0.0f, get_combined_pivot_offset());
xform.translate_local(-get_combined_pivot_offset());
return xform;
}
@ -1606,6 +1609,23 @@ real_t Control::get_rotation_degrees() const {
return Math::rad_to_deg(get_rotation());
}
void Control::set_pivot_offset_ratio(const Vector2 &p_ratio) {
ERR_MAIN_THREAD_GUARD;
if (data.pivot_offset_ratio == p_ratio) {
return;
}
data.pivot_offset_ratio = p_ratio;
queue_redraw();
_notify_transform();
queue_accessibility_update();
}
Vector2 Control::get_pivot_offset_ratio() const {
ERR_READ_THREAD_GUARD_V(Vector2());
return data.pivot_offset_ratio;
}
void Control::set_pivot_offset(const Vector2 &p_pivot) {
ERR_MAIN_THREAD_GUARD;
if (data.pivot_offset == p_pivot) {
@ -1623,6 +1643,11 @@ Vector2 Control::get_pivot_offset() const {
return data.pivot_offset;
}
Vector2 Control::get_combined_pivot_offset() const {
ERR_READ_THREAD_GUARD_V(Vector2());
return data.pivot_offset + data.pivot_offset_ratio * get_size();
}
/// Sizes.
void Control::_update_minimum_size() {
@ -3990,6 +4015,7 @@ void Control::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_rotation_degrees", "degrees"), &Control::set_rotation_degrees);
ClassDB::bind_method(D_METHOD("set_scale", "scale"), &Control::set_scale);
ClassDB::bind_method(D_METHOD("set_pivot_offset", "pivot_offset"), &Control::set_pivot_offset);
ClassDB::bind_method(D_METHOD("set_pivot_offset_ratio", "ratio"), &Control::set_pivot_offset_ratio);
ClassDB::bind_method(D_METHOD("get_begin"), &Control::get_begin);
ClassDB::bind_method(D_METHOD("get_end"), &Control::get_end);
ClassDB::bind_method(D_METHOD("get_position"), &Control::get_position);
@ -3998,6 +4024,8 @@ void Control::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_rotation_degrees"), &Control::get_rotation_degrees);
ClassDB::bind_method(D_METHOD("get_scale"), &Control::get_scale);
ClassDB::bind_method(D_METHOD("get_pivot_offset"), &Control::get_pivot_offset);
ClassDB::bind_method(D_METHOD("get_pivot_offset_ratio"), &Control::get_pivot_offset_ratio);
ClassDB::bind_method(D_METHOD("get_combined_pivot_offset"), &Control::get_combined_pivot_offset);
ClassDB::bind_method(D_METHOD("get_custom_minimum_size"), &Control::get_custom_minimum_size);
ClassDB::bind_method(D_METHOD("get_parent_area_size"), &Control::get_parent_area_size);
ClassDB::bind_method(D_METHOD("get_global_position"), &Control::get_global_position);
@ -4224,7 +4252,8 @@ void Control::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation", PROPERTY_HINT_RANGE, "-360,360,0.1,or_less,or_greater,radians_as_degrees"), "set_rotation", "get_rotation");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation_degrees", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_rotation_degrees", "get_rotation_degrees");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scale"), "set_scale", "get_scale");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "pivot_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_pivot_offset", "get_pivot_offset");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "pivot_offset", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_EDITOR), "set_pivot_offset", "get_pivot_offset");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "pivot_offset_ratio"), "set_pivot_offset_ratio", "get_pivot_offset_ratio");
ADD_SUBGROUP("Container Sizing", "size_flags_");
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");