Skip to content

Commit

Permalink
Samples: Allow NetPeerSettingsWindow to toggle warning/error messages.
Browse files Browse the repository at this point in the history
Need this for testing.
  • Loading branch information
PJB3005 committed May 3, 2024
1 parent afd0998 commit 32820c3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 59 deletions.
2 changes: 2 additions & 0 deletions Samples/SamplesCommon.Core/NetPeerSettingsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
<Border BorderThickness="1" Grid.Column="0" Grid.Row="0">
<StackPanel Margin="2">
<TextBlock FontSize="16" Text="Settings" />
<CheckBox Name="ErrorCheckBox" Content="Display Error messages" />
<CheckBox Name="WarningCheckBox" Content="Display Warning messages" />
<CheckBox Name="DebugCheckBox" Content="Display Debug messages" />
<CheckBox Name="VerboseCheckBox" Content="Display Verbose messages" />
<StackPanel Orientation="Horizontal">
Expand Down
90 changes: 31 additions & 59 deletions Samples/SamplesCommon.Core/NetPeerSettingsWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
using System;
using System.Text;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Threading;
using Lidgren.Network;
using ReactiveUI;

namespace SamplesCommon.Core
{
public class NetPeerSettingsWindow : Window
public partial class NetPeerSettingsWindow : Window
{
private readonly NetPeer _peer;
private readonly DispatcherTimer _timer;

private readonly CheckBox _debugCheckBox;
private readonly CheckBox _verboseCheckBox;
private readonly TextBox _minLagBox;
private readonly Button _closeButton;
private readonly TextBox _pingBox;
private readonly Button _saveButton;
private readonly TextBlock _statistics;
private readonly TextBlock _duplicatesDisplay;
private readonly TextBox _duplicatesBox;
private readonly TextBlock _lossDisplay;
private readonly TextBox _lossBox;
private readonly TextBlock _delayDisplay;
private readonly TextBox _maxLagBox;

public NetPeerSettingsWindow()
{
}
Expand All @@ -50,7 +35,7 @@ private void UpdateStatistics(object sender, EventArgs e)
bdr.Append(conn.Statistics.ToString());
}

_statistics.Text = bdr.ToString();
Statistics.Text = bdr.ToString();
}

public NetPeerSettingsWindow(NetPeer peer)
Expand All @@ -62,22 +47,8 @@ public NetPeerSettingsWindow(NetPeer peer)
(sender, e) => UpdateStatistics());
_timer.Start();

_debugCheckBox = this.FindControl<CheckBox>("DebugCheckBox");
_verboseCheckBox = this.FindControl<CheckBox>("VerboseCheckBox");
_minLagBox = this.FindControl<TextBox>("MinLag");
_maxLagBox = this.FindControl<TextBox>("MaxLag");
_delayDisplay = this.FindControl<TextBlock>("DelayDisplay");
_lossBox = this.FindControl<TextBox>("LossBox");
_lossDisplay = this.FindControl<TextBlock>("LossPercent");
_duplicatesBox = this.FindControl<TextBox>("DuplicatesBox");
_duplicatesDisplay = this.FindControl<TextBlock>("DuplicatesPercent");
_statistics = this.FindControl<TextBlock>("Statistics");
_closeButton = this.FindControl<Button>("CloseButton");
_saveButton = this.FindControl<Button>("SaveButton");
_pingBox = this.FindControl<TextBox>("PingBox");

_saveButton.Command = ReactiveCommand.Create(SaveButtonPressed);
_closeButton.Command = ReactiveCommand.Create(CloseButtonPressed);
SaveButton.Command = ReactiveCommand.Create(SaveButtonPressed);
CloseButton.Command = ReactiveCommand.Create(CloseButtonPressed);

UpdateLabelsAndBoxes();
}
Expand All @@ -94,7 +65,7 @@ private void UpdateStatistics()
bdr.Append(conn.Statistics);
}

_statistics.Text = bdr.ToString();
Statistics.Text = bdr.ToString();
}

private void CloseButtonPressed()
Expand All @@ -111,20 +82,24 @@ private void SaveButtonPressed()

private void Save()
{
_peer.Configuration.SetMessageTypeEnabled(NetIncomingMessageType.DebugMessage,
_debugCheckBox.IsChecked.Value);
_peer.Configuration.SetMessageTypeEnabled(NetIncomingMessageType.ErrorMessage,
ErrorCheckBox.IsChecked.Value);
_peer.Configuration.SetMessageTypeEnabled(NetIncomingMessageType.WarningMessage,
WarningCheckBox.IsChecked.Value);
_peer.Configuration.SetMessageTypeEnabled(NetIncomingMessageType.DebugMessage,
DebugCheckBox.IsChecked.Value);
_peer.Configuration.SetMessageTypeEnabled(NetIncomingMessageType.VerboseDebugMessage,
_verboseCheckBox.IsChecked.Value);
VerboseCheckBox.IsChecked.Value);
#if DEBUG
if (float.TryParse(_lossBox.Text, out var f))
if (float.TryParse(LossBox.Text, out var f))
_peer.Configuration.SimulatedLoss = f / 100f;
if (float.TryParse(_duplicatesBox.Text, out f))
if (float.TryParse(DuplicatesBox.Text, out f))
_peer.Configuration.SimulatedDuplicatesChance = f / 100f;
if (float.TryParse(_minLagBox.Text, out f))
if (float.TryParse(MinLag.Text, out f))
_peer.Configuration.SimulatedMinimumLatency = f / 1000f;
if (float.TryParse(_pingBox.Text, out f))
if (float.TryParse(PingBox.Text, out f))
_peer.Configuration.PingInterval = f / 1000f;
if (float.TryParse(_maxLagBox.Text, out var max))
if (float.TryParse(MaxLag.Text, out var max))
{
max /= 1000f;
var r = max - _peer.Configuration.SimulatedMinimumLatency;
Expand All @@ -133,7 +108,7 @@ private void Save()
_peer.Configuration.SimulatedRandomLatency = r;
var nm = _peer.Configuration.SimulatedMinimumLatency +
_peer.Configuration.SimulatedRandomLatency;
_maxLagBox.Text = ((int) (max * 1000)).ToString();
MaxLag.Text = ((int) (max * 1000)).ToString();
}
}
#endif
Expand All @@ -145,32 +120,29 @@ private void UpdateLabelsAndBoxes()

#if DEBUG
var loss = (pc.SimulatedLoss * 100.0f).ToString();
_lossDisplay.Text = $"{loss} %";
_lossBox.Text = loss;
LossPercent.Text = $"{loss} %";
LossBox.Text = loss;

var dupes = (pc.SimulatedDuplicatesChance * 100.0f).ToString();
_duplicatesDisplay.Text = $"{dupes} %";
_duplicatesBox.Text = dupes;
DuplicatesPercent.Text = $"{dupes} %";
DuplicatesBox.Text = dupes;

var minLat = (pc.SimulatedMinimumLatency * 1000.0f).ToString();
var maxLat = ((pc.SimulatedMinimumLatency + pc.SimulatedRandomLatency) * 1000.0f).ToString();
#else
var minLat = "";
var maxLat = "";
#endif
_delayDisplay.Text = $"{minLat} to {maxLat} ms";
_minLagBox.Text = minLat;
_minLagBox.Text = maxLat;

_debugCheckBox.IsChecked = _peer.Configuration.IsMessageTypeEnabled(NetIncomingMessageType.DebugMessage);
_verboseCheckBox.IsChecked =
DelayDisplay.Text = $"{minLat} to {maxLat} ms";
MinLag.Text = minLat;
MaxLag.Text = maxLat;

ErrorCheckBox.IsChecked = _peer.Configuration.IsMessageTypeEnabled(NetIncomingMessageType.ErrorMessage);
WarningCheckBox.IsChecked = _peer.Configuration.IsMessageTypeEnabled(NetIncomingMessageType.WarningMessage);
DebugCheckBox.IsChecked = _peer.Configuration.IsMessageTypeEnabled(NetIncomingMessageType.DebugMessage);
VerboseCheckBox.IsChecked =
_peer.Configuration.IsMessageTypeEnabled(NetIncomingMessageType.VerboseDebugMessage);
_pingBox.Text = (_peer.Configuration.PingInterval * 1000).ToString();
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
PingBox.Text = (_peer.Configuration.PingInterval * 1000).ToString();
}
}
}
1 change: 1 addition & 0 deletions Samples/SamplesCommon.Core/SamplesCommon.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<SubType>Designer</SubType>
</AvaloniaResource>
<AvaloniaResource Include="Assets\**" />
<AdditionalFiles Include="**\*.xaml" SourceItemGroup="AvaloniaXaml" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 32820c3

Please sign in to comment.