Merge pull request #81812 from MewPurPur/zoomies

Incorporate min and max zoom limits into the EditorZoomWidget
This commit is contained in:
Rémi Verschelde 2023-10-09 15:31:49 +02:00
commit 7513ae6d06
No known key found for this signature in database
GPG key ID: C3336907360768E1
5 changed files with 47 additions and 22 deletions

View file

@ -70,12 +70,36 @@ float EditorZoomWidget::get_zoom() {
}
void EditorZoomWidget::set_zoom(float p_zoom) {
if (p_zoom > 0 && p_zoom != zoom) {
zoom = p_zoom;
float new_zoom = CLAMP(p_zoom, min_zoom, max_zoom);
if (zoom != new_zoom) {
zoom = new_zoom;
_update_zoom_label();
}
}
float EditorZoomWidget::get_min_zoom() {
return min_zoom;
}
float EditorZoomWidget::get_max_zoom() {
return max_zoom;
}
void EditorZoomWidget::setup_zoom_limits(float p_min, float p_max) {
ERR_FAIL_COND(p_min < 0 || p_min > p_max);
min_zoom = p_min;
max_zoom = p_max;
if (zoom > max_zoom) {
set_zoom(max_zoom);
emit_signal(SNAME("zoom_changed"), zoom);
} else if (zoom < min_zoom) {
set_zoom(min_zoom);
emit_signal(SNAME("zoom_changed"), zoom);
}
}
void EditorZoomWidget::set_zoom_by_increments(int p_increment_count, bool p_integer_only) {
// Remove editor scale from the index computation.
const float zoom_noscale = zoom / MAX(1, EDSCALE);
@ -97,7 +121,7 @@ void EditorZoomWidget::set_zoom_by_increments(int p_increment_count, bool p_inte
}
} else {
if (p_increment_count >= 1) {
// Zooming. Convert the current zoom into a denominator.
// Zooming in. Convert the current zoom into a denominator.
float new_zoom = 1.0 / Math::ceil(1.0 / zoom_noscale - p_increment_count);
if (Math::is_equal_approx(zoom_noscale, new_zoom)) {
// New zoom is identical to the old zoom, so try again.
@ -106,7 +130,7 @@ void EditorZoomWidget::set_zoom_by_increments(int p_increment_count, bool p_inte
}
set_zoom(new_zoom * MAX(1, EDSCALE));
} else {
// Dezooming. Convert the current zoom into a denominator.
// Zooming out. Convert the current zoom into a denominator.
float new_zoom = 1.0 / Math::floor(1.0 / zoom_noscale - p_increment_count);
if (Math::is_equal_approx(zoom_noscale, new_zoom)) {
// New zoom is identical to the old zoom, so try again.
@ -118,9 +142,9 @@ void EditorZoomWidget::set_zoom_by_increments(int p_increment_count, bool p_inte
}
} else {
// Base increment factor defined as the twelveth root of two.
// This allow a smooth geometric evolution of the zoom, with the advantage of
// This allows for a smooth geometric evolution of the zoom, with the advantage of
// visiting all integer power of two scale factors.
// note: this is analogous to the 'semitones' interval in the music world
// Note: this is analogous to the 'semitone' interval in the music world
// In order to avoid numerical imprecisions, we compute and edit a zoom index
// with the following relation: zoom = 2 ^ (index / 12)
@ -179,10 +203,11 @@ EditorZoomWidget::EditorZoomWidget() {
zoom_reset = memnew(Button);
zoom_reset->set_flat(true);
zoom_reset->add_theme_style_override("normal", memnew(StyleBoxEmpty));
zoom_reset->add_theme_style_override("hover", memnew(StyleBoxEmpty));
zoom_reset->add_theme_style_override("focus", memnew(StyleBoxEmpty));
zoom_reset->add_theme_style_override("pressed", memnew(StyleBoxEmpty));
Ref<StyleBoxEmpty> empty_stylebox = memnew(StyleBoxEmpty);
zoom_reset->add_theme_style_override("normal", empty_stylebox);
zoom_reset->add_theme_style_override("hover", empty_stylebox);
zoom_reset->add_theme_style_override("focus", empty_stylebox);
zoom_reset->add_theme_style_override("pressed", empty_stylebox);
add_child(zoom_reset);
zoom_reset->add_theme_constant_override("outline_size", Math::ceil(2 * EDSCALE));
zoom_reset->add_theme_color_override("font_outline_color", Color(0, 0, 0));

View file

@ -42,6 +42,8 @@ class EditorZoomWidget : public HBoxContainer {
Button *zoom_plus = nullptr;
float zoom = 1.0;
float min_zoom = 1.0 / 128;
float max_zoom = 128.0;
void _update_zoom_label();
void _button_zoom_minus();
void _button_zoom_reset();
@ -57,6 +59,11 @@ public:
float get_zoom();
void set_zoom(float p_zoom);
void set_zoom_by_increments(int p_increment_count, bool p_integer_only = false);
float get_min_zoom();
float get_max_zoom();
// It's best to setup simultaneously, so min < max can be checked easily.
void setup_zoom_limits(float p_min, float p_max);
// Sets the shortcut context for the zoom buttons. By default their context is this EditorZoomWidget control.
void set_shortcut_context(Node *p_node) const;
};