cleanup input roll column and cell classes

This commit is contained in:
adelikat 2019-10-26 17:06:42 -05:00
parent b54be19e9e
commit 0ce5046061
3 changed files with 114 additions and 140 deletions

View File

@ -22,11 +22,11 @@ namespace BizHawk.Client.EmuHawk
public bool IsDataCell => Column != null && RowIndex.HasValue; public bool IsDataCell => Column != null && RowIndex.HasValue;
public override bool Equals(object obj) public override bool Equals(object obj)
{
if (obj is Cell)
{ {
var cell = obj as Cell; var cell = obj as Cell;
return this.Column == cell.Column && this.RowIndex == cell.RowIndex; if (cell != null)
{
return Column == cell.Column && RowIndex == cell.RowIndex;
} }
return base.Equals(obj); return base.Equals(obj);
@ -40,21 +40,31 @@ namespace BizHawk.Client.EmuHawk
internal class SortCell : IComparer<Cell> internal class SortCell : IComparer<Cell>
{ {
int IComparer<Cell>.Compare(Cell a, Cell b) int IComparer<Cell>.Compare(Cell c1, Cell c2)
{ {
Cell c1 = a as Cell; if (c1 == null && c2 == null)
Cell c2 = b as Cell; {
return 0;
}
if (c2 == null)
{
return 1;
}
if (c1 == null)
{
return -1;
}
if (c1.RowIndex.HasValue) if (c1.RowIndex.HasValue)
{ {
if (c2.RowIndex.HasValue) if (c2.RowIndex.HasValue)
{ {
int row = c1.RowIndex.Value.CompareTo(c2.RowIndex.Value); int row = c1.RowIndex.Value.CompareTo(c2.RowIndex.Value);
if (row == 0) return row == 0
{ ? c1.Column.Name.CompareTo(c2.Column.Name)
return c1.Column.Name.CompareTo(c2.Column.Name); : row;
}
return row;
} }
return 1; return 1;

View File

@ -9,21 +9,11 @@
public string Name { get; set; } public string Name { get; set; }
public string Text { get; set; } public string Text { get; set; }
public ColumnType Type { get; set; } public ColumnType Type { get; set; }
public bool Visible { get; set; } public bool Visible { get; set; } = true;
/// <summary> /// <summary>
/// Column will be drawn with an emphasized look, if true /// Column will be drawn with an emphasized look, if true
/// </summary> /// </summary>
private bool _emphasis; public bool Emphasis { get; set; }
public bool Emphasis
{
get { return _emphasis; }
set { _emphasis = value; }
}
public RollColumn()
{
Visible = true;
}
} }
} }

View File

@ -6,37 +6,12 @@ namespace BizHawk.Client.EmuHawk
{ {
public class RollColumns : List<RollColumn> public class RollColumns : List<RollColumn>
{ {
public RollColumn this[string name] public RollColumn this[string name] => this.SingleOrDefault(column => column.Name == name);
{
get
{
return this.SingleOrDefault(column => column.Name == name);
}
}
public IEnumerable<RollColumn> VisibleColumns public IEnumerable<RollColumn> VisibleColumns => this.Where(c => c.Visible);
{
get
{
return this.Where(c => c.Visible);
}
}
public Action ChangedCallback { get; set; } public Action ChangedCallback { get; set; }
private void DoChangeCallback()
{
// no check will make it crash for user too, not sure which way of alarm we prefer. no alarm at all will cause all sorts of subtle bugs
if (ChangedCallback == null)
{
System.Diagnostics.Debug.Fail($"{nameof(ChangedCallback)} has died!");
}
else
{
ChangedCallback();
}
}
// TODO: this shouldn't be exposed. But in order to not expose it, each RollColumn must have a change callback, and all property changes must call it, it is quicker and easier to just call this when needed // TODO: this shouldn't be exposed. But in order to not expose it, each RollColumn must have a change callback, and all property changes must call it, it is quicker and easier to just call this when needed
public void ColumnsChanged() public void ColumnsChanged()
{ {
@ -45,11 +20,11 @@ namespace BizHawk.Client.EmuHawk
foreach (var col in VisibleColumns) foreach (var col in VisibleColumns)
{ {
col.Left = pos; col.Left = pos;
pos += col.Width.Value; pos += col.Width ?? 0;
col.Right = pos; col.Right = pos;
} }
DoChangeCallback(); ChangedCallback?.Invoke();
} }
public new void Add(RollColumn column) public new void Add(RollColumn column)
@ -67,7 +42,8 @@ namespace BizHawk.Client.EmuHawk
public new void AddRange(IEnumerable<RollColumn> collection) public new void AddRange(IEnumerable<RollColumn> collection)
{ {
foreach (var column in collection) var items = collection.ToList();
foreach (var column in items)
{ {
if (this.Any(c => c.Name == column.Name)) if (this.Any(c => c.Name == column.Name))
{ {
@ -78,7 +54,7 @@ namespace BizHawk.Client.EmuHawk
} }
} }
base.AddRange(collection); base.AddRange(items);
ColumnsChanged(); ColumnsChanged();
} }
@ -86,6 +62,9 @@ namespace BizHawk.Client.EmuHawk
{ {
if (this.Any(c => c.Name == column.Name)) if (this.Any(c => c.Name == column.Name))
{ {
// The designer sucks, doing nothing for now
return;
throw new InvalidOperationException("A column with this name already exists."); throw new InvalidOperationException("A column with this name already exists.");
} }
@ -95,7 +74,8 @@ namespace BizHawk.Client.EmuHawk
public new void InsertRange(int index, IEnumerable<RollColumn> collection) public new void InsertRange(int index, IEnumerable<RollColumn> collection)
{ {
foreach (var column in collection) var items = collection.ToList();
foreach (var column in items)
{ {
if (this.Any(c => c.Name == column.Name)) if (this.Any(c => c.Name == column.Name))
{ {
@ -103,7 +83,7 @@ namespace BizHawk.Client.EmuHawk
} }
} }
base.InsertRange(index, collection); base.InsertRange(index, items);
ColumnsChanged(); ColumnsChanged();
} }
@ -139,14 +119,8 @@ namespace BizHawk.Client.EmuHawk
ColumnsChanged(); ColumnsChanged();
} }
public IEnumerable<string> Groups public IEnumerable<string> Groups => this
{
get
{
return this
.Select(x => x.Group) .Select(x => x.Group)
.Distinct(); .Distinct();
} }
}
}
} }