@@ -220,11 +220,12 @@ void View::DrawThreadOverlays( const ThreadData& thread, const ImVec2& ul, const
220220 }
221221}
222222
223- void View::DrawZoneList ( const TimelineContext& ctx, const std::vector<TimelineDraw>& drawList, int _offset, uint64_t tid, const int maxDepth, const double margin )
223+
224+ void View::DrawZoneList ( const TimelineContext& ctx, const std::vector<TimelineDraw>& drawList, int _offset, uint64_t tid, int maxDepth, double margin )
224225{
225226 auto draw = ImGui::GetWindowDrawList ();
226227 const auto w = ctx.w ;
227- const auto wpos = ctx.wpos + ImVec2 ( margin, 0 . f ) ;
228+ const auto wpos = ctx.wpos ;
228229 const auto dpos = wpos + ImVec2 ( 0 .5f , 0 .5f );
229230 const auto ty = ctx.ty ;
230231 const auto ostep = ty + 1 ;
@@ -233,6 +234,39 @@ void View::DrawZoneList( const TimelineContext& ctx, const std::vector<TimelineD
233234 const auto pxns = ctx.pxns ;
234235 const auto hover = ctx.hover ;
235236 const auto vStart = ctx.vStart ;
237+
238+ const auto DrawZoneText = [&]( uint32_t color, const char * zoneName, ImVec2 tsz, double pr0, double pr1, double px0, double px1, double offset ){
239+ // pr0 and pr1 are the real locations of the zone start/end
240+ // px0 and px1 are the rendered locations of the zone (taking into account minsize and window clamping)
241+ const auto tpx0 = std::max ( px0, margin );
242+ const auto zsz = std::max ( pr1 - pr0, pxns * 0.5 );
243+ if ( tsz.x < zsz )
244+ {
245+ // Zone is big enough to contain text, attempt to draw text centered
246+ const auto x = pr0 + ( pr1 - pr0 - tsz.x ) / 2 ;
247+ if ( x < margin || x > w - tsz.x ) // Would draw outside of the window, align to border.
248+ {
249+ ImGui::PushClipRect ( wpos + ImVec2 ( tpx0, offset ), wpos + ImVec2 ( px1, offset + tsz.y * 2 ), true );
250+ DrawTextContrast ( draw, wpos + ImVec2 ( std::max ( tpx0, std::min ( double ( w - tsz.x ), x ) ), offset ), color, zoneName );
251+ ImGui::PopClipRect ();
252+ }
253+ else if ( pr1 == pr0 ) // Fits inside pxns * 0.5 => Use zone center.
254+ {
255+ DrawTextContrast ( draw, wpos + ImVec2 ( px0 + ( px1 - px0 - tsz.x ) * 0.5 , offset ), color, zoneName );
256+ }
257+ else // Draw at the center of the zone.
258+ {
259+ DrawTextContrast ( draw, wpos + ImVec2 ( x, offset ), color, zoneName );
260+ }
261+ }
262+ else
263+ {
264+ // Draw clipped since zone is too small to contain the text.
265+ ImGui::PushClipRect ( wpos + ImVec2 ( tpx0, offset ), wpos + ImVec2 ( px1, offset + tsz.y * 2 ), true );
266+ DrawTextContrast ( draw, wpos + ImVec2 ( tpx0, offset ), color, zoneName );
267+ ImGui::PopClipRect ();
268+ }
269+ };
236270
237271 for ( auto & v : drawList )
238272 {
@@ -295,9 +329,10 @@ void View::DrawZoneList( const TimelineContext& ctx, const std::vector<TimelineD
295329 }
296330 const auto tmp = RealToString ( v.num );
297331 const auto tsz = ImGui::CalcTextSize ( tmp );
298- if ( tsz.x < px1 - px0 )
332+ const auto tpx0 = std::max ( px0, margin );
333+ if ( tsz.x < px1 - tpx0)
299334 {
300- const auto x = px0 + ( px1 - px0 - tsz.x ) / 2 ;
335+ const auto x = tpx0 + ( px1 - tpx0 - tsz.x ) / 2 ;
301336 DrawTextContrast ( draw, wpos + ImVec2 ( x, offset ), 0xFF4488DD , tmp );
302337 }
303338 break ;
@@ -306,7 +341,10 @@ void View::DrawZoneList( const TimelineContext& ctx, const std::vector<TimelineD
306341 {
307342 auto & ev = *(const ZoneEvent*)v.ev .get ();
308343 const auto end = m_worker.GetZoneEnd ( ev );
309- const auto zsz = std::max ( ( end - ev.Start () ) * pxns, pxns * 0.5 );
344+ const auto pr0 = ( ev.Start () - vStart ) * pxns;
345+ const auto pr1 = ( end - vStart ) * pxns;
346+ const auto zsz = std::max ( pr1 - pr0, pxns * 0.5 );
347+
310348 const auto zoneColor = GetZoneColorData ( ev, tid, v.depth , v.inheritedColor );
311349 const char * zoneName = m_worker.GetZoneName ( ev );
312350
@@ -316,8 +354,6 @@ void View::DrawZoneList( const TimelineContext& ctx, const std::vector<TimelineD
316354 zoneName = ShortenZoneName ( m_vd.shortenName , zoneName, tsz, zsz );
317355 }
318356
319- const auto pr0 = ( ev.Start () - m_vd.zvStart ) * pxns;
320- const auto pr1 = ( end - m_vd.zvStart ) * pxns;
321357 const auto px0 = std::max ( pr0, -10.0 );
322358 const auto px1 = std::max ( { std::min ( pr1, double ( w + 10 ) ), px0 + pxns * 0.5 , px0 + MinVisSize } );
323359 draw->AddRectFilled ( wpos + ImVec2 ( px0, offset ), wpos + ImVec2 ( px1, offset + tsz.y ), zoneColor.color );
@@ -338,30 +374,7 @@ void View::DrawZoneList( const TimelineContext& ctx, const std::vector<TimelineD
338374 DrawLine ( draw, dpos + ImVec2 ( px0, offset + tsz.y ), dpos + ImVec2 ( px0, offset ), dpos + ImVec2 ( px1-1 , offset ), zoneColor.accentColor , zoneColor.thickness );
339375 DrawLine ( draw, dpos + ImVec2 ( px0, offset + tsz.y ), dpos + ImVec2 ( px1-1 , offset + tsz.y ), dpos + ImVec2 ( px1-1 , offset ), darkColor, zoneColor.thickness );
340376 }
341- if ( tsz.x < zsz )
342- {
343- const auto x = ( ev.Start () - m_vd.zvStart ) * pxns + ( ( end - ev.Start () ) * pxns - tsz.x ) / 2 ;
344- if ( x < 0 || x > w - tsz.x )
345- {
346- ImGui::PushClipRect ( wpos + ImVec2 ( px0, offset ), wpos + ImVec2 ( px1, offset + tsz.y * 2 ), true );
347- DrawTextContrast ( draw, wpos + ImVec2 ( std::max ( std::max ( 0 ., px0 ), std::min ( double ( w - tsz.x ), x ) ), offset ), 0xFFFFFFFF , zoneName );
348- ImGui::PopClipRect ();
349- }
350- else if ( ev.Start () == ev.End () )
351- {
352- DrawTextContrast ( draw, wpos + ImVec2 ( px0 + ( px1 - px0 - tsz.x ) * 0.5 , offset ), 0xFFFFFFFF , zoneName );
353- }
354- else
355- {
356- DrawTextContrast ( draw, wpos + ImVec2 ( x, offset ), 0xFFFFFFFF , zoneName );
357- }
358- }
359- else
360- {
361- ImGui::PushClipRect ( wpos + ImVec2 ( px0, offset ), wpos + ImVec2 ( px1, offset + tsz.y * 2 ), true );
362- DrawTextContrast ( draw, wpos + ImVec2 ( std::max ( int64_t ( 0 ), ev.Start () - m_vd.zvStart ) * pxns, offset ), 0xFFFFFFFF , zoneName );
363- ImGui::PopClipRect ();
364- }
377+ DrawZoneText ( 0xFFFFFFFF , zoneName, tsz, pr0, pr1, px0, px1, offset );
365378
366379 if ( hover && ImGui::IsMouseHoveringRect ( wpos + ImVec2 ( px0, offset ), wpos + ImVec2 ( px1, offset + tsz.y + 1 ) ) )
367380 {
@@ -460,30 +473,7 @@ void View::DrawZoneList( const TimelineContext& ctx, const std::vector<TimelineD
460473 DrawLine ( draw, dpos + ImVec2 ( px0, offset + tsz.y ), dpos + ImVec2 ( px0, offset ), dpos + ImVec2 ( px1-1 , offset ), accentColor, 1 .f );
461474 DrawLine ( draw, dpos + ImVec2 ( px0, offset + tsz.y ), dpos + ImVec2 ( px1-1 , offset + tsz.y ), dpos + ImVec2 ( px1-1 , offset ), darkColor, 1 .f );
462475
463- if ( tsz.x < zsz )
464- {
465- const auto x = ( ev.start .Val () - m_vd.zvStart ) * pxns + ( ( end - ev.start .Val () ) * pxns - tsz.x ) / 2 ;
466- if ( x < 0 || x > w - tsz.x )
467- {
468- ImGui::PushClipRect ( wpos + ImVec2 ( px0, offset ), wpos + ImVec2 ( px1, offset + tsz.y * 2 ), true );
469- DrawTextContrast ( draw, wpos + ImVec2 ( std::max ( std::max ( 0 ., px0 ), std::min ( double ( w - tsz.x ), x ) ), offset ), txtColor, symName );
470- ImGui::PopClipRect ();
471- }
472- else if ( ev.start .Val () == ev.end .Val () )
473- {
474- DrawTextContrast ( draw, wpos + ImVec2 ( px0 + ( px1 - px0 - tsz.x ) * 0.5 , offset ), txtColor, symName );
475- }
476- else
477- {
478- DrawTextContrast ( draw, wpos + ImVec2 ( x, offset ), txtColor, symName );
479- }
480- }
481- else
482- {
483- ImGui::PushClipRect ( wpos + ImVec2 ( px0, offset ), wpos + ImVec2 ( px1, offset + tsz.y * 2 ), true );
484- DrawTextContrast ( draw, wpos + ImVec2 ( ( ev.start .Val () - m_vd.zvStart ) * pxns, offset ), txtColor, symName );
485- ImGui::PopClipRect ();
486- }
476+ DrawZoneText ( txtColor, symName, tsz, pr0, pr1, px0, px1, offset );
487477
488478 if ( hover && ImGui::IsMouseHoveringRect ( wpos + ImVec2 ( px0, offset ), wpos + ImVec2 ( px1, offset + tsz.y + 1 ) ) )
489479 {
@@ -542,30 +532,7 @@ void View::DrawZoneList( const TimelineContext& ctx, const std::vector<TimelineD
542532 symName = ShortenZoneName ( m_vd.shortenName , symName, tsz, zsz );
543533 }
544534
545- if ( tsz.x < zsz )
546- {
547- const auto x = ( ev.start .Val () - m_vd.zvStart ) * pxns + ( ( end - ev.start .Val () ) * pxns - tsz.x ) / 2 ;
548- if ( x < 0 || x > w - tsz.x )
549- {
550- ImGui::PushClipRect ( wpos + ImVec2 ( px0, offset ), wpos + ImVec2 ( px1, offset + tsz.y * 2 ), true );
551- DrawTextContrast ( draw, wpos + ImVec2 ( std::max ( std::max ( 0 ., px0 ), std::min ( double ( w - tsz.x ), x ) ), offset ), txtColor, symName );
552- ImGui::PopClipRect ();
553- }
554- else if ( ev.start .Val () == ev.end .Val () )
555- {
556- DrawTextContrast ( draw, wpos + ImVec2 ( px0 + ( px1 - px0 - tsz.x ) * 0.5 , offset ), txtColor, symName );
557- }
558- else
559- {
560- DrawTextContrast ( draw, wpos + ImVec2 ( x, offset ), txtColor, symName );
561- }
562- }
563- else
564- {
565- ImGui::PushClipRect ( wpos + ImVec2 ( px0, offset ), wpos + ImVec2 ( px1, offset + tsz.y * 2 ), true );
566- DrawTextContrast ( draw, wpos + ImVec2 ( std::max ( int64_t ( 0 ), ev.start .Val () - m_vd.zvStart ) * pxns, offset ), txtColor, symName );
567- ImGui::PopClipRect ();
568- }
535+ DrawZoneText ( txtColor, symName, tsz, pr0, pr1, px0, px1, offset );
569536
570537 if ( hover && ImGui::IsMouseHoveringRect ( wpos + ImVec2 ( px0, offset ), wpos + ImVec2 ( px1, offset + tsz.y + 1 ) ) )
571538 {
0 commit comments