public void DrawDetailedLegend( Rect canvas, ref Vector2 scrollPos, int? max, bool positiveOnly = false, bool negativeOnly = false )
{
// set sign
int sign = negativeOnly ? -1 : 1;
List<Chapter> ChaptersOrdered = _chapters
.Where( chapter => !positiveOnly || chapter.pages[periodShown].Any( i => i > 0 ) )
.Where( chapter => !negativeOnly || chapter.pages[periodShown].Any( i => i < 0 ) )
.OrderByDescending( chapter => chapter.Last( periodShown ) * sign ).ToList();
// get out early if no chapters.
if( ChaptersOrdered.Count == 0 )
{
GUI.DrawTexture( canvas.ContractedBy( Utilities.Margin ), Resources.SlightlyDarkBackground );
Utilities.Label( canvas, "FM.HistoryNoChapters".Translate(), null, TextAnchor.MiddleCenter, color: Color.grey );
return;
}
// max
float _max = max ?? ( DrawMaxMarkers
? ChaptersOrdered.Max( chapter => (int)chapter.TrueMax )
: ChaptersOrdered.FirstOrDefault()?.Last( periodShown ) * sign )
?? 0;
// cell height
float height = 30f;
float barHeight = 18f;
// n rows
int n = ChaptersOrdered.Count;
// scrolling region
Rect viewRect = canvas;
viewRect.height = n * height;
if ( viewRect.height > canvas.height )
{
viewRect.width -= 16f + Utilities.Margin;
canvas.width -= Utilities.Margin;
canvas.height -= 1f;
}
Widgets.BeginScrollView( canvas, ref scrollPos, viewRect);
for ( int i = 0; i < n; i++ )
{
// set up rects
Rect row = new Rect( 0f, height * i, viewRect.width, height );
Rect icon = new Rect( Utilities.Margin, height * i, height, height ).ContractedBy( Utilities.Margin / 2f ); // icon is square, size defined by height.
Rect bar = new Rect( Utilities.Margin + height, height * i, viewRect.width - height - Utilities.Margin, height );
// if icons should not be drawn make the bar full size.
if (!DrawIcons) bar.xMin -= height + Utilities.Margin;
// bar details.
Rect barBox = bar.ContractedBy( ( height - barHeight ) / 2f );
Rect barFill = barBox.ContractedBy( 2f );
float maxWidth = barFill.width;
if ( MaxPerChapter )
{
barFill.width *= ChaptersOrdered[i].Last( periodShown ) * sign / (float)ChaptersOrdered[i].TrueMax;
}
else
{
barFill.width *= ChaptersOrdered[i].Last( periodShown ) * sign / _max;
}
GUI.BeginGroup( viewRect );
// if DrawIcons and a thing is set, draw the icon.
ThingDef thing = ChaptersOrdered[i].ThingCount.thingDef;
if( DrawIcons && thing != null )
{
// draw the icon in correct proportions
float proportion = GenUI.IconDrawScale( thing );
Widgets.DrawTextureFitted( icon, thing.uiIcon, proportion );
// draw counts in upper left corner
if ( DrawCounts )
{
Utilities.LabelOutline( icon, ChaptersOrdered[i].ThingCount.count.ToString(), null,
TextAnchor.UpperLeft, 0f, 0f, GameFont.Tiny, Color.white, Color.black );
}
}
// if desired, draw ghost bar
if( DrawMaxMarkers )
{
Rect ghostBarFill = barFill;
ghostBarFill.width = MaxPerChapter ? maxWidth : maxWidth * ( ChaptersOrdered[i].TrueMax / _max );
GUI.color = new Color( 1f, 1f, 1f, .2f );
GUI.DrawTexture( ghostBarFill, ChaptersOrdered[i].Texture ); // coloured texture
GUI.color = Color.white;
}
// draw the main bar.
GUI.DrawTexture( barBox, Resources.SlightlyDarkBackground );
GUI.DrawTexture( barFill, ChaptersOrdered[i].Texture ); // coloured texture
GUI.DrawTexture( barFill, Resources.BarShader ); // slightly fancy overlay (emboss).
// draw on bar info
if ( DrawInfoInBar )
{
string info = ChaptersOrdered[i].label + ": " +
FormatCount( ChaptersOrdered[i].Last( periodShown ) * sign );
if ( DrawMaxMarkers )
{
info += " / " + FormatCount( ChaptersOrdered[i].TrueMax );
}
// offset label a bit downwards and to the right
Rect rowInfoRect = row;
rowInfoRect.y += 3f;
rowInfoRect.x += Utilities.Margin * 2;
// x offset
float xOffset = DrawIcons && thing != null ? height + Utilities.Margin * 2 : Utilities.Margin * 2;
Utilities.LabelOutline( rowInfoRect, info, null, TextAnchor.MiddleLeft, xOffset, 0f, GameFont.Tiny, Color.white, Color.black );
}
// are we currently showing this line?
bool shown = _chaptersShown.Contains( ChaptersOrdered[i] );
// tooltip on entire row
string tooltip = ChaptersOrdered[i].label + ": " + FormatCount( Mathf.Abs( ChaptersOrdered[i].Last( periodShown ) ) );
tooltip += "FM.HistoryClickToEnable".Translate( shown ? "hide" : "show", ChaptersOrdered[i].label );
TooltipHandler.TipRegion( row, tooltip);
// handle input
if ( Widgets.ButtonInvisible( row ) )
{
if ( Event.current.button == 0 )
{
if ( shown )
{
_chaptersShown.Remove( ChaptersOrdered[i] );
}
else
{
_chaptersShown.Add( ChaptersOrdered[i] );
}
} else if ( Event.current.button == 1 )
{
_chaptersShown.Clear();
_chaptersShown.Add( ChaptersOrdered[i] );
}
}
// UI feedback for disabled row
if ( !shown )
{
GUI.DrawTexture( row, Resources.SlightlyDarkBackground );
}
GUI.EndGroup();
}
Widgets.EndScrollView();
}