From 9032231300e6029e53258bdac447ef44c045ad01 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Sat, 29 Jun 2024 13:33:56 +1000 Subject: [PATCH] Predict typing indicator (#29551) It greatly annoys me in the rare instance I do play. --- .../TypingIndicator/TypingIndicatorSystem.cs | 7 +-- .../TypingIndicator/TypingIndicatorSystem.cs | 57 ------------------- .../SharedTypingIndicatorSystem.cs | 56 +++++++++++++++++- 3 files changed, 57 insertions(+), 63 deletions(-) diff --git a/Content.Client/Chat/TypingIndicator/TypingIndicatorSystem.cs b/Content.Client/Chat/TypingIndicator/TypingIndicatorSystem.cs index 844f793fc4..33dbc06ad8 100644 --- a/Content.Client/Chat/TypingIndicator/TypingIndicatorSystem.cs +++ b/Content.Client/Chat/TypingIndicator/TypingIndicatorSystem.cs @@ -65,14 +65,13 @@ public sealed class TypingIndicatorSystem : SharedTypingIndicatorSystem { if (_isClientTyping == isClientTyping) return; - _isClientTyping = isClientTyping; - // check if player controls any pawn + // check if player controls any entity. if (_playerManager.LocalEntity == null) return; - // send a networked event to server - RaiseNetworkEvent(new TypingChangedEvent(isClientTyping)); + _isClientTyping = isClientTyping; + RaisePredictiveEvent(new TypingChangedEvent(isClientTyping)); } private void OnShowTypingChanged(bool showTyping) diff --git a/Content.Server/Chat/TypingIndicator/TypingIndicatorSystem.cs b/Content.Server/Chat/TypingIndicator/TypingIndicatorSystem.cs index c923738930..d180a958ca 100644 --- a/Content.Server/Chat/TypingIndicator/TypingIndicatorSystem.cs +++ b/Content.Server/Chat/TypingIndicator/TypingIndicatorSystem.cs @@ -1,64 +1,7 @@ -using Content.Shared.ActionBlocker; using Content.Shared.Chat.TypingIndicator; -using Robust.Shared.Player; namespace Content.Server.Chat.TypingIndicator; -// Server-side typing system -// It receives networked typing events from clients -// And sync typing indicator using appearance component public sealed class TypingIndicatorSystem : SharedTypingIndicatorSystem { - [Dependency] private readonly ActionBlockerSystem _actionBlocker = default!; - [Dependency] private readonly SharedAppearanceSystem _appearance = default!; - - public override void Initialize() - { - base.Initialize(); - SubscribeLocalEvent(OnPlayerAttached); - SubscribeLocalEvent(OnPlayerDetached); - SubscribeNetworkEvent(OnClientTypingChanged); - } - - private void OnPlayerAttached(PlayerAttachedEvent ev) - { - // when player poses entity we want to make sure that there is typing indicator - EnsureComp(ev.Entity); - // we also need appearance component to sync visual state - EnsureComp(ev.Entity); - } - - private void OnPlayerDetached(EntityUid uid, TypingIndicatorComponent component, PlayerDetachedEvent args) - { - // player left entity body - hide typing indicator - SetTypingIndicatorEnabled(uid, false); - } - - private void OnClientTypingChanged(TypingChangedEvent ev, EntitySessionEventArgs args) - { - var uid = args.SenderSession.AttachedEntity; - if (!Exists(uid)) - { - Log.Warning($"Client {args.SenderSession} sent TypingChangedEvent without an attached entity."); - return; - } - - // check if this entity can speak or emote - if (!_actionBlocker.CanEmote(uid.Value) && !_actionBlocker.CanSpeak(uid.Value)) - { - // nah, make sure that typing indicator is disabled - SetTypingIndicatorEnabled(uid.Value, false); - return; - } - - SetTypingIndicatorEnabled(uid.Value, ev.IsTyping); - } - - private void SetTypingIndicatorEnabled(EntityUid uid, bool isEnabled, AppearanceComponent? appearance = null) - { - if (!Resolve(uid, ref appearance, false)) - return; - - _appearance.SetData(uid, TypingIndicatorVisuals.IsTyping, isEnabled, appearance); - } } diff --git a/Content.Shared/Chat/TypingIndicator/SharedTypingIndicatorSystem.cs b/Content.Shared/Chat/TypingIndicator/SharedTypingIndicatorSystem.cs index 81ebcfb108..dffe3cc185 100644 --- a/Content.Shared/Chat/TypingIndicator/SharedTypingIndicatorSystem.cs +++ b/Content.Shared/Chat/TypingIndicator/SharedTypingIndicatorSystem.cs @@ -1,12 +1,17 @@ +using Content.Shared.ActionBlocker; using Content.Shared.Clothing; +using Robust.Shared.Player; namespace Content.Shared.Chat.TypingIndicator; /// -/// Sync typing indicator icon between client and server. +/// Supports typing indicators on entities. /// public abstract class SharedTypingIndicatorSystem : EntitySystem { + [Dependency] private readonly ActionBlockerSystem _actionBlocker = default!; + [Dependency] private readonly SharedAppearanceSystem _appearance = default!; + /// /// Default ID of /// @@ -16,8 +21,27 @@ public abstract class SharedTypingIndicatorSystem : EntitySystem public override void Initialize() { base.Initialize(); + SubscribeLocalEvent(OnPlayerAttached); + SubscribeLocalEvent(OnPlayerDetached); + SubscribeLocalEvent(OnGotEquipped); SubscribeLocalEvent(OnGotUnequipped); + + SubscribeAllEvent(OnTypingChanged); + } + + private void OnPlayerAttached(PlayerAttachedEvent ev) + { + // when player poses entity we want to make sure that there is typing indicator + EnsureComp(ev.Entity); + // we also need appearance component to sync visual state + EnsureComp(ev.Entity); + } + + private void OnPlayerDetached(EntityUid uid, TypingIndicatorComponent component, PlayerDetachedEvent args) + { + // player left entity body - hide typing indicator + SetTypingIndicatorEnabled(uid, false); } private void OnGotEquipped(EntityUid uid, TypingIndicatorClothingComponent component, ClothingGotEquippedEvent args) @@ -33,6 +57,34 @@ public abstract class SharedTypingIndicatorSystem : EntitySystem if (!TryComp(args.Wearer, out var indicator)) return; - indicator.Prototype = SharedTypingIndicatorSystem.InitialIndicatorId; + indicator.Prototype = InitialIndicatorId; + } + + private void OnTypingChanged(TypingChangedEvent ev, EntitySessionEventArgs args) + { + var uid = args.SenderSession.AttachedEntity; + if (!Exists(uid)) + { + Log.Warning($"Client {args.SenderSession} sent TypingChangedEvent without an attached entity."); + return; + } + + // check if this entity can speak or emote + if (!_actionBlocker.CanEmote(uid.Value) && !_actionBlocker.CanSpeak(uid.Value)) + { + // nah, make sure that typing indicator is disabled + SetTypingIndicatorEnabled(uid.Value, false); + return; + } + + SetTypingIndicatorEnabled(uid.Value, ev.IsTyping); + } + + private void SetTypingIndicatorEnabled(EntityUid uid, bool isEnabled, AppearanceComponent? appearance = null) + { + if (!Resolve(uid, ref appearance, false)) + return; + + _appearance.SetData(uid, TypingIndicatorVisuals.IsTyping, isEnabled, appearance); } } -- 2.51.2