Re-organize all projects (#4166)
This commit is contained in:
157
Content.Client/Viewport/GameScreen.cs
Normal file
157
Content.Client/Viewport/GameScreen.cs
Normal file
@@ -0,0 +1,157 @@
|
||||
using Content.Client.Administration.Managers;
|
||||
using Content.Client.Chat.Managers;
|
||||
using Content.Client.Chat.UI;
|
||||
using Content.Client.Construction.UI;
|
||||
using Content.Client.HUD;
|
||||
using Content.Client.HUD.UI;
|
||||
using Content.Client.Voting;
|
||||
using Content.Shared.Chat;
|
||||
using Content.Shared.Input;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.Input;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.Input.Binding;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Client.Viewport
|
||||
{
|
||||
public class GameScreen : GameScreenBase, IMainViewportState
|
||||
{
|
||||
public static readonly Vector2i ViewportSize = (EyeManager.PixelsPerMeter * 21, EyeManager.PixelsPerMeter * 15);
|
||||
|
||||
[Dependency] private readonly IUserInterfaceManager _userInterfaceManager = default!;
|
||||
[Dependency] private readonly IGameHud _gameHud = default!;
|
||||
[Dependency] private readonly IInputManager _inputManager = default!;
|
||||
[Dependency] private readonly IChatManager _chatManager = default!;
|
||||
[Dependency] private readonly IVoteManager _voteManager = default!;
|
||||
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
|
||||
[Dependency] private readonly IClientAdminManager _adminManager = default!;
|
||||
[Dependency] private readonly IClyde _clyde = default!;
|
||||
[Dependency] private readonly IEyeManager _eyeManager = default!;
|
||||
|
||||
[ViewVariables] private ChatBox? _gameChat;
|
||||
private ConstructionMenuPresenter? _constructionMenu;
|
||||
|
||||
public MainViewport Viewport { get; private set; } = default!;
|
||||
|
||||
public override void Startup()
|
||||
{
|
||||
base.Startup();
|
||||
|
||||
_gameChat = new ChatBox();
|
||||
Viewport = new MainViewport
|
||||
{
|
||||
Viewport =
|
||||
{
|
||||
ViewportSize = ViewportSize
|
||||
}
|
||||
};
|
||||
|
||||
_userInterfaceManager.StateRoot.AddChild(Viewport);
|
||||
LayoutContainer.SetAnchorPreset(Viewport, LayoutContainer.LayoutPreset.Wide);
|
||||
Viewport.SetPositionFirst();
|
||||
|
||||
_userInterfaceManager.StateRoot.AddChild(_gameHud.RootControl);
|
||||
_chatManager.SetChatBox(_gameChat);
|
||||
_voteManager.SetPopupContainer(_gameHud.VoteContainer);
|
||||
_gameChat.DefaultChatFormat = "say \"{0}\"";
|
||||
|
||||
_inputManager.SetInputCommand(ContentKeyFunctions.FocusChat,
|
||||
InputCmdHandler.FromDelegate(_ => FocusChat(_gameChat)));
|
||||
|
||||
_inputManager.SetInputCommand(ContentKeyFunctions.FocusOOC,
|
||||
InputCmdHandler.FromDelegate(_ => FocusChannel(_gameChat, ChatChannel.OOC)));
|
||||
|
||||
_inputManager.SetInputCommand(ContentKeyFunctions.FocusLocalChat,
|
||||
InputCmdHandler.FromDelegate(_ => FocusChannel(_gameChat, ChatChannel.Local)));
|
||||
|
||||
_inputManager.SetInputCommand(ContentKeyFunctions.FocusRadio,
|
||||
InputCmdHandler.FromDelegate(_ => FocusChannel(_gameChat, ChatChannel.Radio)));
|
||||
|
||||
_inputManager.SetInputCommand(ContentKeyFunctions.FocusAdminChat,
|
||||
InputCmdHandler.FromDelegate(_ => FocusChannel(_gameChat, ChatChannel.AdminChat)));
|
||||
|
||||
_inputManager.SetInputCommand(ContentKeyFunctions.CycleChatChannelForward,
|
||||
InputCmdHandler.FromDelegate(_ => _gameChat.CycleChatChannel(true)));
|
||||
|
||||
_inputManager.SetInputCommand(ContentKeyFunctions.CycleChatChannelBackward,
|
||||
InputCmdHandler.FromDelegate(_ => _gameChat.CycleChatChannel(false)));
|
||||
|
||||
SetupPresenters();
|
||||
|
||||
_eyeManager.MainViewport = Viewport.Viewport;
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
DisposePresenters();
|
||||
|
||||
base.Shutdown();
|
||||
|
||||
_gameChat?.Dispose();
|
||||
Viewport.Dispose();
|
||||
_gameHud.RootControl.Orphan();
|
||||
// Clear viewport to some fallback, whatever.
|
||||
_eyeManager.MainViewport = _userInterfaceManager.MainViewport;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// All UI Presenters should be constructed in here.
|
||||
/// </summary>
|
||||
private void SetupPresenters()
|
||||
{
|
||||
_constructionMenu = new ConstructionMenuPresenter(_gameHud);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// All UI Presenters should be disposed in here.
|
||||
/// </summary>
|
||||
private void DisposePresenters()
|
||||
{
|
||||
_constructionMenu?.Dispose();
|
||||
}
|
||||
|
||||
internal static void FocusChat(ChatBox chat)
|
||||
{
|
||||
if (chat.UserInterfaceManager.KeyboardFocused != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
chat.Input.IgnoreNext = true;
|
||||
chat.Input.GrabKeyboardFocus();
|
||||
}
|
||||
internal static void FocusChannel(ChatBox chat, ChatChannel channel)
|
||||
{
|
||||
if (chat.UserInterfaceManager.KeyboardFocused != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
chat.SelectChannel(channel);
|
||||
chat.Input.IgnoreNext = true;
|
||||
chat.Input.GrabKeyboardFocus();
|
||||
}
|
||||
|
||||
public override void FrameUpdate(FrameEventArgs e)
|
||||
{
|
||||
base.FrameUpdate(e);
|
||||
|
||||
Viewport.Viewport.Eye = _eyeManager.CurrentEye;
|
||||
}
|
||||
|
||||
protected override void OnKeyBindStateChanged(ViewportBoundKeyEventArgs args)
|
||||
{
|
||||
if (args.Viewport == null)
|
||||
base.OnKeyBindStateChanged(new ViewportBoundKeyEventArgs(args.KeyEventArgs, Viewport.Viewport));
|
||||
else
|
||||
base.OnKeyBindStateChanged(args);
|
||||
}
|
||||
}
|
||||
}
|
||||
251
Content.Client/Viewport/GameScreenBase.cs
Normal file
251
Content.Client/Viewport/GameScreenBase.cs
Normal file
@@ -0,0 +1,251 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using Content.Client.Clickable;
|
||||
using Content.Client.Interactable;
|
||||
using Content.Client.Interactable.Components;
|
||||
using Content.Client.State;
|
||||
using Content.Shared;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.Input;
|
||||
using Robust.Client.Player;
|
||||
using Robust.Client.State;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Client.UserInterface.CustomControls;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Input;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Client.Viewport
|
||||
{
|
||||
// OH GOD.
|
||||
// Ok actually it's fine.
|
||||
// Instantiated dynamically through the StateManager, Dependencies will be resolved.
|
||||
public partial class GameScreenBase : Robust.Client.State.State, IEntityEventSubscriber
|
||||
{
|
||||
[Dependency] protected readonly IClientEntityManager EntityManager = default!;
|
||||
[Dependency] protected readonly IInputManager InputManager = default!;
|
||||
[Dependency] protected readonly IPlayerManager PlayerManager = default!;
|
||||
[Dependency] protected readonly IEntitySystemManager EntitySystemManager = default!;
|
||||
[Dependency] protected readonly IGameTiming Timing = default!;
|
||||
[Dependency] protected readonly IMapManager MapManager = default!;
|
||||
[Dependency] protected readonly IUserInterfaceManager UserInterfaceManager = default!;
|
||||
[Dependency] protected readonly IConfigurationManager ConfigurationManager = default!;
|
||||
[Dependency] private readonly IEntityManager _entityManager = default!;
|
||||
|
||||
private IEventBus _eventBus => _entityManager.EventBus;
|
||||
|
||||
private IEntity? _lastHoveredEntity;
|
||||
|
||||
private bool _outlineEnabled = true;
|
||||
|
||||
public override void Startup()
|
||||
{
|
||||
InputManager.KeyBindStateChanged += OnKeyBindStateChanged;
|
||||
_eventBus.SubscribeEvent<OutlineToggleMessage>(EventSource.Local, this, HandleOutlineToggle);
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
InputManager.KeyBindStateChanged -= OnKeyBindStateChanged;
|
||||
_eventBus.UnsubscribeEvent<OutlineToggleMessage>(EventSource.Local, this);
|
||||
}
|
||||
|
||||
private void HandleOutlineToggle(OutlineToggleMessage message)
|
||||
{
|
||||
_outlineEnabled = message.Enabled;
|
||||
}
|
||||
|
||||
public override void FrameUpdate(FrameEventArgs e)
|
||||
{
|
||||
base.FrameUpdate(e);
|
||||
|
||||
// If there is no local player, there is no session, and therefore nothing to do here.
|
||||
var localPlayer = PlayerManager.LocalPlayer;
|
||||
if (localPlayer == null)
|
||||
return;
|
||||
|
||||
IEntity? entityToClick = null;
|
||||
var renderScale = 1;
|
||||
if (UserInterfaceManager.CurrentlyHovered is IViewportControl vp)
|
||||
{
|
||||
var mousePosWorld = vp.ScreenToMap(InputManager.MouseScreenPosition.Position);
|
||||
entityToClick = GetEntityUnderPosition(mousePosWorld);
|
||||
|
||||
if (vp is ScalingViewport svp)
|
||||
{
|
||||
renderScale = svp.CurrentRenderScale;
|
||||
}
|
||||
}
|
||||
|
||||
var inRange = false;
|
||||
if (localPlayer.ControlledEntity != null && entityToClick != null)
|
||||
{
|
||||
inRange = localPlayer.InRangeUnobstructed(entityToClick, ignoreInsideBlocker: true);
|
||||
}
|
||||
|
||||
InteractionOutlineComponent? outline;
|
||||
if(!_outlineEnabled || !ConfigurationManager.GetCVar(CCVars.OutlineEnabled))
|
||||
{
|
||||
if(entityToClick != null && entityToClick.TryGetComponent(out outline))
|
||||
{
|
||||
outline.OnMouseLeave(); //Prevent outline remains from persisting post command.
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (entityToClick == _lastHoveredEntity)
|
||||
{
|
||||
if (entityToClick != null && entityToClick.TryGetComponent(out outline))
|
||||
{
|
||||
outline.UpdateInRange(inRange, renderScale);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (_lastHoveredEntity != null && !_lastHoveredEntity.Deleted &&
|
||||
_lastHoveredEntity.TryGetComponent(out outline))
|
||||
{
|
||||
outline.OnMouseLeave();
|
||||
}
|
||||
|
||||
_lastHoveredEntity = entityToClick;
|
||||
|
||||
if (_lastHoveredEntity != null && _lastHoveredEntity.TryGetComponent(out outline))
|
||||
{
|
||||
outline.OnMouseEnter(inRange, renderScale);
|
||||
}
|
||||
}
|
||||
|
||||
public IEntity? GetEntityUnderPosition(MapCoordinates coordinates)
|
||||
{
|
||||
var entitiesUnderPosition = GetEntitiesUnderPosition(coordinates);
|
||||
return entitiesUnderPosition.Count > 0 ? entitiesUnderPosition[0] : null;
|
||||
}
|
||||
|
||||
public IList<IEntity> GetEntitiesUnderPosition(EntityCoordinates coordinates)
|
||||
{
|
||||
return GetEntitiesUnderPosition(coordinates.ToMap(EntityManager));
|
||||
}
|
||||
|
||||
public IList<IEntity> GetEntitiesUnderPosition(MapCoordinates coordinates)
|
||||
{
|
||||
// Find all the entities intersecting our click
|
||||
var entities = IoCManager.Resolve<IEntityLookup>().GetEntitiesIntersecting(coordinates.MapId,
|
||||
Box2.CenteredAround(coordinates.Position, (1, 1)));
|
||||
|
||||
// Check the entities against whether or not we can click them
|
||||
var foundEntities = new List<(IEntity clicked, int drawDepth, uint renderOrder)>();
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
if (entity.TryGetComponent<ClickableComponent>(out var component)
|
||||
&& entity.Transform.IsMapTransform
|
||||
&& component.CheckClick(coordinates.Position, out var drawDepthClicked, out var renderOrder))
|
||||
{
|
||||
foundEntities.Add((entity, drawDepthClicked, renderOrder));
|
||||
}
|
||||
}
|
||||
|
||||
if (foundEntities.Count == 0)
|
||||
return new List<IEntity>();
|
||||
|
||||
foundEntities.Sort(new ClickableEntityComparer());
|
||||
// 0 is the top element.
|
||||
foundEntities.Reverse();
|
||||
return foundEntities.Select(a => a.clicked).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all entities intersecting the given position.
|
||||
///
|
||||
/// Static alternative to GetEntitiesUnderPosition to cut out
|
||||
/// some of the boilerplate needed to get state manager and check the current state.
|
||||
/// </summary>
|
||||
/// <param name="stateManager">state manager to use to get the current game screen</param>
|
||||
/// <param name="coordinates">coordinates to check</param>
|
||||
/// <returns>the entities under the position, empty list if none found</returns>
|
||||
public static IList<IEntity> GetEntitiesUnderPosition(IStateManager stateManager, EntityCoordinates coordinates)
|
||||
{
|
||||
if (stateManager.CurrentState is GameScreenBase gameScreenBase)
|
||||
{
|
||||
return gameScreenBase.GetEntitiesUnderPosition(coordinates);
|
||||
}
|
||||
|
||||
return ImmutableList<IEntity>.Empty;
|
||||
}
|
||||
|
||||
internal class ClickableEntityComparer : IComparer<(IEntity clicked, int depth, uint renderOrder)>
|
||||
{
|
||||
public int Compare((IEntity clicked, int depth, uint renderOrder) x,
|
||||
(IEntity clicked, int depth, uint renderOrder) y)
|
||||
{
|
||||
var val = x.depth.CompareTo(y.depth);
|
||||
if (val != 0)
|
||||
{
|
||||
return val;
|
||||
}
|
||||
|
||||
// Turning this off it can make picking stuff out of lockers and such up a bit annoying.
|
||||
/*
|
||||
val = x.renderOrder.CompareTo(y.renderOrder);
|
||||
if (val != 0)
|
||||
{
|
||||
return val;
|
||||
}
|
||||
*/
|
||||
|
||||
var transX = x.clicked.Transform;
|
||||
var transY = y.clicked.Transform;
|
||||
val = transX.Coordinates.Y.CompareTo(transY.Coordinates.Y);
|
||||
if (val != 0)
|
||||
{
|
||||
return val;
|
||||
}
|
||||
|
||||
return x.clicked.Uid.CompareTo(y.clicked.Uid);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a state change event from outside the simulation to inside the simulation.
|
||||
/// </summary>
|
||||
/// <param name="args">Event data values for a bound key state change.</param>
|
||||
protected virtual void OnKeyBindStateChanged(ViewportBoundKeyEventArgs args)
|
||||
{
|
||||
// If there is no InputSystem, then there is nothing to forward to, and nothing to do here.
|
||||
if(!EntitySystemManager.TryGetEntitySystem(out InputSystem? inputSys))
|
||||
return;
|
||||
|
||||
var kArgs = args.KeyEventArgs;
|
||||
var func = kArgs.Function;
|
||||
var funcId = InputManager.NetworkBindMap.KeyFunctionID(func);
|
||||
|
||||
EntityCoordinates coordinates = default;
|
||||
EntityUid entityToClick = default;
|
||||
if (args.Viewport is IViewportControl vp)
|
||||
{
|
||||
var mousePosWorld = vp.ScreenToMap(kArgs.PointerLocation.Position);
|
||||
entityToClick = GetEntityUnderPosition(mousePosWorld)?.Uid ?? EntityUid.Invalid;
|
||||
|
||||
coordinates = MapManager.TryFindGridAt(mousePosWorld, out var grid) ? grid.MapToGrid(mousePosWorld) :
|
||||
EntityCoordinates.FromMap(EntityManager, MapManager, mousePosWorld);
|
||||
}
|
||||
|
||||
var message = new FullInputCmdMessage(Timing.CurTick, Timing.TickFraction, funcId, kArgs.State,
|
||||
coordinates , kArgs.PointerLocation,
|
||||
entityToClick);
|
||||
|
||||
// client side command handlers will always be sent the local player session.
|
||||
var session = PlayerManager.LocalPlayer?.Session;
|
||||
if (inputSys.HandleInputCommand(session, func, message))
|
||||
{
|
||||
kArgs.Handle();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
15
Content.Client/Viewport/IMainViewport.cs
Normal file
15
Content.Client/Viewport/IMainViewport.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Content.Client.HUD.UI;
|
||||
|
||||
namespace Content.Client.Viewport
|
||||
{
|
||||
/// <summary>
|
||||
/// Client state that has a main viewport.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Used for taking no-UI screenshots (including things like flash overlay).
|
||||
/// </remarks>
|
||||
public interface IMainViewportState
|
||||
{
|
||||
public MainViewport Viewport { get; }
|
||||
}
|
||||
}
|
||||
340
Content.Client/Viewport/ScalingViewport.cs
Normal file
340
Content.Client/Viewport/ScalingViewport.cs
Normal file
@@ -0,0 +1,340 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.Input;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Client.UserInterface.CustomControls;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.Utility;
|
||||
using Robust.Shared.ViewVariables;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
|
||||
namespace Content.Client.Viewport
|
||||
{
|
||||
/// <summary>
|
||||
/// Viewport control that has a fixed viewport size and scales it appropriately.
|
||||
/// </summary>
|
||||
public sealed class ScalingViewport : Control, IViewportControl
|
||||
{
|
||||
[Dependency] private readonly IClyde _clyde = default!;
|
||||
[Dependency] private readonly IInputManager _inputManager = default!;
|
||||
|
||||
// Internal viewport creation is deferred.
|
||||
private IClydeViewport? _viewport;
|
||||
private IEye? _eye;
|
||||
private Vector2i _viewportSize;
|
||||
private int _curRenderScale;
|
||||
private ScalingViewportStretchMode _stretchMode = ScalingViewportStretchMode.Bilinear;
|
||||
private ScalingViewportRenderScaleMode _renderScaleMode = ScalingViewportRenderScaleMode.Fixed;
|
||||
private int _fixedRenderScale = 1;
|
||||
|
||||
private readonly List<CopyPixelsDelegate<Rgba32>> _queuedScreenshots = new();
|
||||
|
||||
public int CurrentRenderScale => _curRenderScale;
|
||||
|
||||
/// <summary>
|
||||
/// The eye to render.
|
||||
/// </summary>
|
||||
public IEye? Eye
|
||||
{
|
||||
get => _eye;
|
||||
set
|
||||
{
|
||||
_eye = value;
|
||||
|
||||
if (_viewport != null)
|
||||
_viewport.Eye = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The size, in unscaled pixels, of the internal viewport.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The actual viewport may have render scaling applied based on parameters.
|
||||
/// </remarks>
|
||||
public Vector2i ViewportSize
|
||||
{
|
||||
get => _viewportSize;
|
||||
set
|
||||
{
|
||||
_viewportSize = value;
|
||||
InvalidateViewport();
|
||||
}
|
||||
}
|
||||
|
||||
// Do not need to InvalidateViewport() since it doesn't affect viewport creation.
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)] public Vector2i? FixedStretchSize { get; set; }
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public ScalingViewportStretchMode StretchMode
|
||||
{
|
||||
get => _stretchMode;
|
||||
set
|
||||
{
|
||||
_stretchMode = value;
|
||||
InvalidateViewport();
|
||||
}
|
||||
}
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public ScalingViewportRenderScaleMode RenderScaleMode
|
||||
{
|
||||
get => _renderScaleMode;
|
||||
set
|
||||
{
|
||||
_renderScaleMode = value;
|
||||
InvalidateViewport();
|
||||
}
|
||||
}
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public int FixedRenderScale
|
||||
{
|
||||
get => _fixedRenderScale;
|
||||
set
|
||||
{
|
||||
_fixedRenderScale = value;
|
||||
InvalidateViewport();
|
||||
}
|
||||
}
|
||||
|
||||
public ScalingViewport()
|
||||
{
|
||||
IoCManager.InjectDependencies(this);
|
||||
RectClipContent = true;
|
||||
}
|
||||
|
||||
protected override void KeyBindDown(GUIBoundKeyEventArgs args)
|
||||
{
|
||||
base.KeyBindDown(args);
|
||||
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
_inputManager.ViewportKeyEvent(this, args);
|
||||
}
|
||||
|
||||
protected override void KeyBindUp(GUIBoundKeyEventArgs args)
|
||||
{
|
||||
base.KeyBindUp(args);
|
||||
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
_inputManager.ViewportKeyEvent(this, args);
|
||||
}
|
||||
|
||||
|
||||
protected override void FrameUpdate(FrameEventArgs args)
|
||||
{
|
||||
EnsureViewportCreated();
|
||||
}
|
||||
|
||||
protected override void Draw(DrawingHandleScreen handle)
|
||||
{
|
||||
DebugTools.AssertNotNull(_viewport);
|
||||
|
||||
_viewport!.Render();
|
||||
|
||||
if (_queuedScreenshots.Count != 0)
|
||||
{
|
||||
var callbacks = _queuedScreenshots.ToArray();
|
||||
|
||||
_viewport.RenderTarget.CopyPixelsToMemory<Rgba32>(image =>
|
||||
{
|
||||
foreach (var callback in callbacks)
|
||||
{
|
||||
callback(image);
|
||||
}
|
||||
});
|
||||
|
||||
_queuedScreenshots.Clear();
|
||||
}
|
||||
|
||||
var drawBox = GetDrawBox();
|
||||
var drawBoxGlobal = drawBox.Translated(GlobalPixelPosition);
|
||||
_viewport.RenderScreenOverlaysBelow(handle, this, drawBoxGlobal);
|
||||
handle.DrawTextureRect(_viewport.RenderTarget.Texture, drawBox);
|
||||
_viewport.RenderScreenOverlaysAbove(handle, this, drawBoxGlobal);
|
||||
}
|
||||
|
||||
public void Screenshot(CopyPixelsDelegate<Rgba32> callback)
|
||||
{
|
||||
_queuedScreenshots.Add(callback);
|
||||
}
|
||||
|
||||
// Draw box in pixel coords to draw the viewport at.
|
||||
private UIBox2i GetDrawBox()
|
||||
{
|
||||
DebugTools.AssertNotNull(_viewport);
|
||||
|
||||
var vpSize = _viewport!.Size;
|
||||
var ourSize = (Vector2) PixelSize;
|
||||
|
||||
if (FixedStretchSize == null)
|
||||
{
|
||||
var (ratioX, ratioY) = ourSize / vpSize;
|
||||
var ratio = Math.Min(ratioX, ratioY);
|
||||
|
||||
var size = vpSize * ratio;
|
||||
// Size
|
||||
var pos = (ourSize - size) / 2;
|
||||
|
||||
return (UIBox2i) UIBox2.FromDimensions(pos, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Center only, no scaling.
|
||||
var pos = (ourSize - FixedStretchSize.Value) / 2;
|
||||
return (UIBox2i) UIBox2.FromDimensions(pos, FixedStretchSize.Value);
|
||||
}
|
||||
}
|
||||
|
||||
private void RegenerateViewport()
|
||||
{
|
||||
DebugTools.AssertNull(_viewport);
|
||||
|
||||
var vpSizeBase = ViewportSize;
|
||||
var ourSize = PixelSize;
|
||||
var (ratioX, ratioY) = ourSize / (Vector2) vpSizeBase;
|
||||
var ratio = Math.Min(ratioX, ratioY);
|
||||
var renderScale = 1;
|
||||
switch (_renderScaleMode)
|
||||
{
|
||||
case ScalingViewportRenderScaleMode.CeilInt:
|
||||
renderScale = (int) Math.Ceiling(ratio);
|
||||
break;
|
||||
case ScalingViewportRenderScaleMode.FloorInt:
|
||||
renderScale = (int) Math.Floor(ratio);
|
||||
break;
|
||||
case ScalingViewportRenderScaleMode.Fixed:
|
||||
renderScale = _fixedRenderScale;
|
||||
break;
|
||||
}
|
||||
|
||||
// Always has to be at least one to avoid passing 0,0 to the viewport constructor
|
||||
renderScale = Math.Max(1, renderScale);
|
||||
|
||||
_curRenderScale = renderScale;
|
||||
|
||||
_viewport = _clyde.CreateViewport(
|
||||
ViewportSize * renderScale,
|
||||
new TextureSampleParameters
|
||||
{
|
||||
Filter = StretchMode == ScalingViewportStretchMode.Bilinear,
|
||||
});
|
||||
|
||||
_viewport.RenderScale = (renderScale, renderScale);
|
||||
|
||||
_viewport.Eye = _eye;
|
||||
}
|
||||
|
||||
protected override void Resized()
|
||||
{
|
||||
base.Resized();
|
||||
|
||||
InvalidateViewport();
|
||||
}
|
||||
|
||||
private void InvalidateViewport()
|
||||
{
|
||||
_viewport?.Dispose();
|
||||
_viewport = null;
|
||||
}
|
||||
|
||||
public MapCoordinates ScreenToMap(Vector2 coords)
|
||||
{
|
||||
if (_eye == null)
|
||||
return default;
|
||||
|
||||
EnsureViewportCreated();
|
||||
|
||||
var matrix = Matrix3.Invert(LocalToScreenMatrix());
|
||||
|
||||
return _viewport!.LocalToWorld(matrix.Transform(coords));
|
||||
}
|
||||
|
||||
public Vector2 WorldToScreen(Vector2 map)
|
||||
{
|
||||
if (_eye == null)
|
||||
return default;
|
||||
|
||||
EnsureViewportCreated();
|
||||
|
||||
var vpLocal = _viewport!.WorldToLocal(map);
|
||||
|
||||
var matrix = LocalToScreenMatrix();
|
||||
|
||||
return matrix.Transform(vpLocal);
|
||||
}
|
||||
|
||||
private Matrix3 LocalToScreenMatrix()
|
||||
{
|
||||
DebugTools.AssertNotNull(_viewport);
|
||||
|
||||
var drawBox = GetDrawBox();
|
||||
var scaleFactor = drawBox.Size / (Vector2) _viewport!.Size;
|
||||
|
||||
if (scaleFactor == (0, 0))
|
||||
// Basically a nonsense scenario, at least make sure to return something that can be inverted.
|
||||
return Matrix3.Identity;
|
||||
|
||||
var scale = Matrix3.CreateScale(scaleFactor);
|
||||
var translate = Matrix3.CreateTranslation(GlobalPixelPosition + drawBox.TopLeft);
|
||||
|
||||
return scale * translate;
|
||||
}
|
||||
|
||||
private void EnsureViewportCreated()
|
||||
{
|
||||
if (_viewport == null)
|
||||
{
|
||||
RegenerateViewport();
|
||||
}
|
||||
|
||||
DebugTools.AssertNotNull(_viewport);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines how the viewport is stretched if it does not match the size of the control perfectly.
|
||||
/// </summary>
|
||||
public enum ScalingViewportStretchMode
|
||||
{
|
||||
/// <summary>
|
||||
/// Bilinear sampling is used.
|
||||
/// </summary>
|
||||
Bilinear = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Nearest neighbor sampling is used.
|
||||
/// </summary>
|
||||
Nearest,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines how the base render scale of the viewport is selected.
|
||||
/// </summary>
|
||||
public enum ScalingViewportRenderScaleMode
|
||||
{
|
||||
/// <summary>
|
||||
/// <see cref="ScalingViewport.FixedRenderScale"/> is used.
|
||||
/// </summary>
|
||||
Fixed = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Floor to the closest integer scale possible.
|
||||
/// </summary>
|
||||
FloorInt,
|
||||
|
||||
/// <summary>
|
||||
/// Ceiling to the closest integer scale possible.
|
||||
/// </summary>
|
||||
CeilInt
|
||||
}
|
||||
}
|
||||
15
Content.Client/Viewport/ViewportExt.cs
Normal file
15
Content.Client/Viewport/ViewportExt.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Robust.Client.UserInterface.CustomControls;
|
||||
|
||||
namespace Content.Client.Viewport
|
||||
{
|
||||
public static class ViewportExt
|
||||
{
|
||||
public static int GetRenderScale(this IViewportControl viewport)
|
||||
{
|
||||
if (viewport is ScalingViewport svp)
|
||||
return svp.CurrentRenderScale;
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
43
Content.Client/Viewport/ViewportManager.cs
Normal file
43
Content.Client/Viewport/ViewportManager.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Client.HUD.UI;
|
||||
using Content.Shared;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.IoC;
|
||||
|
||||
namespace Content.Client.Viewport
|
||||
{
|
||||
/// <summary>
|
||||
/// Event proxy for <see cref="MainViewport"/> to listen to config events.
|
||||
/// </summary>
|
||||
// ReSharper disable once ClassNeverInstantiated.Global
|
||||
public sealed class ViewportManager
|
||||
{
|
||||
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
||||
|
||||
private readonly List<MainViewport> _viewports = new();
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_cfg.OnValueChanged(CCVars.ViewportStretch, _ => UpdateCfg());
|
||||
_cfg.OnValueChanged(CCVars.ViewportSnapToleranceClip, _ => UpdateCfg());
|
||||
_cfg.OnValueChanged(CCVars.ViewportSnapToleranceMargin, _ => UpdateCfg());
|
||||
_cfg.OnValueChanged(CCVars.ViewportScaleRender, _ => UpdateCfg());
|
||||
_cfg.OnValueChanged(CCVars.ViewportFixedScaleFactor, _ => UpdateCfg());
|
||||
}
|
||||
|
||||
private void UpdateCfg()
|
||||
{
|
||||
_viewports.ForEach(v => v.UpdateCfg());
|
||||
}
|
||||
|
||||
public void AddViewport(MainViewport vp)
|
||||
{
|
||||
_viewports.Add(vp);
|
||||
}
|
||||
|
||||
public void RemoveViewport(MainViewport vp)
|
||||
{
|
||||
_viewports.Remove(vp);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user