Enable nullability for `Cell` and co. and clean up

This commit is contained in:
YoshiRulz 2024-03-26 09:44:41 +10:00 committed by James Groom
parent f83602c1c9
commit 9eb9ffa5df
12 changed files with 85 additions and 82 deletions

View File

@ -1,4 +1,6 @@
using System;
#nullable enable
using System;
using System.Diagnostics;
using BizHawk.Common;
@ -11,8 +13,9 @@ namespace BizHawk.Client.EmuHawk
/// </summary>
public sealed class Cell : IComparable<Cell>, IEquatable<Cell>
{
public RollColumn Column { get; internal set; }
public int? RowIndex { get; internal set; }
public RollColumn? Column { get; internal set; } = null;
public int? RowIndex { get; internal set; } = null;
public Cell() { }
@ -25,30 +28,30 @@ namespace BizHawk.Client.EmuHawk
public int CompareTo(Cell other)
=> SortCell.Compare(this, other);
public bool Equals(Cell other)
public bool Equals(Cell? other)
=> other is not null && Column?.Name == other.Column?.Name && RowIndex == other.RowIndex;
public override bool Equals(object obj)
public override bool Equals(object? obj)
=> obj is Cell other && Equals(other);
public override int GetHashCode()
{
return Column.GetHashCode() + RowIndex.GetHashCode();
return Column!.GetHashCode() + RowIndex.GetHashCode();
}
public override string ToString()
=> $"Cell(r: {RowIndex?.ToString() ?? "null"}, c: \"{Column?.Name ?? "(no column)"}\")";
public static bool operator ==(Cell a, Cell b)
public static bool operator ==(Cell? a, Cell? b)
=> a is null ? b is null : a.Equals(b);
public static bool operator !=(Cell a, Cell b)
public static bool operator !=(Cell? a, Cell? b)
=> a is null ? b is not null : !a.Equals(b);
}
internal static class SortCell
{
public static int Compare(Cell c1, Cell c2)
public static int Compare(Cell? c1, Cell? c2)
{
if (c1 is null && c2 is null)
{
@ -65,9 +68,9 @@ namespace BizHawk.Client.EmuHawk
return -1;
}
if (c1.RowIndex.HasValue)
if (c1.RowIndex is not null)
{
if (c2.RowIndex.HasValue)
if (c2.RowIndex is not null)
{
int row = c1.RowIndex.Value.CompareTo(c2.RowIndex.Value);
return row == 0
@ -78,12 +81,12 @@ namespace BizHawk.Client.EmuHawk
return 1;
}
if (c2.RowIndex.HasValue)
if (c2.RowIndex is not null)
{
return -1;
}
return string.CompareOrdinal(c1.Column.Name, c2.Column.Name);
return string.CompareOrdinal(c1.Column!.Name, c2.Column!.Name);
}
}
@ -119,9 +122,7 @@ namespace BizHawk.Client.EmuHawk
public static class CellExtensions
{
public static bool IsDataCell(this Cell cell)
{
return cell != null && cell.RowIndex != null && cell.Column != null;
}
public static bool IsDataCell(this Cell? cell)
=> cell is { RowIndex: not null, Column: not null };
}
}

View File

@ -1,4 +1,6 @@
namespace BizHawk.Client.EmuHawk
#nullable enable
namespace BizHawk.Client.EmuHawk
{
/// <summary>
/// Specifies the type of column of a <see cref="RollColumn"/>

View File

@ -104,7 +104,7 @@ namespace BizHawk.Client.EmuHawk
private void DrawColumnDrag(List<RollColumn> visibleColumns)
{
if (!(_columnDown?.Width > 0)
if (_columnDown is not { Width: > 0 }
|| !_columnDownMoved
|| !_currentX.HasValue
|| !_currentY.HasValue
@ -139,28 +139,27 @@ namespace BizHawk.Client.EmuHawk
private void DrawCellDrag(List<RollColumn> visibleColumns)
{
if (_draggingCell?.RowIndex != null
&& _draggingCell.Column.Width > 0
if (_draggingCell is { RowIndex: int targetRow, Column: { Width: > 0 } targetCol }
&& _currentX.HasValue
&& _currentY.HasValue)
{
var text = "";
int offsetX = 0;
int offsetY = 0;
QueryItemText?.Invoke(_draggingCell.RowIndex.Value, _draggingCell.Column, out text, ref offsetX, ref offsetY);
QueryItemText?.Invoke(targetRow, targetCol, out text, ref offsetX, ref offsetY);
Color bgColor = _backColor;
QueryItemBkColor?.Invoke(_draggingCell.RowIndex.Value, _draggingCell.Column, ref bgColor);
QueryItemBkColor?.Invoke(targetRow, targetCol, ref bgColor);
int columnHeight = CellHeight;
if (HorizontalOrientation)
{
int columnIndex = visibleColumns.IndexOf(_draggingCell.Column);
var columnIndex = visibleColumns.IndexOf(targetCol);
columnHeight = GetHColHeight(columnIndex);
}
int x1 = _currentX.Value - (_draggingCell.Column.Width / 2);
var x1 = _currentX.Value - targetCol.Width / 2;
int y1 = _currentY.Value - (columnHeight / 2);
int x2 = x1 + _draggingCell.Column.Width;
var x2 = x1 + targetCol.Width;
int y2 = y1 + columnHeight;
_renderer.SetBrush(bgColor);
@ -410,7 +409,7 @@ namespace BizHawk.Client.EmuHawk
int top = GetHColTop(i) - _vBar.Value;
int height = GetHColHeight(i);
_renderer.SetBrush(CurrentCell.Column.Emphasis
_renderer.SetBrush(CurrentCell.Column!.Emphasis
? SystemColors.Highlight.Add(0x00222222)
: SystemColors.Highlight);
@ -433,7 +432,7 @@ namespace BizHawk.Client.EmuHawk
int left = column.Left - _hBar.Value;
int width = column.Right - _hBar.Value - left;
_renderer.SetBrush(CurrentCell.Column.Emphasis
_renderer.SetBrush(CurrentCell.Column!.Emphasis
? SystemColors.Highlight.Add(0x00550000)
: SystemColors.Highlight);
@ -564,7 +563,7 @@ namespace BizHawk.Client.EmuHawk
return;
}
int columnIndex = visibleColumns.IndexOf(cell.Column);
var columnIndex = visibleColumns.IndexOf(cell.Column!);
w = CellWidth - 1;
y = GetHColTop(columnIndex) - _vBar.Value + 1;
h = GetHColHeight(columnIndex) - 1;
@ -577,7 +576,7 @@ namespace BizHawk.Client.EmuHawk
return;
}
x = cell.Column.Left - _hBar.Value + 1;
x = cell.Column!.Left - _hBar.Value + 1;
w = cell.Column.Width - 1;
h = CellHeight - 1;
}

View File

@ -57,8 +57,9 @@ namespace BizHawk.Client.EmuHawk
private int[] _horizontalColumnTops; // Updated on paint, contains one extra item to allow inference of last column height
private RollColumn _columnDown;
private RollColumn _columnResizing;
private RollColumn/*?*/ _columnDown;
private RollColumn/*?*/ _columnResizing;
private int? _currentX;
private int? _currentY;
@ -177,13 +178,14 @@ namespace BizHawk.Client.EmuHawk
}
else
{
var maxLength = CurrentCell.Column.Text?.Length ?? 0;
var col = CurrentCell.Column!;
var maxLength = col.Text.Length;
for (int i = 0; i < RowCount; i++)
{
string text = "";
int offSetX = 0, offSetY = 0;
QueryItemText?.Invoke(i, CurrentCell.Column, out text, ref offSetX, ref offSetY);
QueryItemText?.Invoke(i, col, out text, ref offSetX, ref offSetY);
if (text.Length > maxLength)
{
maxLength = text.Length;
@ -191,7 +193,7 @@ namespace BizHawk.Client.EmuHawk
}
var newWidth = (maxLength * _charSize.Width) + (CellWidthPadding * 2);
CurrentCell.Column.Width = (int)newWidth;
col.Width = (int) newWidth;
_columns.ColumnsChanged();
Refresh();
}
@ -535,24 +537,26 @@ namespace BizHawk.Client.EmuHawk
public class ColumnClickEventArgs
{
public ColumnClickEventArgs(RollColumn column)
public ColumnClickEventArgs(RollColumn/*?*/ column)
{
Column = column;
}
public RollColumn Column { get; }
public RollColumn/*?*/ Column { get; }
}
/// <remarks>this is only used in TAStudio, which ignores the args param completely</remarks>
public class ColumnReorderedEventArgs
{
public ColumnReorderedEventArgs(int oldDisplayIndex, int newDisplayIndex, RollColumn column)
public ColumnReorderedEventArgs(int oldDisplayIndex, int newDisplayIndex, RollColumn/*?*/ column)
{
Column = column;
OldDisplayIndex = oldDisplayIndex;
NewDisplayIndex = newDisplayIndex;
}
public RollColumn Column { get; }
public RollColumn/*?*/ Column { get; }
public int OldDisplayIndex { get; }
public int NewDisplayIndex { get; }
}
@ -1156,7 +1160,7 @@ namespace BizHawk.Client.EmuHawk
{
// do marker drag here
}
else if (ModifierKeys == Keys.Shift && CurrentCell.Column.Type == ColumnType.Text)
else if (ModifierKeys is Keys.Shift && CurrentCell.Column! is { Type: ColumnType.Text } col)
{
if (_selectedItems.Count is not 0)
{
@ -1188,7 +1192,6 @@ namespace BizHawk.Client.EmuHawk
additionEndExcl = targetRow + 1;
}
}
var col = CurrentCell.Column;
for (var i = additionStart; i < additionEndExcl; i++) SelectCell(new() { RowIndex = i, Column = col });
}
}
@ -1202,7 +1205,7 @@ namespace BizHawk.Client.EmuHawk
SelectCell(CurrentCell);
}
}
else if (ModifierKeys == Keys.Control && CurrentCell.Column.Type == ColumnType.Text)
else if (ModifierKeys is Keys.Control && CurrentCell.Column!.Type is ColumnType.Text)
{
SelectCell(CurrentCell, toggle: true);
}
@ -1336,12 +1339,12 @@ namespace BizHawk.Client.EmuHawk
RightMouseScrolled?.Invoke(sender, e);
}
private void ColumnClickEvent(RollColumn column)
private void ColumnClickEvent(RollColumn/*?*/ column)
{
ColumnClick?.Invoke(this, new ColumnClickEventArgs(column));
}
private void ColumnRightClickEvent(RollColumn column)
private void ColumnRightClickEvent(RollColumn/*?*/ column)
{
ColumnRightClick?.Invoke(this, new ColumnClickEventArgs(column));
}
@ -1654,13 +1657,15 @@ namespace BizHawk.Client.EmuHawk
private void DoColumnReorder()
{
if (_columnDown != CurrentCell.Column)
if (_columnDown! != CurrentCell.Column!)
{
var oldIndex = _columns.IndexOf(_columnDown);
var newIndex = _columns.IndexOf(CurrentCell.Column);
ColumnReordered?.Invoke(this, new ColumnReorderedEventArgs(oldIndex, newIndex, _columnDown));
//TODO surely this only works properly in one direction?
// also the event is "...Reordered"--past tense--so it should be called AFTER the change --yoshi
_columns.Remove(_columnDown);
_columns.Insert(newIndex, _columnDown);
}
@ -1883,7 +1888,7 @@ namespace BizHawk.Client.EmuHawk
/// </summary>
/// <param name="pixel">The pixel coordinate.</param>
/// <returns>RollColumn object that contains the pixel coordinate or null if none exists.</returns>
private RollColumn ColumnAtPixel(int pixel)
private RollColumn/*?*/ ColumnAtPixel(int pixel)
{
if (_horizontalOrientation)
{

View File

@ -1,4 +1,6 @@
namespace BizHawk.Client.EmuHawk
#nullable enable
namespace BizHawk.Client.EmuHawk
{
public class RollColumn
{

View File

@ -1,4 +1,6 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
@ -6,11 +8,12 @@ namespace BizHawk.Client.EmuHawk
{
public class RollColumns : List<RollColumn>
{
public RollColumn this[string name] => this.SingleOrDefault(column => column.Name == name);
public RollColumn? this[string name]
=> this.SingleOrDefault(column => column.Name == name);
public IEnumerable<RollColumn> VisibleColumns => this.Where(c => c.Visible);
public Action ChangedCallback { get; set; }
public Action? ChangedCallback { get; set; } = null;
// 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()

View File

@ -566,7 +566,7 @@ namespace BizHawk.Client.EmuHawk
private void CheatListView_ColumnClick(object sender, InputRoll.ColumnClickEventArgs e)
{
var column = e.Column;
var column = e.Column!;
if (column.Name != _sortedColumn)
{
_sortReverse = false;

View File

@ -1318,7 +1318,7 @@ namespace BizHawk.Client.EmuHawk
/// </summary>
private void LuaListView_ColumnClick(object sender, InputRoll.ColumnClickEventArgs e)
{
var columnToSort = e.Column.Name;
var columnToSort = e.Column!.Name;
var luaListTemp = new List<LuaFile>();
if (columnToSort != _lastColumnSorted)
{

View File

@ -593,8 +593,7 @@ namespace BizHawk.Client.EmuHawk
{
if (e.Button == MouseButtons.Left)
{
if (BranchView.CurrentCell.IsDataCell()
&& BranchView.CurrentCell.Column.Name == BranchNumberColumnName)
if (BranchView.CurrentCell is { RowIndex: not null, Column.Name: BranchNumberColumnName })
{
BranchView.DragCurrentCell();
}

View File

@ -370,7 +370,7 @@ namespace BizHawk.Client.EmuHawk
{
if (TasView.AnyRowsSelected)
{
var columnName = e.Column.Name;
var columnName = e.Column!.Name;
if (columnName == FrameColumnName)
{
@ -379,7 +379,7 @@ namespace BizHawk.Client.EmuHawk
else if (columnName != CursorColumnName)
{
var frame = TasView.AnyRowsSelected ? TasView.FirstSelectedRowIndex : 0;
string buttonName = TasView.CurrentCell.Column.Name;
var buttonName = TasView.CurrentCell.Column!.Name;
if (ControllerType.BoolButtons.Contains(buttonName))
{
@ -424,8 +424,9 @@ namespace BizHawk.Client.EmuHawk
private void TasView_ColumnRightClick(object sender, InputRoll.ColumnClickEventArgs e)
{
e.Column.Emphasis ^= true;
UpdateAutoFire(e.Column.Name, e.Column.Emphasis);
var col = e.Column!;
col.Emphasis = !col.Emphasis;
UpdateAutoFire(col.Name, col.Emphasis);
TasView.Refresh();
}
@ -542,13 +543,9 @@ namespace BizHawk.Client.EmuHawk
return;
}
if (TasView.CurrentCell?.RowIndex == null || TasView.CurrentCell.Column == null)
{
return;
}
if (TasView.CurrentCell is not { RowIndex: int frame, Column: RollColumn targetCol }) return;
int frame = TasView.CurrentCell.RowIndex.Value;
string buttonName = TasView.CurrentCell.Column.Name;
var buttonName = targetCol.Name;
WasRecording = CurrentTasMovie.IsRecording() || WasRecording;
if (e.Button == MouseButtons.Left)
@ -592,12 +589,12 @@ namespace BizHawk.Client.EmuHawk
}
}
if (TasView.CurrentCell.Column.Name == CursorColumnName)
if (targetCol.Name is CursorColumnName)
{
_startCursorDrag = true;
GoToFrame(TasView.CurrentCell.RowIndex.Value, false, false, true);
GoToFrame(frame, fromLua: false, fromRewinding: false, OnLeftMouseDown: true);
}
else if (TasView.CurrentCell.Column.Name == FrameColumnName)
else if (targetCol.Name is FrameColumnName)
{
if (ModifierKeys == Keys.Alt && CurrentTasMovie.Markers.IsMarker(frame))
{
@ -610,7 +607,7 @@ namespace BizHawk.Client.EmuHawk
_selectionDragState = TasView.IsRowSelected(frame);
}
}
else if (TasView.CurrentCell.Column.Type != ColumnType.Text) // User changed input
else if (targetCol.Type is not ColumnType.Text) // User changed input
{
_playbackInterrupted = !MainForm.EmulatorPaused;
MainForm.PauseEmulator();
@ -628,11 +625,11 @@ namespace BizHawk.Client.EmuHawk
var altOrShift4State = ModifierKeys & (Keys.Alt | Keys.Shift);
if (altOrShift4State is Keys.Alt
|| (applyPatternToPaintedInputToolStripMenuItem.Checked
&& (!onlyOnAutoFireColumnsToolStripMenuItem.Checked || TasView.CurrentCell.Column.Emphasis)))
&& (!onlyOnAutoFireColumnsToolStripMenuItem.Checked || targetCol.Emphasis)))
{
BoolPatterns[ControllerType.BoolButtons.IndexOf(buttonName)].Reset();
_patternPaint = true;
_startRow = TasView.CurrentCell.RowIndex.Value;
_startRow = frame;
_boolPaintState = !CurrentTasMovie.BoolIsPressed(frame, buttonName);
}
else if (altOrShift4State is Keys.Shift)
@ -670,7 +667,7 @@ namespace BizHawk.Client.EmuHawk
{
CurrentTasMovie.ChangeLog.BeginNewBatch($"Paint Bool {buttonName} from frame {frame}");
CurrentTasMovie.ToggleBoolState(TasView.CurrentCell.RowIndex.Value, buttonName);
CurrentTasMovie.ToggleBoolState(frame, buttonName);
_boolPaintState = CurrentTasMovie.BoolIsPressed(frame, buttonName);
_triggerAutoRestore = true;
TastudioPlayMode(true);
@ -687,7 +684,7 @@ namespace BizHawk.Client.EmuHawk
_axisPaintState = CurrentTasMovie.GetAxisState(frame, buttonName);
if (applyPatternToPaintedInputToolStripMenuItem.Checked && (!onlyOnAutoFireColumnsToolStripMenuItem.Checked
|| TasView.CurrentCell.Column.Emphasis))
|| targetCol.Emphasis))
{
AxisPatterns[ControllerType.Axes.IndexOf(buttonName)].Reset();
CurrentTasMovie.SetAxisState(frame, buttonName, AxisPatterns[ControllerType.Axes.IndexOf(buttonName)].GetNextValue());
@ -727,7 +724,7 @@ namespace BizHawk.Client.EmuHawk
}
else if (e.Button == MouseButtons.Right)
{
if (TasView.CurrentCell.Column.Name == FrameColumnName && frame < CurrentTasMovie.InputLogLength)
if (targetCol.Name is FrameColumnName && frame < CurrentTasMovie.InputLogLength)
{
_rightClickControl = (ModifierKeys | Keys.Control) == ModifierKeys;
_rightClickShift = (ModifierKeys | Keys.Shift) == ModifierKeys;
@ -908,14 +905,11 @@ namespace BizHawk.Client.EmuHawk
private void TasView_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (TasView.CurrentCell.Column == null)
{
return;
}
if (TasView.CurrentCell.Column is not { Name: var columnName }) return;
if (e.Button == MouseButtons.Left)
{
if (!AxisEditingMode && TasView.CurrentCell is { RowIndex: not null, Column.Name: FrameColumnName })
if (!AxisEditingMode && TasView.CurrentCell.RowIndex is not null && columnName is FrameColumnName)
{
var existingMarker = CurrentTasMovie.Markers.FirstOrDefault(m => m.Frame == TasView.CurrentCell.RowIndex.Value);

View File

@ -1635,7 +1635,7 @@ namespace BizHawk.Client.EmuHawk
private void WatchListView_ColumnClick(object sender, InputRoll.ColumnClickEventArgs e)
{
var column = e.Column;
var column = e.Column!;
if (column.Name != _sortedColumn)
{
_sortReverse = false;

View File

@ -1252,9 +1252,7 @@ namespace BizHawk.Client.EmuHawk
}
private void WatchListView_ColumnClick(object sender, InputRoll.ColumnClickEventArgs e)
{
OrderColumn(e.Column);
}
=> OrderColumn(e.Column!);
private void ErrorIconButton_Click(object sender, EventArgs e)
{