Skip to content

Commit

Permalink
Implement on-show and on-hide for map controls, plus winset par…
Browse files Browse the repository at this point in the history
…sing fixes (#1450)

* more robust winset, basic events

* enable nested attributes

* Fix bad error message

* not the cleanest, but it works

* Apply suggestions from code review

Co-authored-by: wixoa <[email protected]>

* delete client DreamCommandSystem

* remove duplicate var

* Update OpenDreamClient/Interface/InterfaceMacro.cs

* Update OpenDreamClient/Interface/InterfaceMacro.cs

---------

Co-authored-by: amy <[email protected]>
Co-authored-by: wixoa <[email protected]>
  • Loading branch information
3 people authored Oct 14, 2023
1 parent d75c0b5 commit 871f496
Show file tree
Hide file tree
Showing 19 changed files with 193 additions and 148 deletions.
50 changes: 0 additions & 50 deletions OpenDreamClient/Input/DreamCommandSystem.cs

This file was deleted.

1 change: 0 additions & 1 deletion OpenDreamClient/Interface/Controls/ControlBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ internal sealed class ControlBrowser : InterfaceControl {

[Dependency] private readonly IResourceManager _resourceManager = default!;
[Dependency] private readonly IClientNetManager _netManager = default!;
[Dependency] private readonly IDreamInterfaceManager _interfaceManager = default!;
[Dependency] private readonly IDreamResourceManager _dreamResource = default!;

private readonly ISawmill _sawmill = Logger.GetSawmill("opendream.browser");
Expand Down
3 changes: 1 addition & 2 deletions OpenDreamClient/Interface/Controls/ControlButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace OpenDreamClient.Interface.Controls;

internal sealed class ControlButton : InterfaceControl {
public const string StyleClassDMFButton = "DMFbutton";

private Button _button;

public ControlButton(ControlDescriptor controlDescriptor, ControlWindow window) : base(controlDescriptor, window) { }
Expand Down Expand Up @@ -36,7 +35,7 @@ private void OnButtonClick(BaseButton.ButtonEventArgs args) {
ControlDescriptorButton controlDescriptor = (ControlDescriptorButton)ElementDescriptor;

if (controlDescriptor.Command != null) {
EntitySystem.Get<DreamCommandSystem>().RunCommand(controlDescriptor.Command);
_interfaceManager.RunCommand(controlDescriptor.Command);
}
}
}
9 changes: 4 additions & 5 deletions OpenDreamClient/Interface/Controls/ControlChild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ internal sealed class ControlChild : InterfaceControl {
// todo: robust needs GridSplitter.
// and a non-shit grid control.

[Dependency] private readonly IDreamInterfaceManager _dreamInterface = default!;

private ControlDescriptorChild ChildDescriptor => (ControlDescriptorChild)ElementDescriptor;

Expand All @@ -26,10 +25,10 @@ protected override Control CreateUIElement() {
protected override void UpdateElementDescriptor() {
base.UpdateElementDescriptor();

var newLeftElement = ChildDescriptor.Left != null && _dreamInterface.Windows.TryGetValue(ChildDescriptor.Left, out var leftWindow)
var newLeftElement = ChildDescriptor.Left != null && _interfaceManager.Windows.TryGetValue(ChildDescriptor.Left, out var leftWindow)
? leftWindow.UIElement
: null;
var newRightElement = ChildDescriptor.Right != null && _dreamInterface.Windows.TryGetValue(ChildDescriptor.Right, out var rightWindow)
var newRightElement = ChildDescriptor.Right != null && _interfaceManager.Windows.TryGetValue(ChildDescriptor.Right, out var rightWindow)
? rightWindow.UIElement
: null;

Expand Down Expand Up @@ -74,9 +73,9 @@ protected override void UpdateElementDescriptor() {
}

public override void Shutdown() {
if (ChildDescriptor.Left != null && _dreamInterface.Windows.TryGetValue(ChildDescriptor.Left, out var left))
if (ChildDescriptor.Left != null && _interfaceManager.Windows.TryGetValue(ChildDescriptor.Left, out var left))
left.Shutdown();
if (ChildDescriptor.Right != null && _dreamInterface.Windows.TryGetValue(ChildDescriptor.Right, out var right))
if (ChildDescriptor.Right != null && _interfaceManager.Windows.TryGetValue(ChildDescriptor.Right, out var right))
right.Shutdown();
}

Expand Down
2 changes: 1 addition & 1 deletion OpenDreamClient/Interface/Controls/ControlInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public void RefreshVerbs() {

verbButton.Label.Margin = new Thickness(6, 0, 6, 2);
verbButton.OnPressed += _ => {
EntitySystem.Get<DreamCommandSystem>().RunCommand(verbId);
_dreamInterface.RunCommand(verbId);
};

_grid.Children.Add(verbButton);
Expand Down
3 changes: 1 addition & 2 deletions OpenDreamClient/Interface/Controls/ControlInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace OpenDreamClient.Interface.Controls;

internal sealed class ControlInput : InterfaceControl {
private LineEdit _textBox;

public ControlInput(ControlDescriptor controlDescriptor, ControlWindow window) : base(controlDescriptor, window) { }

protected override Control CreateUIElement() {
Expand All @@ -18,7 +17,7 @@ protected override Control CreateUIElement() {
}

private void TextBox_OnSubmit(LineEdit.LineEditEventArgs lineEditEventArgs) {
EntitySystem.Get<DreamCommandSystem>().RunCommand(_textBox.Text);
_interfaceManager.RunCommand(_textBox.Text);
_textBox.Clear();
}
}
28 changes: 26 additions & 2 deletions OpenDreamClient/Interface/Controls/ControlMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public sealed class ControlMap : InterfaceControl {
public ScalingViewport Viewport { get; private set; }

[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
[Dependency] private readonly IDreamInterfaceManager _dreamInterfaceManager = default!;
private MouseInputSystem _mouseInput;

public ControlMap(ControlDescriptor controlDescriptor, ControlWindow window) : base(controlDescriptor, window) { }
Expand All @@ -38,8 +37,19 @@ public void UpdateViewRange(ViewRange view) {
protected override Control CreateUIElement() {
Viewport = new ScalingViewport { MouseFilter = Control.MouseFilterMode.Stop };
Viewport.OnKeyBindDown += OnViewportKeyBindDown;
Viewport.OnVisibilityChanged += (args) => {
if (args.Visible) {
OnShowEvent();
} else {
OnHideEvent();
}
};
if(ControlDescriptor.IsVisible)
OnShowEvent();
else
OnHideEvent();

UpdateViewRange(_dreamInterfaceManager.View);
UpdateViewRange(_interfaceManager.View);

return new PanelContainer { StyleClasses = {"MapBackground"}, Children = { Viewport } };
}
Expand All @@ -54,4 +64,18 @@ private void OnViewportKeyBindDown(GUIBoundKeyEventArgs e) {
}
}
}

public void OnShowEvent() {
ControlDescriptorMap controlDescriptor = (ControlDescriptorMap)ControlDescriptor;
if (controlDescriptor.OnShowCommand != null) {
_interfaceManager.RunCommand(controlDescriptor.OnShowCommand);
}
}

public void OnHideEvent() {
ControlDescriptorMap controlDescriptor = (ControlDescriptorMap)ControlDescriptor;
if (controlDescriptor.OnHideCommand != null) {
_interfaceManager.RunCommand(controlDescriptor.OnHideCommand);
}
}
}
9 changes: 4 additions & 5 deletions OpenDreamClient/Interface/Controls/ControlWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace OpenDreamClient.Interface.Controls;

public sealed class ControlWindow : InterfaceControl {
[Dependency] private readonly IUserInterfaceManager _uiMgr = default!;
[Dependency] private readonly IDreamInterfaceManager _dreamInterface = default!;
[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;

private readonly ISawmill _sawmill = Logger.GetSawmill("opendream.window");
Expand All @@ -19,7 +18,7 @@ public sealed class ControlWindow : InterfaceControl {

public readonly List<InterfaceControl> ChildControls = new();

public InterfaceMacroSet Macro => _dreamInterface.MacroSets[WindowDescriptor.Macro];
public InterfaceMacroSet Macro => _interfaceManager.MacroSets[WindowDescriptor.Macro];

private WindowDescriptor WindowDescriptor => (WindowDescriptor)ElementDescriptor;

Expand All @@ -35,7 +34,7 @@ protected override void UpdateElementDescriptor() {
// Don't call base.UpdateElementDescriptor();

_menuContainer.RemoveAllChildren();
if (WindowDescriptor.Menu != null && _dreamInterface.Menus.TryGetValue(WindowDescriptor.Menu, out var menu)) {
if (WindowDescriptor.Menu != null && _interfaceManager.Menus.TryGetValue(WindowDescriptor.Menu, out var menu)) {
_menuContainer.AddChild(menu.MenuBar);
_menuContainer.Visible = true;
} else {
Expand Down Expand Up @@ -63,8 +62,8 @@ public OSWindow CreateWindow() {
window.SetHeight = window.MaxHeight;
window.Closing += _ => {
// A window can have a command set to be run when it's closed
if (WindowDescriptor.OnClose != null && _entitySystemManager.TryGetEntitySystem(out DreamCommandSystem? commandSystem)) {
commandSystem.RunCommand(WindowDescriptor.OnClose);
if (WindowDescriptor.OnClose != null) {
_interfaceManager.RunCommand(WindowDescriptor.OnClose);
}

_openWindows.Remove((window, null));
Expand Down
6 changes: 4 additions & 2 deletions OpenDreamClient/Interface/DMF/DMFParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@ private bool TryGetAttribute(out string? element, [NotNullWhen(true)] out string
Token attributeToken = Current();

if (Check(_attributeTokenTypes)) {
if (Check(TokenType.DMF_Period)) { // element.attribute=value
element = attributeToken.Text;
while(Check(TokenType.DMF_Period)) { // element.attribute=value
element ??= "";
if(element.Length > 0) element += ".";
element += attributeToken.Text;
attributeToken = Current();

if (!Check(_attributeTokenTypes)) {
Expand Down
7 changes: 3 additions & 4 deletions OpenDreamClient/Interface/DebugWindows/MacrosWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ namespace OpenDreamClient.Interface.DebugWindows;
public sealed class MacrosWindow : OSWindow {
[Dependency] private readonly IDreamInterfaceManager _interfaceManager = default!;
[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
private readonly DreamCommandSystem _commandSystem;


public MacrosWindow() {
IoCManager.InjectDependencies(this);
_commandSystem = _entitySystemManager.GetEntitySystem<DreamCommandSystem>();

Title = "Macros";
SizeToContent = WindowSizeToContent.WidthAndHeight;
Expand Down Expand Up @@ -60,13 +59,13 @@ private GridContainer CreateMacroTable(InterfaceMacroSet macroSet) {
if (value == null)
return; // Cancelled

_commandSystem.RunCommand(macro.Command.Replace("[[*]]", (string)value));
_interfaceManager.RunCommand(macro.Command.Replace("[[*]]", (string)value));
});

prompt.Owner = ClydeWindow;
prompt.Show();
} else {
_commandSystem.RunCommand(macro.Command);
_interfaceManager.RunCommand(macro.Command);
}
};

Expand Down
4 changes: 4 additions & 0 deletions OpenDreamClient/Interface/Descriptors/ControlDescriptors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ public sealed partial class ControlDescriptorInfo : ControlDescriptor {
}

public sealed partial class ControlDescriptorMap : ControlDescriptor {
[DataField("on-show")]
public string? OnShowCommand;
[DataField("on-hide")]
public string? OnHideCommand;
[DataField("zoom-mode")]
public string ZoomMode = "normal";
}
Expand Down
51 changes: 47 additions & 4 deletions OpenDreamClient/Interface/DreamInterfaceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,47 @@ public void SaveScreenshot(bool openDialog) {
});
}

public void RunCommand(string command){
switch (command) {
case string x when x.StartsWith(".quit"):
IoCManager.Resolve<IClientNetManager>().ClientDisconnect(".quit used");
break;

case string x when x.StartsWith(".screenshot"):
string[] split = command.Split(" ");
SaveScreenshot(split.Length == 1 || split[1] != "auto");
break;

case string x when x.StartsWith(".configure"):
_sawmill.Warning(".configure command is not implemented");
break;

case string x when x.StartsWith(".winset"):
// Everything after .winset, excluding the space and quotes
string winsetParams = command.Substring(7); //clip .winset
winsetParams = winsetParams.Trim(); //clip space
winsetParams = winsetParams.Trim('\"'); //clip quotes

WinSet(null, winsetParams);
break;

default: {
// Send the entire command to the server.
// It has more info about argument types so it can parse it better than we can.
_netManager.ClientSendMessage(new MsgCommand(){Command = command});
break;
}
}
}

public void StartRepeatingCommand(string command) {
_netManager.ClientSendMessage(new MsgCommandRepeatStart(){Command = command});
}

public void StopRepeatingCommand(string command) {
_netManager.ClientSendMessage(new MsgCommandRepeatStop(){Command = command});
}

public void WinSet(string? controlId, string winsetParams) {
DMFLexer lexer = new DMFLexer($"winset({controlId}, \"{winsetParams}\")", winsetParams);
DMFParser parser = new DMFParser(lexer, _serializationManager);
Expand Down Expand Up @@ -470,9 +511,7 @@ bool CheckParserErrors() {

if (elementId == null) {
if (winSet.Attribute == "command") {
DreamCommandSystem commandSystem = _entitySystemManager.GetEntitySystem<DreamCommandSystem>();

commandSystem.RunCommand(winSet.Value);
RunCommand(winSet.Value);
} else {
_sawmill.Error($"Invalid global winset \"{winsetParams}\"");
}
Expand All @@ -485,7 +524,7 @@ bool CheckParserErrors() {
if (element != null) {
element.PopulateElementDescriptor(node, _serializationManager);
} else {
_sawmill.Error($"Invalid element \"{controlId}\"");
_sawmill.Error($"Invalid element \"{elementId}\"");
}
}
}
Expand Down Expand Up @@ -701,5 +740,9 @@ public interface IDreamInterfaceManager {
InterfaceElement? FindElementWithId(string id);
void SaveScreenshot(bool openDialog);
void LoadInterfaceFromSource(string source);

void RunCommand(string command);
void StartRepeatingCommand(string command);
void StopRepeatingCommand(string command);
void WinSet(string? controlId, string winsetParams);
}
12 changes: 12 additions & 0 deletions OpenDreamClient/Interface/DummyDreamInterfaceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,16 @@ public void LoadInterfaceFromSource(string source) {
public void WinSet(string? controlId, string winsetParams) {

}

public void RunCommand(string command) {

}

public void StartRepeatingCommand(string command) {

}

public void StopRepeatingCommand(string command) {

}
}
3 changes: 2 additions & 1 deletion OpenDreamClient/Interface/InterfaceElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ public class InterfaceElement {
public string Id => ElementDescriptor.Id;

public ElementDescriptor ElementDescriptor;

[Dependency] protected readonly IDreamInterfaceManager _interfaceManager = default!;
protected InterfaceElement(ElementDescriptor elementDescriptor) {
ElementDescriptor = elementDescriptor;
IoCManager.InjectDependencies(this);
}

public void PopulateElementDescriptor(MappingDataNode node, ISerializationManager serializationManager) {
Expand Down
Loading

0 comments on commit 871f496

Please sign in to comment.