Merge pull request #52361 from Calinou/minimap-add-hover-pressed-feedback-3.x

Add visual feedback when hovering or dragging the code minimap grabber (3.x)
This commit is contained in:
Gilles Roudière 2021-09-06 17:02:46 +02:00 committed by GitHub
commit ac89f9d7d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 2 deletions

View file

@ -528,6 +528,33 @@ void TextEdit::_update_selection_mode_line() {
click_select_held->start();
}
void TextEdit::_update_minimap_hover() {
const Point2 mp = get_local_mouse_position();
const int xmargin_end = get_size().width - cache.style_normal->get_margin(MARGIN_RIGHT);
const bool hovering_sidebar = mp.x > xmargin_end - minimap_width && mp.x < xmargin_end;
if (!hovering_sidebar) {
if (hovering_minimap) {
// Only redraw if the hovering status changed.
hovering_minimap = false;
update();
}
// Return early to avoid running the operations below when not needed.
return;
}
int row;
_get_minimap_mouse_row(Point2i(mp.x, mp.y), row);
const bool new_hovering_minimap = row >= get_first_visible_line() && row <= get_last_full_visible_line();
if (new_hovering_minimap != hovering_minimap) {
// Only redraw if the hovering status changed.
hovering_minimap = new_hovering_minimap;
update();
}
}
void TextEdit::_update_minimap_click() {
Point2 mp = get_local_mouse_position();
@ -916,8 +943,19 @@ void TextEdit::_notification(int p_what) {
}
int minimap_draw_amount = minimap_visible_lines + times_line_wraps(minimap_line + 1);
// draw the minimap
Color viewport_color = (cache.background_color.get_v() < 0.5) ? Color(1, 1, 1, 0.1) : Color(0, 0, 0, 0.1);
// Draw the minimap.
// Add visual feedback when dragging or hovering the the visible area rectangle.
float viewport_alpha;
if (dragging_minimap) {
viewport_alpha = 0.25;
} else if (hovering_minimap) {
viewport_alpha = 0.175;
} else {
viewport_alpha = 0.1;
}
const Color viewport_color = (cache.background_color.get_v() < 0.5) ? Color(1, 1, 1, viewport_alpha) : Color(0, 0, 0, viewport_alpha);
VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2((xmargin_end + 2), viewport_offset_y, cache.minimap_width, viewport_height), viewport_color);
for (int i = 0; i < minimap_draw_amount; i++) {
minimap_line++;
@ -2586,6 +2624,10 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
}
}
if (draw_minimap && !dragging_selection) {
_update_minimap_hover();
}
if (mm->get_button_mask() & BUTTON_MASK_LEFT && get_viewport()->gui_get_drag_data() == Variant()) { // Ignore if dragging.
_reset_caret_blink_timer();
@ -7352,6 +7394,7 @@ TextEdit::TextEdit() {
smooth_scroll_enabled = false;
scrolling = false;
minimap_clicked = false;
hovering_minimap = false;
dragging_minimap = false;
can_drag_minimap = false;
minimap_scroll_ratio = 0;

View file

@ -392,6 +392,7 @@ private:
bool smooth_scroll_enabled;
bool scrolling;
bool dragging_selection;
bool hovering_minimap;
bool dragging_minimap;
bool can_drag_minimap;
bool minimap_clicked;
@ -476,6 +477,7 @@ private:
void _update_selection_mode_word();
void _update_selection_mode_line();
void _update_minimap_hover();
void _update_minimap_click();
void _update_minimap_drag();
void _scroll_up(real_t p_delta);