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
{
#region Variables
// settings
public float scrollSpeed = 10f;
[AddComponentMenu("UI/Extensions/UIScrollToSelection")]
public class UIScrollToSelection : MonoBehaviour {
//*** ATTRIBUTES ***//
[Header("[ References ]")]
[SerializeField]
private RectTransform layoutListGroup;
// temporary variables
private RectTransform targetScrollObject;
private bool scrollToSelection = true;
[Header("[ Settings ]")]
[SerializeField]
private float scrollSpeed = 10f;
// references
private RectTransform scrollWindow;
private RectTransform currentCanvas;
private ScrollRect targetScrollRect;
private EventSystem events = EventSystem.current;
#endregion
// Use this for initialization
private void Start()
{
targetScrollRect = GetComponent<ScrollRect>();
scrollWindow = targetScrollRect.GetComponent<RectTransform>();
currentCanvas = transform.root.GetComponent<RectTransform>();
//*** PROPERTIES ***//
// REFERENCES
protected RectTransform LayoutListGroup {
get {return layoutListGroup;}
}
// Update is called once per frame
private void Update()
{
// SETTINGS
protected float ScrollSpeed {
get {return scrollSpeed;}
}
// VARIABLES
protected RectTransform TargetScrollObject {get; set;}
protected RectTransform ScrollWindow {get; set;}
protected ScrollRect TargetScrollRect {get; set;}
//*** METHODS - PUBLIC ***//
//*** METHODS - PROTECTED ***//
protected virtual void Awake (){
TargetScrollRect = GetComponent<ScrollRect>();
ScrollWindow = TargetScrollRect.GetComponent<RectTransform>();
}
protected virtual void Start (){
}
protected virtual void Update (){
ScrollRectToLevelSelection();
}
private void ScrollRectToLevelSelection()
{
//*** METHODS - PRIVATE ***//
private void ScrollRectToLevelSelection (){
// check main references
bool referencesAreIncorrect =
(targetScrollRect == null || layoutListGroup == null || scrollWindow == null);
if (referencesAreIncorrect == true)
{
bool referencesAreIncorrect = (TargetScrollRect == null || LayoutListGroup == null || ScrollWindow == null);
if (referencesAreIncorrect == true){
return;
}
// get calculation references
RectTransform selection = events.currentSelectedGameObject != null ?
EventSystem events = EventSystem.current;
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;
// check if scrolling is possible
bool isScrollDirectionUnknown =
(selection == null || lastSelection == null || scrollToSelection == false);
if (isScrollDirectionUnknown == true || selection.transform.parent != layoutListGroup.transform)
if (selection == null ||
selection.transform.parent != LayoutListGroup.transform)
{
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);
float elementHeight = layoutListGroup.sizeDelta.y / layoutListGroup.transform.childCount;
float maskHeight = currentCanvas.sizeDelta.y + scrollWindow.sizeDelta.y;
float listPixelAnchor = layoutListGroup.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 (direction > 0 && selectionPos < listPixelAnchor)
{
if (selectionPos < listPixelAnchor){
offlimitsValue = listPixelAnchor - selectionPos;
}
if (direction < 0 && selectionPos + elementHeight > listPixelAnchor + maskHeight)
{
} else if (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;
TargetScrollRect.verticalNormalizedPosition +=
(offlimitsValue / LayoutListGroup.rect.height) * Time.deltaTime * scrollSpeed;
// save last object we were "heading to" to prevent blocking
targetScrollObject = selection;
TargetScrollObject = selection;
}
}
}

View File

@ -154,7 +154,16 @@ namespace UnityEngine.UI.Extensions
{
float distFromRight = upperRight.x - val;
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;
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);