Updated the Example mod's code

This commit is contained in:
penev92 2023-10-01 22:18:32 +03:00 committed by Gustas
parent 0c53b4c876
commit 2589076a2b
2 changed files with 26 additions and 10 deletions

View File

@ -21,7 +21,7 @@ namespace OpenRA.Mods.Example.Rendering
public class ColorPickerColorShiftInfo : TraitInfo
{
[PaletteReference]
[FieldLoader.RequireAttribute]
[FieldLoader.Require]
[Desc("The name of the palette to base off.")]
public readonly string BasePalette = "";
@ -37,46 +37,57 @@ public class ColorPickerColorShiftInfo : TraitInfo
[Desc("Saturation reference for the color shift.")]
public readonly float ReferenceSaturation = 1;
[Desc("Value reference for the color shift.")]
public readonly float ReferenceValue = 0.95f;
public override object Create(ActorInitializer init) { return new ColorPickerColorShift(this); }
}
public class ColorPickerColorShift : ILoadsPalettes, ITickRender
{
readonly ColorPickerColorShiftInfo info;
readonly ColorPickerManagerInfo colorManager;
Color color;
Color preferredColor;
public ColorPickerColorShift(ColorPickerColorShiftInfo info)
{
colorManager = Game.ModData.DefaultRules.Actors[SystemActors.World].TraitInfo<ColorPickerManagerInfo>();
// All users need to use the same TraitInfo instance, chosen as the default mod rules
var colorManager = Game.ModData.DefaultRules.Actors[SystemActors.World].TraitInfo<IColorPickerManagerInfo>();
colorManager.OnColorPickerColorUpdate += c => preferredColor = c;
preferredColor = Game.Settings.Player.Color;
this.info = info;
}
void ILoadsPalettes.LoadPalettes(WorldRenderer worldRenderer)
{
color = colorManager.Color;
var (_, hue, saturation, _) = color.ToAhsv();
color = preferredColor;
var (r, g, b) = color.ToLinear();
var (hue, saturation, value) = Color.RgbToHsv(r, g, b);
worldRenderer.SetPaletteColorShift(
info.BasePalette,
hue - info.ReferenceHue,
saturation - info.ReferenceSaturation,
value / info.ReferenceValue,
info.MinHue,
info.MaxHue);
}
void ITickRender.TickRender(WorldRenderer worldRenderer, Actor self)
{
if (color == colorManager.Color)
if (color == preferredColor)
return;
color = colorManager.Color;
var (_, hue, saturation, _) = color.ToAhsv();
color = preferredColor;
var (r, g, b) = color.ToLinear();
var (hue, saturation, value) = Color.RgbToHsv(r, g, b);
worldRenderer.SetPaletteColorShift(
info.BasePalette,
hue - info.ReferenceHue,
saturation - info.ReferenceSaturation,
value / info.ReferenceValue,
info.MinHue,
info.MaxHue);
}

View File

@ -20,7 +20,7 @@ namespace OpenRA.Mods.Example.Rendering
public class PlayerColorShiftInfo : TraitInfo
{
[PaletteReference(true)]
[FieldLoader.RequireAttribute]
[FieldLoader.Require]
[Desc("The name of the palette to base off.")]
public readonly string BasePalette = "";
@ -36,6 +36,9 @@ public class PlayerColorShiftInfo : TraitInfo
[Desc("Saturation reference for the color shift.")]
public readonly float ReferenceSaturation = 1;
[Desc("Value reference for the color shift.")]
public readonly float ReferenceValue = 0.95f;
public override object Create(ActorInitializer init) { return new PlayerColorShift(this); }
}
@ -50,12 +53,14 @@ public PlayerColorShift(PlayerColorShiftInfo info)
void ILoadsPlayerPalettes.LoadPlayerPalettes(WorldRenderer worldRenderer, string playerName, Color color, bool replaceExisting)
{
var (_, hue, saturation, _) = color.ToAhsv();
var (r, g, b) = color.ToLinear();
var (hue, saturation, value) = Color.RgbToHsv(r, g, b);
worldRenderer.SetPaletteColorShift(
info.BasePalette + playerName,
hue - info.ReferenceHue,
saturation - info.ReferenceSaturation,
value - info.ReferenceValue,
info.MinHue,
info.MaxHue);
}