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 namespace UnityEngine.UI.Extensions
{ {
[RequireComponent(typeof(ScrollRect))] [RequireComponent(typeof(ScrollRect))]
public class UIScrollToSelection : MonoBehaviour [AddComponentMenu("UI/Extensions/UIScrollToSelection")]
{ public class UIScrollToSelection : MonoBehaviour {
#region Variables
// settings
public float scrollSpeed = 10f;
//*** ATTRIBUTES ***//
[Header("[ References ]")]
[SerializeField] [SerializeField]
private RectTransform layoutListGroup; private RectTransform layoutListGroup;
// temporary variables [Header("[ Settings ]")]
private RectTransform targetScrollObject; [SerializeField]
private bool scrollToSelection = true; private float scrollSpeed = 10f;
// references //*** PROPERTIES ***//
private RectTransform scrollWindow; // REFERENCES
private RectTransform currentCanvas; protected RectTransform LayoutListGroup {
private ScrollRect targetScrollRect; get {return layoutListGroup;}
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>();
} }
// Update is called once per frame // SETTINGS
private void Update() 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(); ScrollRectToLevelSelection();
} }
private void ScrollRectToLevelSelection() //*** METHODS - PRIVATE ***//
{ private void ScrollRectToLevelSelection (){
// check main references // check main references
bool referencesAreIncorrect = bool referencesAreIncorrect = (TargetScrollRect == null || LayoutListGroup == null || ScrollWindow == null);
(targetScrollRect == null || layoutListGroup == null || scrollWindow == null);
if (referencesAreIncorrect == true) if (referencesAreIncorrect == true){
{
return; return;
} }
// get calculation references // get calculation references
RectTransform selection = events.currentSelectedGameObject != null ? EventSystem events = EventSystem.current;
RectTransform selection =
events.currentSelectedGameObject != null ?
events.currentSelectedGameObject.GetComponent<RectTransform>() : events.currentSelectedGameObject.GetComponent<RectTransform>() :
null; null;
RectTransform lastSelection = events.lastSelectedGameObject != null ?
events.lastSelectedGameObject.GetComponent<RectTransform>() :
selection;
if (selection != targetScrollObject)
scrollToSelection = true;
// check if scrolling is possible // check if scrolling is possible
bool isScrollDirectionUnknown = if (selection == null ||
(selection == null || lastSelection == null || scrollToSelection == false); selection.transform.parent != LayoutListGroup.transform)
{
if (isScrollDirectionUnknown == true || selection.transform.parent != layoutListGroup.transform)
return; return;
}
// move the current scroll rect to correct position // move the current scroll rect to correct position
float selectionPos = -selection.anchoredPosition.y; 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 elementHeight = LayoutListGroup.rect.height / LayoutListGroup.transform.childCount;
float maskHeight = currentCanvas.sizeDelta.y + scrollWindow.sizeDelta.y; float maskHeight = ScrollWindow.rect.height;
float listPixelAnchor = layoutListGroup.anchoredPosition.y; float listPixelAnchor = LayoutListGroup.anchoredPosition.y;
// get the element offset value depending on the cursor move direction // get the element offset value depending on the cursor move direction
float offlimitsValue = 0; float offlimitsValue = 0;
if (direction > 0 && selectionPos < listPixelAnchor)
{ if (selectionPos < listPixelAnchor){
offlimitsValue = listPixelAnchor - selectionPos; offlimitsValue = listPixelAnchor - selectionPos;
} } else if (selectionPos + elementHeight > listPixelAnchor + maskHeight){
if (direction < 0 && selectionPos + elementHeight > listPixelAnchor + maskHeight)
{
offlimitsValue = (listPixelAnchor + maskHeight) - (selectionPos + elementHeight); offlimitsValue = (listPixelAnchor + maskHeight) - (selectionPos + elementHeight);
} }
// move the target scroll rect // move the target scroll rect
targetScrollRect.verticalNormalizedPosition += TargetScrollRect.verticalNormalizedPosition +=
(offlimitsValue / layoutListGroup.sizeDelta.y) * Time.deltaTime * scrollSpeed; (offlimitsValue / LayoutListGroup.rect.height) * 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 // 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; float distFromRight = upperRight.x - val;
if (distFromRight > (defaultXOffset * 0.75))
{
//shorten the temporary offset up to a certain distance from the tooltip
xOffSet = distFromRight; 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 //assign the new modified coordinates to the tooltip and convert to screen coordinates
Vector3 newTooltipPos = new Vector3(GUICamera.ViewportToScreenPoint(newPos).x + xOffSet, 0f, 0f); Vector3 newTooltipPos = new Vector3(GUICamera.ViewportToScreenPoint(newPos).x + xOffSet, 0f, 0f);
@ -171,7 +180,16 @@ namespace UnityEngine.UI.Extensions
{ {
float distFromLeft = lowerLeft.x - val; 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; 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 //assign the new modified coordinates to the tooltip and convert to screen coordinates
Vector3 newTooltipPos = new Vector3(GUICamera.ViewportToScreenPoint(newPos).x - xOffSet, 0f, 0f); Vector3 newTooltipPos = new Vector3(GUICamera.ViewportToScreenPoint(newPos).x - xOffSet, 0f, 0f);