Обновление отображения дерева

This commit is contained in:
2025-04-30 16:10:58 +04:00
parent e856f91f9e
commit fda74b661a
4 changed files with 249 additions and 150 deletions
@@ -1,9 +1,9 @@
using FontAwesome.Sharp;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Header;
using System.Diagnostics.CodeAnalysis;
using FontAwesome.Sharp;
namespace Pf2eViewer.EntityTreeViewer;
internal partial class EntityTreeViewerRow : UserControl
internal sealed partial class EntityTreeViewerRow : UserControl
{
public readonly EntityTreeViewerControl ParentControl;
@@ -11,12 +11,66 @@ internal partial class EntityTreeViewerRow : UserControl
public int ViewIndex = 0;
public Bitmap IconCache
{
get
{
if (_iconCache == null)
{
UpdateCache();
}
return _iconCache!;
}
set => _iconCache = value;
}
private Bitmap? _iconCache;
public List<LadderType> Ladder
{
get => _ladder;
set
{
_ladder = value;
_iconCache = null;
}
}
private List<LadderType> _ladder = [];
public override Font Font
{
get => ParentControl.Font;
set => ParentControl.Font = value;
}
public override Color ForeColor
{
get => ParentControl.ForeColor;
set => ParentControl.ForeColor = value;
}
public override Color BackColor
{
get => ParentControl.BackColor;
set => ParentControl.BackColor = value;
}
public int ItemHeight
{
get => ParentControl.ItemHeight;
set => ParentControl.ItemHeight = value;
}
public int DepthSize
{
get => ParentControl.ItemHeight;
set => ParentControl.ItemHeight = value;
}
// Value functions
public bool HasButton => Item.FieldType == StructureFieldType.Complex;
public int Depth => Item.Depth;
public int NameWidth => TextRenderer.MeasureText(NameLabel.Text, Font).Width + DepthSize;
public EntityTreeViewerRow(EntityTreeViewerControl parent, StructureField item)
{
ParentControl = parent;
Font = ParentControl.Font;
Item = item;
@@ -27,52 +81,25 @@ internal partial class EntityTreeViewerRow : UserControl
private void ApplyItem()
{
TypeLabel.Font = new Font(Font.FontFamily, Font.Size - 2, FontStyle.Italic);
TypeLabel.Font = new(Font.FontFamily, Font.Size - 2, FontStyle.Italic);
NameLabel.Text = Item.Name;
TypeLabel.Text = Item.GetTypeString();
if (TypeLabel.Text != "")
{
TypeLabel.Text = $"({TypeLabel.Text})";
TypeLabel.Text = $@"({TypeLabel.Text})";
}
ValueLabel.Text = Item.GetValueString();
TypeLabel.ForeColor = ValueLabel.ForeColor = GetValueColor();
switch (Item)
if (Item.FieldType == StructureFieldType.Complex)
{
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;
ValueLabel.Font = new(Font, FontStyle.Italic);
}
}
public int GetNameWidth()
{
Size textSize = TextRenderer.MeasureText(NameLabel.Text, Font);
return textSize.Width + ParentControl.DepthSize;
UpdateCache();
Refresh();
}
public new void Refresh()
@@ -85,10 +112,14 @@ internal partial class EntityTreeViewerRow : UserControl
Height = ParentControl.ItemHeight;
// Name Panel
NamePanel.Left = Depth * ParentControl.DepthSize;
NamePanel.Left = 0;
NamePanel.Width = ParentControl.NameWidth - NamePanel.Left;
IconPictureBox.Width = ParentControl.ItemHeight;
IconPictureBox.Width = IconCache.Width;
IconPictureBox.Image = IconCache;
NameLabel.Left = IconPictureBox.Width;
NameLabel.Width = NamePanel.Width - NameLabel.Left;
// Value Panel
ValuePanel.Left = NamePanel.Left + NamePanel.Width;
@@ -96,63 +127,133 @@ internal partial class EntityTreeViewerRow : UserControl
Size typeSize = TextRenderer.MeasureText(TypeLabel.Text, TypeLabel.Font);
TypeLabel.Width = typeSize.Width + 3;
}
TypeLabel.ForeColor = ValueLabel.ForeColor = GetValueColor();
private Color BlendColor(Color color, double rate = 0.5)
{
return BlendColor(ForeColor, color, rate);
}
if (Item.GetType() == typeof(StructureFieldComplex))
{
StructureFieldComplex complex = (StructureFieldComplex)Item;
private Color BlendColor(Color color1, Color color2, double rate)
{
return Color.FromArgb(
Mix(color1.A, color2.A),
Mix(color1.R, color2.R),
Mix(color1.G, color2.G),
Mix(color1.B, color2.B)
);
IconPictureBox.Image = complex.Collapsed
? IconChar.PlusSquare.ToBitmap(IconFont.Auto, ParentControl.DepthSize, ForeColor)
: IconChar.MinusSquare.ToBitmap(IconFont.Auto, ParentControl.DepthSize, ForeColor);
}
int Mix(int a, int b) => Convert.ToInt32(Math.Round(a * rate + b * (1 - rate)));
}
public Color GetValueColor()
{
Func<Color, Color> blend = (Color color) => _colorBlend([ForeColor, color]);
switch (Item)
return Item switch
{
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;
StructureFieldPrimitive primitive => Type.GetTypeCode(primitive.Type) switch
{
TypeCode.Boolean => BlendColor(Color.DarkGoldenrod),
TypeCode.Int32 => BlendColor(Color.Blue),
TypeCode.Double => BlendColor(Color.Aqua),
TypeCode.String => BlendColor(Color.Green),
_ => ForeColor,
},
StructureFieldEnum => BlendColor(Color.Red),
_ => 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);
}
// Events
private void IconPictureBox_Click(object sender, EventArgs e)
{
if (Item.GetType() == typeof(StructureFieldComplex))
if (Item.GetType() != typeof(StructureFieldComplex))
{
StructureFieldComplex complex = (StructureFieldComplex)Item;
complex.Collapsed = !complex.Collapsed;
ParentControl.UpdateView();
return;
}
StructureFieldComplex complex = (StructureFieldComplex)Item;
complex.Collapsed = !complex.Collapsed;
ParentControl.UpdateView();
}
private Bitmap? GetIconImage()
{
if (Item.GetType() != typeof(StructureFieldComplex))
{
return null;
}
StructureFieldComplex complex = (StructureFieldComplex)Item;
int size = Convert.ToInt32(Math.Round(DepthSize * 0.6));
return complex.Collapsed
? IconChar.PlusSquare.ToBitmap(IconFont.Auto, size, ForeColor)
: IconChar.MinusSquare.ToBitmap(IconFont.Auto, size, ForeColor);
}
private void UpdateCache()
{
int n = Depth;
int h = ItemHeight;
int w = DepthSize;
_iconCache = new(w * (n + 1), h);
if (_ladder.Count < Depth)
{
return;
}
Graphics g = Graphics.FromImage(_iconCache);
Pen pen = new(Color.FromArgb(127, ForeColor.R, ForeColor.G, ForeColor.B), 1);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
float cy = 0.5f * h;
for (int i = 0; i < n; i++)
{
float cx = (i + 1.5f) * w;
switch (_ladder[i])
{
case LadderType.Last:
g.DrawLine(pen, cx, 0, cx, cy);
break;
case LadderType.Through:
g.DrawLine(pen, cx, 0, cx, h);
break;
case LadderType.Empty:
default:
break;
}
if (i == n - 1)
{
g.DrawLine(pen, cx, cy, (i + 2) * w, cy);
}
}
Bitmap? icon = GetIconImage();
if (icon is not null)
{
float cx = (n + 0.5f) * w;
g.FillRectangle(
new SolidBrush(BackColor),
cx - 0.5f * icon.Width - 1,
cy - 0.5f * icon.Height - 1,
icon.Width + 2,
icon.Height + 2
);
g.DrawImage(icon, new PointF(cx - 0.5f * icon.Width, cy - 0.5f * icon.Height));
}
g.Save();
}
}