using System; using System.Collections.Generic; using System.Linq; namespace StansAssets.SceneManagement.StackVisualizer.Utility { internal static class StackVisualizerUtility { static VisualStackTemplate[] s_EmptyTemplatesCollection = new VisualStackTemplate[0]; public static IEnumerable CreateTemplatesFor(IEnumerable stack) where T : Enum { if (!stack.Any()) { return s_EmptyTemplatesCollection; } // TODO: Use pool to prevent allocations var newStack = stack.Select(st => new VisualStackTemplate() { Title = GetTitleFromEnum(st), FullTitle = GetFullTitleFromEnum(st), Status = VisualStackItemStatus.Disabled }).ToList(); // Set last state as Active newStack.Last().Status = VisualStackItemStatus.Active; // Reverse to display the stack from top to bottom newStack.Reverse(); return newStack; } static Dictionary s_StackTitles; /// /// Method that gives cached string title for a given Enum value. It is a first character of enum. /// /// Enum value. /// Returns first character of enum value name. static string GetTitleFromEnum(T enumValue) where T : Enum { if (s_StackTitles == null) { s_StackTitles = new Dictionary(); foreach (var enumItem in (T[]) Enum.GetValues(typeof(T))) { s_StackTitles.Add(enumItem.GetHashCode(), enumItem.ToString().Substring(0, 1).ToUpper()); } } return s_StackTitles[enumValue.GetHashCode()]; } static Dictionary s_StackFullTitles; /// /// Method that gives cached full string title for a given Enum value. /// /// Enum value. /// Returns first character of enum value name. static string GetFullTitleFromEnum(T enumValue) where T : Enum { if (s_StackFullTitles == null) { s_StackFullTitles = new Dictionary(); foreach (var enumItem in (T[]) Enum.GetValues(typeof(T))) { s_StackFullTitles.Add(enumItem.GetHashCode(), enumItem.ToString()); } } return s_StackFullTitles[enumValue.GetHashCode()]; } } }