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

Added consent Menu #4

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
45 changes: 45 additions & 0 deletions Content.Client/Consent/ClientConsentManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Content.Shared.Consent;
using Robust.Shared.Network;

namespace Content.Client.Consent;

public sealed class ClientConsentManager : IClientConsentManager
{
[Dependency] private readonly IClientNetManager _netManager = default!;

// TODO: sync all players consent settings with ServerConsentManager, for client prediction
private PlayerConsentSettings? _consent;

public bool HasLoaded => _consent is not null;

public event Action? OnServerDataLoaded;

public void Initialize()
{
_netManager.RegisterNetMessage<MsgUpdateConsent>(HandleUpdateConsent);
}

public void UpdateConsent(PlayerConsentSettings consentSettings)
{
var msg = new MsgUpdateConsent
{
Consent = consentSettings
};
_netManager.ClientSendMessage(msg);
}

public PlayerConsentSettings GetConsent()
{
if (_consent is null)
throw new InvalidOperationException("Consent settings not loaded yet?");

return _consent;
}

private void HandleUpdateConsent(MsgUpdateConsent message)
{
_consent = message.Consent;

OnServerDataLoaded?.Invoke();
}
}
7 changes: 7 additions & 0 deletions Content.Client/Consent/ConsentSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Content.Shared.Consent;

namespace Content.Client.Consent;

public sealed class ConsentSystem : SharedConsentSystem
{
}
13 changes: 13 additions & 0 deletions Content.Client/Consent/IClientConsentManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Content.Shared.Consent;

namespace Content.Client.Consent;

public interface IClientConsentManager
{
event Action OnServerDataLoaded;
bool HasLoaded { get; }

void Initialize();
void UpdateConsent(PlayerConsentSettings consentSettings);
PlayerConsentSettings GetConsent();
}
102 changes: 102 additions & 0 deletions Content.Client/Consent/UI/ConsentUiController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using Content.Client.Consent.UI.Windows;
using Content.Client.Gameplay;
using Content.Client.UserInterface.Controls;
using Content.Client.UserInterface.Systems.MenuBar.Widgets;
using Content.Shared.Input;
using Robust.Client.Input;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controllers;
using Robust.Client.UserInterface.Controllers.Implementations;
using Robust.Shared.Input.Binding;
using Robust.Shared.Map;
using Robust.Shared.Player;
using Robust.Shared.Utility;
using JetBrains.Annotations;
using static Robust.Client.UserInterface.Controls.BaseButton;

namespace Content.Client.Consent.UI;

[UsedImplicitly]
public sealed class ConsentUiController : UIController, IOnStateChanged<GameplayState>
{
[Dependency] private readonly IInputManager _input = default!;

private ConsentWindow? _window;

private MenuButton? ConsentButton => UIManager.GetActiveUIWidgetOrNull<GameTopMenuBar>()?.ConsentButton;

public void OnStateEntered(GameplayState state)
{
EnsureWindow();

_input.SetInputCommand(ContentKeyFunctions.OpenConsentWindow,
InputCmdHandler.FromDelegate(_ => ToggleWindow()));
}

public void OnStateExited(GameplayState state)
{
if (_window != null)
{
_window.Dispose();
_window = null;
}
}

public void UnloadButton()
{
if (ConsentButton == null)
{
return;
}

ConsentButton.OnPressed -= ConsentButtonPressed;
}

public void LoadButton()
{
if (ConsentButton == null)
{
return;
}

ConsentButton.OnPressed += ConsentButtonPressed;
}

private void ConsentButtonPressed(ButtonEventArgs args)
{
ToggleWindow();
}

private void EnsureWindow()
{
if (_window is { Disposed: false })
return;

_window = UIManager.CreateWindow<ConsentWindow>();
_window.OnOpen += () => {
if (ConsentButton is not null)
ConsentButton.Pressed = true;
};
_window.OnClose += () => {
if (ConsentButton is not null)
ConsentButton.Pressed = false;
_window.UpdateUi(); // Discard unsaved changes
};
}

private void ToggleWindow()
{
if (_window is null)
return;

UIManager.ClickSound();
if (_window.IsOpen != true)
{
_window.OpenCentered();
}
else
{
_window.Close();
}
}
}
63 changes: 63 additions & 0 deletions Content.Client/Consent/UI/Windows/ConsentWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<controls:FancyWindow
xmlns="https://spacestation14.io"
xmlns:controls="clr-namespace:Content.Client.UserInterface.Controls"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:gfx="clr-namespace:Robust.Client.Graphics;assembly=Robust.Client"
MinSize="620 670"
Title="{Loc consent-window-title}"
Resizable="False">
<BoxContainer Orientation="Vertical" HorizontalExpand="True" Margin="5">
<Label Text="{Loc consent-window-text}" />
<BoxContainer Orientation="Horizontal" Margin="5">
<Label Name="SaveLabel" FontColorOverride="Red" Access="Public" />
<Control HorizontalExpand="True" />
<Button
Name="SaveConsentSettings"
Access="Public"
Text="Save"
Disabled="True" />
</BoxContainer>
<Label Text="{Loc consent-window-freetext-label}" />
<PanelContainer HorizontalExpand="True" VerticalExpand="True" MinWidth="200">
<PanelContainer.PanelOverride>
<gfx:StyleBoxFlat BackgroundColor="#1B1B1E" />
</PanelContainer.PanelOverride>
<TextEdit
Name="ConsentFreetext"
Access="Public"
MinHeight="100"
VerticalExpand="True" />
</PanelContainer>
<Control MinSize="0 10" />
<Label Text="{Loc consent-window-toggles-label}" />

<!-- TODO: Generate these in code by iterating prototypes? -->
<!-- Example Consent Toggle -->
<PanelContainer HorizontalExpand="True" MinWidth="200">
<PanelContainer.PanelOverride>
<gfx:StyleBoxFlat BackgroundColor="#1B1B1E" />
</PanelContainer.PanelOverride>
<BoxContainer Orientation="Vertical" HorizontalExpand="True" Margin="5">
<BoxContainer Orientation="Horizontal" Margin="5">
<Label Text="{Loc consent-example1}" />
<Control HorizontalExpand="True" />
<Button
Name="ConsentToggleExample1Off"
ToggleMode="True"
Access="Public"
Text="Off"
StyleClasses="OpenRight" />
<Button
Name="ConsentToggleExample1On"
ToggleMode="True"
Access="Public"
Text="On"
StyleClasses="OpenLeft" />
</BoxContainer>
<Label Text="{Loc consent-example1-desc}" />
</BoxContainer>
</PanelContainer>


</BoxContainer>
</controls:FancyWindow>
101 changes: 101 additions & 0 deletions Content.Client/Consent/UI/Windows/ConsentWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using Content.Client.UserInterface.Controls;
using Content.Shared.CCVar;
using Content.Shared.Consent;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Configuration;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;

namespace Content.Client.Consent.UI.Windows;

[GenerateTypedNameReferences]
public sealed partial class ConsentWindow : FancyWindow
{
[Dependency] private readonly IClientConsentManager _consentManager = default!;
[Dependency] private readonly IConfigurationManager _configManager = default!;

private ButtonGroup Example1Buttons;

public ConsentWindow()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);

SaveConsentSettings.OnPressed += _ => {
SaveConsentSettings.Disabled = true;
_consentManager.UpdateConsent(GetSettings());
};

_consentManager.OnServerDataLoaded += UpdateUi;
if (_consentManager.HasLoaded)
UpdateUi();

ConsentFreetext.Placeholder = new Rope.Leaf(Loc.GetString("consent-window-freetext-placeholder"));
ConsentFreetext.OnTextChanged += _ => UnsavedChanges();

Example1Buttons = new ButtonGroup();
ConsentToggleExample1On.Group = Example1Buttons;
ConsentToggleExample1On.OnToggled += _ => UnsavedChanges();
ConsentToggleExample1Off.Group = Example1Buttons;
ConsentToggleExample1Off.OnToggled += _ => UnsavedChanges();
}

private PlayerConsentSettings GetSettings()
{
var text = Rope.Collapse(ConsentFreetext.TextRope);
var toggles = new Dictionary<ProtoId<ConsentTogglePrototype>, string>();

if (Example1Buttons.Pressed == ConsentToggleExample1On)
{
toggles["Example1"] = "on";
}

return new(text, toggles);
}

private void UnsavedChanges()
{
// Validate freetext length
var maxLength = _configManager.GetCVar(CCVars.ConsentFreetextMaxLength);
var length = Rope.Collapse(ConsentFreetext.TextRope).Length;

if (length > maxLength)
{
SaveLabel.Text = Loc.GetString("consent-window-char-limit-warning", ("length", length), ("maxLength", maxLength));
SaveConsentSettings.Disabled = true;

return;
}

// If everything is valid, enable save button and inform user they need to save.
SaveLabel.Text = Loc.GetString("consent-window-unsaved-changes");
SaveConsentSettings.Disabled = false;
}

public void UpdateUi()
{
var consent = _consentManager.GetConsent();

ConsentToggleExample1Off.Pressed = true;

ConsentFreetext.TextRope = new Rope.Leaf(consent.Freetext);
foreach (var toggle in consent.Toggles)
{
if (toggle.Key == "Example1" && toggle.Value == "on")
{
ConsentToggleExample1On.Pressed = true;
}
else
{
throw new InvalidOperationException("Invalid consent toggle");
}
}

SaveConsentSettings.Disabled = true;
SaveLabel.Text = "";
}
}
3 changes: 3 additions & 0 deletions Content.Client/Entry/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Content.Client.Changelog;
using Content.Client.Chat.Managers;
using Content.Client.DiscordAuth;
using Content.Client.Consent;
using Content.Client.JoinQueue;
using Content.Client.Eui;
using Content.Client.Flash;
Expand Down Expand Up @@ -57,6 +58,7 @@ public sealed class EntryPoint : GameClient
[Dependency] private readonly RulesManager _rulesManager = default!;
[Dependency] private readonly ViewportManager _viewportManager = default!;
[Dependency] private readonly IUserInterfaceManager _userInterfaceManager = default!;
[Dependency] private readonly IClientConsentManager _clientConsentManager = default!;
[Dependency] private readonly IInputManager _inputManager = default!;
[Dependency] private readonly IOverlayManager _overlayManager = default!;
[Dependency] private readonly IChatManager _chatManager = default!;
Expand Down Expand Up @@ -161,6 +163,7 @@ public override void PostInit()
_overlayManager.AddOverlay(new FlashOverlay());
_overlayManager.AddOverlay(new RadiationPulseOverlay());
_chatManager.Initialize();
_clientConsentManager.Initialize();
_clientPreferencesManager.Initialize();
_euiManager.Initialize();
_voteManager.Initialize();
Expand Down
1 change: 1 addition & 0 deletions Content.Client/Input/ContentContexts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public static void SetupContexts(IInputContextContainer contexts)
common.AddFunction(ContentKeyFunctions.OpenSandboxWindow);
common.AddFunction(ContentKeyFunctions.OpenTileSpawnWindow);
common.AddFunction(ContentKeyFunctions.OpenDecalSpawnWindow);
common.AddFunction(ContentKeyFunctions.OpenConsentWindow);
common.AddFunction(ContentKeyFunctions.OpenAdminMenu);
common.AddFunction(ContentKeyFunctions.OpenGuidebook);
}
Expand Down
2 changes: 2 additions & 0 deletions Content.Client/IoC/ClientContentIoC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Content.Client.Clickable;
using Content.Client.DiscordAuth;
using Content.Client.JoinQueue;
using Content.Client.Consent;
using Content.Client.Options;
using Content.Client.Eui;
using Content.Client.GhostKick;
Expand Down Expand Up @@ -33,6 +34,7 @@ public static void Register()
{
IoCManager.Register<IParallaxManager, ParallaxManager>();
IoCManager.Register<IChatManager, ChatManager>();
IoCManager.Register<IClientConsentManager, ClientConsentManager>();
IoCManager.Register<IClientPreferencesManager, ClientPreferencesManager>();
IoCManager.Register<IStylesheetManager, StylesheetManager>();
IoCManager.Register<IScreenshotHook, ScreenshotHook>();
Expand Down
1 change: 1 addition & 0 deletions Content.Client/Options/UI/Tabs/KeyRebindTab.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ void AddCheckBox(string checkBoxName, bool currentState, Action<BaseButton.Butto
AddButton(ContentKeyFunctions.OpenSandboxWindow);
AddButton(ContentKeyFunctions.OpenTileSpawnWindow);
AddButton(ContentKeyFunctions.OpenDecalSpawnWindow);
AddButton(ContentKeyFunctions.OpenConsentWindow);
AddButton(ContentKeyFunctions.OpenAdminMenu);
AddButton(EngineKeyFunctions.WindowCloseAll);
AddButton(EngineKeyFunctions.WindowCloseRecent);
Expand Down
Loading
Loading