Files
Pf2eDataScrapper/Pf2eViewer/EntityTreeViewer/EntityTreeViewerRow.cs
T

159 lines
4.6 KiB
C#

using FontAwesome.Sharp;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Header;
namespace Pf2eViewer.EntityTreeViewer;
internal partial class EntityTreeViewerRow : UserControl
{
public readonly EntityTreeViewerControl ParentControl;
public readonly StructureField Item;
public int ViewIndex = 0;
public int Depth => Item.Depth;
public EntityTreeViewerRow(EntityTreeViewerControl parent, StructureField item)
{
ParentControl = parent;
Font = ParentControl.Font;
Item = item;
InitializeComponent();
ApplyItem();
Refresh();
}
private void ApplyItem()
{
TypeLabel.Font = new Font(Font.FontFamily, Font.Size - 2, FontStyle.Italic);
NameLabel.Text = Item.Name;
TypeLabel.Text = Item.GetTypeString();
if (TypeLabel.Text != "")
{
TypeLabel.Text = $"({TypeLabel.Text})";
}
ValueLabel.Text = Item.GetValueString();
switch (Item)
{
case StructureFieldPrimitive primitive:
switch (Type.GetTypeCode(primitive.Type))
{
case TypeCode.Boolean:
ValuePanel.ForeColor = Color.Red;
break;
case TypeCode.Int32:
ValuePanel.ForeColor = Color.Blue;
break;
case TypeCode.String:
ValuePanel.ForeColor = Color.OrangeRed;
break;
}
ValueLabel.ForeColor = ValuePanel.ForeColor;
TypeLabel.ForeColor = ValuePanel.ForeColor;
break;
case StructureFieldComplex complex:
ValueLabel.Font = new(Font, FontStyle.Italic);
break;
case StructureFieldNull @null:
break;
case StructureFieldReference reference:
break;
}
}
public int GetNameWidth()
{
Size textSize = TextRenderer.MeasureText(NameLabel.Text, Font);
return textSize.Width + ParentControl.DepthSize;
}
public new void Refresh()
{
base.Refresh();
Left = 0;
Top = ViewIndex * ParentControl.ItemHeight;
Width = ParentControl.ContentPanel.Width - Left - SystemInformation.VerticalScrollBarWidth;
Height = ParentControl.ItemHeight;
// Name Panel
NamePanel.Left = Depth * ParentControl.DepthSize;
NamePanel.Width = ParentControl.NameWidth - NamePanel.Left;
IconPictureBox.Width = ParentControl.ItemHeight;
// Value Panel
ValuePanel.Left = NamePanel.Left + NamePanel.Width;
ValuePanel.Width = Width - ValuePanel.Left;
Size typeSize = TextRenderer.MeasureText(TypeLabel.Text, TypeLabel.Font);
TypeLabel.Width = typeSize.Width + 3;
TypeLabel.ForeColor = ValueLabel.ForeColor = GetValueColor();
if (Item.GetType() == typeof(StructureFieldComplex))
{
StructureFieldComplex complex = (StructureFieldComplex)Item;
IconPictureBox.Image = complex.Collapsed
? IconChar.PlusSquare.ToBitmap(IconFont.Auto, ParentControl.DepthSize, ForeColor)
: IconChar.MinusSquare.ToBitmap(IconFont.Auto, ParentControl.DepthSize, ForeColor);
}
}
public Color GetValueColor()
{
Func<Color, Color> blend = (Color color) => _colorBlend([ForeColor, color]);
switch (Item)
{
case StructureFieldPrimitive primitive:
return Type.GetTypeCode(primitive.Type) switch
{
TypeCode.Boolean => blend(Color.DarkGoldenrod),
TypeCode.Int32 => blend(Color.Blue),
TypeCode.Double => blend(Color.Aqua),
TypeCode.String => blend(Color.Green),
};
case StructureFieldEnum @enum:
return blend(Color.Red);
}
return ForeColor;
}
private Color _colorBlend(List<Color> clrArr)
{
int r = 0;
int g = 0;
int b = 0;
foreach (Color color in clrArr)
{
r += color.R;
g += color.G;
b += color.B;
}
return Color.FromArgb(r / clrArr.Count, g / clrArr.Count, b / clrArr.Count);
}
private void IconPictureBox_Click(object sender, EventArgs e)
{
if (Item.GetType() == typeof(StructureFieldComplex))
{
StructureFieldComplex complex = (StructureFieldComplex)Item;
complex.Collapsed = !complex.Collapsed;
ParentControl.UpdateView();
}
}
}