Skip to content

Commit c6492de

Browse files
author
Mateus Reis
committed
Profiler plot zoom and pan
1 parent ec6a1c0 commit c6492de

2 files changed

Lines changed: 54 additions & 16 deletions

File tree

editor/debugger/editor_profiler.cpp

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ Color EditorProfiler::_get_color_from_signature(const StringName &p_signature) c
152152
return c.lerp(get_theme_color(SNAME("base_color"), EditorStringName(Editor)), 0.07);
153153
}
154154

155+
int EditorProfiler::_get_zoom_left_border() const {
156+
const int max_profiles_shown = frame_metrics.size() / Math::exp(graph_zoom);
157+
return CLAMP(zoom_center - max_profiles_shown / 2, 0, frame_metrics.size() - max_profiles_shown);
158+
}
159+
155160
void EditorProfiler::_item_edited() {
156161
if (updating_frame) {
157162
return;
@@ -237,12 +242,17 @@ void EditorProfiler::_update_plot() {
237242

238243
HashMap<StringName, int> prev_plots;
239244

240-
for (int i = 0; i < total_metrics * w / frame_metrics.size() - 1; i++) {
245+
const int max_profiles_shown = frame_metrics.size() / Math::exp(graph_zoom);
246+
const int left_border = _get_zoom_left_border();
247+
const int profiles_drawn = CLAMP(total_metrics - left_border, 0, max_profiles_shown);
248+
const int pixel_cols = (profiles_drawn * w) / max_profiles_shown - 1;
249+
250+
for (int i = 0; i < pixel_cols; i++) {
241251
for (int j = 0; j < h * 4; j++) {
242252
column[j] = 0;
243253
}
244254

245-
int current = i * frame_metrics.size() / w;
255+
int current = (i * max_profiles_shown / w) + left_border;
246256

247257
for (const StringName &E : plot_sigs) {
248258
const Metric &m = _get_frame_metric(current);
@@ -449,10 +459,12 @@ void EditorProfiler::_graph_tex_draw() {
449459
}
450460
if (seeking) {
451461
int frame = cursor_metric_edit->get_value() - _get_frame_metric(0).frame_number;
452-
int cur_x = (2 * frame + 1) * graph->get_size().x / (2 * frame_metrics.size()) + 1;
462+
frame = frame - _get_zoom_left_border() + 1;
463+
int cur_x = (frame * graph->get_size().width * Math::exp(graph_zoom)) / frame_metrics.size();
464+
cur_x = CLAMP(cur_x, 0, graph->get_size().width);
453465
graph->draw_line(Vector2(cur_x, 0), Vector2(cur_x, graph->get_size().y), theme_cache.seek_line_color);
454466
}
455-
if (hover_metric > -1 && hover_metric < total_metrics) {
467+
if (hover_metric > -1) {
456468
int cur_x = (2 * hover_metric + 1) * graph->get_size().x / (2 * frame_metrics.size()) + 1;
457469
graph->draw_line(Vector2(cur_x, 0), Vector2(cur_x, graph->get_size().y), theme_cache.seek_line_hover_color);
458470
}
@@ -480,22 +492,17 @@ void EditorProfiler::_graph_tex_input(const Ref<InputEvent> &p_ev) {
480492
Ref<InputEventMouse> me = p_ev;
481493
Ref<InputEventMouseButton> mb = p_ev;
482494
Ref<InputEventMouseMotion> mm = p_ev;
495+
MouseButton button_idx = mb.is_valid() ? mb->get_button_index() : MouseButton();
483496

484497
if (
485-
(mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) ||
498+
(mb.is_valid() && button_idx == MouseButton::LEFT && mb->is_pressed()) ||
486499
(mm.is_valid())) {
487500
int x = me->get_position().x - 1;
488-
x = x * frame_metrics.size() / graph->get_size().width;
489-
490-
hover_metric = x;
501+
hover_metric = x * frame_metrics.size() / graph->get_size().width;
491502

492-
if (x < 0) {
493-
x = 0;
494-
}
495-
496-
if (x >= frame_metrics.size()) {
497-
x = frame_metrics.size() - 1;
498-
}
503+
x = x * frame_metrics.size() / graph->get_size().width;
504+
x = x / Math::exp(graph_zoom) + _get_zoom_left_border();
505+
x = CLAMP(x, 0, frame_metrics.size() - 1);
499506

500507
if (mb.is_valid() || (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
501508
updating_frame = true;
@@ -518,9 +525,34 @@ void EditorProfiler::_graph_tex_input(const Ref<InputEvent> &p_ev) {
518525
frame_delay->start();
519526
}
520527
}
528+
}
529+
530+
if (graph_zoom > 0 && mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::MIDDLE) || mm->get_button_mask().has_flag(MouseButtonMask::RIGHT))) {
531+
// Panning.
532+
const int max_profiles_shown = frame_metrics.size() / Math::exp(graph_zoom);
533+
pan_accumulator += (float)mm->get_relative().x * max_profiles_shown / graph->get_size().width;
534+
535+
if (Math::abs(pan_accumulator) > 1) {
536+
zoom_center = CLAMP(zoom_center - (int)pan_accumulator, max_profiles_shown / 2, frame_metrics.size() - max_profiles_shown / 2);
537+
pan_accumulator -= (int)pan_accumulator;
538+
_update_plot();
539+
}
540+
}
521541

522-
graph->queue_redraw();
542+
if (button_idx == MouseButton::WHEEL_DOWN) {
543+
// Zooming.
544+
graph_zoom = MAX(-0.05 + graph_zoom, 0);
545+
_update_plot();
546+
} else if (button_idx == MouseButton::WHEEL_UP) {
547+
if (graph_zoom == 0) {
548+
zoom_center = me->get_position().x;
549+
zoom_center = zoom_center * frame_metrics.size() / graph->get_size().width;
550+
}
551+
graph_zoom = MIN(0.05 + graph_zoom, 2);
552+
_update_plot();
523553
}
554+
555+
graph->queue_redraw();
524556
}
525557

526558
void EditorProfiler::disable_seeking() {

editor/debugger/editor_profiler.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ class EditorProfiler : public VBoxContainer {
104104
TextureRect *graph = nullptr;
105105
Ref<ImageTexture> graph_texture;
106106
Vector<uint8_t> graph_image;
107+
108+
float graph_zoom = 0.0f;
109+
float pan_accumulator = 0.0f;
110+
int zoom_center = -1;
111+
107112
Tree *variables = nullptr;
108113
HSplitContainer *h_split = nullptr;
109114

@@ -155,6 +160,7 @@ class EditorProfiler : public VBoxContainer {
155160
void _graph_tex_input(const Ref<InputEvent> &p_ev);
156161

157162
Color _get_color_from_signature(const StringName &p_signature) const;
163+
int _get_zoom_left_border() const;
158164

159165
void _cursor_metric_changed(double);
160166

0 commit comments

Comments
 (0)