Implement state prediction for lobby checkboxes.

This commit is contained in:
Paul Chote
2022-12-21 09:55:23 +13:00
committed by Gustas
parent e2e541a251
commit 2ba52f1b94
2 changed files with 49 additions and 6 deletions

View File

@@ -101,18 +101,25 @@ namespace OpenRA.Mods.Common.Widgets.Logic
} }
var checkbox = checkboxColumns.Dequeue(); var checkbox = checkboxColumns.Dequeue();
var optionValue = new CachedTransform<Session.Global, Session.LobbyOptionState>( var optionEnabled = new PredictedCachedTransform<Session.Global, bool>(
gs => gs.LobbyOptions[option.Id]); gs => gs.LobbyOptions[option.Id].IsEnabled);
var optionLocked = new CachedTransform<Session.Global, bool>(
gs => gs.LobbyOptions[option.Id].IsLocked);
checkbox.GetText = () => option.Name; checkbox.GetText = () => option.Name;
if (option.Description != null) if (option.Description != null)
checkbox.GetTooltipText = () => option.Description; checkbox.GetTooltipText = () => option.Description;
checkbox.IsVisible = () => true; checkbox.IsVisible = () => true;
checkbox.IsChecked = () => optionValue.Update(orderManager.LobbyInfo.GlobalSettings).IsEnabled; checkbox.IsChecked = () => optionEnabled.Update(orderManager.LobbyInfo.GlobalSettings);
checkbox.IsDisabled = () => configurationDisabled() || optionValue.Update(orderManager.LobbyInfo.GlobalSettings).IsLocked; checkbox.IsDisabled = () => configurationDisabled() || optionLocked.Update(orderManager.LobbyInfo.GlobalSettings);
checkbox.OnClick = () => orderManager.IssueOrder(Order.Command( checkbox.OnClick = () =>
$"option {option.Id} {!optionValue.Update(orderManager.LobbyInfo.GlobalSettings).IsEnabled}")); {
var state = !optionEnabled.Update(orderManager.LobbyInfo.GlobalSettings);
orderManager.IssueOrder(Order.Command($"option {option.Id} {state}"));
optionEnabled.Predict(state);
};
} }
foreach (var option in allOptions.Where(o => !(o is LobbyBooleanOption))) foreach (var option in allOptions.Where(o => !(o is LobbyBooleanOption)))

View File

@@ -419,4 +419,40 @@ namespace OpenRA.Mods.Common.Widgets
return lastOutput; return lastOutput;
} }
} }
public class PredictedCachedTransform<T, U>
{
readonly Func<T, U> transform;
bool initialized;
T lastInput;
U lastOutput;
bool predicted;
U prediction;
public PredictedCachedTransform(Func<T, U> transform)
{
this.transform = transform;
}
public void Predict(U value)
{
predicted = true;
prediction = value;
}
public U Update(T input)
{
if ((predicted || initialized) && ((input == null && lastInput == null) || (input != null && input.Equals(lastInput))))
return predicted ? prediction : lastOutput;
predicted = false;
initialized = true;
lastInput = input;
lastOutput = transform(input);
return lastOutput;
}
}
} }