diff --git a/Assets/Scripts/UI/FactExplorer/FactExplorer.cs b/Assets/Scripts/UI/FactExplorer/FactExplorer.cs index 199362a79643f4377fe4564388b334ea5180b356..439eaf681dcdfb4a8d1d55aad4b676614633deed 100644 --- a/Assets/Scripts/UI/FactExplorer/FactExplorer.cs +++ b/Assets/Scripts/UI/FactExplorer/FactExplorer.cs @@ -33,7 +33,7 @@ private void Update() DestroyIfClickedOutside(); } - public void Initialize(Fact fact) + public void Initialize(Fact fact, Vector3 factPosition) { mainFact = fact; parentFacts = GetParentFacts(); @@ -43,6 +43,8 @@ public void Initialize(Fact fact) Debug.Log($"Children of {mainFact.Label}: {string.Join(", ", childFacts.Select(cf => cf.Label))}"); UpdateFactExplorerUI(); + + MoveToPreferredPosition(factPosition); } #endregion UnityMethods @@ -94,6 +96,21 @@ private void DestroyIfClickedOutside() 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 #region Spawner diff --git a/Assets/Scripts/UI/FactExplorer/OpenFactExplorer.cs b/Assets/Scripts/UI/FactExplorer/OpenFactExplorer.cs index 8e50e2a194b3809e8d74c60d7c1c0b7927bf1278..ddd11571a2971652a127d222725746c7e50b7128 100644 --- a/Assets/Scripts/UI/FactExplorer/OpenFactExplorer.cs +++ b/Assets/Scripts/UI/FactExplorer/OpenFactExplorer.cs @@ -21,7 +21,7 @@ public void OnPointerClick(PointerEventData eventData) var fact = transform.GetComponent<FactWrapper>().fact; factExplorer = Instantiate(factExplorerPrefab.transform, Input.mousePosition, Quaternion.identity, parent); - factExplorer.GetComponent<FactExplorer>().Initialize(fact); + factExplorer.GetComponent<FactExplorer>().Initialize(fact, transform.position); } } }