void BeginSelection() { // Click somewhere in the Game View. if (!UIExtensionsInputManager.GetMouseButtonDown(0)) { return; } //The boxRect will be inactive up until the point we start selecting boxRect.gameObject.SetActive(true); // Get the initial click position of the mouse. origin = new Vector2(UIExtensionsInputManager.MousePosition.x, UIExtensionsInputManager.MousePosition.y); //If the initial click point is not inside the selection mask, we abort the selection if (!PointIsValidAgainstSelectionMask(origin)) { ResetBoxRect(); return; } // The anchor is set to the same place. boxRect.anchoredPosition = origin; MonoBehaviour[] behavioursToGetSelectionsFrom; // If we do not have a group of selectables already set, we'll just loop through every object that's a monobehaviour, and look for selectable interfaces in them if (selectableGroup == null) { behavioursToGetSelectionsFrom = GameObject.FindObjectsOfType <MonoBehaviour>(); } else { behavioursToGetSelectionsFrom = selectableGroup; } //Temporary list to store the found selectables before converting to the main selectables array List <IBoxSelectable> selectableList = new List <IBoxSelectable>(); foreach (MonoBehaviour behaviour in behavioursToGetSelectionsFrom) { //If the behaviour implements the selectable interface, we add it to the selectable list IBoxSelectable selectable = behaviour as IBoxSelectable; if (selectable != null) { selectableList.Add(selectable); //We're using left shift to act as the "Add To Selection" command. So if left shift isn't pressed, we want everything to begin deselected if (!UIExtensionsInputManager.GetKey(KeyCode.LeftShift)) { selectable.selected = false; } } } selectables = selectableList.ToArray(); //For single-click actions, we need to get the selectable that was clicked when selection began (if any) clickedBeforeDrag = GetSelectableAtMousePosition(); }
private void Update() { // On Android the back button is sent as Esc if (UIExtensionsInputManager.GetKeyDown(KeyCode.Escape) && menuStack.Count > 0) { menuStack.Peek().OnBackPressed(); } }
public void OnEndEdit(string txt) { if (!UIExtensionsInputManager.GetKeyDown(KeyCode.Return) && !UIExtensionsInputManager.GetKeyDown(KeyCode.KeypadEnter)) { return; } EnterSubmit.Invoke(txt); if (defocusInput) { UnityEngine.EventSystems.EventSystem.current.SetSelectedGameObject(null); } }
public void Update() { if (_arrayOfElements.Length < 1) { return; } for (var i = 0; i < _arrayOfElements.Length; i++) { distReposition[i] = center.GetComponent <RectTransform>().position.y - _arrayOfElements[i].GetComponent <RectTransform>().position.y; distance[i] = Mathf.Abs(distReposition[i]); //Magnifying effect Vector2 scale = Vector2.Max(minScale, new Vector2(1 / (1 + distance[i] * elementShrinkage.x), (1 / (1 + distance[i] * elementShrinkage.y)))); _arrayOfElements[i].GetComponent <RectTransform>().transform.localScale = new Vector3(scale.x, scale.y, 1f); } // detect focused element float minDistance = Mathf.Min(distance); int oldFocusedElement = focusedElementIndex; for (var i = 0; i < _arrayOfElements.Length; i++) { _arrayOfElements[i].GetComponent <CanvasGroup>().interactable = !disableUnfocused || minDistance == distance[i]; if (minDistance == distance[i]) { focusedElementIndex = i; result = _arrayOfElements[i].GetComponentInChildren <Text>().text; } } if (focusedElementIndex != oldFocusedElement && OnFocusChanged != null) { OnFocusChanged.Invoke(focusedElementIndex); } if (!UIExtensionsInputManager.GetMouseButton(0)) { // scroll slowly to nearest element when not dragged ScrollingElements(); } // stop scrolling past last element from inertia if (stopMomentumOnEnd && (_arrayOfElements[0].GetComponent <RectTransform>().position.y > center.position.y || _arrayOfElements[_arrayOfElements.Length - 1].GetComponent <RectTransform>().position.y < center.position.y)) { scrollRect.velocity = Vector2.zero; } }
void DragSelection() { //Return if we're not dragging or if the selection has been aborted (BoxRect disabled) if (!UIExtensionsInputManager.GetMouseButton(0) || !boxRect.gameObject.activeSelf) { return; } // Store the current mouse position in screen space. Vector2 currentMousePosition = new Vector2(UIExtensionsInputManager.MousePosition.x, UIExtensionsInputManager.MousePosition.y); // How far have we moved the mouse? Vector2 difference = currentMousePosition - origin; // Copy the initial click position to a new variable. Using the original variable will cause // the anchor to move around to wherever the current mouse position is, // which isn't desirable. Vector2 startPoint = origin; // The following code accounts for dragging in various directions. if (difference.x < 0) { startPoint.x = currentMousePosition.x; difference.x = -difference.x; } if (difference.y < 0) { startPoint.y = currentMousePosition.y; difference.y = -difference.y; } // Set the anchor, width and height every frame. boxRect.anchoredPosition = startPoint; boxRect.sizeDelta = difference; //Then we check our list of Selectables to see if they're being preselected or not. foreach (var selectable in selectables) { Vector3 screenPoint = GetScreenPointOfSelectable(selectable); //If the box Rect contains the selectables screen point and that point is inside a valid selection mask, it's being preselected, otherwise it is not. selectable.preSelected = RectTransformUtility.RectangleContainsScreenPoint(boxRect, screenPoint, null) && PointIsValidAgainstSelectionMask(screenPoint); } //Finally, since it's possible for our first clicked object to not be within the bounds of the selection box //If it exists, we always ensure that it is preselected. if (clickedBeforeDrag != null) { clickedBeforeDrag.preSelected = true; } }
void EndSelection() { //Get out if we haven't finished selecting, or if the selection has been aborted (boxRect disabled) if (!UIExtensionsInputManager.GetMouseButtonUp(0) || !boxRect.gameObject.activeSelf) { return; } clickedAfterDrag = GetSelectableAtMousePosition(); ApplySingleClickDeselection(); ApplyPreSelections(); ResetBoxRect(); onSelectionChange.Invoke(GetAllSelected()); }
private void CheckIfScrollingShouldBeLocked() { if (CancelScrollOnInput == false || IsManualScrollingAvailable == true) { return; } for (int i = 0; i < CancelScrollKeycodes.Count; i++) { if (UIExtensionsInputManager.GetKeyDown(CancelScrollKeycodes[i]) == true) { IsManualScrollingAvailable = true; break; } } }
void Update() { if (canUseHorizontalAxis) { ///Controls for the cards. if ((UIExtensionsInputManager.GetAxisRaw("Horizontal") < 0 || UIExtensionsInputManager.GetKey(leftButton)) && cardArrayOffset > 0) { cardArrayOffset--; StartCoroutine(ButtonCooldown()); } else if ((UIExtensionsInputManager.GetAxisRaw("Horizontal") > 0 || UIExtensionsInputManager.GetKey(rightButton)) && cardArrayOffset < cards.Length - 1) { cardArrayOffset++; StartCoroutine(ButtonCooldown()); } } ///This loop moves the cards. I know that none of my lerps are the "right way," but it looks much nicer. for (int i = 0; i < cards.Length; i++) { cards[i].localPosition = Vector3.Lerp(cards[i].localPosition, cardPositions[i + cardArrayOffset], Time.deltaTime * cardMoveSpeed); if (Mathf.Abs(cards[i].localPosition.x - cardPositions[i + cardArrayOffset].x) < 0.01f) { cards[i].localPosition = cardPositions[i + cardArrayOffset]; ///This disables interaction with cards that are not on top of the stack. if (cards[i].localPosition.x == 0) { cards[i].gameObject.GetComponent <CanvasGroup>().interactable = true; } else { cards[i].gameObject.GetComponent <CanvasGroup>().interactable = false; } } } }
public void Update() { Selectable next = null; if (LastObject == null && _system.currentSelectedGameObject != null) { //Find the last selectable object next = _system.currentSelectedGameObject.GetComponent <Selectable>().FindSelectableOnDown(); while (next != null) { LastObject = next; next = next.FindSelectableOnDown(); } } if (UIExtensionsInputManager.GetKeyDown(KeyCode.Tab) && UIExtensionsInputManager.GetKey(KeyCode.LeftShift)) { if (NavigationMode == NavigationMode.Manual && NavigationPath.Length > 0) { for (var i = NavigationPath.Length - 1; i >= 0; i--) { if (_system.currentSelectedGameObject != NavigationPath[i].gameObject) { continue; } next = i == 0 ? NavigationPath[NavigationPath.Length - 1] : NavigationPath[i - 1]; break; } } else { if (_system.currentSelectedGameObject != null) { next = _system.currentSelectedGameObject.GetComponent <Selectable>().FindSelectableOnUp(); if (next == null && CircularNavigation) { next = LastObject; } } else { SelectDefaultObject(out next); } } } else if (UIExtensionsInputManager.GetKeyDown(KeyCode.Tab)) { if (NavigationMode == NavigationMode.Manual && NavigationPath.Length > 0) { for (var i = 0; i < NavigationPath.Length; i++) { if (_system.currentSelectedGameObject != NavigationPath[i].gameObject) { continue; } next = i == (NavigationPath.Length - 1) ? NavigationPath[0] : NavigationPath[i + 1]; break; } } else { if (_system.currentSelectedGameObject != null) { next = _system.currentSelectedGameObject.GetComponent <Selectable>().FindSelectableOnDown(); if (next == null && CircularNavigation) { next = StartingObject; } } else { SelectDefaultObject(out next); } } } else if (_system.currentSelectedGameObject == null) { SelectDefaultObject(out next); } if (CircularNavigation && StartingObject == null) { StartingObject = next; } selectGameObject(next); }