Skip to content

Commit bc172c9

Browse files
authored
Fix some DMF issues (#2336)
1 parent 7041f30 commit bc172c9

File tree

5 files changed

+67
-35
lines changed

5 files changed

+67
-35
lines changed

OpenDreamClient/Interface/Controls/ControlChild.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace OpenDreamClient.Interface.Controls;
1010
internal sealed class ControlChild(ControlDescriptor controlDescriptor, ControlWindow window) : InterfaceControl(controlDescriptor, window) {
1111
private ControlDescriptorChild ChildDescriptor => (ControlDescriptorChild)ElementDescriptor;
1212

13-
private Splitter _splitter;
13+
private Splitter _splitter = default!;
1414

1515
protected override Control CreateUIElement() {
1616
_splitter = new Splitter();

OpenDreamClient/Interface/Controls/ControlInput.cs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
namespace OpenDreamClient.Interface.Controls;
88

9-
internal sealed class ControlInput : InterfaceControl {
10-
private LineEdit _textBox;
9+
internal sealed class ControlInput(ControlDescriptor controlDescriptor, ControlWindow window) : InterfaceControl(controlDescriptor, window) {
10+
private LineEdit _textBox = default!;
1111

12-
public ControlInput(ControlDescriptor controlDescriptor, ControlWindow window) : base(controlDescriptor, window) { }
12+
private ControlDescriptorInput InputDescriptor => (ControlDescriptorInput)ControlDescriptor;
1313

1414
protected override Control CreateUIElement() {
1515
_textBox = new LineEdit();
@@ -19,14 +19,23 @@ protected override Control CreateUIElement() {
1919
}
2020

2121
private void TextBox_OnSubmit(LineEdit.LineEditEventArgs lineEditEventArgs) {
22-
_interfaceManager.RunCommand(_textBox.Text);
23-
_textBox.Clear();
22+
if (InputDescriptor.NoCommand.Value)
23+
return;
24+
25+
var command = InputDescriptor.Command.Value;
26+
if (command.StartsWith('!')) {
27+
_interfaceManager.RunCommand(lineEditEventArgs.Text);
28+
} else {
29+
_interfaceManager.RunCommand(command + lineEditEventArgs.Text);
30+
}
31+
32+
ResetText();
2433
}
2534

2635
protected override void UpdateElementDescriptor() {
2736
base.UpdateElementDescriptor();
28-
ControlDescriptorInput inputDescriptor = (ControlDescriptorInput)ElementDescriptor;
29-
_textBox.Text = inputDescriptor.Text.AsRaw();
37+
38+
ResetText();
3039
}
3140

3241
public override bool TryGetProperty(string property, [NotNullWhen(true)] out IDMFProperty? value) {
@@ -38,4 +47,27 @@ public override bool TryGetProperty(string property, [NotNullWhen(true)] out IDM
3847
return base.TryGetProperty(property, out value);
3948
}
4049
}
50+
51+
public override void SetProperty(string property, string value, bool manualWinset = false) {
52+
switch (property) {
53+
case "focus":
54+
var focusValue = new DMFPropertyBool(value);
55+
if (focusValue.Value)
56+
_textBox.GrabKeyboardFocus();
57+
break;
58+
default:
59+
base.SetProperty(property, value, manualWinset);
60+
break;
61+
}
62+
}
63+
64+
private void ResetText() {
65+
var command = InputDescriptor.Command.Value;
66+
67+
if (command.StartsWith('!')) {
68+
_textBox.Text = command[1..];
69+
} else {
70+
_textBox.Clear();
71+
}
72+
}
4173
}

OpenDreamClient/Interface/Controls/UI/Splitter.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ public Control? Left {
2929
RemoveChild(_left);
3030

3131
_left = value;
32-
if (_left != null)
32+
if (_left != null) {
33+
_left.Orphan();
3334
AddChild(_left);
35+
}
3436
}
3537
}
3638

@@ -44,8 +46,10 @@ public Control? Right {
4446
RemoveChild(_right);
4547

4648
_right = value;
47-
if (_right != null)
49+
if (_right != null) {
50+
_right.Orphan();
4851
AddChild(_right);
52+
}
4953
}
5054
}
5155

OpenDreamClient/Interface/DreamInterfaceManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ public void FrameUpdate(FrameEventArgs frameEventArgs) {
349349
if (Windows.ContainsKey(windowId)) {
350350
window = Windows[windowId];
351351
} else if (Menus.TryGetValue(windowId, out var menu)) {
352-
if (menu.MenuElements.TryGetValue(elementId, out var menuElement))
352+
if (menu.MenuElementsById.TryGetValue(elementId, out var menuElement))
353353
return menuElement;
354354
} else if(MacroSets.TryGetValue(windowId, out var macroSet)) {
355355
if (macroSet.Macros.TryGetValue(elementId, out var macroElement))
@@ -389,7 +389,7 @@ public void FrameUpdate(FrameEventArgs frameEventArgs) {
389389
if (menu.Id.Value == elementId)
390390
return menu;
391391

392-
if (menu.MenuElements.TryGetValue(elementId, out var menuElement))
392+
if (menu.MenuElementsById.TryGetValue(elementId, out var menuElement))
393393
return menuElement;
394394
}
395395

OpenDreamClient/Interface/InterfaceMenu.cs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
namespace OpenDreamClient.Interface;
77

88
public sealed class InterfaceMenu : InterfaceElement {
9-
public readonly Dictionary<string, MenuElement> MenuElements = new();
9+
public readonly Dictionary<string, MenuElement> MenuElementsById = new();
10+
public readonly Dictionary<string, MenuElement> MenuElementsByName = new();
1011
public readonly MenuBar MenuBar;
1112

1213
private readonly bool _pauseMenuCreation;
@@ -26,7 +27,7 @@ public InterfaceMenu(MenuDescriptor descriptor) : base(descriptor) {
2627
}
2728

2829
public void SetGroupChecked(string group, string id) {
29-
foreach (MenuElement menuElement in MenuElements.Values) {
30+
foreach (MenuElement menuElement in MenuElementsById.Values) {
3031
if (menuElement.ElementDescriptor is not MenuElementDescriptor menuElementDescriptor)
3132
continue;
3233

@@ -44,22 +45,24 @@ public override void AddChild(ElementDescriptor descriptor) {
4445
if (string.IsNullOrEmpty(elementDescriptor.Category.Value)) {
4546
element = new(elementDescriptor, this);
4647
} else {
47-
if (!MenuElements.TryGetValue(elementDescriptor.Category.Value, out var parentMenu)) {
48+
if (!MenuElementsById.TryGetValue(elementDescriptor.Category.Value, out var parentMenu) &&
49+
!MenuElementsByName.TryGetValue(elementDescriptor.Category.Value, out parentMenu)) {
4850
//if category is set but the parent element doesn't exist, create it
49-
var parentMenuDescriptor = new MenuElementDescriptor() {
51+
var parentMenuDescriptor = new MenuElementDescriptor {
5052
Id = elementDescriptor.Category
5153
};
5254

5355
parentMenu = new(parentMenuDescriptor, this);
54-
MenuElements.Add(parentMenu.Id.AsRaw(), parentMenu);
56+
MenuElementsById.Add(parentMenu.Id.AsRaw(), parentMenu);
5557
}
5658

5759
//now add this as a child
5860
element = new MenuElement(elementDescriptor, this);
5961
parentMenu.Children.Add(element);
6062
}
6163

62-
MenuElements.Add(element.Id.AsRaw(), element);
64+
MenuElementsById.Add(element.Id.AsRaw(), element);
65+
MenuElementsByName[element.ElementDescriptor.Name.AsRaw()] = element;
6366
CreateMenu(); // Update the menu to include the new child
6467
}
6568

@@ -69,16 +72,16 @@ private void CreateMenu() {
6972

7073
MenuBar.Menus.Clear();
7174

72-
foreach (MenuElement menuElement in MenuElements.Values) {
75+
foreach (MenuElement menuElement in MenuElementsById.Values) {
7376
if (!string.IsNullOrEmpty(menuElement.Category.Value)) // We only want the root-level menus here
7477
continue;
7578

7679
MenuBar.Menu menu = new() {
7780
Title = menuElement.ElementDescriptor.Name.AsRaw()
7881
};
7982

80-
if (menu.Title?.StartsWith("&") ?? false)
81-
menu.Title = menu.Title[1..]; //TODO: First character in name becomes a selection shortcut
83+
// TODO: Character after '&' becomes a selection shortcut
84+
menu.Title = menu.Title.Replace("&", string.Empty);
8285

8386
MenuBar.Menus.Add(menu);
8487
//visit each node in the tree, populating the menu from that
@@ -87,22 +90,16 @@ private void CreateMenu() {
8790
}
8891
}
8992

90-
public sealed class MenuElement : InterfaceElement {
93+
public sealed class MenuElement(MenuElementDescriptor data, InterfaceMenu menu) : InterfaceElement(data) {
9194
public readonly List<MenuElement> Children = new();
9295

9396
private MenuElementDescriptor MenuElementDescriptor => (MenuElementDescriptor) ElementDescriptor;
9497
public DMFPropertyString Category => MenuElementDescriptor.Category;
9598
public DMFPropertyString Command => MenuElementDescriptor.Command;
96-
private readonly InterfaceMenu _menu;
97-
98-
public MenuElement(MenuElementDescriptor data, InterfaceMenu menu) : base(data) {
99-
_menu = menu;
100-
}
10199

102100
public MenuBar.MenuEntry CreateMenuEntry() {
103101
string text = ElementDescriptor.Name.AsRaw();
104-
if (text.StartsWith("&"))
105-
text = text[1..]; //TODO: First character in name becomes a selection shortcut
102+
text = text.Replace("&", string.Empty); // TODO: Character after '&' becomes a selection shortcut
106103

107104
if(Children.Count > 0) {
108105
MenuBar.SubMenu subMenu = new() {
@@ -115,25 +112,24 @@ public MenuBar.MenuEntry CreateMenuEntry() {
115112
return subMenu;
116113
}
117114

118-
if (String.IsNullOrEmpty(text))
115+
if (string.IsNullOrEmpty(text))
119116
return new MenuBar.MenuSeparator();
120117

121118
if(MenuElementDescriptor.CanCheck.Value)
122119
if(MenuElementDescriptor.IsChecked.Value)
123-
text = text + " ☑";
120+
text += " ☑";
124121

125122
MenuBar.MenuButton menuButton = new() {
126123
Text = text
127124
};
128125

129-
130126
menuButton.OnPressed += () => {
131127
if(MenuElementDescriptor.CanCheck.Value)
132128
if(!string.IsNullOrEmpty(MenuElementDescriptor.Group.Value))
133-
_menu.SetGroupChecked(MenuElementDescriptor.Group.Value, MenuElementDescriptor.Id.AsRaw());
129+
menu.SetGroupChecked(MenuElementDescriptor.Group.Value, MenuElementDescriptor.Id.AsRaw());
134130
else
135131
MenuElementDescriptor.IsChecked = new DMFPropertyBool(!MenuElementDescriptor.IsChecked.Value);
136-
_menu.CreateMenu();
132+
menu.CreateMenu();
137133
if(!string.IsNullOrEmpty(MenuElementDescriptor.Command.Value))
138134
_interfaceManager.RunCommand(Command.AsRaw());
139135
};
@@ -146,7 +142,7 @@ public override void AddChild(ElementDescriptor descriptor) {
146142
descriptor = ((MenuElementDescriptor) descriptor).WithCategory(IoCManager.Resolve<ISerializationManager>(), MenuElementDescriptor.Name);
147143

148144
// Pass this on to the parent menu
149-
_menu.AddChild(descriptor);
145+
menu.AddChild(descriptor);
150146
}
151147
}
152148
}

0 commit comments

Comments
 (0)