diff --git a/src/MultiplayerMod/Game/UI/Tools/Events/InterfaceToolEvents.cs b/src/MultiplayerMod/Game/UI/Tools/Events/InterfaceToolEvents.cs index 18472dd0..a8311137 100644 --- a/src/MultiplayerMod/Game/UI/Tools/Events/InterfaceToolEvents.cs +++ b/src/MultiplayerMod/Game/UI/Tools/Events/InterfaceToolEvents.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using HarmonyLib; using MultiplayerMod.ModRuntime.Context; using UnityEngine; @@ -8,12 +9,28 @@ namespace MultiplayerMod.Game.UI.Tools.Events; [HarmonyPatch(typeof(InterfaceTool))] public static class InterfaceToolEvents { - public static event Action? MouseMoved; + public static event Action? MouseMoved; // ReSharper disable once InconsistentNaming, UnusedMember.Local [HarmonyPrefix] [HarmonyPatch(nameof(InterfaceTool.OnMouseMove))] [RequireExecutionLevel(ExecutionLevel.Game)] - private static void OnMouseMove(Vector3 cursor_pos) => MouseMoved?.Invoke(new Vector2(cursor_pos.x, cursor_pos.y)); + private static void OnMouseMove(Vector3 cursor_pos) { + var kScreens = KScreenManager.Instance.screenStack.Where(screen => screen.mouseOver).ToList(); + MouseMoved?.Invoke( + new Vector2(cursor_pos.x, cursor_pos.y), + GetScreenName(kScreens.FirstOrDefault()) + ); + } + private static string? GetScreenName(KScreen? screen) { + switch (screen) { + case TableScreen tableScreen: + return tableScreen.title.ToLower(); + case RootMenu rootMenu: + return rootMenu.detailsScreen.displayName; + default: + return screen?.displayName; + } + } } diff --git a/src/MultiplayerMod/Multiplayer/Commands/Player/UpdatePlayerCursorPosition.cs b/src/MultiplayerMod/Multiplayer/Commands/Player/UpdatePlayerCursorPosition.cs index e55bdbb3..59f6617c 100644 --- a/src/MultiplayerMod/Multiplayer/Commands/Player/UpdatePlayerCursorPosition.cs +++ b/src/MultiplayerMod/Multiplayer/Commands/Player/UpdatePlayerCursorPosition.cs @@ -10,15 +10,17 @@ public class UpdatePlayerCursorPosition : MultiplayerCommand { private PlayerIdentity playerId; private Vector2 position; + private string? screenName; - public UpdatePlayerCursorPosition(PlayerIdentity playerId, Vector2 position) { + public UpdatePlayerCursorPosition(PlayerIdentity playerId, Vector2 position, string? screenName) { this.playerId = playerId; this.position = position; + this.screenName = screenName; } public override void Execute(MultiplayerCommandContext context) { var player = context.Multiplayer.Players[playerId]; - context.EventDispatcher.Dispatch(new PlayerCursorPositionUpdatedEvent(player, position)); + context.EventDispatcher.Dispatch(new PlayerCursorPositionUpdatedEvent(player, position, screenName)); } } diff --git a/src/MultiplayerMod/Multiplayer/Components/CursorComponent.cs b/src/MultiplayerMod/Multiplayer/Components/CursorComponent.cs index 9a85b349..28f2edc4 100644 --- a/src/MultiplayerMod/Multiplayer/Components/CursorComponent.cs +++ b/src/MultiplayerMod/Multiplayer/Components/CursorComponent.cs @@ -24,7 +24,9 @@ public Vector3 Position { } } - public string CursorText { get; set; } = null!; + public string PlayerName { get; set; } = null!; + + public string? ScreenName { get; set; } public void Trace(Vector2 position) { previous = current; @@ -84,7 +86,7 @@ private void Update() { return; gameObject.transform.position = camera.WorldToScreenPoint(GetCurrentPosition()); - textComponent.text = CursorText; + textComponent.text = PlayerName + (ScreenName == null ? "" : $" ({ScreenName})"); } private Vector2 GetCurrentPosition() { diff --git a/src/MultiplayerMod/Multiplayer/Components/CursorManager.cs b/src/MultiplayerMod/Multiplayer/Components/CursorManager.cs index ed4b7260..b79e2123 100644 --- a/src/MultiplayerMod/Multiplayer/Components/CursorManager.cs +++ b/src/MultiplayerMod/Multiplayer/Components/CursorManager.cs @@ -28,14 +28,16 @@ private void OnPlayerLeft(PlayerLeftEvent @event) { private void OnDisable() => subscriptions.Cancel(); private void OnCursorUpdated(PlayerCursorPositionUpdatedEvent updatedEvent) { - if (!cursors.TryGetValue(updatedEvent.Player, out var cursor)) { - cursor = gameObject.AddComponent(); - cursor.Position = updatedEvent.Position; - cursor.CursorText = updatedEvent.Player.Profile.PlayerName; - cursors[updatedEvent.Player] = cursor; + if (!cursors.TryGetValue(updatedEvent.Player, out var cursorComponent)) { + cursorComponent = gameObject.AddComponent(); + cursorComponent.Position = updatedEvent.Position; + cursorComponent.PlayerName = updatedEvent.Player.Profile.PlayerName; + cursorComponent.ScreenName = updatedEvent.ScreenName; + cursors[updatedEvent.Player] = cursorComponent; return; } - cursor.Trace(updatedEvent.Position); + cursorComponent.ScreenName = updatedEvent.ScreenName; + cursorComponent.Trace(updatedEvent.Position); } } diff --git a/src/MultiplayerMod/Multiplayer/CoreOperations/Binders/GameEventsBinder.cs b/src/MultiplayerMod/Multiplayer/CoreOperations/Binders/GameEventsBinder.cs index f59aaf25..a2a2d6df 100644 --- a/src/MultiplayerMod/Multiplayer/CoreOperations/Binders/GameEventsBinder.cs +++ b/src/MultiplayerMod/Multiplayer/CoreOperations/Binders/GameEventsBinder.cs @@ -65,8 +65,8 @@ private void BindSpeedControl() { private void BindMouse() { // TODO: Cursor update may be ignored if MouseMoved isn't triggered after the rate period. // TODO: Will be changed later (probably with current / last sent positions check). - InterfaceToolEvents.MouseMoved += position => throttle10Hz.Run( - () => client.Send(new UpdatePlayerCursorPosition(multiplayer.Players.Current.Id, position)) + InterfaceToolEvents.MouseMoved += (position, screenName) => throttle10Hz.Run( + () => client.Send(new UpdatePlayerCursorPosition(multiplayer.Players.Current.Id, position, screenName)) ); } diff --git a/src/MultiplayerMod/Multiplayer/Players/Events/PlayerCursorPositionUpdatedEvent.cs b/src/MultiplayerMod/Multiplayer/Players/Events/PlayerCursorPositionUpdatedEvent.cs index 76969e04..4f4d203f 100644 --- a/src/MultiplayerMod/Multiplayer/Players/Events/PlayerCursorPositionUpdatedEvent.cs +++ b/src/MultiplayerMod/Multiplayer/Players/Events/PlayerCursorPositionUpdatedEvent.cs @@ -3,4 +3,5 @@ namespace MultiplayerMod.Multiplayer.Players.Events; -public record PlayerCursorPositionUpdatedEvent(MultiplayerPlayer Player, Vector2 Position) : IDispatchableEvent; +public record PlayerCursorPositionUpdatedEvent + (MultiplayerPlayer Player, Vector2 Position, string? ScreenName) : IDispatchableEvent;