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

is-checked and group for menus #1462

Merged
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
8 changes: 8 additions & 0 deletions OpenDreamClient/Interface/Descriptors/MenuDescriptors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public string? Category {
[DataField("can-check")]
public bool CanCheck { get; private set; }

[DataField("is-checked")]
public bool IsChecked { get; set; }

[DataField("group")]
public string? Group { get; private set; }
[DataField("index")]
public int Index { get; private set; }

public MenuElementDescriptor WithCategory(ISerializationManager serialization, string category) {
var copy = serialization.CreateCopy(this, notNullableOverride: true);

Expand Down
55 changes: 51 additions & 4 deletions OpenDreamClient/Interface/InterfaceMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ public InterfaceMenu(MenuDescriptor descriptor) : base(descriptor) {
CreateMenu();
}

public void SetGroupChecked(string group, string id) {
foreach (MenuElement menuElement in MenuElements.Values) {
if (menuElement.ElementDescriptor is not MenuElementDescriptor menuElementDescriptor)
continue;

if (menuElementDescriptor.Group == group) {
menuElementDescriptor.IsChecked = menuElementDescriptor.Id == id;
}
}
}

public override void AddChild(ElementDescriptor descriptor) {
if (descriptor is not MenuElementDescriptor elementDescriptor)
throw new ArgumentException($"Attempted to add a {descriptor} to a menu", nameof(descriptor));
Expand Down Expand Up @@ -80,7 +91,6 @@ public sealed class MenuElement : InterfaceElement {
private MenuElementDescriptor MenuElementDescriptor => (MenuElementDescriptor) ElementDescriptor;
public string? Category => MenuElementDescriptor.Category;
public string Command => MenuElementDescriptor.Command;

private readonly InterfaceMenu _menu;

public MenuElement(MenuElementDescriptor data, InterfaceMenu menu) : base(data) {
Expand All @@ -106,16 +116,53 @@ public MenuBar.MenuEntry CreateMenuEntry() {
if (String.IsNullOrEmpty(text))
return new MenuBar.MenuSeparator();

if(MenuElementDescriptor.CanCheck)
if(MenuElementDescriptor.IsChecked)
text = text + " ☑";

MenuBar.MenuButton menuButton = new() {
Text = text
};

//result.IsCheckable = MenuElementDescriptor.CanCheck;
if (!String.IsNullOrEmpty(Command))
menuButton.OnPressed += () => { EntitySystem.Get<DreamCommandSystem>().RunCommand(Command); };

menuButton.OnPressed += () => {
if(MenuElementDescriptor.CanCheck)
if(!String.IsNullOrEmpty(MenuElementDescriptor.Group))
_menu.SetGroupChecked(MenuElementDescriptor.Group, MenuElementDescriptor.Id);
else
MenuElementDescriptor.IsChecked = !MenuElementDescriptor.IsChecked;
_menu.CreateMenu();
if(!string.IsNullOrEmpty(MenuElementDescriptor.Command))
EntitySystem.Get<DreamCommandSystem>().RunCommand(Command);
};
return menuButton;
}

public override bool TryGetProperty(string property, out string value) {
switch (property) {
case "command":
value = Command;
return true;
case "category":
value = Category ?? "";
return true;
case "can-check":
value = MenuElementDescriptor.CanCheck.ToString();
return true;
case "is-checked":
value = MenuElementDescriptor.IsChecked.ToString();
return true;
case "group":
value = MenuElementDescriptor.Group ?? "";
return true;
case "index":
value = MenuElementDescriptor.Index.ToString();
return true;
default:
return base.TryGetProperty(property, out value);
}
}

public override void AddChild(ElementDescriptor descriptor) {
// Set the child's category to this element
// TODO: The "parent" and "category" attributes seem to be treated differently in BYOND; not the same thing.
Expand Down
Loading