Merge 4.x fixes to 5.2.x source.

Prep for 5.3 branch

--HG--
branch : develop_5.2
pull/413/head
Simon (darkside) Jackson 2015-10-10 17:58:11 +01:00
commit d344d459b1
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 //*** ATTRIBUTES ***//
[Header("[ References ]")]
[SerializeField]
private RectTransform layoutListGroup;
// settings [Header("[ Settings ]")]
public float scrollSpeed = 10f; [SerializeField]
private float scrollSpeed = 10f;
[SerializeField] //*** PROPERTIES ***//
private RectTransform layoutListGroup; // REFERENCES
protected RectTransform LayoutListGroup {
get {return layoutListGroup;}
}
// temporary variables // SETTINGS
private RectTransform targetScrollObject; protected float ScrollSpeed {
private bool scrollToSelection = true; get {return scrollSpeed;}
}
// references // VARIABLES
private RectTransform scrollWindow; protected RectTransform TargetScrollObject {get; set;}
private RectTransform currentCanvas; protected RectTransform ScrollWindow {get; set;}
private ScrollRect targetScrollRect; protected ScrollRect TargetScrollRect {get; set;}
private EventSystem events = EventSystem.current; //*** METHODS - PUBLIC ***//
#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 //*** METHODS - PROTECTED ***//
private void Update() protected virtual void Awake (){
{ TargetScrollRect = GetComponent<ScrollRect>();
ScrollRectToLevelSelection(); ScrollWindow = TargetScrollRect.GetComponent<RectTransform>();
} }
private void ScrollRectToLevelSelection() protected virtual void Start (){
{
// check main references
bool referencesAreIncorrect =
(targetScrollRect == null || layoutListGroup == null || scrollWindow == null);
if (referencesAreIncorrect == true)
{
return;
}
// 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) protected virtual void Update (){
scrollToSelection = true; ScrollRectToLevelSelection();
}
// check if scrolling is possible //*** METHODS - PRIVATE ***//
bool isScrollDirectionUnknown = private void ScrollRectToLevelSelection (){
(selection == null || lastSelection == null || scrollToSelection == false); // check main references
bool referencesAreIncorrect = (TargetScrollRect == null || LayoutListGroup == null || ScrollWindow == null);
if (isScrollDirectionUnknown == true || selection.transform.parent != layoutListGroup.transform) if (referencesAreIncorrect == true){
return; return;
}
// move the current scroll rect to correct position // get calculation references
float selectionPos = -selection.anchoredPosition.y; EventSystem events = EventSystem.current;
int direction = (int)Mathf.Sign(selection.anchoredPosition.y - lastSelection.anchoredPosition.y); RectTransform selection =
events.currentSelectedGameObject != null ?
events.currentSelectedGameObject.GetComponent<RectTransform>() :
null;
float elementHeight = layoutListGroup.sizeDelta.y / layoutListGroup.transform.childCount; // check if scrolling is possible
float maskHeight = currentCanvas.sizeDelta.y + scrollWindow.sizeDelta.y; if (selection == null ||
float listPixelAnchor = layoutListGroup.anchoredPosition.y; selection.transform.parent != LayoutListGroup.transform)
{
return;
}
// get the element offset value depending on the cursor move direction // move the current scroll rect to correct position
float offlimitsValue = 0; float selectionPos = -selection.anchoredPosition.y;
if (direction > 0 && selectionPos < listPixelAnchor)
{ float elementHeight = LayoutListGroup.rect.height / LayoutListGroup.transform.childCount;
offlimitsValue = listPixelAnchor - selectionPos; float maskHeight = ScrollWindow.rect.height;
} float listPixelAnchor = LayoutListGroup.anchoredPosition.y;
if (direction < 0 && selectionPos + elementHeight > listPixelAnchor + maskHeight)
{ // get the element offset value depending on the cursor move direction
offlimitsValue = (listPixelAnchor + maskHeight) - (selectionPos + elementHeight); float offlimitsValue = 0;
}
// move the target scroll rect if (selectionPos < listPixelAnchor){
targetScrollRect.verticalNormalizedPosition += offlimitsValue = listPixelAnchor - selectionPos;
(offlimitsValue / layoutListGroup.sizeDelta.y) * Time.deltaTime * scrollSpeed; } else if (selectionPos + elementHeight > listPixelAnchor + maskHeight){
// check if we reached our destination offlimitsValue = (listPixelAnchor + maskHeight) - (selectionPos + elementHeight);
if (Mathf.Abs(offlimitsValue) < 2f) }
scrollToSelection = false;
// save last object we were "heading to" to prevent blocking // move the target scroll rect
targetScrollObject = selection; 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; 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 //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;
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 //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);