Работа над новым отображением сущностей
This commit is contained in:
@@ -1,16 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using System.Windows.Forms.VisualStyles;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
||||
using System.Data;
|
||||
using Pf2eViewer.EntityTreeViewer;
|
||||
|
||||
namespace Pf2eViewer;
|
||||
|
||||
@@ -20,6 +9,8 @@ public partial class EntityTreeViewerControl : UserControl
|
||||
|
||||
private EntityView? _entityView = null;
|
||||
|
||||
private int _depthSize = 20;
|
||||
|
||||
public int ItemHeight
|
||||
{
|
||||
get => _itemHeight;
|
||||
@@ -36,16 +27,62 @@ public partial class EntityTreeViewerControl : UserControl
|
||||
set
|
||||
{
|
||||
_entityView = value;
|
||||
UpdateData();
|
||||
}
|
||||
}
|
||||
|
||||
public int DepthSize
|
||||
{
|
||||
get => _depthSize;
|
||||
set
|
||||
{
|
||||
_depthSize = value;
|
||||
UpdateView();
|
||||
}
|
||||
}
|
||||
|
||||
public int NameWidth { get; private set; } = 200;
|
||||
|
||||
private readonly List<StructureField> Rows = [];
|
||||
|
||||
private List<EntityTreeViewerRow> ViewerRows = [];
|
||||
|
||||
public DarkModeCS? DarkMode;
|
||||
|
||||
public EntityTreeViewerControl()
|
||||
{
|
||||
InitializeComponent();
|
||||
DoubleBuffered = true;
|
||||
|
||||
ContentPanel.AutoScroll = false;
|
||||
ContentPanel.VerticalScroll.Enabled = true;
|
||||
ContentPanel.VerticalScroll.Visible = true;
|
||||
|
||||
ContentPanel.MouseWheel += ContentPanel_MouseWheel;
|
||||
}
|
||||
|
||||
public void UpdateView()
|
||||
private int _visibleRows;
|
||||
|
||||
public int ScrollPosition
|
||||
{
|
||||
get => ContentPanel.VerticalScroll.Value / ItemHeight;
|
||||
set
|
||||
{
|
||||
VScrollProperties scrollBar = ContentPanel.VerticalScroll;
|
||||
int oldValue = scrollBar.Value / ItemHeight;
|
||||
int visibleItems = scrollBar.LargeChange / ItemHeight;
|
||||
|
||||
int scrollValue = Math.Min(Math.Max(value, 0), _visibleRows - visibleItems);
|
||||
|
||||
if (oldValue != scrollValue)
|
||||
{
|
||||
ContentPanel.VerticalScroll.Value = scrollValue * ItemHeight;
|
||||
UpdateScroll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateData()
|
||||
{
|
||||
ContentPanel.Controls.Clear();
|
||||
|
||||
@@ -56,174 +93,96 @@ public partial class EntityTreeViewerControl : UserControl
|
||||
|
||||
Rows.Clear();
|
||||
|
||||
Rows.Add(new HeaderRow("Entity"));
|
||||
Rows.Add(new ObjectComplex(null, EntityView.Entity));
|
||||
Rows.Add(new HeaderRow(EntityView.SubEntity?.GetType().ToString() ?? "SubEntity"));
|
||||
Rows.Add(new ObjectComplex(null, EntityView.SubEntity));
|
||||
StructureBuilder builder = new();
|
||||
|
||||
List<DisplayRow> displayRows = Rows.SelectMany(r => r.GetTextRows()).ToList();
|
||||
Rows.Add(builder.Create("Entity", EntityView.Entity));
|
||||
|
||||
for (int i = 0; i < displayRows.Count; i++)
|
||||
if (EntityView.SubEntity is not null)
|
||||
{
|
||||
DisplayRow dr = displayRows[i];
|
||||
string typeName = EntityView.SubEntity.GetType().Name;
|
||||
|
||||
TextBox tb = new()
|
||||
{
|
||||
Top = i * ItemHeight,
|
||||
Left = 0,
|
||||
Width = ContentPanel.Width,
|
||||
Height = ItemHeight,
|
||||
Font = Font,
|
||||
Text = string.Join(
|
||||
"",
|
||||
[new(' ', dr.Depth * 2), dr.Name, dr.Value != "" ? " : " : "", dr.Value]
|
||||
),
|
||||
Margin = new(0),
|
||||
};
|
||||
|
||||
ContentPanel.Controls.Add(tb);
|
||||
Rows.Add(builder.Create(typeName, EntityView.SubEntity));
|
||||
}
|
||||
|
||||
UpdateRows();
|
||||
UpdateView();
|
||||
}
|
||||
|
||||
private Panel GetObjectPanel(object obj)
|
||||
public void UpdateRows()
|
||||
{
|
||||
Panel result = new();
|
||||
ViewerRows = Rows.SelectMany(r => r.GetTextRows(this)).ToList();
|
||||
|
||||
Type type = obj.GetType();
|
||||
ContentPanel.Controls.Clear();
|
||||
|
||||
PropertyInfo[] properties = type.GetProperties();
|
||||
ContentPanel.Controls.AddRange(ViewerRows.ToArray());
|
||||
|
||||
UpdateView();
|
||||
}
|
||||
|
||||
public void UpdateView()
|
||||
{
|
||||
foreach (EntityTreeViewerRow row in ViewerRows)
|
||||
{
|
||||
row.Visible = row.Item.Visible;
|
||||
}
|
||||
|
||||
List<EntityTreeViewerRow> rows = ViewerRows.Where(r => r.Item.Visible).ToList();
|
||||
|
||||
int maxNameWidth = -1;
|
||||
int viewRow = 0;
|
||||
|
||||
foreach (EntityTreeViewerRow row in rows)
|
||||
{
|
||||
row.ViewIndex = viewRow++;
|
||||
int nameWidth = row.GetNameWidth() + row.Item.Depth * DepthSize;
|
||||
maxNameWidth = Math.Max(maxNameWidth, nameWidth);
|
||||
}
|
||||
|
||||
NameWidth = maxNameWidth;
|
||||
|
||||
foreach (EntityTreeViewerRow row in rows)
|
||||
{
|
||||
row.Refresh();
|
||||
}
|
||||
|
||||
UpdateScroll();
|
||||
}
|
||||
|
||||
public void UpdateScroll()
|
||||
{
|
||||
List<EntityTreeViewerRow> rows = ViewerRows.Where(r => r.Item.Visible).ToList();
|
||||
_visibleRows = rows.Count;
|
||||
|
||||
ContentPanel.VerticalScroll.Enabled = true;
|
||||
ContentPanel.VerticalScroll.Visible = true;
|
||||
|
||||
ContentPanel.VerticalScroll.SmallChange = ItemHeight;
|
||||
ContentPanel.VerticalScroll.LargeChange = ContentPanel.Height;
|
||||
ContentPanel.VerticalScroll.Minimum = 0;
|
||||
ContentPanel.VerticalScroll.Maximum = _visibleRows * ItemHeight;
|
||||
|
||||
// ContentPanel.VerticalScroll.Value = VerticalScrollBar.Value * ItemHeight;
|
||||
/*
|
||||
FieldInfo[] fields = type.GetFields(
|
||||
BindingFlags.Instance
|
||||
| BindingFlags.Static
|
||||
| BindingFlags.NonPublic
|
||||
| BindingFlags.Public
|
||||
);
|
||||
ContentPanel.Top = -VerticalScrollBar.Value * ItemHeight;
|
||||
ContentPanel.Invalidate();
|
||||
*/
|
||||
for (int i = 0; i < properties.Length; i++)
|
||||
{
|
||||
PropertyInfo property = properties[i];
|
||||
|
||||
TextBox tb = new()
|
||||
{
|
||||
Top = i * ItemHeight,
|
||||
Left = 0,
|
||||
Width = ContentPanel.Width,
|
||||
Height = ItemHeight,
|
||||
Font = Font,
|
||||
Text = property.Name,
|
||||
Margin = new(0),
|
||||
};
|
||||
|
||||
result.Controls.Add(tb);
|
||||
}
|
||||
|
||||
result.Width = ContentPanel.Width;
|
||||
result.Height = properties.Length * ItemHeight * 2;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<Row> Rows = [];
|
||||
// Events
|
||||
|
||||
private class DisplayRow
|
||||
private void VerticalScrollBar_Scroll(object sender, ScrollEventArgs e)
|
||||
{
|
||||
public string Name = "";
|
||||
|
||||
public string Value = "";
|
||||
|
||||
public int Depth = 0;
|
||||
UpdateScroll();
|
||||
}
|
||||
|
||||
private abstract class Row
|
||||
private void ContentPanel_MouseWheel(object? sender, MouseEventArgs e)
|
||||
{
|
||||
public abstract ICollection<DisplayRow> GetTextRows();
|
||||
|
||||
public int Depth = 0;
|
||||
Focus();
|
||||
// ScrollPosition -= e.Delta / 120;
|
||||
}
|
||||
|
||||
private class HeaderRow(string name) : Row
|
||||
private void ContentPanel_Resize(object sender, EventArgs e)
|
||||
{
|
||||
public readonly string Name = name;
|
||||
|
||||
public override ICollection<DisplayRow> GetTextRows()
|
||||
{
|
||||
return [new() { Name = Name, Depth = Depth }];
|
||||
}
|
||||
}
|
||||
|
||||
private abstract class ObjectField(PropertyInfo property, object? parent) : Row
|
||||
{
|
||||
public readonly PropertyInfo? Property = property;
|
||||
|
||||
public readonly string Name = property.Name;
|
||||
|
||||
public readonly Type Type = property.PropertyType;
|
||||
|
||||
public abstract int Rows { get; }
|
||||
|
||||
public static ObjectField Resolve(PropertyInfo property, object? parent)
|
||||
{
|
||||
return property.PropertyType.IsPrimitive
|
||||
? new ObjectPrimitive(property, parent)
|
||||
: new ObjectComplex(property, parent);
|
||||
}
|
||||
}
|
||||
|
||||
private class ObjectPrimitive : ObjectField
|
||||
{
|
||||
public readonly object? Value;
|
||||
|
||||
public override int Rows => 1;
|
||||
|
||||
public ObjectPrimitive(PropertyInfo? property, object? parent)
|
||||
: base(property, parent)
|
||||
{
|
||||
Value = Property.GetValue(parent, null);
|
||||
}
|
||||
|
||||
public override ICollection<DisplayRow> GetTextRows()
|
||||
{
|
||||
return
|
||||
[
|
||||
new()
|
||||
{
|
||||
Name = Name,
|
||||
Value = Value?.ToString() ?? "null",
|
||||
Depth = Depth,
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
private class ObjectComplex : ObjectField
|
||||
{
|
||||
public readonly List<ObjectField> Fields;
|
||||
|
||||
public bool Collapsed = false;
|
||||
|
||||
public override int Rows => 1 + Fields.Select(f => f.Rows).Sum();
|
||||
|
||||
public ObjectComplex(PropertyInfo? property, object? parent)
|
||||
: base(property, parent)
|
||||
{
|
||||
PropertyInfo[] properties = Type.GetProperties();
|
||||
|
||||
object? value = Property?.GetValue(parent, null) ?? parent;
|
||||
|
||||
Fields = properties.Select(f => Resolve(f, value)).ToList();
|
||||
}
|
||||
|
||||
public override ICollection<DisplayRow> GetTextRows()
|
||||
{
|
||||
List<DisplayRow> rows = [new() { Name = Name, Depth = Depth }];
|
||||
|
||||
if (!Collapsed)
|
||||
{
|
||||
rows.AddRange(Fields.SelectMany(f => f.GetTextRows()).ToList());
|
||||
}
|
||||
|
||||
return rows;
|
||||
}
|
||||
UpdateView();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user