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; //Send to the Current Event Data the Mouse Position #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) { if (RestoreTransformAnimationC != null) { StopCoroutine(RestoreTransformAnimationC); } RestoreTransformAnimationC = RestoreTransformAnimation(PreviousItem); StartCoroutine(RestoreTransformAnimationC); //If is not restoring the Previous Item Restore it } } if (MainInputDown) { OnClickOnItem.Invoke(CurrentItem.gameObject); //Invoke OnClick Event on the Controller //CurrentItem.OnSelected.Invoke(); //Invoke OnSelected Event on the Item S_Manager.SelectItem(); } } } } //return; //Skip everything else } #endregion if (MainInputDown) //The moment the mouse/touch start the click/touch { if (UseSelectionZone) { Vector2 NormMousePos = new Vector2(Input.mousePosition.x / Screen.width, Input.mousePosition.y / Screen.height); isInSelectionZone = (ZMinX <= NormMousePos.x && ZMaxX >= NormMousePos.x && ZMinY <= NormMousePos.y && ZMaxY >= NormMousePos.y); } if (UseSelectionZone && !isInSelectionZone) { return; //If Selection Zone is enabled } DeltaSelectorT.StoreTransform(transform); //Store the Current Selector Transform Data MouseStartPosition = Input.mousePosition; //Store the mouse/touch start position IsSwapping = false; //Set swapping to false lastDrag = DeltaSpeed = 0; if (CInertia != null) { StopCoroutine(CInertia); CheckForWorldRotation(); if (isInInertia) { SetNextItemRadial(InertiaLastDrag); } } } if (SoloSelection) //If Solo Selection is active there's no drag so skip the Drag Part { DeltaMousePos = Vector2.zero; goto ReleaseMouse; } #region DRAG/SWIPE on MOUSE/TOUCH if (UseSelectionZone && !isInSelectionZone) { return; //if Selection zone is enable and the mouse first click was not on the action zone skip } if (MainInputPress) //if we are still touching means that we are dragging/swiping { DeltaMousePos = (Vector2)(MouseStartPosition - Input.mousePosition); //Store the amount of distance travelled sice the first click/touch if (!IsSwapping && DeltaMousePos.magnitude > Threshold && DragSpeed != 0 && !SoloSelection) //Find out if is a Swipe/Drag { IsSwapping = true; //Is a Drag/Swipe!! if (RestoreTransformAnimationC != null) { StopCoroutine(RestoreTransformAnimationC); } RestoreTransformAnimationC = RestoreTransformAnimation(CurrentItem); StartCoroutine(RestoreTransformAnimationC); //If is not restoring the Previous Item Restore it } #region Draging and Swapping if Animate Selection is Active if (AnimateSelection && IsSwapping && DragSpeed != 0) //Just Drag if Animate Selection is active { float mult = 1000f / Screen.width; //This is for forcing to drag the same distance while resising the screen if (AnimateSelectionC != null) { StopCoroutine(AnimateSelectionC); //Stop if is in other transition and we are dragging isChangingItem = false; } #region Drag Radial if (S_Editor.SelectorType == SelectorType.Radial) //If is a Radial Selector----------------------------------------------------------- { DragDistance = dragHorizontal ? DeltaMousePos.x : DeltaMousePos.y; DragDistance *= Time.fixedDeltaTime * DragSpeed * mult; //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) * mult; 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(mult); } #endregion DeltaSpeed = DragDistance - lastDrag != 0 ? DragDistance - lastDrag : DeltaSpeed; //Calculate the Speed lastDrag = DragDistance; } #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| { OnClickOnItem.Invoke(current.gameObject); //Invoke OnClick Event on the Controller S_Manager.SelectItem(); } 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--------------------------- { if (inertia && Mathf.Abs(DeltaSpeed) >= minInertiaSpeed) //Make the Inertia { CInertia = C_InertiaRadial(DeltaSpeed); StartCoroutine(CInertia); return; } else { SetNextItemRadial(DragDistance); } } else if (S_Editor.SelectorType == SelectorType.Linear) //Linear Selector--------------------------- { if (inertia && Mathf.Abs(DeltaSpeed) >= (minInertiaSpeed * 0.1f)) //Make the Inertia { CInertia = C_InertiaLineal(DeltaSpeed); StartCoroutine(CInertia); return; } else { IndexSelected = Mathf.RoundToInt(Mathf.Abs(DragDistance) / distance) % Items.Count; } } else if (S_Editor.SelectorType == SelectorType.Grid) //Grid Selector--------------------------- { Vector3 DragMouse = GridVector(1000f / Screen.width) - 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) { 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 } }
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; }