Skip to content

Commit

Permalink
Port
Browse files Browse the repository at this point in the history
  • Loading branch information
FoxxoTrystan committed Mar 24, 2024
1 parent 6bdc514 commit 3d86f02
Show file tree
Hide file tree
Showing 67 changed files with 3,033 additions and 78 deletions.
1 change: 1 addition & 0 deletions Content.Client/Input/ContentContexts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public static void SetupContexts(IInputContextContainer contexts)
human.AddFunction(ContentKeyFunctions.UseItemInHand);
human.AddFunction(ContentKeyFunctions.AltUseItemInHand);
human.AddFunction(ContentKeyFunctions.OpenCharacterMenu);
human.AddFunction(ContentKeyFunctions.OpenLanguageMenu);
human.AddFunction(ContentKeyFunctions.ActivateItemInWorld);
human.AddFunction(ContentKeyFunctions.ThrowItemInHand);
human.AddFunction(ContentKeyFunctions.AltActivateItemInWorld);
Expand Down
18 changes: 18 additions & 0 deletions Content.Client/Language/LanguageMenuWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<DefaultWindow xmlns="https://spacestation14.io"
xmlns:ui="clr-namespace:Content.Client.UserInterface.Controls"
Title="a"
SetSize="300 300">
<BoxContainer Orientation="Vertical" SeparationOverride="4" MinWidth="150">
<PanelContainer Name="CurrentLanguageContainer" Access="Public" StyleClasses="PdaBorderRect">
<Label Name="CurrentLanguageLabel" Access="Public" Text="Current Language:" HorizontalExpand="True"></Label>
</PanelContainer>

<ui:HLine></ui:HLine>

<ScrollContainer HorizontalExpand="True" VerticalExpand="True" HScrollEnabled="False" VScrollEnabled="True" MinHeight="50">
<BoxContainer Name="OptionsList" Access="Public" HorizontalExpand="True" SeparationOverride="2" Orientation="Vertical">
<!-- The rest here is generated programmatically -->
</BoxContainer>
</ScrollContainer>
</BoxContainer>
</DefaultWindow>
123 changes: 123 additions & 0 deletions Content.Client/Language/LanguageMenuWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using Content.Client.Language.Systems;
using Content.Shared.Language;
using Content.Shared.Language.Systems;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Console;
using Robust.Shared.Utility;
using static Content.Shared.Language.Systems.SharedLanguageSystem;

namespace Content.Client.Language;

[GenerateTypedNameReferences]
public sealed partial class LanguageMenuWindow : DefaultWindow
{
private readonly LanguageSystem _clientLanguageSystem;
private readonly List<EntryState> _entries = new();

public LanguageMenuWindow()
{
RobustXamlLoader.Load(this);
_clientLanguageSystem = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<LanguageSystem>();

Title = Loc.GetString("language-menu-window-title");
}

public void UpdateState(string currentLanguage, List<string> spokenLanguages)
{
var langName = LanguagePrototype.GetLocalizedName(currentLanguage);
CurrentLanguageLabel.Text = Loc.GetString("language-menu-current-language", ("language", langName ?? "<error>"));

OptionsList.RemoveAllChildren();
_entries.Clear();

foreach (var language in spokenLanguages)
{
AddLanguageEntry(language);
}

// Disable the button for the currently chosen language
foreach (var entry in _entries)
{
if (entry.button != null)
entry.button.Disabled = entry.language == currentLanguage;
}
}

private void AddLanguageEntry(string language)
{
var proto = _clientLanguageSystem.GetLanguage(language);
var state = new EntryState { language = language };

var container = new BoxContainer();
container.Orientation = BoxContainer.LayoutOrientation.Vertical;

// Create and add a header with the name and the button to select the language
{
var header = new BoxContainer();
header.Orientation = BoxContainer.LayoutOrientation.Horizontal;

header.Orientation = BoxContainer.LayoutOrientation.Horizontal;
header.HorizontalExpand = true;
header.SeparationOverride = 2;

var name = new Label();
name.Text = proto?.LocalizedName ?? "<error>";
name.MinWidth = 50;
name.HorizontalExpand = true;

var button = new Button();
button.Text = "Choose";
button.OnPressed += _ => OnLanguageChosen(language);
state.button = button;

header.AddChild(name);
header.AddChild(button);

container.AddChild(header);
}

// Create and add a collapsible description
{
var body = new CollapsibleBody();
body.HorizontalExpand = true;
body.Margin = new Thickness(4f, 4f);

var description = new RichTextLabel();
description.SetMessage(proto?.LocalizedDescription ?? "<error>");
description.HorizontalExpand = true;

body.AddChild(description);

var collapser = new Collapsible(Loc.GetString("language-menu-description-header"), body);
collapser.Orientation = BoxContainer.LayoutOrientation.Vertical;
collapser.HorizontalExpand = true;

container.AddChild(collapser);
}

// Before adding, wrap the new container in a PanelContainer to give it a distinct look
var wrapper = new PanelContainer();
wrapper.StyleClasses.Add("PdaBorderRect");

wrapper.AddChild(container);
OptionsList.AddChild(wrapper);

_entries.Add(state);
}

private void OnLanguageChosen(string id)
{
var proto = _clientLanguageSystem.GetLanguage(id);
if (proto != null)
_clientLanguageSystem.RequestSetLanguage(proto);
}

private struct EntryState
{
public string language;
public Button? button;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System.Linq;
using System.Numerics;
using Content.Client.Language.Systems;
using Content.Client.UserInterface.Systems.Chat.Controls;
using Content.Client.UserInterface.Systems.Language;
using Content.Shared.Language;
using Robust.Client.UserInterface;
using Robust.Shared.Utility;

namespace Content.Client.Language.Systems.Chat.Controls;

// Mostly copied from ChannelSelectorButton
public sealed class LanguageSelectorButton : ChatPopupButton<LanguageSelectorPopup>
{
public LanguagePrototype? SelectedLanguage { get; private set; }

private const int SelectorDropdownOffset = 38;

public LanguageSelectorButton()
{
Name = "LanguageSelector";

Popup.Selected += Select;

if (Popup.FirstLanguage is { } firstSelector)
{
Select(firstSelector);
}

IoCManager.Resolve<IUserInterfaceManager>().GetUIController<LanguageMenuUIController>().LanguagesUpdatedHook += UpdateLanguage;
}

private void UpdateLanguage((string current, List<string> spoken, List<string> understood) args)
{
Popup.SetLanguages(args.spoken);

// Kill me please
SelectedLanguage = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<LanguageSystem>().GetLanguage(args.current);
Text = LanguageSelectorName(SelectedLanguage!);
}

protected override UIBox2 GetPopupPosition()
{
var globalLeft = GlobalPosition.X;
var globalBot = GlobalPosition.Y + Height;
return UIBox2.FromDimensions(
new Vector2(globalLeft, globalBot),
new Vector2(SizeBox.Width, SelectorDropdownOffset));
}

public void Select(LanguagePrototype language)
{
if (Popup.Visible)
{
Popup.Close();
}

if (SelectedLanguage == language)
return;
SelectedLanguage = language;
IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<LanguageSystem>().RequestSetLanguage(language);

Text = LanguageSelectorName(language);
}

public static string LanguageSelectorName(LanguagePrototype language, bool full = false)
{
var name = language.LocalizedName;

// if the language name is short enough, just return it
if (full || name.Length < 5)
return name;

// If the language name is multi-word, collect first letters and capitalize them
if (name.Contains(' '))
{
var result = name
.Split(" ")
.Select(it => it.FirstOrNull())
.Where(it => it != null)
.Select(it => char.ToUpper(it!.Value));

return new string(result.ToArray());
}

// Alternatively, take the first 5 letters
return name[..5];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Content.Client.Stylesheets;
using Content.Client.UserInterface.Systems.Chat;
using Content.Client.UserInterface.Systems.Chat.Controls;
using Content.Shared.Chat;
using Content.Shared.Language;
using Robust.Client.UserInterface.Controls;

namespace Content.Client.Language.Systems.Chat.Controls;

// Mostly copied from ChannelSelectorItemButton
public sealed class LanguageSelectorItemButton : Button
{
public readonly LanguagePrototype Language;

public bool IsHidden => Parent == null;

public LanguageSelectorItemButton(LanguagePrototype language)
{
Language = language;
AddStyleClass(StyleNano.StyleClassChatChannelSelectorButton);

Text = LanguageSelectorButton.LanguageSelectorName(language, full: true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Content.Client.Language.Systems;
using Content.Client.UserInterface.Systems.Language;
using Content.Shared.Language;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using static Robust.Client.UserInterface.Controls.BaseButton;

namespace Content.Client.Language.Systems.Chat.Controls;

// Mostly copied from LanguageSelectorPopup
public sealed class LanguageSelectorPopup : Popup
{
private readonly BoxContainer _channelSelectorHBox;
private readonly Dictionary<string, LanguageSelectorItemButton> _selectorStates = new();

public event Action<LanguagePrototype>? Selected;

public LanguageSelectorPopup()
{
_channelSelectorHBox = new BoxContainer
{
Orientation = BoxContainer.LayoutOrientation.Horizontal,
SeparationOverride = 1
};

AddChild(_channelSelectorHBox);
}

public LanguagePrototype? FirstLanguage
{
get
{
foreach (var selector in _selectorStates.Values)
{
if (!selector.IsHidden)
return selector.Language;
}

return null;
}
}

public void SetLanguages(List<string> languages)
{
var languageSystem = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<LanguageSystem>();
_channelSelectorHBox.RemoveAllChildren();

foreach (var language in languages)
{
if (!_selectorStates.TryGetValue(language, out var selector))
{
var proto = languageSystem.GetLanguage(language);
if (proto == null)
continue;

selector = new LanguageSelectorItemButton(proto);
_selectorStates[language] = selector;
selector.OnPressed += OnSelectorPressed;
}

if (selector.IsHidden)
{
_channelSelectorHBox.AddChild(selector);
}
}
}

private void OnSelectorPressed(ButtonEventArgs args)
{
var button = (LanguageSelectorItemButton) args.Button;
Select(button.Language);
}

private void Select(LanguagePrototype language)
{
Selected?.Invoke(language);
}
}
Loading

0 comments on commit 3d86f02

Please sign in to comment.