Skip to content

Commit

Permalink
Tooltip implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
DMagic1 committed Feb 1, 2017
1 parent 3152361 commit d66563f
Show file tree
Hide file tree
Showing 2 changed files with 258 additions and 0 deletions.
117 changes: 117 additions & 0 deletions SCANsat.Unity/TooltipHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#region license
/* [Scientific Committee on Advanced Navigation]
* S.C.A.N. Satellite
*
* TooltipHandler - Script to control tooltip activation
*
* Copyright (c)2013 damny;
* Copyright (c)2014 technogeeky <technogeeky@gmail.com>;
* Copyright (c)2014 DMagic
* Copyright (c)2014 (Your Name Here) <your email here>; see LICENSE.txt for licensing details.
*/
#endregion

using System;
using System.Collections.Generic;
using SCANsat.Unity.Unity;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

namespace SCANsat.Unity
{

public class TooltipHandler : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
[SerializeField, TextArea(2, 10)]
private string m_TooltipName = "";
[SerializeField]
private bool m_IsActive = true;
[SerializeField]
private bool m_HelpTip = false;
[SerializeField]
private GameObject _prefab = null;
[SerializeField]
private string _tooltipText = "";

private Canvas _canvas;
private SCAN_Tooltip _tooltip;
private float _scale;

public string TooltipName
{
get { return m_TooltipName; }
}

public Canvas _Canvas
{
set { _canvas = value; }
}

public GameObject Prefab
{
set { _prefab = value; }
}

public float Scale
{
set { _scale = value; }
}

public bool IsActive
{
set { m_IsActive = value; }
}

public bool HelpTip
{
get { return m_HelpTip; }
}

public string TooltipText
{
set { _tooltipText = value; }
}

public void OnPointerEnter(PointerEventData eventData)
{
if (!m_IsActive)
return;

OpenTooltip();
}

public void OnPointerExit(PointerEventData eventData)
{
if (!m_IsActive)
return;

CloseTooltip();
}

private void OpenTooltip()
{
if (_prefab == null || _canvas == null)
return;

_tooltip = Instantiate(_prefab).GetComponent<SCAN_Tooltip>();

if (_tooltip == null)
return;

_tooltip.transform.SetParent(_canvas.transform, false);
_tooltip.transform.SetAsLastSibling();

_tooltip.Setup(_canvas, _tooltipText, _scale);
}

private void CloseTooltip()
{
if (_tooltip == null)
return;

_tooltip.gameObject.SetActive(false);
Destroy(_tooltip.gameObject);
}
}
}
141 changes: 141 additions & 0 deletions SCANsat.Unity/Unity/SCAN_Tooltip.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/// Credit drHogan
/// Sourced from - http://forum.unity3d.com/threads/screenspace-camera-tooltip-controller-sweat-and-tears.293991/#post-1938929
/// updated ddreaper - refactored code to be more performant.
/// *Note - only works for Screenspace Camera canvases at present, needs updating to include Screenspace and Worldspace!

//ToolTip is written by Emiliano Pastorelli, H&R Tallinn (Estonia), http://www.hammerandravens.com
//Copyright (c) 2015 Emiliano Pastorelli, H&R - Hammer&Ravens, Tallinn, Estonia.
//All rights reserved.

//Redistribution and use in source and binary forms are permitted
//provided that the above copyright notice and this paragraph are
//duplicated in all such forms and that any documentation,
//advertising materials, and other materials related to such
//distribution and use acknowledge that the software was developed
//by H&R, Hammer&Ravens. The name of the
//H&R, Hammer&Ravens may not be used to endorse or promote products derived
//from this software without specific prior written permission.
//THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
//IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
//WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

using System;
using UnityEngine;
using UnityEngine.UI;

namespace SCANsat.Unity.Unity
{
[RequireComponent(typeof(RectTransform))]
[AddComponentMenu("UI/Extensions/Tooltip")]
public class SCAN_Tooltip : MonoBehaviour
{
//text of the tooltip
private TextHandler _handler;
private RectTransform _rectTransform;

//if the tooltip is inside a UI element
private bool _inside;

private float width, height;

private float YShift, xShift;

private RenderMode _guiMode;

private Camera _guiCamera;

private void Awake()
{
_rectTransform = GetComponent<RectTransform>();
_handler = GetComponentInChildren<TextHandler>();
}

// Use this for initialization
public void Setup(Canvas c, string t, float f)
{
if (c == null || string.IsNullOrEmpty(t) || _rectTransform == null || _handler == null)
return;

var _canvas = c;
_guiCamera = _canvas.worldCamera;
_guiMode = _canvas.renderMode;

xShift = 5f;
YShift = -5f;

_handler.OnTextUpdate.Invoke(t);

_rectTransform.sizeDelta = new Vector2(_handler.PreferredSize.x + 10f, _handler.PreferredSize.y + 0f);

_rectTransform.localScale = Vector3.one * f;

OnScreenSpaceCamera();

_inside = true;
}

// Update is called once per frame
void FixedUpdate()
{
if (_inside && _guiMode == RenderMode.ScreenSpaceCamera)
OnScreenSpaceCamera();
}

//main tooltip edge of screen guard and movement
public void OnScreenSpaceCamera()
{
Vector3 newPos = _guiCamera.ScreenToViewportPoint(Input.mousePosition - new Vector3(xShift, YShift, 0f));
Vector3 newPosWVP = _guiCamera.ViewportToWorldPoint(newPos);

width = _rectTransform.sizeDelta[0];
height = _rectTransform.sizeDelta[1];

// check and solve problems for the tooltip that goes out of the screen on the horizontal axis
float val;

Vector3 lowerLeft = _guiCamera.ViewportToWorldPoint(new Vector3(0.0f, 0.0f, 0.0f));
Vector3 upperRight = _guiCamera.ViewportToWorldPoint(new Vector3(1.0f, 1.0f, 0.0f));

//check for right edge of screen
val = (newPosWVP.x + width / 2);
if (val > upperRight.x)
{
Vector3 shifter = new Vector3(val - upperRight.x, 0f, 0f);
Vector3 newWorldPos = new Vector3(newPosWVP.x - shifter.x, newPosWVP.y, 0f);
newPos.x = _guiCamera.WorldToViewportPoint(newWorldPos).x;
}
//check for left edge of screen
val = (newPosWVP.x - width / 2);
if (val < lowerLeft.x)
{
Vector3 shifter = new Vector3(lowerLeft.x - val, 0f, 0f);
Vector3 newWorldPos = new Vector3(newPosWVP.x + shifter.x, newPosWVP.y, 0f);
newPos.x = _guiCamera.WorldToViewportPoint(newWorldPos).x;
}

// check and solve problems for the tooltip that goes out of the screen on the vertical axis

//check for upper edge of the screen
val = (newPosWVP.y + height);
if (val > upperRight.y)
{
Vector3 shifter = new Vector3(0f, 30f + height, 0f);
Vector3 newWorldPos = new Vector3(newPosWVP.x, newPosWVP.y - shifter.y, 0f);
newPos.y = _guiCamera.WorldToViewportPoint(newWorldPos).y;
}

//check for lower edge of the screen (if the shifts of the tooltip are kept as in this code, no need for this as the tooltip always appears above the mouse bu default)
val = newPosWVP.y;
if (val < lowerLeft.y)
{
Vector3 shifter = new Vector3(0f, 10f, 0f);
Vector3 newWorldPos = new Vector3(newPosWVP.x, lowerLeft.y + shifter.y, 0f);
newPos.y = _guiCamera.WorldToViewportPoint(newWorldPos).y;
}
newPosWVP = _guiCamera.ViewportToWorldPoint(newPos);
this.transform.position = new Vector3(newPosWVP.x, newPosWVP.y, 1f);
//this.gameObject.SetActive(true);
_inside = true;
}
}
}

0 comments on commit d66563f

Please sign in to comment.