Replacement UIScrollToSelection script from author

Minor patches to experimental new ToolTip script

--HG--
branch : develop_4.6
release
Simon (darkside) Jackson 2015-10-10 17:51:20 +01:00
parent 4b63e87664
commit b55c22383b
2 changed files with 104 additions and 87 deletions

View File

@ -10,97 +10,96 @@ using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions
{
[RequireComponent(typeof(ScrollRect))]
public class UIScrollToSelection : MonoBehaviour
{
[AddComponentMenu("UI/Extensions/UIScrollToSelection")]
public class UIScrollToSelection : MonoBehaviour {
#region Variables
//*** ATTRIBUTES ***//
[Header("[ References ]")]
[SerializeField]
private RectTransform layoutListGroup;
// settings
public float scrollSpeed = 10f;
[Header("[ Settings ]")]
[SerializeField]
private float scrollSpeed = 10f;
[SerializeField]
private RectTransform layoutListGroup;
//*** PROPERTIES ***//
// REFERENCES
protected RectTransform LayoutListGroup {
get {return layoutListGroup;}
}
// temporary variables
private RectTransform targetScrollObject;
private bool scrollToSelection = true;
// SETTINGS
protected float ScrollSpeed {
get {return scrollSpeed;}
}
// references
private RectTransform scrollWindow;
private RectTransform currentCanvas;
private ScrollRect targetScrollRect;
// VARIABLES
protected RectTransform TargetScrollObject {get; set;}
protected RectTransform ScrollWindow {get; set;}
protected ScrollRect TargetScrollRect {get; set;}
private EventSystem events = EventSystem.current;
#endregion
//*** METHODS - PUBLIC ***//
// Use this for initialization
private void Start()
{
targetScrollRect = GetComponent<ScrollRect>();
scrollWindow = targetScrollRect.GetComponent<RectTransform>();
currentCanvas = transform.root.GetComponent<RectTransform>();
}
// Update is called once per frame
private void Update()
{
ScrollRectToLevelSelection();
}
//*** METHODS - PROTECTED ***//
protected virtual void Awake (){
TargetScrollRect = GetComponent<ScrollRect>();
ScrollWindow = TargetScrollRect.GetComponent<RectTransform>();
}
private void ScrollRectToLevelSelection()
{
// check main references
bool referencesAreIncorrect =
(targetScrollRect == null || layoutListGroup == null || scrollWindow == null);
if (referencesAreIncorrect == true)
{
return;
}
protected virtual void Start (){
// get calculation references
RectTransform selection = events.currentSelectedGameObject != null ?
events.currentSelectedGameObject.GetComponent<RectTransform>() :
null;
RectTransform lastSelection = events.lastSelectedGameObject != null ?
events.lastSelectedGameObject.GetComponent<RectTransform>() :
selection;
}
if (selection != targetScrollObject)
scrollToSelection = true;
protected virtual void Update (){
ScrollRectToLevelSelection();
}
// check if scrolling is possible
bool isScrollDirectionUnknown =
(selection == null || lastSelection == null || scrollToSelection == false);
//*** METHODS - PRIVATE ***//
private void ScrollRectToLevelSelection (){
// check main references
bool referencesAreIncorrect = (TargetScrollRect == null || LayoutListGroup == null || ScrollWindow == null);
if (isScrollDirectionUnknown == true || selection.transform.parent != layoutListGroup.transform)
return;
if (referencesAreIncorrect == true){
return;
}
// move the current scroll rect to correct position
float selectionPos = -selection.anchoredPosition.y;
int direction = (int)Mathf.Sign(selection.anchoredPosition.y - lastSelection.anchoredPosition.y);
// get calculation references
EventSystem events = EventSystem.current;
RectTransform selection =
events.currentSelectedGameObject != null ?
events.currentSelectedGameObject.GetComponent<RectTransform>() :
null;
float elementHeight = layoutListGroup.sizeDelta.y / layoutListGroup.transform.childCount;
float maskHeight = currentCanvas.sizeDelta.y + scrollWindow.sizeDelta.y;
float listPixelAnchor = layoutListGroup.anchoredPosition.y;
// check if scrolling is possible
if (selection == null ||
selection.transform.parent != LayoutListGroup.transform)
{
return;
}
// get the element offset value depending on the cursor move direction
float offlimitsValue = 0;
if (direction > 0 && selectionPos < listPixelAnchor)
{
offlimitsValue = listPixelAnchor - selectionPos;
}
if (direction < 0 && selectionPos + elementHeight > listPixelAnchor + maskHeight)
{
offlimitsValue = (listPixelAnchor + maskHeight) - (selectionPos + elementHeight);
}
// move the target scroll rect
targetScrollRect.verticalNormalizedPosition +=
(offlimitsValue / layoutListGroup.sizeDelta.y) * Time.deltaTime * scrollSpeed;
// check if we reached our destination
if (Mathf.Abs(offlimitsValue) < 2f)
scrollToSelection = false;
// save last object we were "heading to" to prevent blocking
targetScrollObject = selection;
}
}
// move the current scroll rect to correct position
float selectionPos = -selection.anchoredPosition.y;
float elementHeight = LayoutListGroup.rect.height / LayoutListGroup.transform.childCount;
float maskHeight = ScrollWindow.rect.height;
float listPixelAnchor = LayoutListGroup.anchoredPosition.y;
// get the element offset value depending on the cursor move direction
float offlimitsValue = 0;
if (selectionPos < listPixelAnchor){
offlimitsValue = listPixelAnchor - selectionPos;
} else if (selectionPos + elementHeight > listPixelAnchor + maskHeight){
offlimitsValue = (listPixelAnchor + maskHeight) - (selectionPos + elementHeight);
}
// move the target scroll rect
TargetScrollRect.verticalNormalizedPosition +=
(offlimitsValue / LayoutListGroup.rect.height) * Time.deltaTime * scrollSpeed;
// save last object we were "heading to" to prevent blocking
TargetScrollObject = selection;
}
}
}

View File

@ -154,7 +154,16 @@ namespace UnityEngine.UI.Extensions
{
float distFromRight = upperRight.x - val;
xOffSet = distFromRight;
if (distFromRight > (defaultXOffset * 0.75))
{
//shorten the temporary offset up to a certain distance from the tooltip
xOffSet = distFromRight;
}
else
{
//if the distance becomes too short flip the tooltip to below the pointer (by offset+twice the height of the tooltip)
xOffSet = ((defaultXOffset) - (tooltipRealWidth) * 2f);
}
//assign the new modified coordinates to the tooltip and convert to screen coordinates
Vector3 newTooltipPos = new Vector3(GUICamera.ViewportToScreenPoint(newPos).x + xOffSet, 0f, 0f);
@ -171,7 +180,16 @@ namespace UnityEngine.UI.Extensions
{
float distFromLeft = lowerLeft.x - val;
xOffSet = -distFromLeft;
if (distFromLeft < (defaultXOffset * 0.75 - tooltipRealWidth))
{
//shorten the temporary offset up to a certain distance from the tooltip
xOffSet = -distFromLeft;
}
else
{
//if the distance becomes too short flip the tooltip to above the pointer (by twice the height of the tooltip)
xOffSet = ((tooltipRealWidth) * 2f);
}
//assign the new modified coordinates to the tooltip and convert to screen coordinates
Vector3 newTooltipPos = new Vector3(GUICamera.ViewportToScreenPoint(newPos).x - xOffSet, 0f, 0f);