Files
OpenRA/OpenRA.Mods.Common/Widgets/Logic/Ingame/MenuButtonsChromeLogic.cs
RoosterDragon 83561d639d Update LangVersion to C# 9.
mono was the bottleneck restricting our ability to use a newer C# version. mono 6.12 is currently available. Although poorly documented on their website, this supports C# 9. https://www.mono-project.com/docs/about-mono/versioning/#mono-source-versioning indicates mono 6.12 uses Roslyn 3.9.0. https://github.com/dotnet/roslyn/blob/main/docs/wiki/NuGet-packages.md#versioning indicates Roslyn 3.9.0 supports C# 9.

This unlocks C# 8 and C# 9 features previously unavailable to us.
- https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history#c-version-80
- https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history#c-version-9

A newer version of StyleCop is required to avoid rules tripping up on the new syntax. Currently only prerelease versions are available but their use is encouraged https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3420#issuecomment-994899135

Fix style rule violations on existing rules where the newer language version makes some existing casts redundant or allows use of the null coalescing assignment operator.
2023-04-05 15:27:41 +03:00

125 lines
3.5 KiB
C#

#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.Linq;
using OpenRA.Mods.Common.Traits;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic
{
public class MenuButtonsChromeLogic : ChromeLogic
{
readonly World world;
readonly Widget worldRoot;
readonly Widget menuRoot;
bool disableSystemButtons;
Widget currentWidget;
[ObjectCreator.UseCtor]
public MenuButtonsChromeLogic(Widget widget, World world)
{
this.world = world;
worldRoot = Ui.Root.Get("WORLD_ROOT");
menuRoot = Ui.Root.Get("MENU_ROOT");
// System buttons
var options = widget.GetOrNull<MenuButtonWidget>("OPTIONS_BUTTON");
if (options != null)
{
var blinking = false;
var lp = world.LocalPlayer;
options.IsDisabled = () => disableSystemButtons;
options.OnClick = () =>
{
blinking = false;
OpenMenuPanel(options, new WidgetArgs()
{
{ "initialPanel", IngameInfoPanel.AutoSelect }
});
};
options.IsHighlighted = () => blinking && Game.LocalTick % 50 < 25;
if (lp != null)
{
void StartBlinking(Player player, bool inhibitAnnouncement)
{
if (!inhibitAnnouncement && player == world.LocalPlayer)
blinking = true;
}
var mo = lp.PlayerActor.TraitOrDefault<MissionObjectives>();
if (mo != null)
mo.ObjectiveAdded += StartBlinking;
}
}
var debug = widget.GetOrNull<MenuButtonWidget>("DEBUG_BUTTON");
if (debug != null)
{
// Can't use DeveloperMode.Enabled because there is a hardcoded hack to *always*
// enable developer mode for singleplayer games, but we only want to show the button
// if it has been explicitly enabled
var def = world.Map.Rules.Actors[SystemActors.Player].TraitInfo<DeveloperModeInfo>().CheckboxEnabled;
var enabled = world.LobbyInfo.GlobalSettings.OptionOrDefault("cheats", def);
debug.IsVisible = () => enabled;
debug.IsDisabled = () => disableSystemButtons;
debug.OnClick = () => OpenMenuPanel(debug, new WidgetArgs()
{
{ "initialPanel", IngameInfoPanel.Debug }
});
}
}
void OpenMenuPanel(MenuButtonWidget button, WidgetArgs widgetArgs = null)
{
disableSystemButtons = true;
var cachedPause = world.PredictedPaused;
if (button.HideIngameUI)
{
// Cancel custom input modes (guard, building placement, etc)
world.CancelInputMode();
worldRoot.IsVisible = () => false;
}
if (button.Pause && world.LobbyInfo.NonBotClients.Count() == 1)
world.SetPauseState(true);
var cachedDisableWorldSounds = Game.Sound.DisableWorldSounds;
if (button.DisableWorldSounds)
Game.Sound.DisableWorldSounds = true;
widgetArgs ??= new WidgetArgs();
widgetArgs.Add("onExit", () =>
{
if (button.HideIngameUI)
worldRoot.IsVisible = () => true;
if (button.DisableWorldSounds)
Game.Sound.DisableWorldSounds = cachedDisableWorldSounds;
if (button.Pause && world.LobbyInfo.NonBotClients.Count() == 1)
world.SetPauseState(cachedPause);
menuRoot.RemoveChild(currentWidget);
disableSystemButtons = false;
});
currentWidget = Game.LoadWidget(world, button.MenuContainer, menuRoot, widgetArgs);
Game.RunAfterTick(Ui.ResetTooltips);
}
}
}