Skip to content

Commit

Permalink
Fix GetWorldViewbounds() and GetWorldViewport() (#5060)
Browse files Browse the repository at this point in the history
* Fix GetWorldViewbounds() and GetWorldViewport()

* Remove some uses of `CurrentMap` and `CurrentEye`
  • Loading branch information
ElectroJr authored May 19, 2024
1 parent da7abc6 commit 85f74c3
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 43 deletions.
2 changes: 1 addition & 1 deletion RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ END TEMPLATE-->

### Bugfixes

*None yet*
* `IEyeManager.GetWorldViewbounds()` and `IEyeManager.GetWorldViewbounds()` should now return the correct bounds if the main viewport does not take up the whole screen.

### Other

Expand Down
4 changes: 2 additions & 2 deletions Robust.Client/Debugging/DebugPhysicsSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private void DrawWorld(DrawingHandleWorld worldHandle, OverlayDrawArgs args)
{
var viewBounds = args.WorldBounds;
var viewAABB = args.WorldAABB;
var mapId = _eyeManager.CurrentMap;
var mapId = args.MapId;

if ((_debugPhysicsSystem.Flags & PhysicsDebugFlags.Shapes) != 0)
{
Expand Down Expand Up @@ -373,7 +373,7 @@ private void DrawWorld(DrawingHandleWorld worldHandle, OverlayDrawArgs args)

private void DrawScreen(DrawingHandleScreen screenHandle, OverlayDrawArgs args)
{
var mapId = _eyeManager.CurrentMap;
var mapId = args.MapId;
var mousePos = _inputManager.MouseScreenPosition;

if ((_debugPhysicsSystem.Flags & PhysicsDebugFlags.ShapeInfo) != 0x0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public DebugLightOverlay(EntityLookupSystem lookup, IEyeManager eyeManager, IMap

protected internal override void Draw(in OverlayDrawArgs args)
{
var map = _eyeManager.CurrentMap;
var map = args.MapId;
if (map == MapId.Nullspace) return;

foreach (var (_, treeComp) in _trees.GetIntersectingTrees(map, args.WorldBounds))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public GridChunkBoundsOverlay(IEntityManager entManager, IEyeManager eyeManager,

protected internal override void Draw(in OverlayDrawArgs args)
{
var currentMap = _eyeManager.CurrentMap;
var currentMap = args.MapId;
var viewport = args.WorldBounds;
var worldHandle = args.WorldHandle;

Expand Down
2 changes: 1 addition & 1 deletion Robust.Client/GameStates/NetInterpOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected internal override void Draw(in OverlayDrawArgs args)
while (query.MoveNext(out var uid, out var transform))
{
// if not on the same map, continue
if (transform.MapID != _eyeManager.CurrentMap || _container.IsEntityInContainer(uid))
if (transform.MapID != args.MapId || _container.IsEntityInContainer(uid))
continue;

if (transform.GridUid == uid)
Expand Down
21 changes: 7 additions & 14 deletions Robust.Client/Graphics/ClientEye/EyeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,22 @@ void IEyeManager.Initialize()
/// <inheritdoc />
public Box2 GetWorldViewport()
{
var vpSize = _displayManager.ScreenSize;

var topLeft = ScreenToMap(Vector2.Zero);
var topRight = ScreenToMap(new Vector2(vpSize.X, 0));
var bottomRight = ScreenToMap(vpSize);
var bottomLeft = ScreenToMap(new Vector2(0, vpSize.Y));

var left = MathHelper.Min(topLeft.X, topRight.X, bottomRight.X, bottomLeft.X);
var bottom = MathHelper.Min(topLeft.Y, topRight.Y, bottomRight.Y, bottomLeft.Y);
var right = MathHelper.Max(topLeft.X, topRight.X, bottomRight.X, bottomLeft.X);
var top = MathHelper.Max(topLeft.Y, topRight.Y, bottomRight.Y, bottomLeft.Y);

return new Box2(left, bottom, right, top);
return GetWorldViewbounds().CalcBoundingBox();
}

/// <inheritdoc />
public Box2Rotated GetWorldViewbounds()
{
var vpSize = _displayManager.ScreenSize;
// This is an inefficient and roundabout way of geting the viewport.
// But its a method that shouldn't get used much.

var vp = MainViewport as Control;
var vpSize = vp?.PixelSize ?? _displayManager.ScreenSize;

var topRight = ScreenToMap(new Vector2(vpSize.X, 0)).Position;
var bottomLeft = ScreenToMap(new Vector2(0, vpSize.Y)).Position;

// This assumes the main viewports eye and the main eye are the same.
var rotation = new Angle(CurrentEye.Rotation);
var center = (bottomLeft + topRight) / 2;

Expand Down
18 changes: 11 additions & 7 deletions Robust.Client/Graphics/ClientEye/IEyeManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Numerics;
using System;
using System.Numerics;
using Robust.Client.UserInterface.CustomControls;
using Robust.Shared.Graphics;
using Robust.Shared.Map;
Expand All @@ -13,26 +14,29 @@ namespace Robust.Client.Graphics
public interface IEyeManager
{
/// <summary>
/// The current eye that is being used to render the game.
/// The primary eye, which is usually the eye associated with the main viewport.
/// </summary>
/// <remarks>
/// Generally, you should avoid using this whenever possible. E.g., when rendering overlays should use the
/// eye & viewbounds that gets passed to the draw method.
/// Setting this property to null will use the default eye.
/// </remarks>
IEye CurrentEye { get; set; }

IViewportControl MainViewport { get; set; }

/// <summary>
/// The ID of the map on which the current eye is "placed".
/// </summary>
[Obsolete]
MapId CurrentMap { get; }

/// <summary>
/// A world-space box that is at LEAST the area covered by the viewport.
/// A world-space box that is at LEAST the area covered by the main viewport.
/// May be larger due to say rotation.
/// </summary>
Box2 GetWorldViewport();

/// <summary>
/// A world-space box of the area visible in the main viewport.
/// </summary>
Box2Rotated GetWorldViewbounds();

/// <summary>
Expand All @@ -43,7 +47,7 @@ public interface IEyeManager
void GetScreenProjectionMatrix(out Matrix3 projMatrix);

/// <summary>
/// Projects a point from world space to UI screen space using the current camera.
/// Projects a point from world space to UI screen space using the main viewport.
/// </summary>
/// <param name="point">Point in world to transform.</param>
/// <returns>Corresponding point in UI screen space.</returns>
Expand Down
9 changes: 6 additions & 3 deletions Robust.Client/Graphics/Clyde/Clyde.HLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ private void RenderViewport(Viewport viewport)
var worldBounds = CalcWorldBounds(viewport);
var worldAABB = worldBounds.CalcBoundingBox();

if (_eyeManager.CurrentMap != MapId.Nullspace)
if (eye.Position.MapId != MapId.Nullspace)
{
using (DebugGroup("Lights"))
using (_prof.Group("Lights"))
Expand Down Expand Up @@ -544,9 +544,12 @@ private void RenderViewport(Viewport viewport)
UIBox2.FromDimensions(Vector2.Zero, viewport.Size), new Color(1, 1, 1, 0.5f));
}

using (_prof.Group("Overlays WS"))
if (eye.Position.MapId != MapId.Nullspace)
{
RenderOverlays(viewport, OverlaySpace.WorldSpace, worldAABB, worldBounds);
using (_prof.Group("Overlays WS"))
{
RenderOverlays(viewport, OverlaySpace.WorldSpace, worldAABB, worldBounds);
}
}

_currentViewport = oldVp;
Expand Down
8 changes: 4 additions & 4 deletions Robust.Client/Placement/Modes/AlignSnapgridCenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SnapgridCenter : PlacementMode

public SnapgridCenter(PlacementManager pMan) : base(pMan) { }

public override void Render(DrawingHandleWorld handle)
public override void Render(in OverlayDrawArgs args)
{
if (Grid != null)
{
Expand All @@ -34,18 +34,18 @@ public override void Render(DrawingHandleWorld handle)
{
var from = ScreenToWorld(new Vector2(a, 0));
var to = ScreenToWorld(new Vector2(a, viewportSize.Y));
handle.DrawLine(from, to, new Color(0, 0, 1f));
args.WorldHandle.DrawLine(from, to, new Color(0, 0, 1f));
}
for (var a = gridstart.Y; a < viewportSize.Y; a += SnapSize * EyeManager.PixelsPerMeter)
{
var from = ScreenToWorld(new Vector2(0, a));
var to = ScreenToWorld(new Vector2(viewportSize.X, a));
handle.DrawLine(from, to, new Color(0, 0, 1f));
args.WorldHandle.DrawLine(from, to, new Color(0, 0, 1f));
}
}

// Draw grid BELOW the ghost thing.
base.Render(handle);
base.Render(args);
}

public override void AlignPlacementMode(ScreenCoordinates mouseScreen)
Expand Down
12 changes: 6 additions & 6 deletions Robust.Client/Placement/PlacementManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -628,20 +628,20 @@ private bool DeactivateSpecialPlacement()
return true;
}

private void Render(DrawingHandleWorld handle)
private void Render(in OverlayDrawArgs args)
{
if (CurrentMode == null || !IsActive)
{
if (EraserRect.HasValue)
{
handle.UseShader(_drawingShader);
handle.DrawRect(EraserRect.Value, new Color(255, 0, 0, 50));
handle.UseShader(null);
args.WorldHandle.UseShader(_drawingShader);
args.WorldHandle.DrawRect(EraserRect.Value, new Color(255, 0, 0, 50));
args.WorldHandle.UseShader(null);
}
return;
}

CurrentMode.Render(handle);
CurrentMode.Render(args);

if (CurrentPermission is not {Range: > 0} ||
!CurrentMode.RangeRequired ||
Expand All @@ -650,7 +650,7 @@ private void Render(DrawingHandleWorld handle)

var worldPos = EntityManager.GetComponent<TransformComponent>(controlled).WorldPosition;

handle.DrawCircle(worldPos, CurrentPermission.Range, new Color(1, 1, 1, 0.25f));
args.WorldHandle.DrawCircle(worldPos, CurrentPermission.Range, new Color(1, 1, 1, 0.25f));
}

private void HandleStartPlacement(MsgPlacement msg)
Expand Down
5 changes: 3 additions & 2 deletions Robust.Client/Placement/PlacementMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected PlacementMode(PlacementManager pMan)
/// <returns></returns>
public abstract bool IsValidPosition(EntityCoordinates position);

public virtual void Render(DrawingHandleWorld handle)
public virtual void Render(in OverlayDrawArgs args)
{
var uid = pManager.CurrentPlacementOverlayEntity;
if (!pManager.EntityManager.TryGetComponent(uid, out SpriteComponent? sprite) || !sprite.Visible)
Expand Down Expand Up @@ -125,7 +125,8 @@ public virtual void Render(DrawingHandleWorld handle)
var worldRot = pManager.EntityManager.GetComponent<TransformComponent>(coordinate.EntityId).WorldRotation + dirAng;

sprite.Color = IsValidPosition(coordinate) ? ValidPlaceColor : InvalidPlaceColor;
spriteSys.Render(uid.Value, sprite, handle, pManager.EyeManager.CurrentEye.Rotation, worldRot, worldPos);
var rot = args.Viewport.Eye?.Rotation ?? default;
spriteSys.Render(uid.Value, sprite, args.WorldHandle, rot, worldRot, worldPos);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Robust.Client/Placement/PlacementOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public PlacementOverlay(PlacementManager manager)

protected internal override void Draw(in OverlayDrawArgs args)
{
_manager.Render(args.WorldHandle);
_manager.Render(args);
}
}
}
Expand Down

0 comments on commit 85f74c3

Please sign in to comment.