Use covariant return types.

Use the covariant return type feature of C# 9 to allow method overrides to be more specific about their return type. By exposing this information, e.g. for Widget.Clone(), callers will have more information. This allows various casts to be removed as the information is now available within the type system.
This commit is contained in:
RoosterDragon
2024-09-21 12:08:15 +01:00
committed by Gustas
parent 9c568aba33
commit aa121dcb2f
54 changed files with 60 additions and 62 deletions

View File

@@ -744,7 +744,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var allAssetTypes = new[] { AssetType.Sprite, AssetType.Model, AssetType.Audio, AssetType.Video, AssetType.Unknown };
foreach (var type in allAssetTypes)
{
var assetType = (CheckboxWidget)assetTypeTemplate.Clone();
var assetType = assetTypeTemplate.Clone();
var text = type.ToString();
assetType.GetText = () => text;
assetType.IsChecked = () => assetTypesToDisplay.HasFlag(type);

View File

@@ -53,7 +53,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
foreach (var line in desc.Split('\n', StringSplitOptions.None))
{
descWidth = Math.Max(descWidth, descFont.Measure(line).X);
var lineLabel = (LabelWidget)descTemplate.Clone();
var lineLabel = descTemplate.Clone();
lineLabel.GetText = () => line;
lineLabel.Bounds.Y = descOffset;
widget.AddChild(lineLabel);

View File

@@ -115,7 +115,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var color = presetColors[colorIndex];
var newSwatch = (ColorBlockWidget)presetColorTemplate.Clone();
var newSwatch = presetColorTemplate.Clone();
newSwatch.GetColor = () => color;
newSwatch.IsVisible = () => true;
newSwatch.Bounds.X = i * newSwatch.Bounds.Width;
@@ -136,7 +136,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{
var colorIndex = j * paletteCols + i;
var newSwatch = (ColorBlockWidget)customColorTemplate.Clone();
var newSwatch = customColorTemplate.Clone();
var getColor = new CachedTransform<Color, Color>(c => colorManager.MakeValid(c, world.LocalRandom, Array.Empty<Color>(), Array.Empty<Color>()));
newSwatch.GetColor = () => getColor.Update(Game.Settings.Player.CustomColors[colorIndex]);

View File

@@ -83,7 +83,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var font = Game.Renderer.Fonts[template.Font];
var lines = modCredits ? modLines : engineLines;
var label = (LabelWidget)template.Clone();
var label = template.Clone();
label.GetText = () => lines;
label.IncreaseHeightToFitCurrentText();
scrollPanel.AddChild(label);

View File

@@ -145,7 +145,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var categoryHeight = 5 + selectButtons.Bounds.Height;
foreach (var cat in FilteredCategories)
{
var category = (CheckboxWidget)categoryTemplate.Clone();
var category = categoryTemplate.Clone();
category.GetText = () => cat;
category.IsChecked = () => SelectedCategories.Contains(cat);
category.IsVisible = () => true;

View File

@@ -101,7 +101,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
MapOverlays[] allCategories = { MapOverlays.Grid, MapOverlays.Buildable, MapOverlays.Marker };
foreach (var cat in allCategories)
{
var category = (CheckboxWidget)categoryTemplate.Clone();
var category = categoryTemplate.Clone();
category.GetText = () => cat.ToString();
category.IsVisible = () => true;

View File

@@ -97,7 +97,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
if (visibilityOption == MapVisibility.Shellmap && !map.Visibility.HasFlag(visibilityOption))
continue;
var checkbox = (CheckboxWidget)visOptionTemplate.Clone();
var checkbox = visOptionTemplate.Clone();
checkbox.GetText = () => visibilityOption.ToString();
checkbox.IsChecked = () => map.Visibility.HasFlag(visibilityOption);
checkbox.OnClick = () => map.Visibility ^= visibilityOption;

View File

@@ -235,7 +235,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
// Create the item manually so the click handlers can refer to itself
// This simplifies the rename handling (only needs to update ItemKey)
var item = gameTemplate.Clone() as ScrollItemWidget;
var item = gameTemplate.Clone();
item.ItemKey = savePath;
item.IsVisible = () => true;
item.IsSelected = () => selectedSave == item.ItemKey;

View File

@@ -295,7 +295,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
ButtonWidget AddButton(string id, string label)
{
var button = buttonTemplate.Clone() as ButtonWidget;
var button = buttonTemplate.Clone();
var lastButton = buttons.LastOrDefault();
if (lastButton != null)
{

View File

@@ -338,7 +338,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
listPanel.RemoveChildren();
foreach (var package in availablePackages)
{
var containerWidget = (ContainerWidget)checkboxListTemplate.Clone();
var containerWidget = checkboxListTemplate.Clone();
var checkboxWidget = containerWidget.Get<CheckboxWidget>("PACKAGE_CHECKBOX");
var title = FluentProvider.GetMessage(package.Title);
checkboxWidget.GetText = () => title;
@@ -385,7 +385,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
foreach (var i in kv.Value)
{
var item = i;
var labelWidget = (LabelWidget)labelListTemplate.Clone();
var labelWidget = labelListTemplate.Clone();
labelWidget.GetText = () => item;
listPanel.AddChild(labelWidget);
}

View File

@@ -33,7 +33,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var bottomMargin = sources.Bounds.Height;
foreach (var source in sourceTitles)
{
var label = (LabelWidget)template.Clone();
var label = template.Clone();
var title = source;
label.GetText = () => title;
label.Bounds.Y = sources.Bounds.Height;

View File

@@ -86,7 +86,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
foreach (var panel in panels)
{
var container = panelTemplate.Clone() as ContainerWidget;
var container = panelTemplate.Clone();
container.Id = panel.Key;
panelContainer.AddChild(container);
@@ -167,7 +167,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
ButtonWidget AddSettingsTab(string id, string label)
{
var tab = tabTemplate.Clone() as ButtonWidget;
var tab = tabTemplate.Clone();
var lastButton = buttons.LastOrDefault();
if (lastButton != null)
{

View File

@@ -43,7 +43,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var bottom = 0;
for (var i = 0; i < lines.Length; i++)
{
var line = (LabelWidget)label.Clone();
var line = label.Clone();
var lineText = lines[i];
line.Bounds.Y += spacing.Bounds.Y + i * spacing.Bounds.Height;
line.Bounds.Width = textWidth;

View File

@@ -68,7 +68,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
foreach (var (name, value) in GetSystemInformation().Values)
{
var label = template.Clone() as LabelWidget;
var label = template.Clone();
var text = name + ": " + value;
label.GetText = () => text;
sysInfoData.AddChild(label);