diff options
Diffstat (limited to 'editor/plugins/canvas_item_editor_plugin.cpp')
-rw-r--r-- | editor/plugins/canvas_item_editor_plugin.cpp | 125 |
1 files changed, 21 insertions, 104 deletions
diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp index 261621e10a..139569ac8a 100644 --- a/editor/plugins/canvas_item_editor_plugin.cpp +++ b/editor/plugins/canvas_item_editor_plugin.cpp @@ -1296,11 +1296,11 @@ bool CanvasItemEditor::_gui_input_zoom_or_pan(const Ref<InputEvent> &p_event, bo view_offset.y += int(EditorSettings::get_singleton()->get("editors/2d/pan_speed")) / zoom * b->get_factor(); update_viewport(); } else { - float new_zoom = _get_next_zoom_value(-1); + zoom_widget->set_zoom_by_increments(-1); if (b->get_factor() != 1.f) { - new_zoom = zoom * ((new_zoom / zoom - 1.f) * b->get_factor() + 1.f); + zoom_widget->set_zoom(zoom * ((zoom_widget->get_zoom() / zoom - 1.f) * b->get_factor() + 1.f)); } - _zoom_on_position(new_zoom, b->get_position()); + _zoom_on_position(zoom_widget->get_zoom(), b->get_position()); } return true; } @@ -1311,11 +1311,11 @@ bool CanvasItemEditor::_gui_input_zoom_or_pan(const Ref<InputEvent> &p_event, bo view_offset.y -= int(EditorSettings::get_singleton()->get("editors/2d/pan_speed")) / zoom * b->get_factor(); update_viewport(); } else { - float new_zoom = _get_next_zoom_value(1); + zoom_widget->set_zoom_by_increments(1); if (b->get_factor() != 1.f) { - new_zoom = zoom * ((new_zoom / zoom - 1.f) * b->get_factor() + 1.f); + zoom_widget->set_zoom(zoom * ((zoom_widget->get_zoom() / zoom - 1.f) * b->get_factor() + 1.f)); } - _zoom_on_position(new_zoom, b->get_position()); + _zoom_on_position(zoom_widget->get_zoom(), b->get_position()); } return true; } @@ -1391,12 +1391,13 @@ bool CanvasItemEditor::_gui_input_zoom_or_pan(const Ref<InputEvent> &p_event, bo // If control key pressed, then zoom instead of pan if (pan_gesture->get_control()) { const float factor = pan_gesture->get_delta().y; - float new_zoom = _get_next_zoom_value(-1); + zoom_widget->set_zoom_by_increments(1); if (factor != 1.f) { - new_zoom = zoom * ((new_zoom / zoom - 1.f) * factor + 1.f); + zoom_widget->set_zoom(zoom * ((zoom_widget->get_zoom() / zoom - 1.f) * factor + 1.f)); } - _zoom_on_position(new_zoom, pan_gesture->get_position()); + _zoom_on_position(zoom_widget->get_zoom(), pan_gesture->get_position()); + return true; } @@ -4219,9 +4220,6 @@ void CanvasItemEditor::_notification(int p_what) { key_auto_insert_button->add_theme_color_override("icon_pressed_color", key_auto_color.lerp(Color(1, 0, 0), 0.55)); animation_menu->set_icon(get_theme_icon("GuiTabMenuHl", "EditorIcons")); - zoom_minus->set_icon(get_theme_icon("ZoomLess", "EditorIcons")); - zoom_plus->set_icon(get_theme_icon("ZoomMore", "EditorIcons")); - presets_menu->set_icon(get_theme_icon("ControlLayout", "EditorIcons")); PopupMenu *p = presets_menu->get_popup(); @@ -4579,33 +4577,6 @@ void CanvasItemEditor::_set_anchors_preset(Control::LayoutPreset p_preset) { undo_redo->commit_action(); } -float CanvasItemEditor::_get_next_zoom_value(int p_increment_count) const { - // Base increment factor defined as the twelveth root of two. - // This allow 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 - // In order to avoid numerical imprecisions, we compute and edit a zoom index - // with the following relation: zoom = 2 ^ (index / 12) - - if (zoom < CMP_EPSILON || p_increment_count == 0) { - return 1.f; - } - - // Remove Editor scale from the index computation - float zoom_noscale = zoom / MAX(1, EDSCALE); - - // zoom = 2**(index/12) => log2(zoom) = index/12 - float closest_zoom_index = Math::round(Math::log(zoom_noscale) * 12.f / Math::log(2.f)); - - float new_zoom_index = closest_zoom_index + p_increment_count; - float new_zoom = Math::pow(2.f, new_zoom_index / 12.f); - - // Restore Editor scale transformation - new_zoom *= MAX(1, EDSCALE); - - return new_zoom; -} - void CanvasItemEditor::_zoom_on_position(float p_zoom, Point2 p_position) { p_zoom = CLAMP(p_zoom, MIN_ZOOM, MAX_ZOOM); @@ -4630,36 +4601,12 @@ void CanvasItemEditor::_zoom_on_position(float p_zoom, Point2 p_position) { view_offset = view_offset_int + (view_offset_frac * closest_zoom_factor).round() / closest_zoom_factor; } - _update_zoom_label(); + zoom_widget->set_zoom(zoom); update_viewport(); } -void CanvasItemEditor::_update_zoom_label() { - String zoom_text; - // The zoom level displayed is relative to the editor scale - // (like in most image editors). Its lower bound is clamped to 1 as some people - // lower the editor scale to increase the available real estate, - // even if their display doesn't have a particularly low DPI. - if (zoom >= 10) { - // Don't show a decimal when the zoom level is higher than 1000 %. - zoom_text = TS->format_number(rtos(Math::round((zoom / MAX(1, EDSCALE)) * 100))) + " " + TS->percent_sign(); - } else { - zoom_text = TS->format_number(rtos(Math::snapped((zoom / MAX(1, EDSCALE)) * 100, 0.1))) + " " + TS->percent_sign(); - } - - zoom_reset->set_text(zoom_text); -} - -void CanvasItemEditor::_button_zoom_minus() { - _zoom_on_position(_get_next_zoom_value(-6), viewport_scrollable->get_size() / 2.0); -} - -void CanvasItemEditor::_button_zoom_reset() { - _zoom_on_position(1.0 * MAX(1, EDSCALE), viewport_scrollable->get_size() / 2.0); -} - -void CanvasItemEditor::_button_zoom_plus() { - _zoom_on_position(_get_next_zoom_value(6), viewport_scrollable->get_size() / 2.0); +void CanvasItemEditor::_update_zoom(float p_zoom) { + _zoom_on_position(p_zoom, viewport_scrollable->get_size() / 2.0); } void CanvasItemEditor::_button_toggle_smart_snap(bool p_status) { @@ -5401,7 +5348,7 @@ void CanvasItemEditor::_focus_selection(int p_op) { zoom = scale_x < scale_y ? scale_x : scale_y; zoom *= 0.90; viewport->update(); - _update_zoom_label(); + zoom_widget->set_zoom(zoom); call_deferred("_popup_callback", VIEW_CENTER_TO_SELECTION); } } @@ -5446,7 +5393,7 @@ Dictionary CanvasItemEditor::get_state() const { state["show_rulers"] = show_rulers; state["show_guides"] = show_guides; state["show_helpers"] = show_helpers; - state["show_zoom_control"] = zoom_hb->is_visible(); + state["show_zoom_control"] = zoom_widget->is_visible(); state["show_edit_locks"] = show_edit_locks; state["show_transformation_gizmos"] = show_transformation_gizmos; state["snap_rotation"] = snap_rotation; @@ -5464,7 +5411,7 @@ void CanvasItemEditor::set_state(const Dictionary &p_state) { // Compensate the editor scale, so that the editor scale can be changed // and the zoom level will still be the same (relative to the editor scale). zoom = float(p_state["zoom"]) * MAX(1, EDSCALE); - _update_zoom_label(); + zoom_widget->set_zoom(zoom); } if (state.has("ofs")) { @@ -5594,7 +5541,7 @@ void CanvasItemEditor::set_state(const Dictionary &p_state) { if (state.has("show_zoom_control")) { // This one is not user-controllable, but instrumentable - zoom_hb->set_visible(state["show_zoom_control"]); + zoom_widget->set_visible(state["show_zoom_control"]); } if (state.has("snap_rotation")) { @@ -5770,11 +5717,6 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) { controls_vb = memnew(VBoxContainer); controls_vb->set_begin(Point2(5, 5)); - zoom_hb = memnew(HBoxContainer); - // Bring the zoom percentage closer to the zoom buttons - zoom_hb->add_theme_constant_override("separation", Math::round(-8 * EDSCALE)); - controls_vb->add_child(zoom_hb); - viewport = memnew(CanvasItemEditorViewport(p_editor, this)); viewport_scrollable->add_child(viewport); viewport->set_mouse_filter(MOUSE_FILTER_PASS); @@ -5821,35 +5763,10 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) { viewport->add_child(controls_vb); - zoom_minus = memnew(Button); - zoom_minus->set_flat(true); - zoom_hb->add_child(zoom_minus); - zoom_minus->connect("pressed", callable_mp(this, &CanvasItemEditor::_button_zoom_minus)); - zoom_minus->set_shortcut(ED_SHORTCUT("canvas_item_editor/zoom_minus", TTR("Zoom Out"), KEY_MASK_CMD | KEY_MINUS)); - zoom_minus->set_shortcut_context(this); - zoom_minus->set_focus_mode(FOCUS_NONE); - - zoom_reset = memnew(Button); - zoom_reset->set_flat(true); - zoom_hb->add_child(zoom_reset); - zoom_reset->add_theme_constant_override("outline_size", 1); - zoom_reset->add_theme_color_override("font_outline_color", Color(0, 0, 0)); - zoom_reset->add_theme_color_override("font_color", Color(1, 1, 1)); - zoom_reset->connect("pressed", callable_mp(this, &CanvasItemEditor::_button_zoom_reset)); - zoom_reset->set_shortcut(ED_SHORTCUT("canvas_item_editor/zoom_reset", TTR("Zoom Reset"), KEY_MASK_CMD | KEY_0)); - zoom_reset->set_shortcut_context(this); - zoom_reset->set_focus_mode(FOCUS_NONE); - zoom_reset->set_text_align(Button::TextAlign::ALIGN_CENTER); - // Prevent the button's size from changing when the text size changes - zoom_reset->set_custom_minimum_size(Size2(75 * EDSCALE, 0)); - - zoom_plus = memnew(Button); - zoom_plus->set_flat(true); - zoom_hb->add_child(zoom_plus); - zoom_plus->connect("pressed", callable_mp(this, &CanvasItemEditor::_button_zoom_plus)); - zoom_plus->set_shortcut(ED_SHORTCUT("canvas_item_editor/zoom_plus", TTR("Zoom In"), KEY_MASK_CMD | KEY_EQUAL)); // Usually direct access key for PLUS - zoom_plus->set_shortcut_context(this); - zoom_plus->set_focus_mode(FOCUS_NONE); + zoom_widget = memnew(EditorZoomWidget); + controls_vb->add_child(zoom_widget); + zoom_widget->set_anchors_and_offsets_preset(Control::PRESET_TOP_LEFT, Control::PRESET_MODE_MINSIZE, 2 * EDSCALE); + zoom_widget->connect("zoom_changed", callable_mp(this, &CanvasItemEditor::_update_zoom)); updating_scroll = false; |