Skip to content

Commit

Permalink
Merge pull request #373 from FoxxoTrystan/consent-fixes
Browse files Browse the repository at this point in the history
Consent UI Generated
  • Loading branch information
Fansana authored Nov 23, 2024
2 parents 8928cea + e76362e commit 5c2b443
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 61 deletions.
31 changes: 4 additions & 27 deletions Content.Client/Consent/UI/Windows/ConsentWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,10 @@
<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}" />
<ScrollContainer HorizontalExpand="True" VerticalExpand="True" HScrollEnabled="False" VScrollEnabled="True" MinHeight="50">
<BoxContainer Name="ConsentList" Access="Public" HorizontalExpand="True" SeparationOverride="2" Orientation="Vertical">
<!-- The rest here is generated programmatically -->
</BoxContainer>
</PanelContainer>


</ScrollContainer>
</BoxContainer>
</controls:FancyWindow>
110 changes: 87 additions & 23 deletions Content.Client/Consent/UI/Windows/ConsentWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
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;
Expand All @@ -17,15 +15,16 @@ public sealed partial class ConsentWindow : FancyWindow
{
[Dependency] private readonly IClientConsentManager _consentManager = default!;
[Dependency] private readonly IConfigurationManager _configManager = default!;

private ButtonGroup Example1Buttons;
[Dependency] private readonly IPrototypeManager _protoManager = default!;
private readonly List<EntryState> _entries = new();

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

SaveConsentSettings.OnPressed += _ => {
SaveConsentSettings.OnPressed += _ =>
{
SaveConsentSettings.Disabled = true;
_consentManager.UpdateConsent(GetSettings());
};
Expand All @@ -36,22 +35,17 @@ public ConsentWindow()

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)
foreach (var entry in _entries)
{
toggles["Example1"] = "on";
if (entry.Button != null && entry.Button.Pressed)
toggles[entry.Consent.ID] = "on";
}

return new(text, toggles);
Expand All @@ -76,26 +70,96 @@ private void UnsavedChanges()
SaveConsentSettings.Disabled = false;
}

public void UpdateUi()
private void AddConsentEntry(ConsentTogglePrototype prototype)
{
var consent = _consentManager.GetConsent();
var state = new EntryState { Consent = prototype };

ConsentToggleExample1Off.Pressed = true;
var container = new BoxContainer { Orientation = BoxContainer.LayoutOrientation.Vertical };

ConsentFreetext.TextRope = new Rope.Leaf(consent.Freetext);
var header = new BoxContainer
{
Orientation = BoxContainer.LayoutOrientation.Horizontal,
Margin = new Thickness(5f, 5f)
};

var name = new Label
{
Text = Loc.GetString($"consent-{prototype.ID}-name"),
HorizontalExpand = true
};

var buttonOff = new Button { Text = "Off" };
buttonOff.StyleClasses.Add("OpenRight");
buttonOff.Pressed = true;

var buttonOn = new Button { Text = "On" };
buttonOn.StyleClasses.Add("OpenLeft");
state.Button = buttonOn;

buttonOff.OnPressed += _ => ButtonOnPress(buttonOff, buttonOn);
buttonOn.OnPressed += _ => ButtonOnPress(buttonOn, buttonOff);

var consent = _consentManager.GetConsent();
foreach (var toggle in consent.Toggles)
{
if (toggle.Key == "Example1" && toggle.Value == "on")
if (toggle.Key == prototype.ID && toggle.Value == "on")
{
ConsentToggleExample1On.Pressed = true;
}
else
{
throw new InvalidOperationException("Invalid consent toggle");
buttonOn.Pressed = true;
buttonOff.Pressed = false;
continue;
}
}

header.AddChild(name);
header.AddChild(buttonOff);
header.AddChild(buttonOn);

container.AddChild(header);

var desc = new Label
{
Text = Loc.GetString($"consent-{prototype.ID}-desc"),
};

container.AddChild(desc);

var wrapper = new PanelContainer();
wrapper.StyleClasses.Add("PdaBorderRect");

wrapper.AddChild(container);
ConsentList.AddChild(wrapper);

_entries.Add(state);
}

private void ButtonOnPress(Button currentButton, Button otherbutton)
{
currentButton.Pressed = true;
otherbutton.Pressed = false;
UnsavedChanges();
}

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

ConsentFreetext.TextRope = new Rope.Leaf(consent.Freetext);

if (ConsentList.ChildCount > 0)
ConsentList.RemoveAllChildren();
_entries.Clear();

var consentprototypelist = _protoManager.EnumeratePrototypes<ConsentTogglePrototype>();
foreach (var prototype in consentprototypelist)
AddConsentEntry(prototype);

SaveConsentSettings.Disabled = true;
SaveLabel.Text = "";
}

private struct EntryState
{
public ConsentTogglePrototype Consent;
public Button? Button;
}
}
6 changes: 0 additions & 6 deletions Content.Shared/Consent/ConsentTogglePrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,4 @@ public sealed partial class ConsentTogglePrototype : IPrototype
{
[IdDataField]
public string ID { get; private set; } = default!;

/// <summary>
/// The name that will show in UI. Must be loc string.
/// </summary>
[DataField]
public string Name = default!;
}
4 changes: 2 additions & 2 deletions Resources/Locale/en-US/Blep/consent.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ consent-examine-verb = Consent Info
consent-examine-not-set = This player has no consent preferences set. Ask for consent first before engaging in any erotic roleplay.
# Consent toggles
consent-example1 = Example Consent Toggle
consent-example1-desc = This is just here as an example for how to add consent toggles.
consent-Example-name = Example Consent Toggle
consent-Example-desc = This is just here as an example for how to add consent toggles.
3 changes: 0 additions & 3 deletions Resources/Prototypes/Consent/examples.yml

This file was deleted.

2 changes: 2 additions & 0 deletions Resources/Prototypes/consent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# - type: consentToggle
# id: Example

0 comments on commit 5c2b443

Please sign in to comment.