Tabs -> spaces

pull/413/head
Lucas Vinicius 2018-11-25 23:04:28 -02:00
parent 6c639c7f51
commit f9e216adae
2 changed files with 270 additions and 270 deletions

View File

@ -30,17 +30,17 @@ namespace UnityEngine.UI.Extensions
private Text _text; private Text _text;
private RectTransform _rectTransform, canvasRectTransform; private RectTransform _rectTransform, canvasRectTransform;
[Tooltip("The canvas used by the tooltip as positioning and scaling reference. Should usually be the root Canvas of the hierarchy this component is in")] [Tooltip("The canvas used by the tooltip as positioning and scaling reference. Should usually be the root Canvas of the hierarchy this component is in")]
public Canvas canvas; public Canvas canvas;
[Tooltip("Sets if tooltip triggers will run ForceUpdateCanvases and refresh the tooltip's layout group " + [Tooltip("Sets if tooltip triggers will run ForceUpdateCanvases and refresh the tooltip's layout group " +
"(if any) when hovered, in order to prevent momentaneous misplacement sometimes caused by ContentSizeFitters")] "(if any) when hovered, in order to prevent momentaneous misplacement sometimes caused by ContentSizeFitters")]
public bool tooltipTriggersCanForceCanvasUpdate = false; public bool tooltipTriggersCanForceCanvasUpdate = false;
/// <summary> /// <summary>
/// the tooltip's Layout Group, if any /// the tooltip's Layout Group, if any
/// </summary> /// </summary>
private LayoutGroup _layoutGroup; private LayoutGroup _layoutGroup;
//if the tooltip is inside a UI element //if the tooltip is inside a UI element
private bool _inside; private bool _inside;
@ -49,72 +49,72 @@ namespace UnityEngine.UI.Extensions
public float YShift,xShift; public float YShift,xShift;
[HideInInspector] [HideInInspector]
public RenderMode guiMode; public RenderMode guiMode;
private Camera _guiCamera; private Camera _guiCamera;
public Camera GuiCamera public Camera GuiCamera
{
get
{
if (!_guiCamera) {
_guiCamera = Camera.main;
}
return _guiCamera;
}
}
private Vector3 screenLowerLeft, screenUpperRight, shiftingVector;
/// <summary>
/// a screen-space point where the tooltip would be placed before applying X and Y shifts and border checks
/// </summary>
private Vector3 baseTooltipPos;
private Vector3 newTTPos;
private Vector3 adjustedNewTTPos;
private Vector3 adjustedTTLocalPos;
private Vector3 shifterForBorders;
private float borderTest;
// Standard Singleton Access
private static ToolTip instance;
public static ToolTip Instance
{
get
{
if (instance == null)
instance = FindObjectOfType<ToolTip>();
return instance;
}
}
void Reset() {
canvas = GetComponentInParent<Canvas>();
canvas = canvas.rootCanvas;
}
// Use this for initialization
public void Awake()
{ {
instance = this; get
if (!canvas) { {
canvas = GetComponentInParent<Canvas>(); if (!_guiCamera) {
canvas = canvas.rootCanvas; _guiCamera = Camera.main;
} }
_guiCamera = canvas.worldCamera; return _guiCamera;
}
}
private Vector3 screenLowerLeft, screenUpperRight, shiftingVector;
/// <summary>
/// a screen-space point where the tooltip would be placed before applying X and Y shifts and border checks
/// </summary>
private Vector3 baseTooltipPos;
private Vector3 newTTPos;
private Vector3 adjustedNewTTPos;
private Vector3 adjustedTTLocalPos;
private Vector3 shifterForBorders;
private float borderTest;
// Standard Singleton Access
private static ToolTip instance;
public static ToolTip Instance
{
get
{
if (instance == null)
instance = FindObjectOfType<ToolTip>();
return instance;
}
}
void Reset() {
canvas = GetComponentInParent<Canvas>();
canvas = canvas.rootCanvas;
}
// Use this for initialization
public void Awake()
{
instance = this;
if (!canvas) {
canvas = GetComponentInParent<Canvas>();
canvas = canvas.rootCanvas;
}
_guiCamera = canvas.worldCamera;
guiMode = canvas.renderMode; guiMode = canvas.renderMode;
_rectTransform = GetComponent<RectTransform>(); _rectTransform = GetComponent<RectTransform>();
canvasRectTransform = canvas.GetComponent<RectTransform>(); canvasRectTransform = canvas.GetComponent<RectTransform>();
_layoutGroup = GetComponentInChildren<LayoutGroup>(); _layoutGroup = GetComponentInChildren<LayoutGroup>();
_text = GetComponentInChildren<Text>(); _text = GetComponentInChildren<Text>();
_inside = false; _inside = false;
@ -125,19 +125,19 @@ namespace UnityEngine.UI.Extensions
public void SetTooltip(string ttext, Vector3 basePos, bool refreshCanvasesBeforeGetSize = false) public void SetTooltip(string ttext, Vector3 basePos, bool refreshCanvasesBeforeGetSize = false)
{ {
baseTooltipPos = basePos; baseTooltipPos = basePos;
//set the text //set the text
if (_text) { if (_text) {
_text.text = ttext; _text.text = ttext;
} }
else { else {
Debug.LogWarning("[ToolTip] Couldn't set tooltip text, tooltip has no child Text component"); Debug.LogWarning("[ToolTip] Couldn't set tooltip text, tooltip has no child Text component");
} }
ContextualTooltipUpdate(refreshCanvasesBeforeGetSize); ContextualTooltipUpdate(refreshCanvasesBeforeGetSize);
} }
//call this function on mouse exit to deactivate the template //call this function on mouse exit to deactivate the template
public void HideTooltip() public void HideTooltip()
@ -151,165 +151,165 @@ namespace UnityEngine.UI.Extensions
{ {
if (_inside) if (_inside)
{ {
ContextualTooltipUpdate(); ContextualTooltipUpdate();
} }
} }
/// <summary> /// <summary>
/// forces rebuilding of Canvases in order to update the tooltip's content size fitting. /// forces rebuilding of Canvases in order to update the tooltip's content size fitting.
/// Can prevent the tooltip from being visibly misplaced for one frame when being resized. /// Can prevent the tooltip from being visibly misplaced for one frame when being resized.
/// Only runs if tooltipTriggersCanForceCanvasUpdate is true /// Only runs if tooltipTriggersCanForceCanvasUpdate is true
/// </summary> /// </summary>
public void RefreshTooltipSize() { public void RefreshTooltipSize() {
if (tooltipTriggersCanForceCanvasUpdate) { if (tooltipTriggersCanForceCanvasUpdate) {
Canvas.ForceUpdateCanvases(); Canvas.ForceUpdateCanvases();
if (_layoutGroup) { if (_layoutGroup) {
_layoutGroup.enabled = false; _layoutGroup.enabled = false;
_layoutGroup.enabled = true; _layoutGroup.enabled = true;
} }
} }
} }
/// <summary> /// <summary>
/// Runs the appropriate tooltip placement method, according to the parent canvas's render mode /// Runs the appropriate tooltip placement method, according to the parent canvas's render mode
/// </summary> /// </summary>
/// <param name="refreshCanvasesBeforeGettingSize"></param> /// <param name="refreshCanvasesBeforeGettingSize"></param>
public void ContextualTooltipUpdate(bool refreshCanvasesBeforeGettingSize = false) { public void ContextualTooltipUpdate(bool refreshCanvasesBeforeGettingSize = false) {
switch (guiMode) { switch (guiMode) {
case RenderMode.ScreenSpaceCamera: case RenderMode.ScreenSpaceCamera:
OnScreenSpaceCamera(refreshCanvasesBeforeGettingSize); OnScreenSpaceCamera(refreshCanvasesBeforeGettingSize);
break; break;
case RenderMode.ScreenSpaceOverlay: case RenderMode.ScreenSpaceOverlay:
OnScreenSpaceOverlay(refreshCanvasesBeforeGettingSize); OnScreenSpaceOverlay(refreshCanvasesBeforeGettingSize);
break; break;
} }
} }
//main tooltip edge of screen guard and movement - camera //main tooltip edge of screen guard and movement - camera
public void OnScreenSpaceCamera(bool refreshCanvasesBeforeGettingSize = false) public void OnScreenSpaceCamera(bool refreshCanvasesBeforeGettingSize = false)
{ {
shiftingVector.x = xShift; shiftingVector.x = xShift;
shiftingVector.y = YShift; shiftingVector.y = YShift;
baseTooltipPos.z = canvas.planeDistance; baseTooltipPos.z = canvas.planeDistance;
newTTPos = GuiCamera.ScreenToViewportPoint(baseTooltipPos - shiftingVector); newTTPos = GuiCamera.ScreenToViewportPoint(baseTooltipPos - shiftingVector);
adjustedNewTTPos = GuiCamera.ViewportToWorldPoint(newTTPos); adjustedNewTTPos = GuiCamera.ViewportToWorldPoint(newTTPos);
gameObject.SetActive(true); gameObject.SetActive(true);
if (refreshCanvasesBeforeGettingSize) RefreshTooltipSize(); if (refreshCanvasesBeforeGettingSize) RefreshTooltipSize();
//consider scaled dimensions when comparing against the edges //consider scaled dimensions when comparing against the edges
width = transform.lossyScale.x * _rectTransform.sizeDelta[0]; width = transform.lossyScale.x * _rectTransform.sizeDelta[0];
height = transform.lossyScale.y * _rectTransform.sizeDelta[1]; height = transform.lossyScale.y * _rectTransform.sizeDelta[1];
// check and solve problems for the tooltip that goes out of the screen on the horizontal axis // check and solve problems for the tooltip that goes out of the screen on the horizontal axis
RectTransformUtility.ScreenPointToWorldPointInRectangle(canvasRectTransform, Vector2.zero, GuiCamera, out screenLowerLeft); RectTransformUtility.ScreenPointToWorldPointInRectangle(canvasRectTransform, Vector2.zero, GuiCamera, out screenLowerLeft);
RectTransformUtility.ScreenPointToWorldPointInRectangle(canvasRectTransform, new Vector2(Screen.width, Screen.height), GuiCamera, out screenUpperRight); RectTransformUtility.ScreenPointToWorldPointInRectangle(canvasRectTransform, new Vector2(Screen.width, Screen.height), GuiCamera, out screenUpperRight);
//check for right edge of screen //check for right edge of screen
borderTest = (adjustedNewTTPos.x + width / 2); borderTest = (adjustedNewTTPos.x + width / 2);
if (borderTest > screenUpperRight.x) if (borderTest > screenUpperRight.x)
{ {
shifterForBorders.x = borderTest - screenUpperRight.x; shifterForBorders.x = borderTest - screenUpperRight.x;
adjustedNewTTPos.x -= shifterForBorders.x; adjustedNewTTPos.x -= shifterForBorders.x;
} }
//check for left edge of screen //check for left edge of screen
borderTest = (adjustedNewTTPos.x - width / 2); borderTest = (adjustedNewTTPos.x - width / 2);
if (borderTest < screenLowerLeft.x) if (borderTest < screenLowerLeft.x)
{ {
shifterForBorders.x = screenLowerLeft.x - borderTest; shifterForBorders.x = screenLowerLeft.x - borderTest;
adjustedNewTTPos.x += shifterForBorders.x; adjustedNewTTPos.x += shifterForBorders.x;
} }
// check and solve problems for the tooltip that goes out of the screen on the vertical axis // check and solve problems for the tooltip that goes out of the screen on the vertical axis
//check for lower edge of the screen //check for lower edge of the screen
borderTest = (adjustedNewTTPos.y - height / 2); borderTest = (adjustedNewTTPos.y - height / 2);
if (borderTest < screenLowerLeft.y) { if (borderTest < screenLowerLeft.y) {
shifterForBorders.y = screenLowerLeft.y - borderTest; shifterForBorders.y = screenLowerLeft.y - borderTest;
adjustedNewTTPos.y += shifterForBorders.y; adjustedNewTTPos.y += shifterForBorders.y;
} }
//check for upper edge of the screen //check for upper edge of the screen
borderTest = (adjustedNewTTPos.y + height / 2); borderTest = (adjustedNewTTPos.y + height / 2);
if (borderTest > screenUpperRight.y) if (borderTest > screenUpperRight.y)
{ {
shifterForBorders.y = borderTest - screenUpperRight.y; shifterForBorders.y = borderTest - screenUpperRight.y;
adjustedNewTTPos.y -= shifterForBorders.y; adjustedNewTTPos.y -= shifterForBorders.y;
} }
//failed attempt to circumvent issues caused when rotating the camera //failed attempt to circumvent issues caused when rotating the camera
adjustedNewTTPos = transform.rotation * adjustedNewTTPos; adjustedNewTTPos = transform.rotation * adjustedNewTTPos;
transform.position = adjustedNewTTPos; transform.position = adjustedNewTTPos;
adjustedTTLocalPos = transform.localPosition; adjustedTTLocalPos = transform.localPosition;
adjustedTTLocalPos.z = 0; adjustedTTLocalPos.z = 0;
transform.localPosition = adjustedTTLocalPos; transform.localPosition = adjustedTTLocalPos;
_inside = true; _inside = true;
} }
//main tooltip edge of screen guard and movement - overlay //main tooltip edge of screen guard and movement - overlay
public void OnScreenSpaceOverlay(bool refreshCanvasesBeforeGettingSize = false) { public void OnScreenSpaceOverlay(bool refreshCanvasesBeforeGettingSize = false) {
shiftingVector.x = xShift; shiftingVector.x = xShift;
shiftingVector.y = YShift; shiftingVector.y = YShift;
newTTPos = (baseTooltipPos - shiftingVector) / canvas.scaleFactor; newTTPos = (baseTooltipPos - shiftingVector) / canvas.scaleFactor;
adjustedNewTTPos = newTTPos; adjustedNewTTPos = newTTPos;
gameObject.SetActive(true); gameObject.SetActive(true);
if (refreshCanvasesBeforeGettingSize) RefreshTooltipSize(); if (refreshCanvasesBeforeGettingSize) RefreshTooltipSize();
width = _rectTransform.sizeDelta[0]; width = _rectTransform.sizeDelta[0];
height = _rectTransform.sizeDelta[1]; height = _rectTransform.sizeDelta[1];
// check and solve problems for the tooltip that goes out of the screen on the horizontal axis // check and solve problems for the tooltip that goes out of the screen on the horizontal axis
//screen's 0 = overlay canvas's 0 (always?) //screen's 0 = overlay canvas's 0 (always?)
screenLowerLeft = Vector3.zero; screenLowerLeft = Vector3.zero;
screenUpperRight = canvasRectTransform.sizeDelta; screenUpperRight = canvasRectTransform.sizeDelta;
//check for right edge of screen //check for right edge of screen
borderTest = (newTTPos.x + width / 2); borderTest = (newTTPos.x + width / 2);
if (borderTest > screenUpperRight.x) { if (borderTest > screenUpperRight.x) {
shifterForBorders.x = borderTest - screenUpperRight.x; shifterForBorders.x = borderTest - screenUpperRight.x;
adjustedNewTTPos.x -= shifterForBorders.x; adjustedNewTTPos.x -= shifterForBorders.x;
} }
//check for left edge of screen //check for left edge of screen
borderTest = (adjustedNewTTPos.x - width / 2); borderTest = (adjustedNewTTPos.x - width / 2);
if (borderTest < screenLowerLeft.x) { if (borderTest < screenLowerLeft.x) {
shifterForBorders.x = screenLowerLeft.x - borderTest; shifterForBorders.x = screenLowerLeft.x - borderTest;
adjustedNewTTPos.x += shifterForBorders.x; adjustedNewTTPos.x += shifterForBorders.x;
} }
// check and solve problems for the tooltip that goes out of the screen on the vertical axis // check and solve problems for the tooltip that goes out of the screen on the vertical axis
//check for lower edge of the screen //check for lower edge of the screen
borderTest = (adjustedNewTTPos.y - height / 2); borderTest = (adjustedNewTTPos.y - height / 2);
if (borderTest < screenLowerLeft.y) { if (borderTest < screenLowerLeft.y) {
shifterForBorders.y = screenLowerLeft.y - borderTest; shifterForBorders.y = screenLowerLeft.y - borderTest;
adjustedNewTTPos.y += shifterForBorders.y; adjustedNewTTPos.y += shifterForBorders.y;
} }
//check for upper edge of the screen //check for upper edge of the screen
borderTest = (adjustedNewTTPos.y + height / 2); borderTest = (adjustedNewTTPos.y + height / 2);
if (borderTest > screenUpperRight.y) { if (borderTest > screenUpperRight.y) {
shifterForBorders.y = borderTest - screenUpperRight.y; shifterForBorders.y = borderTest - screenUpperRight.y;
adjustedNewTTPos.y -= shifterForBorders.y; adjustedNewTTPos.y -= shifterForBorders.y;
} }
//remove scale factor for the actual positioning of the TT //remove scale factor for the actual positioning of the TT
adjustedNewTTPos *= canvas.scaleFactor; adjustedNewTTPos *= canvas.scaleFactor;
transform.position = adjustedNewTTPos; transform.position = adjustedNewTTPos;
_inside = true; _inside = true;
} }
} }
} }

View File

@ -5,104 +5,104 @@ using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions namespace UnityEngine.UI.Extensions
{ {
[RequireComponent(typeof(RectTransform))] [RequireComponent(typeof(RectTransform))]
[AddComponentMenu("UI/Extensions/Tooltip/Tooltip Trigger")] [AddComponentMenu("UI/Extensions/Tooltip/Tooltip Trigger")]
public class TooltipTrigger : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, ISelectHandler, IDeselectHandler public class TooltipTrigger : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, ISelectHandler, IDeselectHandler
{ {
[TextAreaAttribute] [TextAreaAttribute]
public string text; public string text;
public enum TooltipPositioningType { public enum TooltipPositioningType {
mousePosition, mousePosition,
mousePositionAndFollow, mousePositionAndFollow,
transformPosition transformPosition
} }
[Tooltip("Defines where the tooltip will be placed and how that placement will occur. Transform position will always be used if this element wasn't selected via mouse")] [Tooltip("Defines where the tooltip will be placed and how that placement will occur. Transform position will always be used if this element wasn't selected via mouse")]
public TooltipPositioningType tooltipPositioningType = TooltipPositioningType.mousePosition; public TooltipPositioningType tooltipPositioningType = TooltipPositioningType.mousePosition;
/// <summary> /// <summary>
/// This info is needed to make sure we make the necessary translations if the tooltip and this trigger are children of different space canvases /// This info is needed to make sure we make the necessary translations if the tooltip and this trigger are children of different space canvases
/// </summary> /// </summary>
private bool isChildOfOverlayCanvas = false; private bool isChildOfOverlayCanvas = false;
private bool hovered = false; private bool hovered = false;
public Vector3 offset; public Vector3 offset;
void Start() { void Start() {
//attempt to check if our canvas is overlay or not and check our "is overlay" accordingly //attempt to check if our canvas is overlay or not and check our "is overlay" accordingly
Canvas ourCanvas = GetComponentInParent<Canvas>(); Canvas ourCanvas = GetComponentInParent<Canvas>();
if (ourCanvas && ourCanvas.renderMode == RenderMode.ScreenSpaceOverlay) { if (ourCanvas && ourCanvas.renderMode == RenderMode.ScreenSpaceOverlay) {
isChildOfOverlayCanvas = true; isChildOfOverlayCanvas = true;
} }
} }
/// <summary> /// <summary>
/// Checks if the tooltip and the transform this trigger is attached to are children of differently-spaced Canvases /// Checks if the tooltip and the transform this trigger is attached to are children of differently-spaced Canvases
/// </summary> /// </summary>
public bool WorldToScreenIsRequired public bool WorldToScreenIsRequired
{ {
get get
{ {
return (isChildOfOverlayCanvas && ToolTip.Instance.guiMode == RenderMode.ScreenSpaceCamera) || return (isChildOfOverlayCanvas && ToolTip.Instance.guiMode == RenderMode.ScreenSpaceCamera) ||
(!isChildOfOverlayCanvas && ToolTip.Instance.guiMode == RenderMode.ScreenSpaceOverlay); (!isChildOfOverlayCanvas && ToolTip.Instance.guiMode == RenderMode.ScreenSpaceOverlay);
} }
} }
public void OnPointerEnter(PointerEventData eventData) public void OnPointerEnter(PointerEventData eventData)
{ {
switch (tooltipPositioningType) { switch (tooltipPositioningType) {
case TooltipPositioningType.mousePosition: case TooltipPositioningType.mousePosition:
StartHover(Input.mousePosition + offset, true); StartHover(Input.mousePosition + offset, true);
break; break;
case TooltipPositioningType.mousePositionAndFollow: case TooltipPositioningType.mousePositionAndFollow:
StartHover(Input.mousePosition + offset, true); StartHover(Input.mousePosition + offset, true);
hovered = true; hovered = true;
StartCoroutine(HoveredMouseFollowingLoop()); StartCoroutine(HoveredMouseFollowingLoop());
break; break;
case TooltipPositioningType.transformPosition: case TooltipPositioningType.transformPosition:
StartHover((WorldToScreenIsRequired ? StartHover((WorldToScreenIsRequired ?
ToolTip.Instance.GuiCamera.WorldToScreenPoint(transform.position) : ToolTip.Instance.GuiCamera.WorldToScreenPoint(transform.position) :
transform.position) + offset, true); transform.position) + offset, true);
break; break;
} }
} }
IEnumerator HoveredMouseFollowingLoop() { IEnumerator HoveredMouseFollowingLoop() {
while (hovered) { while (hovered) {
StartHover(Input.mousePosition + offset); StartHover(Input.mousePosition + offset);
yield return null; yield return null;
} }
} }
public void OnSelect(BaseEventData eventData) public void OnSelect(BaseEventData eventData)
{ {
StartHover((WorldToScreenIsRequired ? StartHover((WorldToScreenIsRequired ?
ToolTip.Instance.GuiCamera.WorldToScreenPoint(transform.position) : ToolTip.Instance.GuiCamera.WorldToScreenPoint(transform.position) :
transform.position) + offset, true); transform.position) + offset, true);
} }
public void OnPointerExit(PointerEventData eventData) public void OnPointerExit(PointerEventData eventData)
{ {
StopHover(); StopHover();
} }
public void OnDeselect(BaseEventData eventData) public void OnDeselect(BaseEventData eventData)
{ {
StopHover(); StopHover();
} }
void StartHover(Vector3 position, bool shouldCanvasUpdate = false) void StartHover(Vector3 position, bool shouldCanvasUpdate = false)
{ {
ToolTip.Instance.SetTooltip(text, position, shouldCanvasUpdate); ToolTip.Instance.SetTooltip(text, position, shouldCanvasUpdate);
} }
void StopHover() void StopHover()
{ {
hovered = false; hovered = false;
ToolTip.Instance.HideTooltip(); ToolTip.Instance.HideTooltip();
} }
} }
} }