/*--------------------------------------------------------------------------------------------*/ private void UpdateSelectionProgress() { HoverItemHighlightState highState = GetComponent <HoverItemHighlightState>(); if (vSelectionStart == null) { HoverItemData itemData = GetComponent <HoverItem>().Data; IItemDataSelectable selData = (itemData as IItemDataSelectable); if (selData == null || !selData.IsStickySelected) { SelectionProgress = 0; return; } HoverItemHighlightState.Highlight?nearestHigh = highState.NearestHighlight; float nearDist = highState.InteractionSettings.StickyReleaseDistance; float minHighDist = (nearestHigh == null ? float.MaxValue : nearestHigh.Value.Distance); SelectionProgress = Mathf.InverseLerp(nearDist, vDistanceUponSelection, minHighDist); return; } float ms = (float)(DateTime.UtcNow - (DateTime)vSelectionStart).TotalMilliseconds; SelectionProgress = Math.Min(1, ms / highState.InteractionSettings.SelectionMilliseconds); }
/*--------------------------------------------------------------------------------------------*/ private void UpdateItems() { List <ICursorData> cursors = CursorDataProvider.Cursors; int cursorCount = cursors.Count; for (int i = 0; i < cursorCount; i++) { ICursorData cursor = cursors[i]; cursor.MaxItemHighlightProgress = 0; cursor.MaxItemSelectionProgress = 0; if (!cursor.CanCauseSelections) { continue; } HoverItemHighlightState.Highlight?high; HoverItemHighlightState highState = FindNearestItemToCursor(cursor.Type, out high); if (highState == null || high == null) { continue; } highState.SetNearestAcrossAllItemsForCursor(cursor.Type); cursor.MaxItemHighlightProgress = high.Value.Progress; } }
/*--------------------------------------------------------------------------------------------*/ private HoverItemHighlightState FindNearestItemToCursor(CursorType pCursorType, out HoverItemHighlightState.Highlight?pNearestHigh) { float minDist = float.MaxValue; HoverItemHighlightState nearestItem = null; pNearestHigh = null; for (int i = 0; i < vHighStates.Count; i++) { HoverItemHighlightState item = vHighStates[i]; if (!item.gameObject.activeInHierarchy || item.IsHighlightPrevented) { continue; } HoverItemHighlightState.Highlight?high = item.GetHighlight(pCursorType); if (high == null || high.Value.Distance >= minDist) { continue; } minDist = high.Value.Distance; nearestItem = item; pNearestHigh = high; } return(nearestItem); }
/*--------------------------------------------------------------------------------------------*/ private void FillCursorLists() { for (int i = 0; i < vItems.Count; i++) { HoverItemData data = vItems[i].Data; IItemDataSelectable selData = (IItemDataSelectable)data; HoverItemHighlightState highState = data.GetComponent <HoverItemHighlightState>(); ICursorData cursorData = highState.NearestHighlight.Value.Cursor; if (cursorData.Idle.Progress >= 1) { selData.DeselectStickySelections(); continue; } HoverItemSelectionState selState = data.GetComponent <HoverItemSelectionState>(); HoverRenderer rend = data.GetComponent <HoverItemRendererUpdater>().ActiveRenderer; var info = new StickySelectionInfo { ItemWorldPosition = rend.GetCenterWorldPosition(), SelectionProgress = selState.SelectionProgress }; cursorData.ActiveStickySelections.Add(info); } }
/*--------------------------------------------------------------------------------------------*/ private HoverItemHighlightState FindNearestItemWithinProxOfCursor(CursorType pCursorType, out HoverItemHighlightState.Highlight?pNearestHigh) { float minDist = float.MaxValue; HoverItemHighlightState nearestItem = null; pNearestHigh = null; for (int i = 0; i < vActiveHighStates.Count; i++) { HoverItemHighlightState highState = vActiveHighStates[i]; HoverItemHighlightState.Highlight?high = highState.GetHighlight(pCursorType); if (high == null || high.Value.Progress <= 0 || high.Value.Distance >= minDist) { continue; } minDist = high.Value.Distance; nearestItem = highState; pNearestHigh = high; } return(nearestItem); }
/*--------------------------------------------------------------------------------------------*/ private bool UpdateState() { HoverItemData itemData = GetComponent <HoverItem>().Data; IItemDataSelectable selData = (itemData as IItemDataSelectable); WasSelectedThisFrame = false; if (selData == null) { return(false); } //// HoverItemHighlightState highState = GetComponent <HoverItemHighlightState>(); bool canSelect = ( !highState.IsHighlightPrevented && highState.IsNearestAcrossAllItemsForAnyCursor && selData.AllowSelection ); if (SelectionProgress <= 0 || !canSelect) { selData.DeselectStickySelections(); } if (!canSelect || highState.MaxHighlightProgress < 1) { IsSelectionPrevented = false; vSelectionStart = null; return(false); } //// if (IsSelectionPrevented) { vSelectionStart = null; return(false); } if (vSelectionStart == null) { vSelectionStart = DateTime.UtcNow; return(false); } if (SelectionProgress < 1) { return(false); } vSelectionStart = null; IsSelectionPrevented = true; WasSelectedThisFrame = true; vDistanceUponSelection = highState.NearestHighlight.Value.Distance; selData.Select(); return(true); }
/*--------------------------------------------------------------------------------------------*/ private RaycastResult?CalcNearestRaycastResult(ICursorData pCursor) { if (!pCursor.IsRaycast || !pCursor.CanCauseSelections) { return(null); } float minHighSqrDist = float.MaxValue; float minCastSqrDist = float.MaxValue; var worldRay = new Ray(pCursor.WorldPosition, pCursor.WorldRotation * pCursor.RaycastLocalDirection); RaycastResult result = new RaycastResult(); result.WorldPosition = worldRay.GetPoint(10000); result.WorldRotation = pCursor.WorldRotation; for (int i = 0; i < vHighStates.Count; i++) { HoverItemHighlightState item = vHighStates[i]; if (item.IsHighlightPreventedViaAnyDisplay()) { continue; } RaycastResult raycast; Vector3 nearHighWorldPos = item.ProximityProvider .GetNearestWorldPosition(worldRay, out raycast); if (!raycast.IsHit) { continue; } float highSqrDist = (raycast.WorldPosition - nearHighWorldPos).sqrMagnitude; float castSqrDist = (raycast.WorldPosition - pCursor.WorldPosition).sqrMagnitude; //float hitSqrDist = Mathf.Pow(item.InteractionSettings.HighlightDistanceMin, 2); float hitSqrDist = 0.0000001f; highSqrDist = Mathf.Max(highSqrDist, hitSqrDist); bool isHighlightWorse = (highSqrDist > minHighSqrDist); bool isComparingHits = (highSqrDist <= hitSqrDist && minHighSqrDist <= hitSqrDist); bool isRaycastNearer = (castSqrDist < minCastSqrDist); if (isHighlightWorse || (isComparingHits && !isRaycastNearer)) { continue; } minHighSqrDist = highSqrDist; minCastSqrDist = castSqrDist; result = raycast; } return(result); }
/*--------------------------------------------------------------------------------------------*/ private void UpdateItems() { List <ICursorData> cursors = CursorDataProvider.Cursors; int cursorCount = cursors.Count; for (int i = 0; i < cursorCount; i++) { ICursorData cursor = cursors[i]; cursor.MaxItemHighlightProgress = 0; cursor.MaxItemSelectionProgress = 0; if (!cursor.CanCauseSelections) { continue; } HoverItemHighlightState highState = FindNearestItemWithinProxOfCursor( cursor.Type, out HoverItemHighlightState.Highlight? high); if (highState == null || high == null) { continue; } highState.SetNearestAcrossAllItemsForCursor(cursor.Type); cursor.MaxItemHighlightProgress = high.Value.Progress; } for (int i = 0; i < vActiveHighStates.Count; i++) { HoverItemHighlightState highState = vActiveHighStates[i]; HoverItemSelectionState selState = highState.GetComponent <HoverItemSelectionState>(); highState.UpdateViaManager(); if (selState == null) { continue; } selState.UpdateViaManager(); if (selState.WasSelectedThisFrame) { ICursorData selCursor = highState.NearestHighlight?.Cursor; //Debug.Log("Item selected: "+selState.transform.ToDebugPath()+" / "+ // selCursor?.Type, selState); OnItemSelected.Invoke(highState.GetComponent <HoverItem>(), selCursor); } } }
/*--------------------------------------------------------------------------------------------*/ private void UpdateNearestCursor() { HoverItemHighlightState highState = GetComponent <HoverItemHighlightState>(); HoverItemHighlightState.Highlight?nearestHigh = highState.NearestHighlight; if (nearestHigh == null) { return; } ICursorData cursor = nearestHigh.Value.Cursor; cursor.MaxItemSelectionProgress = Mathf.Max( cursor.MaxItemSelectionProgress, SelectionProgress); }
//////////////////////////////////////////////////////////////////////////////////////////////// /*--------------------------------------------------------------------------------------------*/ private void ResetItems() { for (int i = 0; i < vHighStates.Count; i++) { HoverItemHighlightState highState = vHighStates[i]; if (highState == null) { vHighStates.RemoveAt(i); i--; Debug.LogWarning("Found and removed a null item; use RemoveItem() instead."); continue; } highState.ResetAllNearestStates(); } }
/*--------------------------------------------------------------------------------------------*/ private bool UpdateState() { HoverItemData itemData = GetComponent <HoverItem>().Data; IItemDataSelectable selData = (itemData as IItemDataSelectable); WasSelectedThisFrame = false; if (selData == null || selData.IgnoreSelection) { return(false); } //// HoverItemHighlightState highState = GetComponent <HoverItemHighlightState>(); bool hasNearestCursorWithFullHigh = false; bool canDeselect = ( highState.IsHighlightPrevented || !highState.IsNearestAcrossAllItemsForAnyCursor || !selData.IsEnabled ); for (int i = 0; i < highState.Highlights.Count; i++) { HoverItemHighlightState.Highlight high = highState.Highlights[i]; if (high.IsNearestAcrossAllItems && high.Progress >= 1) { hasNearestCursorWithFullHigh = true; break; } } if (SelectionProgress <= 0 || canDeselect) { selData.DeselectStickySelections(); } if (canDeselect || !hasNearestCursorWithFullHigh) { IsSelectionPrevented = false; vSelectionStart = null; return(false); } //// if (IsSelectionPrevented) { vSelectionStart = null; return(false); } if (vSelectionStart == null) { vSelectionStart = DateTime.UtcNow; return(false); } if (SelectionProgress < 1) { return(false); } vSelectionStart = null; IsSelectionPrevented = true; WasSelectedThisFrame = true; vDistanceUponSelection = highState.NearestHighlight.Value.Distance; selData.Select(); return(true); }