150 lines
3.7 KiB
C#
150 lines
3.7 KiB
C#
using System.ComponentModel;
|
|
using Pf2eModel;
|
|
using Pf2eModel.Model.Base;
|
|
using Pf2eModel.Model.Common.I18n;
|
|
using Pf2eModel.Model.Enum;
|
|
|
|
namespace Pf2eViewer;
|
|
|
|
public partial class DatabaseViewerControl : UserControl
|
|
{
|
|
public readonly Pf2eDbContext Context = new();
|
|
|
|
private int _perPage = 30;
|
|
|
|
private int _page = 1;
|
|
|
|
private EntityType _type;
|
|
|
|
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
|
|
public int PerPage
|
|
{
|
|
get => _perPage;
|
|
set
|
|
{
|
|
_perPage = value;
|
|
UpdateView();
|
|
}
|
|
}
|
|
|
|
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
|
|
public int Page
|
|
{
|
|
get => _page;
|
|
set
|
|
{
|
|
_page = value;
|
|
UpdateView();
|
|
}
|
|
}
|
|
|
|
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
|
|
public int Count { get; private set; }
|
|
|
|
public int MaxPage => Convert.ToInt32(Math.Ceiling(1.0 * Count / PerPage));
|
|
|
|
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
|
|
public EntityView CurrentEntity { get; private set; } = null!;
|
|
|
|
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
|
|
public EntityType Type
|
|
{
|
|
get => _type;
|
|
set
|
|
{
|
|
_type = value;
|
|
UpdateView();
|
|
}
|
|
}
|
|
|
|
private bool _updating = false;
|
|
|
|
public DatabaseViewerControl()
|
|
{
|
|
InitializeComponent();
|
|
|
|
BindingNavigator.BindingSource = DbBindingSource;
|
|
DataGridView.AutoGenerateColumns = true;
|
|
DataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
|
}
|
|
|
|
private void DatabaseViewerControl_Load(object sender, EventArgs e)
|
|
{
|
|
UpdateView();
|
|
}
|
|
|
|
private void BindingNavigator_RefreshItems(object sender, EventArgs e)
|
|
{
|
|
if (_updating)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Page = (int)(BindingNavigator?.BindingSource?.Current ?? 1);
|
|
UpdateView();
|
|
}
|
|
|
|
public void UpdateView()
|
|
{
|
|
_updating = true;
|
|
|
|
IQueryable<Entity> query = Context.Entities.Where(e =>
|
|
e.Type == Type
|
|
&& (
|
|
// ReSharper disable once EntityFramework.UnsupportedServerSideFunctionCall
|
|
SearchTextBox.Text == ""
|
|
|| Pf2eDbContext.LocaleSearch(e.Name, SearchTextBox.Text)
|
|
)
|
|
);
|
|
|
|
Count = query.Count();
|
|
|
|
PagesBindingSource.DataSource = Enumerable.Range(1, MaxPage).ToList();
|
|
BindingNavigator.BindingSource = PagesBindingSource;
|
|
|
|
DbBindingSource.DataSource = query
|
|
.Skip((Page - 1) * PerPage)
|
|
.Take(PerPage)
|
|
.Select(e => new EntityGridRow(e))
|
|
.ToList();
|
|
|
|
if (DataGridView.Rows.Count > 0)
|
|
{
|
|
DataGridView.Rows[0].Selected = true;
|
|
}
|
|
DataGridView.Update();
|
|
|
|
_updating = false;
|
|
}
|
|
|
|
private void DataGridView_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
|
|
{
|
|
if (DataGridView.SelectedRows.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
EntityGridRow data = (EntityGridRow)DataGridView.SelectedRows[0].DataBoundItem;
|
|
CurrentEntity = new(Context, Type, data.Id);
|
|
|
|
EntityChange?.Invoke(CurrentEntity);
|
|
}
|
|
|
|
public delegate void EntityChangeHandler(EntityView newEntity);
|
|
|
|
public event EntityChangeHandler? EntityChange;
|
|
|
|
internal class EntityGridRow(Entity e)
|
|
{
|
|
public int Id { get; set; } = e.Id;
|
|
|
|
public string Name { get; set; } = e.Name[LocaleType.English];
|
|
|
|
public EntityRarity Rarity { get; set; } = e.Rarity;
|
|
|
|
private readonly Entity _e = e;
|
|
|
|
public Entity GetEntity() => _e;
|
|
}
|
|
}
|