Skip to content
Snippets Groups Projects
Commit 59002868 authored by Tobias Schöner's avatar Tobias Schöner
Browse files

feat: Factscreen now reacts to grouping toggle

parent 00e1c5e2
No related branches found
No related tags found
No related merge requests found
...@@ -439,7 +439,7 @@ GameObject: ...@@ -439,7 +439,7 @@ GameObject:
- component: {fileID: 4370335918965838469} - component: {fileID: 4370335918965838469}
- component: {fileID: 4267244877370467227} - component: {fileID: 4267244877370467227}
m_Layer: 5 m_Layer: 5
m_Name: Toggle m_Name: GroupingToggle
m_TagString: Untagged m_TagString: Untagged
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
...@@ -513,7 +513,19 @@ MonoBehaviour: ...@@ -513,7 +513,19 @@ MonoBehaviour:
m_Group: {fileID: 0} m_Group: {fileID: 0}
onValueChanged: onValueChanged:
m_PersistentCalls: 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 m_IsOn: 0
--- !u!1 &6430644988760938474 --- !u!1 &6430644988760938474
GameObject: GameObject:
......
...@@ -3,16 +3,20 @@ ...@@ -3,16 +3,20 @@
using TMPro; using TMPro;
using System; using System;
using static CommunicationEvents; using static CommunicationEvents;
using System.Linq;
using UnityEngine.UI;
using System.Collections;
public class DisplayFacts : MonoBehaviour public class DisplayFacts : MonoBehaviour
{ {
public static Dictionary<Type, GameObject> prefabDictionary; public static Dictionary<Type, GameObject> prefabDictionary;
public static Dictionary<string, GameObject> displayedFacts = new(); public static Dictionary<string, GameObject> displayedFacts = new();
[SerializeField] private Transform factscreenContent;
[SerializeField] private GameObject factSpotPrefab; [SerializeField] private GameObject factSpotPrefab;
public Transform factscreenContent; private bool showOnlyFavorites = false;
private bool showGrouped = false;
[Header("FactPrefabs")] [Header("FactPrefabs")]
public GameObject prefab_Point; public GameObject prefab_Point;
...@@ -39,7 +43,7 @@ public class DisplayFacts : MonoBehaviour ...@@ -39,7 +43,7 @@ public class DisplayFacts : MonoBehaviour
public GameObject prefab_TestFact; public GameObject prefab_TestFact;
#region UnityMethods
//Start is called before the first frame update //Start is called before the first frame update
void Start() void Start()
{ {
...@@ -70,18 +74,43 @@ void Start() ...@@ -70,18 +74,43 @@ void Start()
AddFactEvent.AddListener(AddFact); AddFactEvent.AddListener(AddFact);
RemoveFactEvent.AddListener(RemoveFact); RemoveFactEvent.AddListener(RemoveFact);
AnimateExistingFactEvent.AddListener(AnimateFact); AnimateExistingFactEvent.AddListener(AnimateFact);
FactFavorisation.ChangeFavoriteEvent.AddListener(OnFavoriteChange);
} }
#endregion UnityMethods
#region Implementationö
public void AddFact(Fact fact) { 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); var display = CreateDisplay(transform, fact);
display.transform.localPosition = Vector3.zero; display.transform.localPosition = Vector3.zero;
displayedFacts.Add(fact.Id, display); 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) public void RemoveFact(Fact fact)
{ {
// destroy factSpot (parent of displayed fact) and the fact display with it // 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); displayedFacts.Remove(fact.Id);
} }
...@@ -90,9 +119,83 @@ public void AnimateFact(Fact fact) { ...@@ -90,9 +119,83 @@ public void AnimateFact(Fact fact) {
factIcon.GetComponentInChildren<ImageHintAnimation>().AnimationTrigger(); factIcon.GetComponentInChildren<ImageHintAnimation>().AnimationTrigger();
} }
private GameObject CreateDisplay(Transform transform, Fact fact)
#region Favorites
public void FavoritesFilterChanged(Toggle t)
{ {
var spot = Instantiate(factSpotPrefab, factscreenContent); showOnlyFavorites = t.isOn;
return fact.instantiateDisplay(prefabDictionary[fact.GetType()], spot.transform); 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
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment