Certain UI elements that are supposed to hide if a feature is disabled are properly refreshed at the beginning

Setup hover UI functionality
This commit is contained in:
LadyAliceMargatroid 2024-08-26 20:08:43 -07:00
parent e9c8da9c51
commit e143d2ad3d
7 changed files with 204 additions and 2 deletions

View File

@ -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<RectTransform>();
}
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);
}
}
}

View File

@ -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;
}
}
}

View File

@ -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(){

View File

@ -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();
}

View File

@ -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<HoverUIChild>();
if (hoverChild) {
hoverChild.hoverText = value;
}
}
}
}

View File

@ -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);

View File

@ -149,6 +149,8 @@
<Compile Include="DevTools\DevDebugManager.cs" />
<Compile Include="DevTools\DevDebugManagerUI.cs" />
<Compile Include="DevTools\DevDebugOpen.cs" />
<Compile Include="DevTools\HoverUI\HoverUIChild.cs" />
<Compile Include="DevTools\HoverUI\HoverUIManager.cs" />
<Compile Include="DevTools\Panels\BasePanel.cs" />
<Compile Include="DevTools\Panels\Collections\DungeonFlowCacheAssets.cs" />
<Compile Include="DevTools\Panels\DunFlowPanel.cs" />