Small UI refactor pieces (#11026)
* ActionType rename Name to DisplayName * Gameplay State rename+move
This commit is contained in:
159
Content.Client/Gameplay/GameplayState.cs
Normal file
159
Content.Client/Gameplay/GameplayState.cs
Normal file
@@ -0,0 +1,159 @@
|
||||
using Content.Client.Alerts.UI;
|
||||
using Content.Client.Chat;
|
||||
using Content.Client.Chat.Managers;
|
||||
using Content.Client.Chat.UI;
|
||||
using Content.Client.Construction.UI;
|
||||
using Content.Client.Hands;
|
||||
using Content.Client.HUD;
|
||||
using Content.Client.HUD.UI;
|
||||
using Content.Client.Viewport;
|
||||
using Content.Client.Voting;
|
||||
using Content.Shared.Chat;
|
||||
using Content.Shared.CCVar;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.Input;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Client.UserInterface.CustomControls;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Client.Gameplay
|
||||
{
|
||||
public sealed class GameplayState : GameplayStateBase, 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 IEyeManager _eyeManager = default!;
|
||||
[Dependency] private readonly IOverlayManager _overlayManager = default!;
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
|
||||
|
||||
[ViewVariables] private ChatBox? _gameChat;
|
||||
private ConstructionMenuPresenter? _constructionMenu;
|
||||
private AlertsFramePresenter? _alertsFramePresenter;
|
||||
|
||||
private FpsCounter _fpsCounter = default!;
|
||||
|
||||
public MainViewport Viewport { get; private set; } = default!;
|
||||
|
||||
protected override void Startup()
|
||||
{
|
||||
base.Startup();
|
||||
|
||||
_gameChat = new HudChatBox {PreferredChannel = ChatSelectChannel.Local};
|
||||
|
||||
UserInterfaceManager.StateRoot.AddChild(_gameChat);
|
||||
LayoutContainer.SetAnchorAndMarginPreset(_gameChat, LayoutContainer.LayoutPreset.TopRight, margin: 10);
|
||||
LayoutContainer.SetAnchorAndMarginPreset(_gameChat, LayoutContainer.LayoutPreset.TopRight, margin: 10);
|
||||
LayoutContainer.SetMarginLeft(_gameChat, -475);
|
||||
LayoutContainer.SetMarginBottom(_gameChat, HudChatBox.InitialChatBottom);
|
||||
|
||||
_chatManager.ChatBoxOnResized(new ChatResizedEventArgs(HudChatBox.InitialChatBottom));
|
||||
|
||||
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);
|
||||
|
||||
ChatInput.SetupChatInputHandlers(_inputManager, _gameChat);
|
||||
|
||||
SetupPresenters();
|
||||
|
||||
_eyeManager.MainViewport = Viewport.Viewport;
|
||||
|
||||
_overlayManager.AddOverlay(new ShowHandItemOverlay());
|
||||
|
||||
_fpsCounter = new FpsCounter(_gameTiming);
|
||||
_userInterfaceManager.StateRoot.AddChild(_fpsCounter);
|
||||
_fpsCounter.Visible = _configurationManager.GetCVar(CCVars.HudFpsCounterVisible);
|
||||
_configurationManager.OnValueChanged(CCVars.HudFpsCounterVisible, (show) => { _fpsCounter.Visible = show; });
|
||||
}
|
||||
|
||||
protected override void Shutdown()
|
||||
{
|
||||
_overlayManager.RemoveOverlay<ShowHandItemOverlay>();
|
||||
DisposePresenters();
|
||||
|
||||
base.Shutdown();
|
||||
|
||||
_gameChat?.Dispose();
|
||||
Viewport.Dispose();
|
||||
_gameHud.RootControl.Orphan();
|
||||
// Clear viewport to some fallback, whatever.
|
||||
_eyeManager.MainViewport = _userInterfaceManager.MainViewport;
|
||||
_fpsCounter.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// All UI Presenters should be constructed in here.
|
||||
/// </summary>
|
||||
private void SetupPresenters()
|
||||
{
|
||||
// HUD
|
||||
_alertsFramePresenter = new AlertsFramePresenter();
|
||||
|
||||
// Windows
|
||||
_constructionMenu = new ConstructionMenuPresenter(_gameHud);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// All UI Presenters should be disposed in here.
|
||||
/// </summary>
|
||||
private void DisposePresenters()
|
||||
{
|
||||
// Windows
|
||||
_constructionMenu?.Dispose();
|
||||
|
||||
// HUD
|
||||
_alertsFramePresenter?.Dispose();
|
||||
}
|
||||
|
||||
internal static void FocusChat(ChatBox chat)
|
||||
{
|
||||
if (chat.UserInterfaceManager.KeyboardFocused != null)
|
||||
return;
|
||||
|
||||
chat.Focus();
|
||||
}
|
||||
|
||||
internal static void FocusChannel(ChatBox chat, ChatSelectChannel channel)
|
||||
{
|
||||
if (chat.UserInterfaceManager.KeyboardFocused != null)
|
||||
return;
|
||||
|
||||
chat.Focus(channel);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
161
Content.Client/Gameplay/GameplayStateBase.cs
Normal file
161
Content.Client/Gameplay/GameplayStateBase.cs
Normal file
@@ -0,0 +1,161 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Client.Clickable;
|
||||
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.Containers;
|
||||
using Robust.Shared.Input;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Client.Gameplay
|
||||
{
|
||||
// OH GOD.
|
||||
// Ok actually it's fine.
|
||||
// Instantiated dynamically through the StateManager, Dependencies will be resolved.
|
||||
[Virtual]
|
||||
public class GameplayStateBase : State, IEntityEventSubscriber
|
||||
{
|
||||
[Dependency] private readonly IInputManager _inputManager = default!;
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
|
||||
[Dependency] private readonly IGameTiming _timing = default!;
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] protected readonly IUserInterfaceManager UserInterfaceManager = default!;
|
||||
[Dependency] private readonly IEntityManager _entityManager = default!;
|
||||
|
||||
private ClickableEntityComparer _comparer = default!;
|
||||
|
||||
protected override void Startup()
|
||||
{
|
||||
_inputManager.KeyBindStateChanged += OnKeyBindStateChanged;
|
||||
_comparer = new ClickableEntityComparer(_entityManager);
|
||||
}
|
||||
|
||||
protected override void Shutdown()
|
||||
{
|
||||
_inputManager.KeyBindStateChanged -= OnKeyBindStateChanged;
|
||||
}
|
||||
|
||||
public EntityUid? GetEntityUnderPosition(MapCoordinates coordinates)
|
||||
{
|
||||
var entitiesUnderPosition = GetEntitiesUnderPosition(coordinates);
|
||||
return entitiesUnderPosition.Count > 0 ? entitiesUnderPosition[0] : null;
|
||||
}
|
||||
|
||||
public IList<EntityUid> GetEntitiesUnderPosition(EntityCoordinates coordinates)
|
||||
{
|
||||
return GetEntitiesUnderPosition(coordinates.ToMap(_entityManager));
|
||||
}
|
||||
|
||||
public IList<EntityUid> GetEntitiesUnderPosition(MapCoordinates coordinates)
|
||||
{
|
||||
// Find all the entities intersecting our click
|
||||
var entities = EntitySystem.Get<EntityLookupSystem>().GetEntitiesIntersecting(coordinates.MapId,
|
||||
Box2.CenteredAround(coordinates.Position, (1, 1)));
|
||||
|
||||
var containerSystem = _entitySystemManager.GetEntitySystem<SharedContainerSystem>();
|
||||
|
||||
// Check the entities against whether or not we can click them
|
||||
var foundEntities = new List<(EntityUid clicked, int drawDepth, uint renderOrder)>();
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
if (_entityManager.TryGetComponent<ClickableComponent?>(entity, out var component)
|
||||
&& !containerSystem.IsEntityInContainer(entity)
|
||||
&& component.CheckClick(coordinates.Position, out var drawDepthClicked, out var renderOrder))
|
||||
{
|
||||
foundEntities.Add((entity, drawDepthClicked, renderOrder));
|
||||
}
|
||||
}
|
||||
|
||||
if (foundEntities.Count == 0)
|
||||
return Array.Empty<EntityUid>();
|
||||
|
||||
foundEntities.Sort(_comparer);
|
||||
// 0 is the top element.
|
||||
foundEntities.Reverse();
|
||||
return foundEntities.Select(a => a.clicked).ToList();
|
||||
}
|
||||
|
||||
private sealed class ClickableEntityComparer : IComparer<(EntityUid clicked, int depth, uint renderOrder)>
|
||||
{
|
||||
private readonly IEntityManager _entities;
|
||||
|
||||
public ClickableEntityComparer(IEntityManager entities)
|
||||
{
|
||||
_entities = entities;
|
||||
}
|
||||
|
||||
public int Compare((EntityUid clicked, int depth, uint renderOrder) x,
|
||||
(EntityUid 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 = _entities.GetComponent<TransformComponent>(x.clicked);
|
||||
var transY = _entities.GetComponent<TransformComponent>(y.clicked);
|
||||
val = transX.Coordinates.Y.CompareTo(transY.Coordinates.Y);
|
||||
if (val != 0)
|
||||
{
|
||||
return val;
|
||||
}
|
||||
|
||||
return x.clicked.CompareTo(y.clicked);
|
||||
}
|
||||
}
|
||||
|
||||
/// <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 = null;
|
||||
if (args.Viewport is IViewportControl vp)
|
||||
{
|
||||
var mousePosWorld = vp.ScreenToMap(kArgs.PointerLocation.Position);
|
||||
entityToClick = GetEntityUnderPosition(mousePosWorld);
|
||||
|
||||
coordinates = _mapManager.TryFindGridAt(mousePosWorld, out var grid) ? grid.MapToGrid(mousePosWorld) :
|
||||
EntityCoordinates.FromMap(_mapManager, mousePosWorld);
|
||||
}
|
||||
|
||||
var message = new FullInputCmdMessage(_timing.CurTick, _timing.TickFraction, funcId, kArgs.State,
|
||||
coordinates , kArgs.PointerLocation,
|
||||
entityToClick ?? default); // TODO make entityUid nullable
|
||||
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user