Skip to content

Commit bca8f28

Browse files
committed
Centering around the mouse pointer.
1 parent dbefb70 commit bca8f28

File tree

3 files changed

+97
-5
lines changed

3 files changed

+97
-5
lines changed

‎server/TracyTimelineController.cpp‎

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <algorithm>
2+
13
#include "imgui.h"
24

35
#include "TracyTimelineController.hpp"
@@ -8,6 +10,8 @@ namespace tracy
810
TimelineController::TimelineController( View& view, Worker& worker )
911
: m_height( 0 )
1012
, m_scroll( 0 )
13+
, m_centerItemkey( nullptr )
14+
, m_centerItemOffsetY( 0 )
1115
, m_firstFrame( true )
1216
, m_view( view )
1317
, m_worker( worker )
@@ -24,8 +28,83 @@ void TimelineController::Begin()
2428
m_items.clear();
2529
}
2630

27-
void TimelineController::End( double pxns, const ImVec2& wpos, bool hover, float yMin, float yMax )
31+
void TimelineController::UpdateCenterItem()
32+
{
33+
ImVec2 mousePos = ImGui::GetMousePos();
34+
35+
m_centerItemkey = nullptr;
36+
m_centerItemOffsetY = 0;
37+
38+
if( m_firstFrame || !ImGui::IsMousePosValid( &mousePos ) ) return;
39+
40+
const auto timelineMousePosY = mousePos.y - ImGui::GetWindowPos().y;
41+
int centerY = timelineMousePosY + ImGui::GetScrollY();
42+
43+
int yBegin = 0;
44+
int yEnd = 0;
45+
for( auto& item : m_items )
46+
{
47+
m_centerItemkey = item->GetKey();
48+
yBegin = yEnd;
49+
yEnd += item->GetNextFrameHeight();
50+
51+
const auto inLowerBounds = m_centerItemkey == m_items.front()->GetKey() || yBegin <= centerY;
52+
const auto inUpperBounds = m_centerItemkey == m_items.back()->GetKey() || centerY < yEnd;
53+
54+
if( inLowerBounds && inUpperBounds )
55+
{
56+
m_centerItemOffsetY = centerY - yBegin;
57+
break;
58+
}
59+
}
60+
}
61+
62+
std::optional<int> TimelineController::CalculateScrollPosition() const
63+
{
64+
if( !m_centerItemkey ) return std::nullopt;
65+
66+
ImVec2 mousePos = ImGui::GetMousePos();
67+
68+
if( !ImGui::IsMousePosValid( &mousePos ) ) return std::nullopt;
69+
70+
const auto timelineMousePosY = mousePos.y - ImGui::GetWindowPos().y;
71+
72+
int yBegin = 0;
73+
int yEnd = 0;
74+
for( auto& item : m_items )
75+
{
76+
yBegin = yEnd;
77+
yEnd += item->GetNextFrameHeight();
78+
79+
if( item->GetKey() != m_centerItemkey ) continue;
80+
81+
int scrollY = yBegin + m_centerItemOffsetY - timelineMousePosY;
82+
83+
return scrollY;
84+
}
85+
86+
return std::nullopt;
87+
}
88+
89+
void TimelineController::End( double pxns, const ImVec2& wpos, bool hover, bool vcenter, float yMin, float yMax )
2890
{
91+
auto shouldUpdateCenterItem = [&] () {
92+
const auto& mouseDelta = ImGui::GetIO().MouseDelta;
93+
const auto mouseMoved = mouseDelta.x != 0.0f || mouseDelta.y != 0.0f;
94+
const auto imguiChangedScroll = m_scroll != ImGui::GetScrollY();
95+
return ( ( imguiChangedScroll || mouseMoved ) && !ImGui::IsMouseDown( 1 ) ) || !m_centerItemkey;
96+
};
97+
98+
if( !vcenter )
99+
{
100+
m_centerItemkey = nullptr;
101+
m_centerItemOffsetY = 0;
102+
}
103+
else if( shouldUpdateCenterItem() )
104+
{
105+
UpdateCenterItem();
106+
}
107+
29108
int yOffset = 0;
30109

31110
for( auto& item : m_items )
@@ -36,6 +115,14 @@ void TimelineController::End( double pxns, const ImVec2& wpos, bool hover, float
36115
yOffset += currentFrameItemHeight;
37116
}
38117

118+
if( const auto scrollY = CalculateScrollPosition() )
119+
{
120+
int clampedScrollY = std::min<int>( *scrollY, yOffset );
121+
ImGui::SetScrollY( clampedScrollY );
122+
int minHeight = ImGui::GetWindowHeight() + clampedScrollY;
123+
yOffset = std::max( yOffset, minHeight );
124+
}
125+
39126
const auto scrollPos = ImGui::GetScrollY();
40127
if( ( scrollPos == 0 && m_scroll != 0 ) || yOffset > m_height )
41128
{

‎server/TracyTimelineController.hpp‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define __TRACYTIMELINECONTROLLER_HPP__
33

44
#include <assert.h>
5+
#include <optional>
56
#include <vector>
67

78
#include "../public/common/TracyForceInline.hpp"
@@ -18,7 +19,7 @@ class TimelineController
1819

1920
void FirstFrameExpired();
2021
void Begin();
21-
void End( double pxns, const ImVec2& wpos, bool hover, float yMin, float yMax );
22+
void End( double pxns, const ImVec2& wpos, bool hover, bool vcenter, float yMin, float yMax );
2223

2324
template<class T, class U>
2425
void AddItem( U* data )
@@ -39,12 +40,18 @@ class TimelineController
3940
}
4041

4142
private:
43+
void UpdateCenterItem();
44+
std::optional<int> CalculateScrollPosition() const;
45+
4246
std::vector<TimelineItem*> m_items;
4347
unordered_flat_map<const void*, std::unique_ptr<TimelineItem>> m_itemMap;
4448

4549
float m_height;
4650
float m_scroll;
4751

52+
const void* m_centerItemkey;
53+
int m_centerItemOffsetY;
54+
4855
bool m_firstFrame;
4956

5057
View& m_view;

‎server/TracyView_Timeline.cpp‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,6 @@ void View::DrawTimeline()
327327
if( m_yDelta != 0 )
328328
{
329329
auto& io = ImGui::GetIO();
330-
auto y = ImGui::GetScrollY();
331-
ImGui::SetScrollY( y - m_yDelta );
332330
io.MouseClickedPos[1].y = io.MousePos.y;
333331
}
334332

@@ -380,7 +378,7 @@ void View::DrawTimeline()
380378
}
381379
}
382380

383-
m_tc.End( pxns, wpos, hover, yMin, yMax );
381+
m_tc.End( pxns, wpos, hover, drawMouseLine && m_viewMode == ViewMode::Paused, yMin, yMax );
384382
ImGui::EndChild();
385383

386384
m_lockHighlight = m_nextLockHighlight;

0 commit comments

Comments
 (0)