Add null check to supress quality errors

This commit is contained in:
Derrick Timmermans 2021-07-06 14:39:53 +02:00
parent ffe18ebe51
commit 4b1b5a88fe
No known key found for this signature in database
GPG Key ID: 8681B60806EF4A17

View File

@ -18,6 +18,8 @@ namespace osu.Game.Graphics.Containers
{ {
private int? selectedIndex; private int? selectedIndex;
public T Selected => (selectedIndex >= 0 && selectedIndex < Count) ? this[selectedIndex.Value] : null;
private void setSelected(int? value) private void setSelected(int? value)
{ {
if (selectedIndex == value) if (selectedIndex == value)
@ -51,6 +53,7 @@ namespace osu.Game.Graphics.Containers
} }
public void Deselect() => setSelected(null); public void Deselect() => setSelected(null);
public void Select(T item) public void Select(T item)
{ {
var newIndex = IndexOf(item); var newIndex = IndexOf(item);
@ -61,19 +64,20 @@ namespace osu.Game.Graphics.Containers
setSelected(IndexOf(item)); setSelected(IndexOf(item));
} }
public T Selected => (selectedIndex >= 0 && selectedIndex < Count) ? this[selectedIndex.Value] : null;
private readonly Dictionary<T, Action<SelectionState>> handlerMap = new Dictionary<T, Action<SelectionState>>(); private readonly Dictionary<T, Action<SelectionState>> handlerMap = new Dictionary<T, Action<SelectionState>>();
public override void Add(T drawable) public override void Add(T drawable)
{ {
// This event is used to update selection state when modified within the drawable itself.
// It is added to a dictionary so that we can unsubscribe if the drawable is removed from this container
handlerMap[drawable] = state => selectionChanged(drawable, state);
drawable.StateChanged += handlerMap[drawable];
base.Add(drawable); base.Add(drawable);
if (drawable != null)
{
// This event is used to update selection state when modified within the drawable itself.
// It is added to a dictionary so that we can unsubscribe if the drawable is removed from this container
handlerMap[drawable] = state => selectionChanged(drawable, state);
drawable.StateChanged += handlerMap[drawable];
}
} }
public override bool Remove(T drawable) public override bool Remove(T drawable)
@ -81,7 +85,7 @@ namespace osu.Game.Graphics.Containers
if (!base.Remove(drawable)) if (!base.Remove(drawable))
return false; return false;
if (handlerMap.TryGetValue(drawable, out var action)) if (drawable != null && handlerMap.TryGetValue(drawable, out var action))
{ {
drawable.StateChanged -= action; drawable.StateChanged -= action;
handlerMap.Remove(drawable); handlerMap.Remove(drawable);