Move ColorValidator logic into a new ColorPickerManager trait.

This commit is contained in:
Paul Chote
2021-04-11 21:27:51 +01:00
committed by teinarss
parent 52577c1de9
commit 7b58f03f1c
11 changed files with 23 additions and 23 deletions

View File

@@ -385,11 +385,11 @@ namespace OpenRA.Mods.Common.Server
};
// Pick a random color for the bot
var validator = server.ModData.Manifest.Get<ColorValidator>();
var colorManager = server.ModData.DefaultRules.Actors[SystemActors.World].TraitInfo<ColorPickerManagerInfo>();
var terrainColors = server.ModData.DefaultTerrainInfo[server.Map.TileSet].RestrictedPlayerColors;
var playerColors = server.LobbyInfo.Clients.Select(c => c.Color)
.Concat(server.Map.Players.Players.Values.Select(p => p.Color));
bot.Color = bot.PreferredColor = validator.RandomPresetColor(server.Random, terrainColors, playerColors);
bot.Color = bot.PreferredColor = colorManager.RandomPresetColor(server.Random, terrainColors, playerColors);
server.LobbyInfo.Clients.Add(bot);
}
@@ -1089,7 +1089,7 @@ namespace OpenRA.Mods.Common.Server
{
lock (server.LobbyInfo)
{
var validator = server.ModData.Manifest.Get<ColorValidator>();
var colorManager = server.ModData.DefaultRules.Actors[SystemActors.World].TraitInfo<ColorPickerManagerInfo>();
var askColor = askedColor;
Action<string> onError = message =>
@@ -1102,7 +1102,7 @@ namespace OpenRA.Mods.Common.Server
var playerColors = server.LobbyInfo.Clients.Where(c => c.Index != playerIndex).Select(c => c.Color)
.Concat(server.Map.Players.Players.Values.Select(p => p.Color)).ToList();
return validator.MakeValid(askColor, server.Random, terrainColors, playerColors, onError);
return colorManager.MakeValid(askColor, server.Random, terrainColors, playerColors, onError);
}
}

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information
/*
* Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
* Copyright 2007-2021 The OpenRA Developers (see AUTHORS)
* 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
@@ -14,10 +14,12 @@ using System.Collections.Generic;
using System.Linq;
using OpenRA.Primitives;
using OpenRA.Support;
using OpenRA.Traits;
namespace OpenRA.Mods.Common
namespace OpenRA.Mods.Common.Traits
{
public class ColorValidator : IGlobalModData
[Desc("Configuration options for the lobby player color picker. Attach this to the world actor.")]
public class ColorPickerManagerInfo : TraitInfo<ColorPickerManager>
{
// The bigger the color threshold, the less permissive is the algorithm
public readonly int Threshold = 0x50;
@@ -182,4 +184,6 @@ namespace OpenRA.Mods.Common
return color;
}
}
public class ColorPickerManager { }
}

View File

@@ -65,8 +65,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic
}
// Set the initial state
var validator = modData.Manifest.Get<ColorValidator>();
mixer.SetPaletteRange(validator.HsvSaturationRange[0], validator.HsvSaturationRange[1], validator.HsvValueRange[0], validator.HsvValueRange[1]);
var colorManager = world.WorldActor.Info.TraitInfo<ColorPickerManagerInfo>();
mixer.SetPaletteRange(colorManager.HsvSaturationRange[0], colorManager.HsvSaturationRange[1], colorManager.HsvValueRange[0], colorManager.HsvValueRange[1]);
mixer.Set(initialColor);
hueSlider.Value = HueFromColor(initialColor);
@@ -116,10 +116,10 @@ namespace OpenRA.Mods.Common.Widgets.Logic
for (var i = 0; i < paletteCols; i++)
{
var colorIndex = j * paletteCols + i;
if (colorIndex >= validator.TeamColorPresets.Length)
if (colorIndex >= colorManager.TeamColorPresets.Length)
break;
var color = validator.TeamColorPresets[colorIndex];
var color = colorManager.TeamColorPresets[colorIndex];
var newSwatch = (ColorBlockWidget)presetColorTemplate.Clone();
newSwatch.GetColor = () => color;