Updated the Example mod's code
This commit is contained in:
parent
0c53b4c876
commit
2589076a2b
@ -21,7 +21,7 @@ namespace OpenRA.Mods.Example.Rendering
|
|||||||
public class ColorPickerColorShiftInfo : TraitInfo
|
public class ColorPickerColorShiftInfo : TraitInfo
|
||||||
{
|
{
|
||||||
[PaletteReference]
|
[PaletteReference]
|
||||||
[FieldLoader.RequireAttribute]
|
[FieldLoader.Require]
|
||||||
[Desc("The name of the palette to base off.")]
|
[Desc("The name of the palette to base off.")]
|
||||||
public readonly string BasePalette = "";
|
public readonly string BasePalette = "";
|
||||||
|
|
||||||
@ -37,46 +37,57 @@ public class ColorPickerColorShiftInfo : TraitInfo
|
|||||||
[Desc("Saturation reference for the color shift.")]
|
[Desc("Saturation reference for the color shift.")]
|
||||||
public readonly float ReferenceSaturation = 1;
|
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 override object Create(ActorInitializer init) { return new ColorPickerColorShift(this); }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ColorPickerColorShift : ILoadsPalettes, ITickRender
|
public class ColorPickerColorShift : ILoadsPalettes, ITickRender
|
||||||
{
|
{
|
||||||
readonly ColorPickerColorShiftInfo info;
|
readonly ColorPickerColorShiftInfo info;
|
||||||
readonly ColorPickerManagerInfo colorManager;
|
|
||||||
Color color;
|
Color color;
|
||||||
|
Color preferredColor;
|
||||||
|
|
||||||
public ColorPickerColorShift(ColorPickerColorShiftInfo info)
|
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;
|
this.info = info;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ILoadsPalettes.LoadPalettes(WorldRenderer worldRenderer)
|
void ILoadsPalettes.LoadPalettes(WorldRenderer worldRenderer)
|
||||||
{
|
{
|
||||||
color = colorManager.Color;
|
color = preferredColor;
|
||||||
var (_, hue, saturation, _) = color.ToAhsv();
|
var (r, g, b) = color.ToLinear();
|
||||||
|
var (hue, saturation, value) = Color.RgbToHsv(r, g, b);
|
||||||
|
|
||||||
worldRenderer.SetPaletteColorShift(
|
worldRenderer.SetPaletteColorShift(
|
||||||
info.BasePalette,
|
info.BasePalette,
|
||||||
hue - info.ReferenceHue,
|
hue - info.ReferenceHue,
|
||||||
saturation - info.ReferenceSaturation,
|
saturation - info.ReferenceSaturation,
|
||||||
|
value / info.ReferenceValue,
|
||||||
info.MinHue,
|
info.MinHue,
|
||||||
info.MaxHue);
|
info.MaxHue);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ITickRender.TickRender(WorldRenderer worldRenderer, Actor self)
|
void ITickRender.TickRender(WorldRenderer worldRenderer, Actor self)
|
||||||
{
|
{
|
||||||
if (color == colorManager.Color)
|
if (color == preferredColor)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
color = colorManager.Color;
|
color = preferredColor;
|
||||||
var (_, hue, saturation, _) = color.ToAhsv();
|
var (r, g, b) = color.ToLinear();
|
||||||
|
var (hue, saturation, value) = Color.RgbToHsv(r, g, b);
|
||||||
|
|
||||||
worldRenderer.SetPaletteColorShift(
|
worldRenderer.SetPaletteColorShift(
|
||||||
info.BasePalette,
|
info.BasePalette,
|
||||||
hue - info.ReferenceHue,
|
hue - info.ReferenceHue,
|
||||||
saturation - info.ReferenceSaturation,
|
saturation - info.ReferenceSaturation,
|
||||||
|
value / info.ReferenceValue,
|
||||||
info.MinHue,
|
info.MinHue,
|
||||||
info.MaxHue);
|
info.MaxHue);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,7 +20,7 @@ namespace OpenRA.Mods.Example.Rendering
|
|||||||
public class PlayerColorShiftInfo : TraitInfo
|
public class PlayerColorShiftInfo : TraitInfo
|
||||||
{
|
{
|
||||||
[PaletteReference(true)]
|
[PaletteReference(true)]
|
||||||
[FieldLoader.RequireAttribute]
|
[FieldLoader.Require]
|
||||||
[Desc("The name of the palette to base off.")]
|
[Desc("The name of the palette to base off.")]
|
||||||
public readonly string BasePalette = "";
|
public readonly string BasePalette = "";
|
||||||
|
|
||||||
@ -36,6 +36,9 @@ public class PlayerColorShiftInfo : TraitInfo
|
|||||||
[Desc("Saturation reference for the color shift.")]
|
[Desc("Saturation reference for the color shift.")]
|
||||||
public readonly float ReferenceSaturation = 1;
|
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); }
|
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)
|
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(
|
worldRenderer.SetPaletteColorShift(
|
||||||
info.BasePalette + playerName,
|
info.BasePalette + playerName,
|
||||||
hue - info.ReferenceHue,
|
hue - info.ReferenceHue,
|
||||||
saturation - info.ReferenceSaturation,
|
saturation - info.ReferenceSaturation,
|
||||||
|
value - info.ReferenceValue,
|
||||||
info.MinHue,
|
info.MinHue,
|
||||||
info.MaxHue);
|
info.MaxHue);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user