diff --git a/Assets/Resources/Prefabs/UI/Facts/Factscreen.prefab b/Assets/Resources/Prefabs/UI/Facts/Factscreen.prefab index 2fb44abb3b4119c570a0f728962a197a3fa1d197..76e7316da2b0e247b4d1a88cff63aeb4deb2ef58 100644 --- a/Assets/Resources/Prefabs/UI/Facts/Factscreen.prefab +++ b/Assets/Resources/Prefabs/UI/Facts/Factscreen.prefab @@ -439,7 +439,7 @@ GameObject: - component: {fileID: 4370335918965838469} - component: {fileID: 4267244877370467227} m_Layer: 5 - m_Name: Toggle + m_Name: GroupingToggle m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -513,7 +513,19 @@ MonoBehaviour: m_Group: {fileID: 0} onValueChanged: m_PersistentCalls: - m_Calls: [] + m_Calls: + - m_Target: {fileID: 3581652732795482906} + m_TargetAssemblyTypeName: DisplayFacts, Assembly-CSharp + m_MethodName: GroupingChanged + m_Mode: 2 + m_Arguments: + m_ObjectArgument: {fileID: 4267244877370467227} + m_ObjectArgumentAssemblyTypeName: UnityEngine.UI.Toggle, UnityEngine.UI + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 m_IsOn: 0 --- !u!1 &6430644988760938474 GameObject: diff --git a/Assets/Scripts/InventoryStuff/DisplayFacts.cs b/Assets/Scripts/InventoryStuff/DisplayFacts.cs index 9713b8f2dba2b94671f9093bfe3448ad85f86fb8..cac01a98c489be66cfa3e39b07cde5df6468c8f2 100644 --- a/Assets/Scripts/InventoryStuff/DisplayFacts.cs +++ b/Assets/Scripts/InventoryStuff/DisplayFacts.cs @@ -3,16 +3,20 @@ using TMPro; using System; using static CommunicationEvents; +using System.Linq; +using UnityEngine.UI; +using System.Collections; public class DisplayFacts : MonoBehaviour { public static Dictionary<Type, GameObject> prefabDictionary; - public static Dictionary<string, GameObject> displayedFacts = new(); + [SerializeField] private Transform factscreenContent; [SerializeField] private GameObject factSpotPrefab; - public Transform factscreenContent; + private bool showOnlyFavorites = false; + private bool showGrouped = false; [Header("FactPrefabs")] public GameObject prefab_Point; @@ -39,7 +43,7 @@ public class DisplayFacts : MonoBehaviour public GameObject prefab_TestFact; - + #region UnityMethods //Start is called before the first frame update void Start() { @@ -70,18 +74,43 @@ void Start() AddFactEvent.AddListener(AddFact); RemoveFactEvent.AddListener(RemoveFact); AnimateExistingFactEvent.AddListener(AnimateFact); + FactFavorisation.ChangeFavoriteEvent.AddListener(OnFavoriteChange); } + #endregion UnityMethods + #region Implementationö public void AddFact(Fact fact) { + // index where the new display would be inserted if in showGroup is active + int siblingIdx = 0; + if (showGrouped) + { + var facts = GetChildObjects(factscreenContent.transform).Select(c => c.GetComponentInChildren<FactWrapper>().fact).ToList(); + siblingIdx = GetIndexInSortedList(fact, facts); + } + + // create display var display = CreateDisplay(transform, fact); display.transform.localPosition = Vector3.zero; displayedFacts.Add(fact.Id, display); + + // disable if showOnlyFavorites is true and fact is no favorite + display.transform.parent.gameObject.SetActive(!(showOnlyFavorites && !display.GetComponent<FactFavorisation>().IsFavorite)); + + // if showGrouped is true: move to correct ordered position + if (showGrouped) + display.transform.parent.transform.SetSiblingIndex(siblingIdx); + } + + private GameObject CreateDisplay(Transform transform, Fact fact) + { + var spot = Instantiate(factSpotPrefab, factscreenContent); + return fact.instantiateDisplay(prefabDictionary[fact.GetType()], spot.transform); } public void RemoveFact(Fact fact) { // destroy factSpot (parent of displayed fact) and the fact display with it - Destroy(displayedFacts[fact.Id].transform.parent); + Destroy(displayedFacts[fact.Id].transform.parent.gameObject); displayedFacts.Remove(fact.Id); } @@ -90,9 +119,83 @@ public void AnimateFact(Fact fact) { factIcon.GetComponentInChildren<ImageHintAnimation>().AnimationTrigger(); } - private GameObject CreateDisplay(Transform transform, Fact fact) + + #region Favorites + public void FavoritesFilterChanged(Toggle t) { - var spot = Instantiate(factSpotPrefab, factscreenContent); - return fact.instantiateDisplay(prefabDictionary[fact.GetType()], spot.transform); + showOnlyFavorites = t.isOn; + if (!showOnlyFavorites) // show all + displayedFacts.Values.ToList().ForEach(nFav => nFav.transform.parent.gameObject.SetActive(!showOnlyFavorites)); + else + { + // hide not favorites + var notFavorites = displayedFacts.Values.Where(go => !go.GetComponent<FactFavorisation>().IsFavorite).ToList(); + notFavorites.ForEach(nFav => nFav.transform.parent.gameObject.SetActive(false)); + } + } + + private void OnFavoriteChange(Fact changedFact, bool isFavourite) + { + if (!showOnlyFavorites) + return; + + var id = changedFact.Id; + if (displayedFacts.ContainsKey(id)) + displayedFacts[id].transform.parent.gameObject.SetActive(isFavourite); + } + #endregion Favorites + + #region Grouping + public void GroupingChanged(Toggle t) + { + showGrouped = t.isOn; + + List<Transform> vals = GetChildObjects(factscreenContent.transform); + List<Transform> ordered = new(); + if (showGrouped) + { + var comparer = new FactTypeComparer(); + ordered = vals.OrderBy(tr => tr.GetComponentInChildren<FactWrapper>().fact, comparer).ToList(); + } + else + ordered = vals.OrderBy(tr => displayedFacts.Keys.ToList().IndexOf(tr.GetComponentInChildren<FactWrapper>().fact.Id)).ToList(); + + for (int i = 0; i < ordered.Count; i++) + ordered[i].transform.SetSiblingIndex(i); + } + private int GetIndexInSortedList(Fact f, List<Fact> toCheck) + { + var index = toCheck.BinarySearch(f, new FactTypeComparer()); + if (index < 0) index = ~index; + return index; + } + + internal class FactTypeComparer : IComparer<Fact> + { + /// <summary> + /// Compare two facts by type and label + /// </summary> + /// <param name="x"></param> + /// <param name="y"></param> + /// <returns></returns> + public int Compare(Fact x, Fact y) + { + if (x.GetType() == y.GetType()) // same type: compare labels + return string.Compare(x.Label, y.Label); + else // different types: compare type + return string.Compare(x.GetType().ToString(), y.GetType().ToString()); + } + } + #endregion Grouping + + #region Helper + private static List<Transform> GetChildObjects(Transform parent) + { + List<Transform> children = new(); + foreach (Transform val in parent) + children.Add(val); + return children; } + #endregion Helper + #endregion Implementation }