2023-01-27 19:00:05 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2022-08-15 11:29:45 +00:00
|
|
|
#include "imgui.h"
|
|
|
|
|
|
|
|
#include "TracyTimelineController.hpp"
|
|
|
|
|
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
2022-09-03 16:34:11 +00:00
|
|
|
TimelineController::TimelineController( View& view, Worker& worker )
|
2022-08-15 11:29:45 +00:00
|
|
|
: m_height( 0 )
|
2022-12-18 22:40:03 +00:00
|
|
|
, m_scroll( 0 )
|
2023-01-27 19:00:05 +00:00
|
|
|
, m_centerItemkey( nullptr )
|
|
|
|
, m_centerItemOffsetY( 0 )
|
2022-08-20 15:02:29 +00:00
|
|
|
, m_firstFrame( true )
|
2022-09-03 14:38:39 +00:00
|
|
|
, m_view( view )
|
|
|
|
, m_worker( worker )
|
2022-08-15 11:29:45 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-08-20 15:02:29 +00:00
|
|
|
void TimelineController::FirstFrameExpired()
|
|
|
|
{
|
|
|
|
m_firstFrame = false;
|
|
|
|
}
|
|
|
|
|
2022-09-03 14:45:53 +00:00
|
|
|
void TimelineController::Begin()
|
2022-08-15 11:29:45 +00:00
|
|
|
{
|
2022-09-03 14:45:53 +00:00
|
|
|
m_items.clear();
|
|
|
|
}
|
|
|
|
|
2023-01-27 19:00:05 +00:00
|
|
|
void TimelineController::UpdateCenterItem()
|
|
|
|
{
|
|
|
|
ImVec2 mousePos = ImGui::GetMousePos();
|
|
|
|
|
|
|
|
m_centerItemkey = nullptr;
|
|
|
|
m_centerItemOffsetY = 0;
|
|
|
|
|
|
|
|
if( m_firstFrame || !ImGui::IsMousePosValid( &mousePos ) ) return;
|
|
|
|
|
|
|
|
const auto timelineMousePosY = mousePos.y - ImGui::GetWindowPos().y;
|
|
|
|
int centerY = timelineMousePosY + ImGui::GetScrollY();
|
|
|
|
|
|
|
|
int yBegin = 0;
|
|
|
|
int yEnd = 0;
|
|
|
|
for( auto& item : m_items )
|
|
|
|
{
|
|
|
|
m_centerItemkey = item->GetKey();
|
|
|
|
yBegin = yEnd;
|
|
|
|
yEnd += item->GetNextFrameHeight();
|
|
|
|
|
|
|
|
const auto inLowerBounds = m_centerItemkey == m_items.front()->GetKey() || yBegin <= centerY;
|
|
|
|
const auto inUpperBounds = m_centerItemkey == m_items.back()->GetKey() || centerY < yEnd;
|
|
|
|
|
|
|
|
if( inLowerBounds && inUpperBounds )
|
|
|
|
{
|
|
|
|
m_centerItemOffsetY = centerY - yBegin;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<int> TimelineController::CalculateScrollPosition() const
|
|
|
|
{
|
|
|
|
if( !m_centerItemkey ) return std::nullopt;
|
|
|
|
|
|
|
|
ImVec2 mousePos = ImGui::GetMousePos();
|
|
|
|
|
|
|
|
if( !ImGui::IsMousePosValid( &mousePos ) ) return std::nullopt;
|
|
|
|
|
|
|
|
const auto timelineMousePosY = mousePos.y - ImGui::GetWindowPos().y;
|
|
|
|
|
|
|
|
int yBegin = 0;
|
|
|
|
int yEnd = 0;
|
|
|
|
for( auto& item : m_items )
|
|
|
|
{
|
|
|
|
yBegin = yEnd;
|
|
|
|
yEnd += item->GetNextFrameHeight();
|
|
|
|
|
|
|
|
if( item->GetKey() != m_centerItemkey ) continue;
|
|
|
|
|
|
|
|
int scrollY = yBegin + m_centerItemOffsetY - timelineMousePosY;
|
|
|
|
|
|
|
|
return scrollY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimelineController::End( double pxns, const ImVec2& wpos, bool hover, bool vcenter, float yMin, float yMax )
|
2022-09-03 14:45:53 +00:00
|
|
|
{
|
2023-01-27 19:00:05 +00:00
|
|
|
auto shouldUpdateCenterItem = [&] () {
|
2023-01-31 19:35:17 +00:00
|
|
|
const auto imguiChangedScroll = m_scroll != ImGui::GetScrollY();
|
2023-01-27 19:00:05 +00:00
|
|
|
const auto& mouseDelta = ImGui::GetIO().MouseDelta;
|
|
|
|
const auto mouseMoved = mouseDelta.x != 0.0f || mouseDelta.y != 0.0f;
|
2023-01-31 19:35:17 +00:00
|
|
|
const auto& mousePos = ImGui::GetIO().MousePos;
|
|
|
|
const auto mouseVisible = ImGui::IsMousePosValid( &mousePos );
|
|
|
|
return ( ( imguiChangedScroll || mouseMoved || !mouseVisible ) && !ImGui::IsMouseDown( 1 ) ) || !m_centerItemkey;
|
2023-01-27 19:00:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if( !vcenter )
|
|
|
|
{
|
|
|
|
m_centerItemkey = nullptr;
|
|
|
|
m_centerItemOffsetY = 0;
|
|
|
|
}
|
|
|
|
else if( shouldUpdateCenterItem() )
|
|
|
|
{
|
|
|
|
UpdateCenterItem();
|
|
|
|
}
|
|
|
|
|
2023-01-27 19:00:05 +00:00
|
|
|
int yOffset = 0;
|
|
|
|
|
2022-09-03 14:45:53 +00:00
|
|
|
for( auto& item : m_items )
|
|
|
|
{
|
2023-01-27 19:00:05 +00:00
|
|
|
auto currentFrameItemHeight = item->GetNextFrameHeight();
|
2023-01-27 19:00:05 +00:00
|
|
|
item->Draw( m_firstFrame, pxns, yOffset, wpos, hover, yMin, yMax );
|
2023-01-27 19:00:05 +00:00
|
|
|
if( m_firstFrame ) currentFrameItemHeight = item->GetNextFrameHeight();
|
|
|
|
yOffset += currentFrameItemHeight;
|
2022-09-03 14:45:53 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 19:00:05 +00:00
|
|
|
if( const auto scrollY = CalculateScrollPosition() )
|
|
|
|
{
|
|
|
|
int clampedScrollY = std::min<int>( *scrollY, yOffset );
|
|
|
|
ImGui::SetScrollY( clampedScrollY );
|
|
|
|
int minHeight = ImGui::GetWindowHeight() + clampedScrollY;
|
|
|
|
yOffset = std::max( yOffset, minHeight );
|
|
|
|
}
|
|
|
|
|
2022-12-18 22:40:03 +00:00
|
|
|
const auto scrollPos = ImGui::GetScrollY();
|
2023-01-27 19:00:05 +00:00
|
|
|
if( ( scrollPos == 0 && m_scroll != 0 ) || yOffset > m_height )
|
2022-12-18 22:40:03 +00:00
|
|
|
{
|
2023-01-27 19:00:05 +00:00
|
|
|
m_height = yOffset;
|
2022-12-18 22:40:03 +00:00
|
|
|
}
|
|
|
|
m_scroll = scrollPos;
|
2022-08-15 11:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|