public static void SetIndicatorSprite(this HUDNavigationElement element, Sprite sharedOffscreenSprite, bool offscreen = false) { if (element.Indicator == null) { return; } // get indicator icon settings IconSettings iconSettings = element.IndicatorIcon; if (offscreen) { iconSettings = element.OffscreenIndicatorIcon; } // get new indicator sprite Sprite newSprite = (iconSettings.OverrideIcon != null) ? iconSettings.OverrideIcon : element.Icon; if (offscreen && iconSettings.OverrideIcon == null) { newSprite = sharedOffscreenSprite; } // set new indicator sprite if (element.Indicator.sprite != newSprite) { element.Indicator.sprite = newSprite; } // set indicator size and color element.Indicator.rectTransform.sizeDelta = Vector2.one * iconSettings.IconSize; element.Indicator.color = iconSettings.IconColor; }
public static void SetMarkerActive(this HUDNavigationElement element, NavigationElementType elementType, bool value) { Image marker = null; if (elementType == NavigationElementType.Radar) { marker = element.RadarMarker; } else if (elementType == NavigationElementType.CompassBar) { marker = element.CompassBarMarker; } // set marker active/inactive if (marker != null) { // only update whifen value has changed if (value != marker.gameObject.activeSelf) { if (value) // appeared { element.OnAppear.Invoke(elementType); } else // disappeared { element.OnDisappear.Invoke(elementType); } // set active state marker.gameObject.SetActive(value); } } }
public static void SetIndicatorOffscreenRotation(this HUDNavigationElement element, Quaternion rotation) { // set indicator rotation if (element.Indicator.OffscreenPointer != null) { element.Indicator.OffscreenPointer.transform.rotation = rotation; } }
public void RemoveNavigationElement(HUDNavigationElement element) { if (element == null) { return; } // remove element from list NavigationElements.Remove(element); }
public static void SetIndicatorRotation(this HUDNavigationElement element, Quaternion rotation) { if (element.Indicator == null) { return; } // set indicator rotation element.Indicator.transform.rotation = rotation; }
public static void SetIndicatorPosition(this HUDNavigationElement element, Vector3 indicatorPos, RectTransform parentRect) { if (element.Indicator == null) { return; } // set indicator position element.Indicator.transform.position = new Vector3(indicatorPos.x + parentRect.localPosition.x, indicatorPos.y + parentRect.localPosition.y, 0f) + element.IndicatorOffset; }
public static void SetIndicatorScale(this HUDNavigationElement element, bool scalingEnabled, float distance, float scaleRadius, float minScale) { if (!scalingEnabled || element.ignoreIndicatorScaling) { return; } // set indicator scale float scale = (distance - 1f) / (scaleRadius - 1f); element.Indicator.PrefabRect.localScale = Vector2.Lerp(Vector2.one * minScale, Vector2.one, Mathf.Clamp01(scale)); }
public static void SetIndicatorFade(this HUDNavigationElement element, bool fadingEnabled, float distance, float fadeRadius, float minFade) { if (!fadingEnabled || element.ignoreIndicatorFading || element.Indicator.PrefabCanvasGroup == null) { return; } // set indicator fade float fade = (distance - 1f) / (fadeRadius - 1f); element.Indicator.PrefabCanvasGroup.alpha = Mathf.Lerp(1f * minFade, 1f, Mathf.Clamp01(fade)); }
public static void ShowRadarAboveArrow(this HUDNavigationElement element, bool value) { if (element.Radar.ArrowAbove == null) { return; } // only update if value has changed if (value != element.Radar.ArrowAbove.gameObject.activeSelf) { element.Radar.ArrowAbove.gameObject.SetActive(value); } }
public static void ShowMinimapBelowArrow(this HUDNavigationElement element, bool value) { if (element.Minimap.ArrowBelow == null) { return; } // only update if value has changed if (value != element.Minimap.ArrowBelow.gameObject.activeSelf) { element.Minimap.ArrowBelow.gameObject.SetActive(value); } }
public void AddNavigationElement(HUDNavigationElement element) { if (element == null) { return; } // add element, if it doesn't exist yet if (!NavigationElements.Contains(element)) { NavigationElements.Add(element); } }
public static void SetIndicatorScale(this HUDNavigationElement element, float distance, float scaleRadius, float minScale) { if (element.Indicator == null) { return; } // set indicator scale float scale = (distance - 1f) / (scaleRadius - 1f); scale = Mathf.Clamp01(scale); element.Indicator.rectTransform.localScale = Vector2.Lerp(Vector2.one * minScale, Vector2.one, scale); }
public static void ShowIndicatorDistance(this HUDNavigationElement element, bool value) { if (element.IndicatorDistance == null) { return; } // only update if value has changed if (value != element.IndicatorDistance.gameObject.activeSelf) { element.IndicatorDistance.gameObject.SetActive(value); } }
void UpdateCompassBarElement(HUDNavigationElement element, Vector3 screenPos, float distance) { // check if element is hidden within the compass bar if (element.HideInCompassBar) { element.SetMarkerActive(NavigationElementType.CompassBar, false); return; } // update distance text if (useCompassBarDistanceText && element.Distance != null) { element.Distance.text = string.Format(CompassBarDistanceText.TextFormat, (int)distance); } // check distance if (distance > CompassBarRadius && !element.IgnoreCompassBarRadius) { element.SetMarkerActive(NavigationElementType.CompassBar, false); // invoke events if (element.IsInCompassBarRadius) { element.IsInCompassBarRadius = false; element.OnLeaveRadius.Invoke(NavigationElementType.CompassBar); } return; } // invoke events if (!element.IsInCompassBarRadius) { element.IsInCompassBarRadius = true; element.OnEnterRadius.Invoke(NavigationElementType.CompassBar); } // set marker position if (screenPos.z <= 0) { // hide marker and skip element element.SetMarkerActive(NavigationElementType.CompassBar, false); return; } // set marker active element.SetMarkerActive(NavigationElementType.CompassBar, true); // set marker position element.SetMarkerPosition(NavigationElementType.CompassBar, screenPos, _HUDNavigationCanvas.CompassBar.ElementContainer); }
public static void SetMarkerPosition(this HUDNavigationElement element, NavigationElementType elementType, Vector3 markerPos, RectTransform parentRect = null) { // set marker position if (elementType == NavigationElementType.Radar) { element.Radar.transform.localPosition = markerPos; } else if (elementType == NavigationElementType.CompassBar) { element.CompassBar.transform.position = new Vector3(markerPos.x + parentRect.localPosition.x, parentRect.position.y, 0f); } else if (elementType == NavigationElementType.Minimap) { element.Minimap.transform.localPosition = markerPos; } }
void UpdateCompassBarElement(HUDNavigationElement element, Vector3 screenPos, float distance) { // check if element is hidden within the compass bar if (element.hideInCompassBar) { element.SetMarkerActive(NavigationElementType.CompassBar, false); return; } // check distance if (distance > compassBarRadius && !element.ignoreCompassBarRadius) { element.SetMarkerActive(NavigationElementType.CompassBar, false); // invoke events if (element.IsInCompassBarRadius) { element.IsInCompassBarRadius = false; element.OnLeaveRadius.Invoke(element, NavigationElementType.CompassBar); } return; } // invoke events if (!element.IsInCompassBarRadius) { element.IsInCompassBarRadius = true; element.OnEnterRadius.Invoke(element, NavigationElementType.CompassBar); } // set marker position if (screenPos.z <= 0) { // hide marker and skip element element.SetMarkerActive(NavigationElementType.CompassBar, false); return; } // show compass bar distance? element.ShowCompassBarDistance((int)distance); // set marker active element.SetMarkerActive(NavigationElementType.CompassBar, true); // set marker position element.SetMarkerPosition(NavigationElementType.CompassBar, screenPos, _HUDNavigationCanvas.CompassBar.ElementContainer); }
public static void SetMarkerActive(this HUDNavigationElement element, NavigationElementType elementType, bool value) { // get marker gameobject GameObject markerGO = null; switch (elementType) { case NavigationElementType.Radar: markerGO = element.Radar.gameObject; break; case NavigationElementType.CompassBar: markerGO = element.CompassBar.gameObject; break; case NavigationElementType.Minimap: markerGO = element.Minimap.gameObject; break; default: break; } // set marker gameobject active/inactive if (markerGO != null) { // only update if value has changed if (value != markerGO.activeSelf) { // invoke events if (value) // appeared { element.OnAppear.Invoke(element, elementType); } else // disappeared { element.OnDisappear.Invoke(element, elementType); } // set active state markerGO.gameObject.SetActive(value); } } }
public static void SetIndicatorActive(this HUDNavigationElement element, bool value) { // only update, if value has changed if (value != element.Indicator.gameObject.activeSelf) { // invoke events if (value) // appeared { element.OnAppear.Invoke(element, NavigationElementType.Indicator); } else // disappeared { element.OnDisappear.Invoke(element, NavigationElementType.Indicator); } // set indicator active/inactive element.Indicator.gameObject.SetActive(value); } }
public static void ShowCompassBarDistance(this HUDNavigationElement element, int distance = 0) { if (element.CompassBar.DistanceText == null) { return; } // only update if value has changed bool useDistanceText = element.useCompassBarDistanceText; if (useDistanceText != element.CompassBar.DistanceText.gameObject.activeSelf) { element.CompassBar.DistanceText.gameObject.SetActive(useDistanceText); } // update distance text if active if (useDistanceText) // TODO add TextMeshPro support? { element.CompassBar.DistanceText.text = string.Format(element.compassBarDistanceTextFormat, distance); } }
public void CopySettings(HUDNavigationElement element) { if (element == null) { return; } // misc this.Prefabs = element.Prefabs; // radar settings this.hideInRadar = element.hideInRadar; this.ignoreRadarRadius = element.ignoreRadarRadius; this.rotateWithGameObject = element.rotateWithGameObject; this.useRadarHeightSystem = element.useRadarHeightSystem; // compass bar settings this.hideInCompassBar = element.hideInCompassBar; this.ignoreCompassBarRadius = element.ignoreCompassBarRadius; this.useCompassBarDistanceText = element.useCompassBarDistanceText; this.compassBarDistanceTextFormat = element.compassBarDistanceTextFormat; // indicator settings this.showIndicator = element.showIndicator; this.showOffscreenIndicator = element.showOffscreenIndicator; this.ignoreIndicatorRadius = element.ignoreIndicatorRadius; this.ignoreIndicatorHideDistance = element.ignoreIndicatorHideDistance; this.ignoreIndicatorScaling = element.ignoreIndicatorScaling; this.ignoreIndicatorFading = element.ignoreIndicatorFading; this.useIndicatorDistanceText = element.useIndicatorDistanceText; this.showOffscreenIndicatorDistance = element.showOffscreenIndicatorDistance; this.indicatorOnscreenDistanceTextFormat = element.indicatorOnscreenDistanceTextFormat; this.indicatorOffscreenDistanceTextFormat = element.indicatorOffscreenDistanceTextFormat; // minimap settings this.hideInMinimap = element.hideInMinimap; this.ignoreMinimapRadius = element.ignoreMinimapRadius; this.rotateWithGameObjectMM = element.rotateWithGameObjectMM; this.useMinimapHeightSystem = element.useMinimapHeightSystem; }
public static void SetIndicatorOnOffscreen(this HUDNavigationElement element, bool value) { // show/hide onscreen rect if (element.Indicator.OnscreenRect != null) { // only update, if value has changed if (value != element.Indicator.OnscreenRect.gameObject.activeSelf) { element.Indicator.OnscreenRect.gameObject.SetActive(value); } } // show/hide offscreen rect if (element.Indicator.OffscreenRect != null) { // only update, if value has changed if (!value != element.Indicator.OffscreenRect.gameObject.activeSelf) { element.Indicator.OffscreenRect.gameObject.SetActive(!value); } } }
public static void ShowIndicatorDistance(this HUDNavigationElement element, bool onScreen, int distance = 0) { // show/hide distance text Text distanceText = (onScreen) ? element.Indicator.OnscreenDistanceText : element.Indicator.OffscreenDistanceText; if (distanceText != null) { bool showDistance = (onScreen) ? element.useIndicatorDistanceText : element.useIndicatorDistanceText && element.showOffscreenIndicatorDistance; // only update if value has changed if (showDistance != distanceText.gameObject.activeSelf) { distanceText.gameObject.SetActive(showDistance); } // update distance text if active if (showDistance) // TODO add TextMeshPro support? { distanceText.text = string.Format((onScreen) ? element.indicatorOnscreenDistanceTextFormat : element.indicatorOffscreenDistanceTextFormat, distance); } } }
public static bool IsVisibleOnScreen(this HUDNavigationElement element, Vector3 screenPos) { return(screenPos.z > 0 && screenPos.x > 0 && screenPos.x < Screen.width && screenPos.y > 0 && screenPos.y < Screen.height); }
public static float GetIconRadius(this HUDNavigationElement element, NavigationElementType elementType) { float radius = (elementType == NavigationElementType.Radar) ? element.Radar.PrefabRect.sizeDelta.x : element.Minimap.PrefabRect.sizeDelta.x; return(radius / 2f); }
void UpdateRadarElement(HUDNavigationElement element, Vector3 screenPos, float distance) { float _scaledRadius = RadarRadius * RadarZoom; float _scaledMaxRadius = RadarMaxRadius * RadarZoom; // check for destroyed marker if (element.RadarMarker == null) { return; } // check if element is hidden within the compass bar if (element.HideInRadar) { element.SetMarkerActive(NavigationElementType.Radar, false); return; } // check distance if (distance > _scaledRadius) { // invoke events if (element.IsInRadarRadius) { element.IsInRadarRadius = false; element.OnLeaveRadius.Invoke(NavigationElementType.Radar); } // check max distance if (distance > _scaledMaxRadius) { element.SetMarkerActive(NavigationElementType.Radar, false); return; } // set scaled distance when out of range distance = _scaledRadius; } else { // rotate marker Transform rotationReference = GetRotationReference(); if (RadarType == RadarTypes.RotateRadar) { element.RadarMarker.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, -element.transform.eulerAngles.y + rotationReference.eulerAngles.y)); } else { element.RadarMarker.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, -element.transform.eulerAngles.y)); } // invoke events if (!element.IsInRadarRadius) { element.IsInRadarRadius = true; element.OnEnterRadius.Invoke(NavigationElementType.Radar); } } // set marker active element.SetMarkerActive(NavigationElementType.Radar, true); // calculate marker position Vector3 posOffset = element.GetPositionOffset(PlayerController); Vector3 markerPos = new Vector3(posOffset.x, posOffset.z, 0f); markerPos.Normalize(); markerPos *= (distance / _scaledRadius) * (_HUDNavigationCanvas.Radar.ElementContainer.GetRadius() - element.GetIconRadius()); // set marker position element.SetMarkerPosition(NavigationElementType.Radar, markerPos); }
void UpdateIndicatorElement(HUDNavigationElement element, Vector3 screenPos, float distance) { if (useIndicators && element.ShowIndicator) { // update distance text if (useIndicatorDistanceText && element.IndicatorDistance != null) { element.IndicatorDistance.text = string.Format(IndicatorDistanceText.TextFormat, (int)distance); } // check distance and visibility if ((distance > IndicatorRadius && !element.IgnoreIndicatorRadius) || (screenPos.z <= 0 && !useIndicatorOffscreen)) { element.SetIndicatorActive(false); // invoke events if (element.IsInIndicatorRadius) { element.IsInIndicatorRadius = false; element.OnLeaveRadius.Invoke(NavigationElementType.Indicator); } } else { // calculate off-screen position, if indicator is not in sight if (useIndicatorOffscreen && !element.IsVisibleOnScreen(screenPos)) { // flip if indicator is behind us if (screenPos.z < 0f) { screenPos.x = Screen.width - screenPos.x; screenPos.y = Screen.height - screenPos.y; } // calculate off-screen position/rotation Vector3 screenCenter = new Vector3(Screen.width, Screen.height, 0f) / 2f; screenPos -= screenCenter; float angle = Mathf.Atan2(screenPos.y, screenPos.x); angle -= 90f * Mathf.Deg2Rad; float cos = Mathf.Cos(angle); float sin = -Mathf.Sin(angle); float cotangent = cos / sin; screenPos = screenCenter + new Vector3(sin * 50f, cos * 50f, 0f); // is indicator inside the defined bounds? Vector3 screenBounds = screenCenter * (1f - IndicatorOffscreenBorder); float boundsY = (cos > 0f) ? screenBounds.y : -screenBounds.y; screenPos = new Vector3(boundsY / cotangent, boundsY, 0f); // when out of bounds, get point on appropriate side if (screenPos.x > screenBounds.x) // out => right { screenPos = new Vector3(screenBounds.x, screenBounds.x * cotangent, 0f); } else if (screenPos.x < -screenBounds.x) // out => left { screenPos = new Vector3(-screenBounds.x, -screenBounds.x * cotangent, 0f); } screenPos += screenCenter; // update indicator rotation element.SetIndicatorRotation(Quaternion.Euler(0f, 0f, angle * Mathf.Rad2Deg)); // update indicator icon element.SetIndicatorSprite(IndicatorOffscreenSprite, true); // show indicator distance offscreen? element.ShowIndicatorDistance(!hideIndicatorDistanceOffscreen); } else { // reset indicator rotation element.SetIndicatorRotation(Quaternion.identity); // reset indicator icon element.SetIndicatorSprite(null, false); // show indicator distance element.ShowIndicatorDistance(useIndicatorDistanceText); } // update indicator values element.SetIndicatorPosition(screenPos, _HUDNavigationCanvas.Indicator.ElementContainer); element.SetIndicatorScale(distance, IndicatorScaleRadius, IndicatorMinScale); element.SetIndicatorActive(true); // invoke events if (!element.IsInIndicatorRadius) { element.IsInIndicatorRadius = true; element.OnEnterRadius.Invoke(NavigationElementType.Indicator); } } } else { element.SetIndicatorActive(false); } }
public static float GetDistance(this HUDNavigationElement element, Transform other) { return(Vector2.Distance(new Vector2(element.transform.position.x, element.transform.position.z), new Vector2(other.position.x, other.position.z))); }
public static void SetIndicatorPosition(this HUDNavigationElement element, Vector3 indicatorPos, RectTransform parentRect) { // set indicator position element.Indicator.transform.position = new Vector3(indicatorPos.x + parentRect.localPosition.x, indicatorPos.y + parentRect.localPosition.y, 0f); }
public static Vector3 GetPositionOffset(this HUDNavigationElement element, Vector3 otherPosition) { return(element.transform.position - otherPosition); }
public static Vector3 GetPosition(this HUDNavigationElement element) { return(element.transform.position); }