Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using System.Linq;
using UnityEngine.UI;
public class FactExplorer : MonoBehaviour
{
[Header("PrefabComponents")]
[SerializeField] private Transform factParentsUI;
[SerializeField] private Transform mainFactUI;
[SerializeField] private Transform factChildrenUI;
[SerializeField] private Transform linesUI;
[Header("Prefabs")]
[SerializeField] private GameObject factSpotPrefab;
private Fact mainFact;
private List<Fact> parentFacts;
private List<Fact> childFacts;
private void Update()
{
DestroyIfClickedOutside();
}
public void Initialize(Fact fact)
{
mainFact = fact;
parentFacts = GetParentFacts();
childFacts = GetChildFacts();
Debug.Log($"Parents of {mainFact.Label}: {string.Join(", ", parentFacts.Select(cf => cf.Label))}");
Debug.Log($"Children of {mainFact.Label}: {string.Join(", ", childFacts.Select(cf => cf.Label))}");
UpdateFactExplorerUI();
}
#region Implementation
private List<Fact> GetParentFacts()
{
//TODO: TSC how to get all current facts without this dumb workaround
var allFacts = DisplayFacts.displayedFacts.Values.Select(x => x.GetComponent<FactWrapper>().fact);
List<Fact> _parentFacts = new();
// get all facts that reference the mainFact
foreach (Fact f in allFacts)
if (GetReferencedPids(f).Contains(mainFact.Id)) // if f contains reference to mainFact
_parentFacts.Add(f);
return _parentFacts;
}
private List<Fact> GetChildFacts()
{
List<Fact> childFacts = new();
// get all properties of Type mainFactType that are strings and start with "pid"
foreach (string factId in GetReferencedPids(mainFact))
childFacts.Add(StageStatic.stage.factState[factId]);
return childFacts;
}
private void UpdateFactExplorerUI()
{
SpawnUIFacts(factParentsUI, parentFacts);
SpawnUIFacts(mainFactUI, new List<Fact>() { mainFact });
SpawnUIFacts(factChildrenUI, childFacts);
}
private void DestroyIfClickedOutside()
{
if ((Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)) &&
!RectTransformUtility.RectangleContainsScreenPoint(transform.GetComponent<RectTransform>(), Input.mousePosition, null))
{
Destroy(gameObject);
}
}
#endregion Implementation
#region Helper
/// <summary>
/// Check all fields of a Fact for references to other facts and return their ids
/// </summary>
/// <param name="f"></param>
/// <returns>An Enumerable containing the ids of facts referenced by the input fact</returns>
private static IEnumerable<string> GetReferencedPids(Fact f)
{
return (f.GetType().GetFields()
.Where(field => field.FieldType == typeof(string) && field.Name.ToLower().StartsWith("pid"))
).Select(fi => (string)fi.GetValue(f));
}
private void SpawnUIFacts(Transform uiParent, List<Fact> toSpawn)
{
uiParent.gameObject.DestroyAllChildren();
foreach (Fact f in toSpawn)
{
var spot = Instantiate(factSpotPrefab, uiParent);
// TODO: this link to DisplayFacts is not ideal: maybe refactor to SciptableObject or such
var display = f.instantiateDisplay(DisplayFacts.prefabDictionary[f.GetType()], spot.transform);
display.transform.localPosition = Vector3.zero;
}
}
#endregion Helper
}