public virtual void Submit_Input() { if (!isInInertia && !IsSwapping && !IsChangingItem) { OnClickOnItem.Invoke(CurrentItem.gameObject); //Invoke OnClick Event on the Controller CurrentItem.OnSelected.Invoke(); //Invoke OnSelected Event on the Item S_Manager.SelectItem(); } }
/// <summary> ///Drag/Swipe with Mouse/Touch and Action by Click/Tap /// </summary> protected virtual void SelectionAction() { CurrentEventData.position = Input.mousePosition; #region Hover && SoloSelection if (Hover && SoloSelection) { List <RaycastResult> results = new List <RaycastResult>(); EventSystem.current.RaycastAll(CurrentEventData, results); //HERE IS THE MODIfICATION if (results.Count > 0) { GameObject HitObject = results[0].gameObject; if (HitObject.GetComponentInParent <SelectorManager>() != S_Manager) { return; //Its not the same selector Skip All } MItem HitObjectItem = HitObject.GetComponentInParent <MItem>(); if (HitItem != HitObjectItem) { HitItem = HitObjectItem; if (HitItem) { int Next = Items.FindIndex(item => item == HitItem); if (IndexSelected != Next) { IndexSelected = Next; if (PreviousItem && !PreviousItem.IsRestoring) { StartCoroutine(RestoreTransformAnimation(PreviousItem)); //If is not restoring the Previous Item Restore it } } if (MainInputDown) { IsSelectTransformAnimation = true; OnClickOnItem.Invoke(CurrentItem.gameObject); //Invoke OnClick Event on the Controller CurrentItem.OnSelected.Invoke(); //Invoke OnSelected Event on the Item } } } } //return; //Skip everything else } #endregion if (MainInputDown) //The moment the mouse/touch start the click/touch { MouseStartPosition = Input.mousePosition; //Store the mouse/touch start position DeltaSelectorT.StoreTransform(transform); //Store the Current Selector Transform Data IsSwapping = false; //Set swapping to false } if (SoloSelection) { DeltaMousePos = Vector2.zero; goto ReleaseMouse; } #region DRAG/SWIPE on MOUSE/TOUCH if (MainInputPress) //if we are still touching means that we are dragging/swiping { DeltaMousePos = MouseStartPosition - Input.mousePosition; //Store the amount of distance travelled sice the first click/touch if (DeltaMousePos.magnitude > Threshold && !IsSwapping && DragSpeed != 0 && !SoloSelection) //Find out if is a Swipe/Drag { IsSwapping = true; //Is a Drag/Swipe!! StartCoroutine(RestoreTransformAnimation(CurrentItem)); //Restore the Current Item to their last pos } #region Dragin and Swapping if Animate Selection is Active if (AnimateSelection && IsSwapping && DragSpeed != 0) //Just Drag if Animate Selection is active { #region Drag Radial if (S_Editor.SelectorType == SelectorType.Radial) //If is a Radial Selector----------------------------------------------------------- { DragDistance = dragHorizontal ? DeltaMousePos.x : DeltaMousePos.y; DragDistance = DragDistance * Time.fixedDeltaTime * DragSpeed; //Rotate while drag switch (S_Editor.RadialAxis) { case RadialAxis.Up: transform.localEulerAngles = DeltaSelectorT.LocalEulerAngles + new Vector3(0, DragDistance, 0); //Rotate on Y axis the Selector break; case RadialAxis.Right: transform.localEulerAngles = DeltaSelectorT.LocalEulerAngles + new Vector3(DragDistance, 0, 0); //Rotate on X axis the Selector This one has probems with gimbal break; case RadialAxis.Forward: transform.localEulerAngles = DeltaSelectorT.LocalEulerAngles + new Vector3(0, 0, DragDistance); //Rotate on Z axis the Selector break; default: break; } if (IsSwapping) { CheckForWorldRotation(); //Align Items to the World if Use World is Active } } #endregion #region Drag Linear else if (S_Editor.SelectorType == SelectorType.Linear) //If is a Linear Selector----------------------------------------------------------- { float magnitude = dragHorizontal ? DeltaMousePos.x : DeltaMousePos.y; DragDistance = -(DeltaSelectorT.LocalPosition - InitialTransform.LocalPosition).magnitude + (-magnitude * DragSpeed * 0.002f); float maxDistance = -distance * (Items.Count - 1); DragDistance = Mathf.Clamp(DragDistance, maxDistance, 0); transform.localPosition = InitialTransform.LocalPosition + S_Editor.LinearVector * DragDistance; } #endregion #region Drag Grid else if (S_Editor.SelectorType == SelectorType.Grid) { transform.localPosition = GridVector(); } #endregion } #endregion } #endregion #region Release Mouse Click/Touch ReleaseMouse: if (MainInputUp) //if is released the Click/Touch─────────────────────────────────────────────────────────────────────────────────────── { IsSwapping = false; //Set Swapping to false if (DeltaMousePos.magnitude <= Threshold) //if it was a CLICK/TAP and it was not on top a IU Element { List <RaycastResult> results = new List <RaycastResult>(); EventSystem.current.RaycastAll(CurrentEventData, results); if (results.Count > 0) { MItem HitItem = results[0].gameObject.GetComponentInParent <MItem>(); if (HitItem) { if (HitItem.GetComponentInParent <SelectorManager>() != S_Manager) { return; //Its not the same Selector } int Next = Items.FindIndex(item => item == HitItem); if (IndexSelected == Next) //If we Click/Touch the Selected item Invoke |ON CLICK| { IsSelectTransformAnimation = true; OnClickOnItem.Invoke(current.gameObject); //Invoke OnClick Event on the Controller current.OnSelected.Invoke(); //Invoke OnSelected Event on the Item } else //If another visible item was touch change to that one; { if (ClickToFocus) { IndexSelected = Next; //Focus on the Next Item if lcick to focus is Active } } } } else if (ChangeOnEmptySpace) // if we did not hit any item the it was a click/touch on Left or Right of the Screen { Vector2 center = new Vector2(Screen.width / 2f, Screen.height / 2f); if ((Input.mousePosition.x < center.x && dragHorizontal) || ((Input.mousePosition.y < center.y && !dragHorizontal))) { SelectNextItem(false); } else { SelectNextItem(true); } } } else if (DragSpeed != 0) //Else if it was a swipe and swipe is active, average to the nearest item on the camera view { if (S_Editor.SelectorType == SelectorType.Radial) //Circular Selector--------------------------- { int Next = Mathf.RoundToInt(DragDistance / S_Editor.Angle); if (S_Editor.RadialAxis != RadialAxis.Up) { Next = -Next; } if (Next < 0) { Next = Items.Count + (Next % Items.Count); } IndexSelected += Next; // ???????????????????? } else if (S_Editor.SelectorType == SelectorType.Linear) //Linear Selector--------------------------- { IndexSelected = Mathf.RoundToInt(Mathf.Abs(DragDistance) / distance) % Items.Count; } else if (S_Editor.SelectorType == SelectorType.Grid) //Grid Selector--------------------------- { Vector3 DragMouse = GridVector() - InitialTransform.LocalPosition; //Get the Position on the Grid int Next = 0; float mag = float.MaxValue; for (int i = 0; i < Items.Count; i++) { float currentmag = Mathf.Abs((DragMouse + Items[i].transform.localPosition).magnitude); //Find the nearest item. if (currentmag < mag) { Next = i; mag = currentmag; } } IndexSelected = Next; //Set the Focused Item to the next } DragDistance = 0; //Reset Drag Distance } } #endregion }
void Update() { if (!IsActive) { return; } SelectionAction(); AnimateIdle(); if (Submit.GetInput) { IsSelectTransformAnimation = true; OnClickOnItem.Invoke(CurrentItem.gameObject); //Invoke OnClick Event on the Controller CurrentItem.OnSelected.Invoke(); //Invoke OnSelected Event on the Item } if (S_Editor.SelectorType != SelectorType.Grid) { if (ChangeLeft.GetInput || ChangeDown.GetInput) { SelectNextItem(false); //Change to Previous item } if (ChangeRight.GetInput || ChangeUp.GetInput) { SelectNextItem(true); //Change to Next item } } else { #region GRID Selection int NextItemIndex = IndexSelected; int CurrentRow = IndexSelected / S_Editor.Grid; if (ChangeLeft.GetInput) //Change Left { NextItemIndex--; NextItemIndex = (NextItemIndex % S_Editor.Grid) + (CurrentRow * S_Editor.Grid); if (NextItemIndex >= Items.Count) { NextItemIndex = Items.Count - 1; } if (NextItemIndex < 0) { NextItemIndex = S_Editor.Grid - 1; } } else if (ChangeRight.GetInput) //Change Right { NextItemIndex++; if (NextItemIndex == Items.Count) { NextItemIndex = GridTotal; } NextItemIndex = NextItemIndex % S_Editor.Grid + (CurrentRow * S_Editor.Grid); } else if (ChangeUp.GetInput) //Change UP { NextItemIndex += S_Editor.Grid; if (NextItemIndex >= Items.Count) { NextItemIndex = NextItemIndex % S_Editor.Grid; } } else if (ChangeDown.GetInput) { NextItemIndex -= S_Editor.Grid; if (NextItemIndex < 0) { if (GridTotal + NextItemIndex >= Items.Count) { NextItemIndex -= S_Editor.Grid; } NextItemIndex = GridTotal + NextItemIndex; } } if (ChangeLeft.GetInput || ChangeDown.GetInput || ChangeRight.GetInput || ChangeUp.GetInput) { SelectNextItem(NextItemIndex); //Change to Previous item } #endregion } }
void Update() { if (!IsActive) { return; } SelectionAction(); AnimateIdle(); if (Submit.GetInput) { if (!isInInertia && !IsSwapping && !IsChangingItem) { OnClickOnItem.Invoke(CurrentItem.gameObject); //Invoke OnClick Event on the Controller CurrentItem.OnSelected.Invoke(); //Invoke OnSelected Event on the Item S_Manager.SelectItem(); } } if (S_Editor.SelectorType != SelectorType.Grid) { if (ChangeLeft.GetInput || ChangeDown.GetInput) { SelectNextItem(false); //Change to Previous item } if (ChangeRight.GetInput || ChangeUp.GetInput) { SelectNextItem(true); //Change to Next item } } else { #region GRID Selection int NextItemIndex = IndexSelected; int CurrentRow = IndexSelected / S_Editor.Grid; if (ChangeLeft.GetInput) //Change Left { NextItemIndex--; NextItemIndex = (NextItemIndex % S_Editor.Grid) + (CurrentRow * S_Editor.Grid); if (NextItemIndex >= Items.Count) { NextItemIndex = Items.Count - 1; } if (NextItemIndex < 0) { NextItemIndex = S_Editor.Grid - 1; } } else if (ChangeRight.GetInput) //Change Right { NextItemIndex++; if (NextItemIndex == Items.Count) { NextItemIndex = GridTotal; } NextItemIndex = NextItemIndex % S_Editor.Grid + (CurrentRow * S_Editor.Grid); } else if (ChangeUp.GetInput) //Change UP { NextItemIndex += S_Editor.Grid; if (NextItemIndex >= Items.Count) { NextItemIndex = NextItemIndex % S_Editor.Grid; } } else if (ChangeDown.GetInput) { NextItemIndex -= S_Editor.Grid; if (NextItemIndex < 0) { if (GridTotal + NextItemIndex >= Items.Count) { NextItemIndex -= S_Editor.Grid; } NextItemIndex = GridTotal + NextItemIndex; } } if (ChangeLeft.GetInput || ChangeDown.GetInput || ChangeRight.GetInput || ChangeUp.GetInput) { SelectNextItem(NextItemIndex); //Change to Previous item } #endregion } // forcing the selector to center X // TODO : not have this be a magic number ( based on the number of columns ...) Vector3 forcedXLocation = new Vector3(-2f, transform.localPosition.y, transform.localPosition.z); transform.localPosition = forcedXLocation; }