[Core] Add scalar versions of Vector* min/max/clamp/snap(ped)

Convenience for a number of cases operating on single values
This commit is contained in:
A Thousand Ships 2024-03-03 14:37:52 +01:00
parent f91db3dc58
commit 308dbb8c63
No known key found for this signature in database
GPG key ID: 2033189A662F8BD7
73 changed files with 588 additions and 105 deletions

View file

@ -142,8 +142,8 @@ Size2 Control::_edit_get_scale() const {
void Control::_edit_set_rect(const Rect2 &p_edit_rect) {
ERR_FAIL_COND_MSG(!Engine::get_singleton()->is_editor_hint(), "This function can only be used from editor plugins.");
set_position((get_position() + get_transform().basis_xform(p_edit_rect.position)).snapped(Vector2(1, 1)), ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled());
set_size(p_edit_rect.size.snapped(Vector2(1, 1)), ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled());
set_position((get_position() + get_transform().basis_xform(p_edit_rect.position)).snappedf(1), ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled());
set_size(p_edit_rect.size.snappedf(1), ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled());
}
Rect2 Control::_edit_get_rect() const {

View file

@ -501,7 +501,7 @@ void GraphEdit::_graph_element_resize_request(const Vector2 &p_new_minsize, Node
// Snap the new size to the grid if snapping is enabled.
Vector2 new_size = p_new_minsize;
if (snapping_enabled ^ Input::get_singleton()->is_key_pressed(Key::CTRL)) {
new_size = new_size.snapped(Vector2(snapping_distance, snapping_distance));
new_size = new_size.snappedf(snapping_distance);
}
// Disallow resizing the frame to a size smaller than the minimum size of the attached nodes.
@ -851,7 +851,7 @@ void GraphEdit::_set_position_of_frame_attached_nodes(GraphFrame *p_frame, const
Vector2 pos = (attached_node->get_drag_from() * zoom + drag_accum) / zoom;
if (snapping_enabled ^ Input::get_singleton()->is_key_pressed(Key::CTRL)) {
pos = pos.snapped(Vector2(snapping_distance, snapping_distance));
pos = pos.snappedf(snapping_distance);
}
// Recursively move graph frames.
@ -1678,7 +1678,7 @@ void GraphEdit::gui_input(const Ref<InputEvent> &p_ev) {
// Snapping can be toggled temporarily by holding down Ctrl.
// This is done here as to not toggle the grid when holding down Ctrl.
if (snapping_enabled ^ Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
pos = pos.snapped(Vector2(snapping_distance, snapping_distance));
pos = pos.snappedf(snapping_distance);
}
graph_element->set_position_offset(pos);

View file

@ -180,7 +180,7 @@ void GraphEditArranger::arrange_nodes() {
if (graph_edit->is_snapping_enabled()) {
float snapping_distance = graph_edit->get_snapping_distance();
pos = pos.snapped(Vector2(snapping_distance, snapping_distance));
pos = pos.snappedf(snapping_distance);
}
graph_node->set_position_offset(pos);
graph_node->set_drag(false);

View file

@ -41,7 +41,7 @@ Size2 ProgressBar::get_minimum_size() const {
TextLine tl = TextLine(txt, theme_cache.font, theme_cache.font_size);
minimum_size.height = MAX(minimum_size.height, theme_cache.background_style->get_minimum_size().height + tl.get_size().y);
} else { // this is needed, else the progressbar will collapse
minimum_size = minimum_size.max(Size2(1, 1));
minimum_size = minimum_size.maxf(1);
}
return minimum_size;
}

View file

@ -103,7 +103,7 @@ bool TextureButton::has_point(const Point2 &p_point) const {
point *= scale;
// finally, we need to check if the point is inside a rectangle with a position >= 0,0 and a size <= mask_size
rect.position = Point2().max(_texture_region.position);
rect.position = _texture_region.position.maxf(0);
rect.size = mask_size.min(_texture_region.size);
}

View file

@ -249,7 +249,7 @@ Point2 TextureProgressBar::get_relative_center() {
p += rad_center_off;
p.x /= progress->get_width();
p.y /= progress->get_height();
p = p.clamp(Point2(), Point2(1, 1));
p = p.clampf(0, 1);
return p;
}

View file

@ -3429,7 +3429,7 @@ Rect2 Tree::_get_content_rect() const {
const real_t v_size = v_scroll->is_visible() ? (v_scroll->get_combined_minimum_size().x + theme_cache.scrollbar_h_separation) : 0;
const real_t h_size = h_scroll->is_visible() ? (h_scroll->get_combined_minimum_size().y + theme_cache.scrollbar_v_separation) : 0;
const Point2 scroll_begin = _get_scrollbar_layout_rect().get_end() - Vector2(v_size, h_size);
const Size2 offset = (content_rect.get_end() - scroll_begin).max(Vector2(0, 0));
const Size2 offset = (content_rect.get_end() - scroll_begin).maxf(0);
return content_rect.grow_individual(0, 0, -offset.x, -offset.y);
}