CurrencyAlertClassic/KamiLib/Drawing/InfoBoxTable.cs

115 lines
2.9 KiB
C#
Raw Normal View History

2023-10-13 09:38:52 +00:00
using System.Collections.Generic;
using System.Numerics;
using ImGuiNET;
using KamiLib.Interfaces;
namespace KamiLib.Drawing;
public class InfoBoxTable
{
private readonly InfoBox owner;
private readonly float weight;
private readonly List<InfoBoxTableRow> rows = new();
private string emptyListString = string.Empty;
public InfoBoxTable(InfoBox owner, float weight = 0.5f)
{
this.owner = owner;
this.weight = weight;
}
public InfoBoxTableRow BeginRow()
{
return new InfoBoxTableRow(this);
}
public InfoBoxTable AddRow(InfoBoxTableRow row)
{
rows.Add(row);
return this;
}
public InfoBox EndTable()
{
owner.AddAction(() =>
{
if (rows.Count == 0)
{
if (emptyListString != string.Empty)
{
ImGui.TextColored(Colors.Orange, emptyListString);
}
}
else
{
if (ImGui.BeginTable($"", 2, ImGuiTableFlags.None, new Vector2(owner.InnerWidth, 0)))
{
ImGui.TableSetupColumn("", ImGuiTableColumnFlags.None, 1f * (weight));
ImGui.TableSetupColumn("", ImGuiTableColumnFlags.None, 1f * (1 - weight));
foreach (var row in rows)
{
ImGui.TableNextColumn();
ImGui.PushTextWrapPos(GetWrapPosition());
row.FirstColumn?.Invoke();
ImGui.PopTextWrapPos();
ImGui.TableNextColumn();
ImGui.PushTextWrapPos(GetWrapPosition());
row.SecondColumn?.Invoke();
ImGui.PopTextWrapPos();
}
ImGui.EndTable();
}
}
});
return owner;
}
private static float GetWrapPosition()
{
var region = ImGui.GetContentRegionAvail();
var cursor = ImGui.GetCursorPos();
var wrapPosition = cursor.X + region.X;
return wrapPosition;
}
public InfoBoxTable AddConfigurationRows(IEnumerable<IInfoBoxTableConfigurationRow> configurableRows, string? emptyEnumerableString = null)
{
if(emptyEnumerableString is not null)
{
emptyListString = emptyEnumerableString;
}
foreach (var row in configurableRows)
{
row.GetConfigurationRow(this);
}
return this;
}
public InfoBoxTable AddDataRows(IEnumerable<IInfoBoxTableDataRow> dataRows, string? emptyEnumerableString = null)
{
if(emptyEnumerableString is not null)
{
emptyListString = emptyEnumerableString;
}
foreach (var row in dataRows)
{
row.GetDataRow(this);
}
return this;
}
}