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

feat: FactExplorer will now try to overlay mainFact on clicked Fact

if a part of FactExplorer would be out of screen bounds it will be clamped to be fully visible
parent 54ea0f53
Branches
No related tags found
No related merge requests found
...@@ -33,7 +33,7 @@ private void Update() ...@@ -33,7 +33,7 @@ private void Update()
DestroyIfClickedOutside(); DestroyIfClickedOutside();
} }
public void Initialize(Fact fact) public void Initialize(Fact fact, Vector3 factPosition)
{ {
mainFact = fact; mainFact = fact;
parentFacts = GetParentFacts(); parentFacts = GetParentFacts();
...@@ -43,6 +43,8 @@ public void Initialize(Fact fact) ...@@ -43,6 +43,8 @@ public void Initialize(Fact fact)
Debug.Log($"Children of {mainFact.Label}: {string.Join(", ", childFacts.Select(cf => cf.Label))}"); Debug.Log($"Children of {mainFact.Label}: {string.Join(", ", childFacts.Select(cf => cf.Label))}");
UpdateFactExplorerUI(); UpdateFactExplorerUI();
MoveToPreferredPosition(factPosition);
} }
#endregion UnityMethods #endregion UnityMethods
...@@ -94,6 +96,21 @@ private void DestroyIfClickedOutside() ...@@ -94,6 +96,21 @@ private void DestroyIfClickedOutside()
Destroy(gameObject); Destroy(gameObject);
} }
} }
private void MoveToPreferredPosition(Vector3 prefPos)
{
LayoutRebuilder.ForceRebuildLayoutImmediate(transform.GetComponent<RectTransform>());
// calculate opimal position
var deltaPos = mainFactUI.position - prefPos;
transform.position -= deltaPos;
// clamp position, so that no parts of the FactExplorer are out of screen
RectTransform feRect = GetComponent<RectTransform>();
Vector2 apos = feRect.anchoredPosition;
apos.x = Mathf.Clamp(apos.x, 0, Screen.width - feRect.sizeDelta.x);
apos.y = Mathf.Clamp(apos.y, -Screen.height + feRect.sizeDelta.y, 0);
feRect.anchoredPosition = apos;
}
#endregion Implementation #endregion Implementation
#region Spawner #region Spawner
......
...@@ -21,7 +21,7 @@ public void OnPointerClick(PointerEventData eventData) ...@@ -21,7 +21,7 @@ public void OnPointerClick(PointerEventData eventData)
var fact = transform.GetComponent<FactWrapper>().fact; var fact = transform.GetComponent<FactWrapper>().fact;
factExplorer = Instantiate(factExplorerPrefab.transform, Input.mousePosition, Quaternion.identity, parent); factExplorer = Instantiate(factExplorerPrefab.transform, Input.mousePosition, Quaternion.identity, parent);
factExplorer.GetComponent<FactExplorer>().Initialize(fact); factExplorer.GetComponent<FactExplorer>().Initialize(fact, transform.position);
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment