Skip to content

Commit

Permalink
The List Items now report the movement direction
Browse files Browse the repository at this point in the history
  • Loading branch information
justalemon committed Nov 14, 2020
1 parent 16a758c commit 22ddc3e
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions LemonUI/Menus/NativeListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ namespace LemonUI.Menus
/// <param name="e">A <see cref="ItemChangedEventArgs{T}"/> with the information of the selected item.</param>
public delegate void ItemChangedEventHandler<T>(object sender, ItemChangedEventArgs<T> e);

/// <summary>
/// The movement direction of the item change.
/// </summary>
public enum Direction
{
/// <summary>
/// The Direction is Unknown.
/// </summary>
Unknown = 0,
/// <summary>
/// The item was moved to the Left.
/// </summary>
Left = 1,
/// <summary>
/// The item was moved to the Right.
/// </summary>
Right = 2,
}

/// <summary>
/// Represents the change of the selection of an item.
/// </summary>
Expand All @@ -22,16 +41,21 @@ public class ItemChangedEventArgs<T>
/// <summary>
/// The new object.
/// </summary>
public T Object { get; }
public T Object { get; set; }
/// <summary>
/// The index of the object.
/// </summary>
public int Index { get; }
/// <summary>
/// The direction of the Item Changed event.
/// </summary>
public Direction Direction { get; }

internal ItemChangedEventArgs(T obj, int index)
internal ItemChangedEventArgs(T obj, int index, Direction direction)
{
Object = obj;
Index = index;
Direction = direction;
}
}

Expand Down Expand Up @@ -111,7 +135,7 @@ public int SelectedIndex
return;
}
index = value;
ItemChanged?.Invoke(this, new ItemChangedEventArgs<T>(SelectedItem, index));
TriggerEvent(value, Direction.Unknown);
UpdateIndex();
}
}
Expand Down Expand Up @@ -196,6 +220,13 @@ public NativeListItem(string title, string subtitle, params T[] objs) : base(tit

#region Private Functions

/// <summary>
/// Triggers the <seealso cref="ItemChangedEventHandler{T}"/> event.
/// </summary>
private void TriggerEvent(int index, Direction direction)
{
ItemChanged?.Invoke(this, new ItemChangedEventArgs<T>(items[index], index, direction));
}
/// <summary>
/// Updates the currently selected item based on the index.
/// </summary>
Expand Down Expand Up @@ -251,15 +282,17 @@ public override void GoLeft()
}

// If this is the first item, go back to the last one
if (SelectedIndex == 0)
if (index == 0)
{
SelectedIndex = Items.Count - 1;
index = Items.Count - 1;
}
// Otherwise, return to the previous one
else
{
SelectedIndex--;
index--;
}
TriggerEvent(index, Direction.Left);
UpdateIndex();
}
/// <summary>
/// Moves to the next item.
Expand All @@ -273,15 +306,17 @@ public override void GoRight()
}

// If this is the last item, go back to the first one
if (SelectedIndex == Items.Count - 1)
if (index == Items.Count - 1)
{
SelectedIndex = 0;
index = 0;
}
// Otherwise, continue to the next one
else
{
SelectedIndex++;
index++;
}
TriggerEvent(index, Direction.Right);
UpdateIndex();
}
/// <summary>
/// Draws the List on the screen.
Expand Down

0 comments on commit 22ddc3e

Please sign in to comment.