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

Feature/show player screen #246

Merged
merged 3 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 28 additions & 3 deletions src/MultiplayerMod/Game/UI/Tools/Events/InterfaceToolEvents.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Linq;
using HarmonyLib;
using JetBrains.Annotations;
using MultiplayerMod.ModRuntime.Context;
using UnityEngine;

Expand All @@ -8,12 +10,35 @@ namespace MultiplayerMod.Game.UI.Tools.Events;
[HarmonyPatch(typeof(InterfaceTool))]
public static class InterfaceToolEvents {

public static event Action<Vector2>? MouseMoved;
public static event Action<MouseMovedEventArgs>? MouseMoved;

// ReSharper disable once InconsistentNaming, UnusedMember.Local
// ReSharper disable once InconsistentNaming
[UsedImplicitly]
[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();
var kScreen = kScreens.FirstOrDefault();
MouseMoved?.Invoke(
new MouseMovedEventArgs(
new Vector2(cursor_pos.x, cursor_pos.y),
GetScreenName(kScreen),
kScreen?.GetType()
)
);
}

private static string? GetScreenName(KScreen? screen) => screen switch {
TableScreen tableScreen => tableScreen.title.ToLower(),
RootMenu rootMenu => rootMenu.detailsScreen.displayName,
_ => screen?.displayName
};

[Serializable]
public record MouseMovedEventArgs(
Vector2 Position,
string? ScreenName,
Type? ScreenType
);
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
using System;
using MultiplayerMod.Game.UI.Tools.Events;
using MultiplayerMod.Multiplayer.Players;
using MultiplayerMod.Multiplayer.Players.Events;
using UnityEngine;

namespace MultiplayerMod.Multiplayer.Commands.Player;

[Serializable]
public class UpdatePlayerCursorPosition : MultiplayerCommand {

private PlayerIdentity playerId;
private Vector2 position;
private InterfaceToolEvents.MouseMovedEventArgs mouseMovedEventArgs;

public UpdatePlayerCursorPosition(PlayerIdentity playerId, Vector2 position) {
public UpdatePlayerCursorPosition(
PlayerIdentity playerId,
InterfaceToolEvents.MouseMovedEventArgs mouseMovedEventArgs
) {
this.playerId = playerId;
this.position = position;
this.mouseMovedEventArgs = mouseMovedEventArgs;
}

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, mouseMovedEventArgs));
}

}
17 changes: 14 additions & 3 deletions src/MultiplayerMod/Multiplayer/Components/CursorComponent.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
Expand All @@ -12,6 +14,7 @@ private record TimedCursor(Vector2 Position, long Time);
private TimedCursor current = null!;

private Camera camera = null!;
private Image imageComponent = null!;
private TextMeshProUGUI textComponent = null!;

private bool initialized;
Expand All @@ -24,7 +27,10 @@ public Vector3 Position {
}
}

public string CursorText { get; set; } = null!;
public string PlayerName { get; set; } = null!;

public string? ScreenName { get; set; }
public Type? ScreenType { get; set; }

public void Trace(Vector2 position) {
previous = current;
Expand Down Expand Up @@ -53,7 +59,7 @@ private void CreateCursorGameObject(Texture2D cursorTexture) {
rectTransform.sizeDelta = new Vector2(cursorTexture.width, cursorTexture.height);
rectTransform.pivot = new Vector2(0, 1); // Align to top left corner.

var imageComponent = imageGameObject.AddComponent<Image>();
imageComponent = imageGameObject.AddComponent<Image>();
imageComponent.sprite = Sprite.Create(
cursorTexture,
new Rect(0, 0, cursorTexture.width, cursorTexture.height),
Expand Down Expand Up @@ -83,8 +89,13 @@ private void Update() {
if (!initialized)
return;

var isOnTheSameScreen = KScreenManager.Instance.screenStack.FirstOrDefault(screen => screen.mouseOver)
?.GetType() == ScreenType;

gameObject.transform.position = camera.WorldToScreenPoint(GetCurrentPosition());
textComponent.text = CursorText;
textComponent.text = PlayerName + (isOnTheSameScreen ? "" : $" ({ScreenName ?? "World"})");

imageComponent.color = textComponent.color = isOnTheSameScreen ? Color.white : new Color(1, 1, 1, 0.5f);
}

private Vector2 GetCurrentPosition() {
Expand Down
16 changes: 10 additions & 6 deletions src/MultiplayerMod/Multiplayer/Components/CursorManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ 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<CursorComponent>();
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>();
cursorComponent.PlayerName = updatedEvent.Player.Profile.PlayerName;
cursorComponent.Position = updatedEvent.MouseMovedEventArgs.Position;
cursorComponent.ScreenName = updatedEvent.MouseMovedEventArgs.ScreenName;
cursorComponent.ScreenType = updatedEvent.MouseMovedEventArgs.ScreenType;
cursors[updatedEvent.Player] = cursorComponent;
return;
}
cursor.Trace(updatedEvent.Position);
cursorComponent.ScreenName = updatedEvent.MouseMovedEventArgs.ScreenName;
cursorComponent.ScreenType = updatedEvent.MouseMovedEventArgs.ScreenType;
cursorComponent.Trace(updatedEvent.MouseMovedEventArgs.Position);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<UpdatePlayerCursorPosition>(
() => client.Send(new UpdatePlayerCursorPosition(multiplayer.Players.Current.Id, position))
InterfaceToolEvents.MouseMoved += args => throttle10Hz.Run<UpdatePlayerCursorPosition>(
() => client.Send(new UpdatePlayerCursorPosition(multiplayer.Players.Current.Id, args))
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using MultiplayerMod.Core.Events;
using UnityEngine;
using MultiplayerMod.Game.UI.Tools.Events;

namespace MultiplayerMod.Multiplayer.Players.Events;

public record PlayerCursorPositionUpdatedEvent(MultiplayerPlayer Player, Vector2 Position) : IDispatchableEvent;
public record PlayerCursorPositionUpdatedEvent(
MultiplayerPlayer Player,
InterfaceToolEvents.MouseMovedEventArgs MouseMovedEventArgs
) : IDispatchableEvent;