From 22ddc3ee1b199284b0ea6646df6fc60eb6ad6d14 Mon Sep 17 00:00:00 2001 From: Hannele Ruiz Date: Sat, 14 Nov 2020 01:05:37 -0300 Subject: [PATCH] The List Items now report the movement direction --- LemonUI/Menus/NativeListItem.cs | 53 +++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/LemonUI/Menus/NativeListItem.cs b/LemonUI/Menus/NativeListItem.cs index 955c1430..63b931b6 100644 --- a/LemonUI/Menus/NativeListItem.cs +++ b/LemonUI/Menus/NativeListItem.cs @@ -13,6 +13,25 @@ namespace LemonUI.Menus /// A with the information of the selected item. public delegate void ItemChangedEventHandler(object sender, ItemChangedEventArgs e); + /// + /// The movement direction of the item change. + /// + public enum Direction + { + /// + /// The Direction is Unknown. + /// + Unknown = 0, + /// + /// The item was moved to the Left. + /// + Left = 1, + /// + /// The item was moved to the Right. + /// + Right = 2, + } + /// /// Represents the change of the selection of an item. /// @@ -22,16 +41,21 @@ public class ItemChangedEventArgs /// /// The new object. /// - public T Object { get; } + public T Object { get; set; } /// /// The index of the object. /// public int Index { get; } + /// + /// The direction of the Item Changed event. + /// + public Direction Direction { get; } - internal ItemChangedEventArgs(T obj, int index) + internal ItemChangedEventArgs(T obj, int index, Direction direction) { Object = obj; Index = index; + Direction = direction; } } @@ -111,7 +135,7 @@ public int SelectedIndex return; } index = value; - ItemChanged?.Invoke(this, new ItemChangedEventArgs(SelectedItem, index)); + TriggerEvent(value, Direction.Unknown); UpdateIndex(); } } @@ -196,6 +220,13 @@ public NativeListItem(string title, string subtitle, params T[] objs) : base(tit #region Private Functions + /// + /// Triggers the event. + /// + private void TriggerEvent(int index, Direction direction) + { + ItemChanged?.Invoke(this, new ItemChangedEventArgs(items[index], index, direction)); + } /// /// Updates the currently selected item based on the index. /// @@ -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(); } /// /// Moves to the next item. @@ -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(); } /// /// Draws the List on the screen.