Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: 1742 map centering #1864

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public interface IMapCameraController

void SetPositionAndZoom(Vector2 coordinates, float zoom);

void TranslateTo(Vector2 coordinates, float duration, Action onComplete = null);
void TranslateTo(Vector2 coordinates, float duration, Action? onComplete = null);

/// <summary>
/// Pauses rendering without releasing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ internal partial class MapCameraController : IMapCameraControllerInternal

private const int MAX_TEXTURE_SIZE = 4096;

public event Action<IMapActivityOwner, IMapCameraControllerInternal> OnReleasing;
public event Action<float, float> ZoomChanged;
public event Action<IMapActivityOwner, IMapCameraControllerInternal>? OnReleasing;
public event Action<float, float>? ZoomChanged;

public MapLayer EnabledLayers { get; private set; }

Expand All @@ -33,13 +33,13 @@ internal partial class MapCameraController : IMapCameraControllerInternal
private readonly IMapCullingController cullingController;
private readonly MapCameraObject mapCameraObject;

private RenderTexture renderTexture;
private RenderTexture? renderTexture;

// Zoom Thresholds in Parcels
private Vector2Int zoomValues;

private Rect cameraPositionBounds;
private Sequence translationSequence;
private Sequence? translationSequence;

public MapCameraController(
IMapInteractivityControllerInternal interactivityBehavior,
Expand Down Expand Up @@ -81,11 +81,11 @@ public void ResizeTexture(Vector2Int textureResolution)
{
if (!Camera) return;

if (renderTexture.IsCreated())
if (renderTexture != null && renderTexture.IsCreated())
renderTexture.Release();

textureResolution = ClampTextureResolution(textureResolution);
renderTexture.width = textureResolution.x;
renderTexture!.width = textureResolution.x;
renderTexture.height = textureResolution.y;
renderTexture.Create();

Expand Down Expand Up @@ -124,7 +124,7 @@ public void SetZoom(float value)

public void SetPosition(Vector2 coordinates)
{
translationSequence.Kill();
translationSequence?.Kill();
translationSequence = null;

Vector3 position = coordsUtils.CoordsToPositionUnclamped(coordinates);
Expand All @@ -140,15 +140,15 @@ public void SetLocalPosition(Vector2 localCameraPosition)

private void SetLocalPositionClamped(Vector2 localCameraPosition)
{
translationSequence.Kill();
translationSequence?.Kill();
translationSequence = null;

mapCameraObject.transform.localPosition = ClampLocalPosition(localCameraPosition);
}

public void SetPositionAndZoom(Vector2 coordinates, float zoom)
{
translationSequence.Kill();
translationSequence?.Kill();
translationSequence = null;

SetCameraSize(zoom);
Expand All @@ -158,14 +158,14 @@ public void SetPositionAndZoom(Vector2 coordinates, float zoom)
cullingController.SetCameraDirty(this);
}

public void TranslateTo(Vector2 coordinates, float duration, Action onComplete = null)
public void TranslateTo(Vector2 coordinates, float duration, Action? onComplete = null)
{
translationSequence = DOTween.Sequence();
translationSequence = DOTween.Sequence()!;

Vector3 position = coordsUtils.CoordsToPositionUnclamped(coordinates);
Vector3 targetPosition = ClampLocalPosition(new Vector3(position.x, position.y, CAMERA_HEIGHT));

translationSequence.Join(mapCameraObject.transform.DOLocalMove(targetPosition, duration).SetEase(Ease.OutQuart))
translationSequence.Join(mapCameraObject.transform.DOLocalMove(targetPosition, duration).SetEase(Ease.OutQuart)!)
.OnComplete(() =>
{
CalculateCameraPositionBounds();
Expand Down Expand Up @@ -245,22 +245,24 @@ public Rect GetCameraRect()
public void Release(IMapActivityOwner owner)
{
cullingController.OnCameraRemoved(this);
renderTexture?.Release();
if (renderTexture != null)
renderTexture.Release();
interactivityBehavior.Release();
OnReleasing?.Invoke(owner, this);
}

public void Dispose()
{
translationSequence.Kill();
translationSequence?.Kill();
translationSequence = null;

if (mapCameraObject != null)
UnityObjectUtils.SafeDestroy(mapCameraObject.gameObject);

interactivityBehavior.Dispose();

renderTexture?.Release();
if (renderTexture != null)
renderTexture.Release();
renderTexture = null;
}
}
Expand Down
3 changes: 2 additions & 1 deletion Explorer/Assets/DCL/Navmap/NavmapController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class NavmapController : IMapActivityOwner, ISection, IDisposable
private readonly Dictionary<NavmapSections, TabSelectorView> tabsBySections;
private readonly Dictionary<NavmapSections, ISection> mapSections;
private readonly Mouse mouse;
private readonly StringBuilder parcelTitleStringBuilder = new ();

private CancellationTokenSource animationCts;
private IMapCameraController cameraController;

Expand All @@ -55,7 +57,6 @@ public class NavmapController : IMapActivityOwner, ISection, IDisposable
private Vector2 lastParcelHovered;
private NavmapSections lastShownSection;
private MapRenderImage.ParcelClickData lastParcelClicked;
private StringBuilder parcelTitleStringBuilder = new StringBuilder();

public IReadOnlyDictionary<MapLayer, IMapLayerParameter> LayersParameters { get; } = new Dictionary<MapLayer, IMapLayerParameter>
{ { MapLayer.PlayerMarker, new PlayerMarkerParameter { BackgroundIsActive = true } } };
Expand Down
29 changes: 23 additions & 6 deletions Explorer/Assets/DCL/Navmap/NavmapLocationController.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,54 @@
using Arch.Core;
using DCL.Character.Components;
using DCL.MapRenderer.MapCameraController;
using UnityEngine;
using Utility;

namespace DCL.Navmap
{
public class NavmapLocationController
{
private readonly World world;
private readonly Entity playerEntity;
private const float TRANSITION_TIME = 0.5f;

private IMapCameraController cameraController;
private CharacterTransform playerTransformComponent;

private readonly World world;
private readonly Entity playerEntity;

public NavmapLocationController(NavmapLocationView view, World world, Entity playerEntity)
{
this.world = world;
this.playerEntity = playerEntity;
world.TryGet(playerEntity, out playerTransformComponent);

view.CenterToPlayerButton.onClick.AddListener(OnCenterToPlayer);
view.CenterToPlayerButton.onClick.AddListener(CenterToPlayer);
}

public void InjectCameraController(IMapCameraController controller)
{
this.cameraController = controller;

if (TryGetCoordinates(out var coordinates))
cameraController.SetPosition(coordinates);
}

private void OnCenterToPlayer()
private void CenterToPlayer()
{
if(world.TryGet(playerEntity, out playerTransformComponent))
cameraController.TranslateTo(ParcelMathHelper.WorldToGridPositionUnclamped(playerTransformComponent.Transform.position),TRANSITION_TIME);
if (TryGetCoordinates(out var coordinates))
cameraController.TranslateTo(coordinates, TRANSITION_TIME);
}

private bool TryGetCoordinates(out Vector2 coordinates)
{
if (world.TryGet(playerEntity, out playerTransformComponent))
{
coordinates = ParcelMathHelper.WorldToGridPositionUnclamped(playerTransformComponent.Transform.position);
return true;
}

coordinates = Vector2.zero;
return false;
}
}
}
10 changes: 5 additions & 5 deletions Explorer/Assets/DCL/UI/SectionSelectorController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Cysharp.Threading.Tasks;
using DCL.Character.CharacterMotion.Components;
using DG.Tweening;
using System;
using System.Collections.Generic;
using System.Threading;
Expand Down Expand Up @@ -47,14 +45,15 @@ public void SetAnimationState(bool isOn, TabSelectorView selectorToggle)
}
}

public async UniTaskVoid OnTabSelectorToggleValueChangedAsync(bool isOn, TabSelectorView selectorToggle, T section, CancellationToken ct, bool animate = true)
public UniTaskVoid OnTabSelectorToggleValueChangedAsync(bool isOn, TabSelectorView selectorToggle, T section, CancellationToken ct, bool animate = true)
{
if (!isOn || EnumUtils.Equals(section, previousSection))
return;
return new UniTaskVoid();

SetAnimationState(true, selectorToggle);

if (animate) { AnimatePanelsAsync(sections[previousSection], sections[section], section, ct); }
if (animate)
AnimatePanelsAsync(sections[previousSection], sections[section], section, ct);
else
{
sections[previousSection].Deactivate();
Expand All @@ -64,6 +63,7 @@ public async UniTaskVoid OnTabSelectorToggleValueChangedAsync(bool isOn, TabSele
SetPanelsPosition(sections[previousSection].GetRectTransform(), sections[section].GetRectTransform());
previousSection = section;
}
return new UniTaskVoid();
}

public void ResetAnimators()
Expand Down
Loading