HeavenStudioPlus/Assets/Scripts/LevelEditor/Tooltip.cs

120 lines
4 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using TMPro;
2022-03-14 14:21:05 +00:00
namespace HeavenStudio.Editor
{
public class Tooltip : MonoBehaviour
{
2022-01-18 00:40:23 +00:00
private RectTransform rectTransform;
[SerializeField] private RectTransform canvasRect;
[SerializeField] private RectTransform background;
[SerializeField] private TMP_Text text;
[SerializeField] private CanvasGroup group;
public static Tooltip instance { get; private set; }
private void Awake()
{
instance = this;
2022-01-18 00:40:23 +00:00
rectTransform = GetComponent<RectTransform>();
group.alpha = 0;
}
private void Update()
{
2022-06-28 19:57:32 +00:00
Vector3 anchoredPosition = Input.mousePosition;
Camera camera = Editor.instance.EditorCamera;
2022-06-28 19:57:32 +00:00
Vector3 canvasScale = Editor.instance.MainCanvas.transform.localScale;
Vector2 scale = new Vector2(canvasScale.x, canvasScale.y);
float toolTipScale = camera.pixelWidth / 1280f;
2022-01-18 00:40:23 +00:00
2022-06-28 19:57:32 +00:00
if (anchoredPosition.x + background.rect.width * toolTipScale > camera.pixelWidth)
2022-01-18 00:40:23 +00:00
{
2022-06-28 19:57:32 +00:00
anchoredPosition.x = camera.pixelWidth - background.rect.width * toolTipScale;
2022-01-18 00:40:23 +00:00
}
2022-06-28 19:57:32 +00:00
if (anchoredPosition.x < -camera.pixelWidth)
2022-01-18 00:40:23 +00:00
{
2022-06-28 19:57:32 +00:00
anchoredPosition.x = -camera.pixelWidth;
2022-01-18 00:40:23 +00:00
}
2022-06-28 19:57:32 +00:00
if (anchoredPosition.y + background.rect.height * toolTipScale > camera.pixelHeight)
2022-01-18 00:40:23 +00:00
{
2022-06-28 19:57:32 +00:00
anchoredPosition.y = camera.pixelHeight - background.rect.height * toolTipScale;
2022-01-18 00:40:23 +00:00
}
2022-06-28 19:57:32 +00:00
if (anchoredPosition.y < -camera.pixelHeight)
2022-01-18 00:40:23 +00:00
{
2022-06-28 19:57:32 +00:00
anchoredPosition.y = -camera.pixelHeight;
2022-01-18 00:40:23 +00:00
}
2022-06-28 19:57:32 +00:00
anchoredPosition.z = camera.nearClipPlane;
anchoredPosition = camera.ScreenToWorldPoint(anchoredPosition);
rectTransform.anchoredPosition = anchoredPosition / scale;
2022-01-18 00:40:23 +00:00
}
public static void OnEnter(string tooltipText, string altTooltipText)
2022-01-18 00:40:23 +00:00
{
instance.OnEnterPrivate(tooltipText, altTooltipText);
}
2022-01-18 00:40:23 +00:00
public static void OnExit()
{
2022-01-18 00:40:23 +00:00
instance.OnExitPrivate();
Editor.instance.tooltipText.text = "";
Editor.instance.tooltipText.ForceMeshUpdate();
}
private void OnEnterPrivate(string tooltipText, string altTooltipText)
{
2022-01-18 00:40:23 +00:00
group.alpha = 1;
Advanced Blocks (#720) * play sfx and play animation blocks i also changed prescheduleFunction to preFunction, and removed the unused preFunction argument in GameAction i can revert this if need be but it just seemed vestigial * count in rework + preloading, multisound addition multisound was using an array that was converted to a list..? very silly when you consider it's a list first so sometimes it's list -> array -> list lol new Count-In and Play SFX block preloads sfx now!! epic. * prefab-ify event properties, Button EntityType * things are very nearly working! however i just hit an insane hurdle. how do i modify a dropdown while still being able to access the index/int value of that param directly. UGHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH * okay it's WORKING now i just need to do some better dropdown stuff * ITS WORKING ITS WORKING ITS WORKING arbitrary animations, now accessible to those without prefab knowledge! and it's piss easy to use!! * about to make a struct + class, tooltip improvements gonna make the struct define it, then the class will actually be the dropdown this is gonna make things so so so so much easier to comprehend * finishing up, probably one more commit after this * split up Dropdown into Dropdown and DropdownObj, which basically fixed all of my problems lol * fixed a count bug * added param tooltip toggle * grah it's ALMOST DONE * it's 99.9% finished. just some touch ups, i don't think i even know of any bugs * alright, looks like that's all the bugs gone * EVERYTHING IS FINISHED!!
2024-02-26 01:46:23 +00:00
text.text = tooltipText;
text.ForceMeshUpdate();
Vector2 textSize = text.GetRenderedValues(false);
Vector2 paddingSize = new Vector2(8, 8);
background.sizeDelta = textSize + paddingSize;
Editor.instance.tooltipText.text = altTooltipText.Replace("\n", "");
Editor.instance.tooltipText.ForceMeshUpdate();
2022-01-18 00:40:23 +00:00
}
private void OnExitPrivate()
{
group.alpha = 0;
}
private void SetText(string tooltipText)
{
text.text = tooltipText;
text.ForceMeshUpdate();
Vector2 textSize = text.GetRenderedValues(false);
Vector2 paddingSize = new Vector2(8, 8);
background.sizeDelta = textSize + paddingSize;
}
Advanced Blocks (#720) * play sfx and play animation blocks i also changed prescheduleFunction to preFunction, and removed the unused preFunction argument in GameAction i can revert this if need be but it just seemed vestigial * count in rework + preloading, multisound addition multisound was using an array that was converted to a list..? very silly when you consider it's a list first so sometimes it's list -> array -> list lol new Count-In and Play SFX block preloads sfx now!! epic. * prefab-ify event properties, Button EntityType * things are very nearly working! however i just hit an insane hurdle. how do i modify a dropdown while still being able to access the index/int value of that param directly. UGHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH * okay it's WORKING now i just need to do some better dropdown stuff * ITS WORKING ITS WORKING ITS WORKING arbitrary animations, now accessible to those without prefab knowledge! and it's piss easy to use!! * about to make a struct + class, tooltip improvements gonna make the struct define it, then the class will actually be the dropdown this is gonna make things so so so so much easier to comprehend * finishing up, probably one more commit after this * split up Dropdown into Dropdown and DropdownObj, which basically fixed all of my problems lol * fixed a count bug * added param tooltip toggle * grah it's ALMOST DONE * it's 99.9% finished. just some touch ups, i don't think i even know of any bugs * alright, looks like that's all the bugs gone * EVERYTHING IS FINISHED!!
2024-02-26 01:46:23 +00:00
public static void AddTooltip(GameObject g, string tooltipText, string altTooltipText = null)
{
Advanced Blocks (#720) * play sfx and play animation blocks i also changed prescheduleFunction to preFunction, and removed the unused preFunction argument in GameAction i can revert this if need be but it just seemed vestigial * count in rework + preloading, multisound addition multisound was using an array that was converted to a list..? very silly when you consider it's a list first so sometimes it's list -> array -> list lol new Count-In and Play SFX block preloads sfx now!! epic. * prefab-ify event properties, Button EntityType * things are very nearly working! however i just hit an insane hurdle. how do i modify a dropdown while still being able to access the index/int value of that param directly. UGHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH * okay it's WORKING now i just need to do some better dropdown stuff * ITS WORKING ITS WORKING ITS WORKING arbitrary animations, now accessible to those without prefab knowledge! and it's piss easy to use!! * about to make a struct + class, tooltip improvements gonna make the struct define it, then the class will actually be the dropdown this is gonna make things so so so so much easier to comprehend * finishing up, probably one more commit after this * split up Dropdown into Dropdown and DropdownObj, which basically fixed all of my problems lol * fixed a count bug * added param tooltip toggle * grah it's ALMOST DONE * it's 99.9% finished. just some touch ups, i don't think i even know of any bugs * alright, looks like that's all the bugs gone * EVERYTHING IS FINISHED!!
2024-02-26 01:46:23 +00:00
altTooltipText ??= tooltipText;
EventTrigger et = g.AddComponent<EventTrigger>();
EventTrigger.Entry pointerEnter = new EventTrigger.Entry();
pointerEnter.eventID = EventTriggerType.PointerEnter;
pointerEnter.callback.AddListener((data) => { OnEnter(tooltipText, altTooltipText); });
EventTrigger.Entry pointerExit = new EventTrigger.Entry();
pointerExit.eventID = EventTriggerType.PointerExit;
pointerExit.callback.AddListener((data) => { OnExit(); });
et.triggers.Add(pointerEnter);
et.triggers.Add(pointerExit);
}
}
}