From e143d2ad3d8a290824620bd8f1ec8db3e459f509 Mon Sep 17 00:00:00 2001 From: LadyAliceMargatroid Date: Mon, 26 Aug 2024 20:08:43 -0700 Subject: [PATCH] Certain UI elements that are supposed to hide if a feature is disabled are properly refreshed at the beginning Setup hover UI functionality --- .../DevTools/HoverUI/HoverUIChild.cs | 77 +++++++++++++++ .../DevTools/HoverUI/HoverUIManager.cs | 96 +++++++++++++++++++ .../DevTools/Panels/DunFlowPanel.cs | 3 + .../DevTools/Panels/DunGenPlusPanel.cs | 10 +- .../DevTools/UIElements/BaseUIElement.cs | 10 +- .../UIElements/Collections/Parameters.cs | 8 ++ DunGenPlus/DunGenPlus/DunGenPlus.csproj | 2 + 7 files changed, 204 insertions(+), 2 deletions(-) create mode 100644 DunGenPlus/DunGenPlus/DevTools/HoverUI/HoverUIChild.cs create mode 100644 DunGenPlus/DunGenPlus/DevTools/HoverUI/HoverUIManager.cs diff --git a/DunGenPlus/DunGenPlus/DevTools/HoverUI/HoverUIChild.cs b/DunGenPlus/DunGenPlus/DevTools/HoverUI/HoverUIChild.cs new file mode 100644 index 0000000..a697a1a --- /dev/null +++ b/DunGenPlus/DunGenPlus/DevTools/HoverUI/HoverUIChild.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine.EventSystems; +using UnityEngine; + +namespace DunGenPlus.DevTools.HoverUI { + internal class HoverUIChild: MonoBehaviour, IPointerEnterHandler, IPointerExitHandler { + + public enum DisplayDirection { Up, Down, Left, Right }; + + [Header("Display Values")] + public DisplayDirection direction = DisplayDirection.Up; + public float directionDistance = 16f; + public RectTransform rectTransform; + public bool hovering; + + [Header("Display")] + [TextArea(2, 4)] + public string hoverText; + + void Reset(){ + rectTransform = GetComponent(); + } + + public string GetHoverString => hoverText; + + public void OnPointerEnter(PointerEventData eventData) { + HoverUIManager.Instance.UpdateDisplay(this); + hovering = true; + } + + public void OnPointerExit(PointerEventData eventData) { + HoverUIManager.Instance.ClearDisplay(this); + hovering = false; + } + + private void OnDisable() { + HoverUIManager.Instance.ClearDisplay(this); + hovering = false; + } + + public (Vector2 pivot, Vector3 position) GetRenderPosition(){ + return GetRenderPosition(direction, directionDistance); + } + + public (Vector2 pivot, Vector3 position) GetRenderPosition(DisplayDirection direction, float directionDistance){ + Vector2 pivot; + Vector3 position; + + var corners = new Vector3[4]; + rectTransform.GetWorldCorners(corners); + + if (direction == DisplayDirection.Up){ + pivot = new Vector2(0.5f, 0f); + position = (corners[1] + corners[2]) * 0.5f + new Vector3(0f, directionDistance); + } else if (direction == DisplayDirection.Down){ + pivot = new Vector2(0.5f, 1f); + position = (corners[0] + corners[3]) * 0.5f - new Vector3(0f, directionDistance); + } else if (direction == DisplayDirection.Left){ + pivot = new Vector2(1f, 0.5f); + position = (corners[0] + corners[1]) * 0.5f - new Vector3(directionDistance, 0f); + } else if (direction == DisplayDirection.Right){ + pivot = new Vector2(0f, 0.5f); + position = (corners[2] + corners[3]) * 0.5f + new Vector3(directionDistance, 0f); + } else { + pivot = Vector2.zero; + position = Vector3.zero; + } + + return (pivot, position); + } + + } +} diff --git a/DunGenPlus/DunGenPlus/DevTools/HoverUI/HoverUIManager.cs b/DunGenPlus/DunGenPlus/DevTools/HoverUI/HoverUIManager.cs new file mode 100644 index 0000000..6b669aa --- /dev/null +++ b/DunGenPlus/DunGenPlus/DevTools/HoverUI/HoverUIManager.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TMPro; +using UnityEngine; + +namespace DunGenPlus.DevTools.HoverUI { + internal class HoverUIManager : MonoBehaviour { + + public static HoverUIManager Instance { get; private set; } + + + [Header("References/Default UI")] + public RectTransform mainCanvasRectTransform; + public Canvas canvas; + public RectTransform background; + + public RectTransform textMeshRectTransform; + public TextMeshProUGUI textMesh; + public Vector2 preferredTextMeshSize = new Vector2(600f, 50f); + + [Header("Debug")] + public HoverUIChild previousChild; + + private void Awake() { + Instance = this; + } + + public void UpdateDisplay(HoverUIChild child) { + + var text = child.GetHoverString; + if (string.IsNullOrWhiteSpace(text)) { + return; + } + + previousChild = child; + canvas.enabled = true; + + textMesh.text = text; + textMeshRectTransform.sizeDelta = preferredTextMeshSize; + textMesh.ForceMeshUpdate(); + + var render = textMesh.GetRenderedValues(); + var margin = textMesh.margin; + var sizeDelta = render + new Vector2(margin.x + margin.z, margin.y + margin.w); + + var posPivot = GetPositionAndPivot(sizeDelta); + + background.pivot = posPivot.pivot; + background.position = posPivot.position; + + background.sizeDelta = sizeDelta; + textMeshRectTransform.sizeDelta = sizeDelta; + textMesh.ForceMeshUpdate(); + } + + public (Vector2 position, Vector2 pivot) GetPositionAndPivot(Vector2 sizeDelta){ + if (previousChild == null) return (Vector2.zero, Vector2.zero); + return GetPositionAndPivot(sizeDelta, previousChild.GetRenderPosition()); + } + + public (Vector2 position, Vector2 pivot) GetPositionAndPivot(Vector2 sizeDelta, (Vector2 pivot, Vector3 position) referencePos){ + var scaledSizeDelta = sizeDelta * mainCanvasRectTransform.localScale.x; + + var pos = referencePos.position; + var pivot = referencePos.pivot; + var corners = new Vector3[4]; + mainCanvasRectTransform.GetWorldCorners(corners); + + var left = corners[0].x; + var bottom = corners[0].y; + var right = corners[2].x; + var top = corners[2].y; + + pos.x = Mathf.Clamp(pos.x, left + scaledSizeDelta.x * pivot.x, right - scaledSizeDelta.x * (1f - pivot.x)); + pos.y = Mathf.Clamp(pos.y, bottom + scaledSizeDelta.y * pivot.y, top - scaledSizeDelta.y * (1f - pivot.y)); + + return (pos, pivot); + } + + + public void RefreshDisplay(){ + if (previousChild) UpdateDisplay(previousChild); + } + + public void ClearDisplay(HoverUIChild child){ + if (previousChild != child) return; + previousChild = null; + canvas.enabled = false; + } + + } + +} diff --git a/DunGenPlus/DunGenPlus/DevTools/Panels/DunFlowPanel.cs b/DunGenPlus/DunGenPlus/DevTools/Panels/DunFlowPanel.cs index 26c431d..70f4754 100644 --- a/DunGenPlus/DunGenPlus/DevTools/Panels/DunFlowPanel.cs +++ b/DunGenPlus/DunGenPlus/DevTools/Panels/DunFlowPanel.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using UnityEngine; +using static UnityEngine.Rendering.DebugUI; namespace DunGenPlus.DevTools.Panels { internal class DunFlowPanel : BasePanel { @@ -49,6 +50,8 @@ namespace DunGenPlus.DevTools.Panels { manager.CreateSpaceUIField(parentTransform); manager.CreateListUIField(parentTransform, "Lines", selectedDungeonFlow.Lines); manager.CreateSpaceUIField(parentTransform); + + branchPathParentGameobject.SetActive(selectedDungeonFlow.BranchMode == BranchMode.Global); } public void ClearPanel(){ diff --git a/DunGenPlus/DunGenPlus/DevTools/Panels/DunGenPlusPanel.cs b/DunGenPlus/DunGenPlus/DevTools/Panels/DunGenPlusPanel.cs index 1279b65..168d195 100644 --- a/DunGenPlus/DunGenPlus/DevTools/Panels/DunGenPlusPanel.cs +++ b/DunGenPlus/DunGenPlus/DevTools/Panels/DunGenPlusPanel.cs @@ -13,6 +13,7 @@ using UnityEngine.UI; using DunGenPlus.DevTools.UIElements; using DunGenPlus.DevTools.UIElements.Collections; using DunGenPlus.DevTools.Panels.Collections; +using static UnityEngine.Rendering.DebugUI; namespace DunGenPlus.DevTools.Panels { internal class DunGenPlusPanel : BasePanel { @@ -149,7 +150,14 @@ namespace DunGenPlus.DevTools.Panels { manager.CreateBoolInputField(parentTransform, "Use Random Guaranteed Scrap", properties.MiscellaneousProperties.UseRandomGuaranteedScrapSpawn, SetUseRandomGuaranteedScrap); manager.CreateSpaceUIField(parentTransform); - dungeonBoundsHelperGameObject.SetActive(selectedExtenderer.Properties.DungeonBoundsProperties.UseDungeonBounds); + mainPathParentGameobject.SetActive(properties.MainPathProperties.MainPathCount > 1); + dungeonBoundsParentGameobject.SetActive(properties.DungeonBoundsProperties.UseDungeonBounds); + dungeonBoundsHelperGameObject.SetActive(properties.DungeonBoundsProperties.UseDungeonBounds); + archetypesNodesParentGameobject.SetActive(properties.NormalNodeArchetypesProperties.AddArchetypesToNormalNodes); + forcedTilesParentGameobject.SetActive(properties.ForcedTilesProperties.UseForcedTiles); + branchLoopBoostParentGameobject.SetActive(properties.BranchPathMultiSimulationProperties.UseBranchPathMultiSim); + maxShadowsParentGameobject.SetActive(properties.MiscellaneousProperties.UseMaxShadowsRequestUpdate); + UpdateDungeonBoundsHelper(); } diff --git a/DunGenPlus/DunGenPlus/DevTools/UIElements/BaseUIElement.cs b/DunGenPlus/DunGenPlus/DevTools/UIElements/BaseUIElement.cs index a4555ae..0c694ad 100644 --- a/DunGenPlus/DunGenPlus/DevTools/UIElements/BaseUIElement.cs +++ b/DunGenPlus/DunGenPlus/DevTools/UIElements/BaseUIElement.cs @@ -1,4 +1,5 @@ -using DunGenPlus.DevTools.UIElements.Collections; +using DunGenPlus.DevTools.HoverUI; +using DunGenPlus.DevTools.UIElements.Collections; using System; using System.Collections.Generic; using System.Linq; @@ -20,6 +21,7 @@ namespace DunGenPlus.DevTools.UIElements { public void SetupBase(TitleParameter titleParameter) { title = titleParameter.text; SetText(title); + SetHoverText(titleParameter.hoverText); layoutOffset = titleParameter.offset; if (layoutElement) { @@ -32,6 +34,12 @@ namespace DunGenPlus.DevTools.UIElements { titleTextMesh.text = value; } + public void SetHoverText(string value){ + var hoverChild = GetComponentInChildren(); + if (hoverChild) { + hoverChild.hoverText = value; + } + } } } diff --git a/DunGenPlus/DunGenPlus/DevTools/UIElements/Collections/Parameters.cs b/DunGenPlus/DunGenPlus/DevTools/UIElements/Collections/Parameters.cs index 579693f..fe08629 100644 --- a/DunGenPlus/DunGenPlus/DevTools/UIElements/Collections/Parameters.cs +++ b/DunGenPlus/DunGenPlus/DevTools/UIElements/Collections/Parameters.cs @@ -8,10 +8,18 @@ namespace DunGenPlus.DevTools.UIElements.Collections { internal struct TitleParameter { public string text; public float offset; + public string hoverText; public TitleParameter(string text, float offset = 0f) { this.text = text; this.offset = offset; + this.hoverText = null; + } + + public TitleParameter(string text, string hoverText, float offset = 0f){ + this.text = text; + this.offset = offset; + this.hoverText = hoverText; } public static implicit operator TitleParameter(string text) => new TitleParameter(text); diff --git a/DunGenPlus/DunGenPlus/DunGenPlus.csproj b/DunGenPlus/DunGenPlus/DunGenPlus.csproj index 604af37..3223092 100644 --- a/DunGenPlus/DunGenPlus/DunGenPlus.csproj +++ b/DunGenPlus/DunGenPlus/DunGenPlus.csproj @@ -149,6 +149,8 @@ + +