Refactor the use of offset in TimelineItem::Draw() and TimelineController::End().

This commit is contained in:
Tomaž Vöröš 2023-01-27 20:00:05 +01:00
parent 52b6af88ca
commit 7b9b810421
5 changed files with 37 additions and 30 deletions

View File

@ -24,17 +24,20 @@ void TimelineController::Begin()
m_items.clear(); m_items.clear();
} }
void TimelineController::End( double pxns, int offset, const ImVec2& wpos, bool hover, float yMin, float yMax ) void TimelineController::End( double pxns, const ImVec2& wpos, bool hover, float yMin, float yMax )
{ {
int yOffset = 0;
for( auto& item : m_items ) for( auto& item : m_items )
{ {
item->Draw( m_firstFrame, pxns, offset, wpos, hover, yMin, yMax ); item->Draw( m_firstFrame, pxns, yOffset, wpos, hover, yMin, yMax );
yOffset += item->GetNextFrameHeight();
} }
const auto scrollPos = ImGui::GetScrollY(); const auto scrollPos = ImGui::GetScrollY();
if( ( scrollPos == 0 && m_scroll != 0 ) || offset > m_height ) if( ( scrollPos == 0 && m_scroll != 0 ) || yOffset > m_height )
{ {
m_height = offset; m_height = yOffset;
} }
m_scroll = scrollPos; m_scroll = scrollPos;
} }

View File

@ -18,7 +18,7 @@ public:
void FirstFrameExpired(); void FirstFrameExpired();
void Begin(); void Begin();
void End( double pxns, int offset, const ImVec2& wpos, bool hover, float yMin, float yMax ); void End( double pxns, const ImVec2& wpos, bool hover, float yMin, float yMax );
template<class T, class U> template<class T, class U>
void AddItem( U* data ) void AddItem( U* data )

View File

@ -17,11 +17,14 @@ TimelineItem::TimelineItem( View& view, Worker& worker )
{ {
} }
void TimelineItem::Draw( bool firstFrame, double pxns, int& offset, const ImVec2& wpos, bool hover, float yMin, float yMax ) void TimelineItem::Draw( bool firstFrame, double pxns, int yOffset, const ImVec2& wpos, bool hover, float yMin, float yMax )
{ {
const auto yBegin = yOffset;
auto yEnd = yOffset;
if( !IsVisible() ) if( !IsVisible() )
{ {
if( m_height != 0 ) AdjustThreadHeight( firstFrame, offset, offset ); if( m_height != 0 ) AdjustThreadHeight( firstFrame, yBegin, yEnd );
return; return;
} }
if( IsEmpty() ) return; if( IsEmpty() ) return;
@ -29,32 +32,31 @@ void TimelineItem::Draw( bool firstFrame, double pxns, int& offset, const ImVec2
const auto w = ImGui::GetContentRegionAvail().x - 1; const auto w = ImGui::GetContentRegionAvail().x - 1;
const auto ty = ImGui::GetTextLineHeight(); const auto ty = ImGui::GetTextLineHeight();
const auto ostep = ty + 1; const auto ostep = ty + 1;
const auto yPos = wpos.y + offset; const auto yPos = wpos.y + yBegin;
const auto dpos = wpos + ImVec2( 0.5f, 0.5f ); const auto dpos = wpos + ImVec2( 0.5f, 0.5f );
const auto oldOffset = offset;
auto draw = ImGui::GetWindowDrawList(); auto draw = ImGui::GetWindowDrawList();
ImGui::PushID( this ); ImGui::PushID( this );
ImGui::PushClipRect( wpos + ImVec2( 0, offset ), wpos + ImVec2( w, offset + m_height ), true ); ImGui::PushClipRect( wpos + ImVec2( 0, yBegin ), wpos + ImVec2( w, yBegin + m_height ), true );
offset += ostep; yEnd += ostep;
if( m_showFull ) if( m_showFull )
{ {
if( !DrawContents( pxns, offset, wpos, hover, yMin, yMax ) && !m_view.GetViewData().drawEmptyLabels ) if( !DrawContents( pxns, yEnd, wpos, hover, yMin, yMax ) && !m_view.GetViewData().drawEmptyLabels )
{ {
offset = oldOffset; yEnd = yBegin;
AdjustThreadHeight( firstFrame, oldOffset, offset ); AdjustThreadHeight( firstFrame, yBegin, yEnd );
ImGui::PopClipRect(); ImGui::PopClipRect();
ImGui::PopID(); ImGui::PopID();
return; return;
} }
} }
DrawOverlay( wpos + ImVec2( 0, oldOffset ), wpos + ImVec2( w, offset ) ); DrawOverlay( wpos + ImVec2( 0, yBegin ), wpos + ImVec2( w, yEnd ) );
ImGui::PopClipRect(); ImGui::PopClipRect();
float labelWidth; float labelWidth;
const auto hdrOffset = oldOffset; const auto hdrOffset = yBegin;
const bool drawHeader = yPos + ty >= yMin && yPos <= yMax; const bool drawHeader = yPos + ty >= yMin && yPos <= yMax;
if( drawHeader ) if( drawHeader )
{ {
@ -112,39 +114,38 @@ void TimelineItem::Draw( bool firstFrame, double pxns, int& offset, const ImVec2
ImGui::EndPopup(); ImGui::EndPopup();
} }
offset += 0.2f * ostep; yEnd += 0.2f * ostep;
AdjustThreadHeight( firstFrame, oldOffset, offset ); AdjustThreadHeight( firstFrame, yBegin, yEnd );
ImGui::PopID(); ImGui::PopID();
} }
void TimelineItem::AdjustThreadHeight( bool firstFrame, int oldOffset, int& offset ) void TimelineItem::AdjustThreadHeight( bool firstFrame, int yBegin, int yEnd )
{ {
const auto speed = 4.0; const auto speed = 4.0;
const auto baseMove = 1.0; const auto baseMove = 1.0;
const auto h = offset - oldOffset; const auto newHeight = yEnd - yBegin;
if( firstFrame ) if( firstFrame )
{ {
m_height = h; m_height = newHeight;
} }
else if( m_height != h ) else if( m_height != newHeight )
{ {
const auto diff = h - m_height; const auto diff = newHeight - m_height;
const auto preClampMove = diff * speed * ImGui::GetIO().DeltaTime; const auto preClampMove = diff * speed * ImGui::GetIO().DeltaTime;
if( diff > 0 ) if( diff > 0 )
{ {
const auto move = preClampMove + baseMove; const auto move = preClampMove + baseMove;
m_height = int( std::min<double>( m_height + move, h ) ); m_height = int( std::min<double>( m_height + move, newHeight ) );
} }
else else
{ {
const auto move = preClampMove - baseMove; const auto move = preClampMove - baseMove;
m_height = int( std::max<double>( m_height + move, h ) ); m_height = int( std::max<double>( m_height + move, newHeight ) );
} }
s_wasActive = true; s_wasActive = true;
} }
offset = oldOffset + m_height;
} }
void TimelineItem::VisibilityCheckbox() void TimelineItem::VisibilityCheckbox()

View File

@ -17,7 +17,8 @@ public:
TimelineItem( View& view, Worker& worker ); TimelineItem( View& view, Worker& worker );
virtual ~TimelineItem() = default; virtual ~TimelineItem() = default;
void Draw( bool firstFrame, double pxns, int& offset, const ImVec2& wpos, bool hover, float yMin, float yMax ); // draws the timeilne item and also updates the next frame height value
void Draw( bool firstFrame, double pxns, int yOffset, const ImVec2& wpos, bool hover, float yMin, float yMax );
void VisibilityCheckbox(); void VisibilityCheckbox();
virtual void SetVisible( bool visible ) { m_visible = visible; } virtual void SetVisible( bool visible ) { m_visible = visible; }
@ -25,6 +26,9 @@ public:
void SetShowFull( bool showFull ) { m_showFull = showFull; } void SetShowFull( bool showFull ) { m_showFull = showFull; }
// returns 0 instead of the correct value for the first frame
int GetNextFrameHeight() const { return m_height; }
protected: protected:
virtual uint32_t HeaderColor() const = 0; virtual uint32_t HeaderColor() const = 0;
virtual uint32_t HeaderColorInactive() const = 0; virtual uint32_t HeaderColorInactive() const = 0;
@ -46,7 +50,7 @@ protected:
bool m_showFull; bool m_showFull;
private: private:
void AdjustThreadHeight( bool firstFrame, int oldOffset, int& offset ); void AdjustThreadHeight( bool firstFrame, int yBegin, int yEnd );
int m_height; int m_height;

View File

@ -341,7 +341,6 @@ void View::DrawTimeline()
draw = ImGui::GetWindowDrawList(); draw = ImGui::GetWindowDrawList();
const auto ty = ImGui::GetTextLineHeight(); const auto ty = ImGui::GetTextLineHeight();
int offset = 0;
const auto to = 9.f; const auto to = 9.f;
const auto th = ( ty - to ) * sqrt( 3 ) * 0.5; const auto th = ( ty - to ) * sqrt( 3 ) * 0.5;
@ -381,7 +380,7 @@ void View::DrawTimeline()
} }
} }
m_tc.End( pxns, offset, wpos, hover, yMin, yMax ); m_tc.End( pxns, wpos, hover, yMin, yMax );
ImGui::EndChild(); ImGui::EndChild();
m_lockHighlight = m_nextLockHighlight; m_lockHighlight = m_nextLockHighlight;