From 84102af090c50bf0ceec5f72214718a437ce4e5e Mon Sep 17 00:00:00 2001 From: Zae Date: Thu, 12 Sep 2024 23:58:42 +0800 Subject: [PATCH] GraphEdit: Improve dotted pattern grid performance --- scene/gui/graph_edit.cpp | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/scene/gui/graph_edit.cpp b/scene/gui/graph_edit.cpp index 60b3e371a0a..11a6411e65f 100644 --- a/scene/gui/graph_edit.cpp +++ b/scene/gui/graph_edit.cpp @@ -1646,24 +1646,34 @@ void GraphEdit::_draw_grid() { Color transparent_grid_minor = theme_cache.grid_minor; transparent_grid_minor.a *= CLAMP(1.0 * (zoom - 0.4), 0, 1); - for (int i = from_pos.x; i < from_pos.x + len.x; i++) { - for (int j = from_pos.y; j < from_pos.y + len.y; j++) { - Color color = transparent_grid_minor; + // Minor dots. + if (transparent_grid_minor.a != 0) { + for (int i = from_pos.x; i < from_pos.x + len.x; i++) { + for (int j = from_pos.y; j < from_pos.y + len.y; j++) { + if (ABS(i) % GRID_MINOR_STEPS_PER_MAJOR_DOT == 0 && ABS(j) % GRID_MINOR_STEPS_PER_MAJOR_DOT == 0) { + continue; + } - if (ABS(i) % GRID_MINOR_STEPS_PER_MAJOR_DOT == 0 && ABS(j) % GRID_MINOR_STEPS_PER_MAJOR_DOT == 0) { - color = theme_cache.grid_major; + float base_offset_x = i * snapping_distance * zoom - offset.x * zoom; + float base_offset_y = j * snapping_distance * zoom - offset.y * zoom; + + draw_rect(Rect2(base_offset_x - 1, base_offset_y - 1, 3, 3), transparent_grid_minor); } - - if (color.a == 0) { - continue; - } - - float base_offset_x = i * snapping_distance * zoom - offset.x * zoom; - float base_offset_y = j * snapping_distance * zoom - offset.y * zoom; - - draw_rect(Rect2(base_offset_x - 1, base_offset_y - 1, 3, 3), color); } } + + // Major dots. + if (theme_cache.grid_major.a != 0) { + for (int i = from_pos.x - from_pos.x % GRID_MINOR_STEPS_PER_MAJOR_DOT; i < from_pos.x + len.x; i += GRID_MINOR_STEPS_PER_MAJOR_DOT) { + for (int j = from_pos.y - from_pos.y % GRID_MINOR_STEPS_PER_MAJOR_DOT; j < from_pos.y + len.y; j += GRID_MINOR_STEPS_PER_MAJOR_DOT) { + float base_offset_x = i * snapping_distance * zoom - offset.x * zoom; + float base_offset_y = j * snapping_distance * zoom - offset.y * zoom; + + draw_rect(Rect2(base_offset_x - 1, base_offset_y - 1, 3, 3), theme_cache.grid_major); + } + } + } + } break; } }