Resurrected old colour picker.

This commit is contained in:
darkademic
2023-03-10 16:55:55 +00:00
committed by Pavel Penev
parent dcac966d49
commit 265f915442
13 changed files with 264 additions and 89 deletions

View File

@@ -27,7 +27,7 @@ namespace OpenRA.Mods.Common.Widgets
public float H { get; private set; }
public float S { get; private set; }
public float V { get; private set; }
float minSat, maxSat;
float minSat, maxSat, minVal, maxVal;
Sheet mixerSheet;
Sprite mixerSprite;
@@ -37,7 +37,6 @@ namespace OpenRA.Mods.Common.Widgets
public ColorMixerWidget(ModData modData)
{
modRules = modData.DefaultRules;
V = 1.0f;
}
public ColorMixerWidget(ColorMixerWidget other)
@@ -51,13 +50,19 @@ namespace OpenRA.Mods.Common.Widgets
V = other.V;
minSat = other.minSat;
maxSat = other.maxSat;
minVal = other.minVal;
maxVal = other.maxVal;
}
public void SetColorLimits(float minSaturation, float maxSaturation, float v)
public void SetColorLimits(float minSaturation, float maxSaturation, float minValue, float maxValue, float? newHue = null)
{
minSat = minSaturation;
maxSat = maxSaturation;
V = v;
minVal = minValue;
maxVal = maxValue;
if (newHue == null)
newHue = H;
var buffer = new byte[4 * 256 * 256];
unsafe
@@ -66,19 +71,25 @@ namespace OpenRA.Mods.Common.Widgets
fixed (byte* cc = &buffer[0])
{
var c = (int*)cc;
for (var s = 0; s < 256; s++)
for (var h = 0; h < 256; h++)
for (var v = 0; v < 256; v++)
{
for (var s = 0; s < 256; s++)
{
#pragma warning disable IDE0047
(*(c + s * 256 + h)) = Color.FromAhsv(h / 255f, 1 - s / 255f, V).ToArgb();
(*(c + s * 256 + v)) = Color.FromAhsv(newHue.Value, 1 - s / 255f, v / 255f).ToArgb();
#pragma warning restore IDE0047
}
}
}
}
var rect = new Rectangle(0, (int)(255 * (1 - maxSat)), 255, (int)(255 * (maxSat - minSat)) + 1);
mixerSprite = new Sprite(mixerSheet, rect, TextureChannel.RGBA);
var rect = new Rectangle(
(int)(255 * minVal),
(int)(255 * (1 - maxSat)),
(int)(255 * (maxVal - minVal)),
(int)(255 * (maxSat - minSat)) + 1);
mixerSprite = new Sprite(mixerSheet, rect, TextureChannel.RGBA);
mixerSheet.GetTexture().SetData(buffer, 256, 256);
}
@@ -87,7 +98,7 @@ namespace OpenRA.Mods.Common.Widgets
base.Initialize(args);
mixerSheet = new Sheet(SheetType.BGRA, new Size(256, 256));
SetColorLimits(minSat, maxSat, V);
SetColorLimits(minSat, maxSat, minVal, maxVal);
}
public override void Draw()
@@ -103,17 +114,17 @@ namespace OpenRA.Mods.Common.Widgets
void SetValueFromPx(int2 xy)
{
var rb = RenderBounds;
var h = xy.X * 1f / rb.Width;
var v = float2.Lerp(minVal, maxVal, xy.X * 1f / rb.Width);
var s = float2.Lerp(minSat, maxSat, 1 - xy.Y * 1f / rb.Height);
H = h.Clamp(0, 1f);
V = v.Clamp(minVal, maxVal);
S = s.Clamp(minSat, maxSat);
}
int2 PxFromValue()
{
var rb = RenderBounds;
var x = RenderBounds.Width * H;
var y = RenderBounds.Height * (1 - (S - minSat) / (maxSat - minSat));
var x = rb.Width * (V - minVal) / (maxVal - minVal);
var y = rb.Height * (1 - (S - minSat) / (maxSat - minSat));
return new int2((int)x.Clamp(0, rb.Width), (int)y.Clamp(0, rb.Height));
}
@@ -162,12 +173,13 @@ namespace OpenRA.Mods.Common.Widgets
/// </summary>
public void Set(Color color)
{
var (_, h, s, _) = color.ToAhsv();
var (_, h, s, v) = color.ToAhsv();
if (H != h || S != s)
if (H != h || S != s || V != v)
{
H = h;
S = s.Clamp(minSat, maxSat);
V = v.Clamp(minVal, maxVal);
OnChange();
}
}

View File

@@ -0,0 +1,69 @@
#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 OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets
{
public class HueSliderWidget : SliderWidget
{
Sprite hueSprite;
Sprite pickerSprite;
public HueSliderWidget() { }
public HueSliderWidget(HueSliderWidget other)
: base(other) { }
public override void Initialize(WidgetArgs args)
{
base.Initialize(args);
var hueSheet = new Sheet(SheetType.BGRA, new Size(256, 1));
var buffer = new byte[4 * 256];
unsafe
{
fixed (byte* cc = &buffer[0])
{
var c = (int*)cc;
for (var h = 0; h < 256; h++)
{
#pragma warning disable IDE0047
(*(c + 0 * 256 + h)) = Color.FromAhsv(h / 255f, 1, 1).ToArgb();
#pragma warning restore IDE0047
}
}
}
var rect = new Rectangle(0, 0, 256, 1);
hueSprite = new Sprite(hueSheet, new Rectangle(0, 0, 256, 1), TextureChannel.RGBA);
hueSheet.GetTexture().SetData(buffer, 256, 1);
pickerSprite = ChromeProvider.GetImage("lobby-bits", "huepicker");
}
public override void Draw()
{
if (!IsVisible())
return;
var ro = RenderOrigin;
var rb = RenderBounds;
WidgetUtils.DrawSprite(hueSprite, ro, rb.Size);
var pos = RenderOrigin + new int2(PxFromValue(Value).Clamp(0, rb.Width - 1) - (int)pickerSprite.Size.X / 2, (rb.Height - (int)pickerSprite.Size.Y) / 2);
WidgetUtils.DrawSprite(pickerSprite, pos);
}
}
}

View File

@@ -29,14 +29,25 @@ namespace OpenRA.Mods.Common.Widgets.Logic
Dictionary<string, MiniYaml> logicArgs)
{
var mixer = widget.Get<ColorMixerWidget>("MIXER");
var hueSlider = widget.Get<HueSliderWidget>("HUE_SLIDER");
// Set the initial state
// All users need to use the same TraitInfo instance, chosen as the default mod rules
var colorManager = modData.DefaultRules.Actors[SystemActors.World].TraitInfo<ColorPickerManagerInfo>();
mixer.SetColorLimits(colorManager.HsvSaturationRange[0], colorManager.HsvSaturationRange[1], colorManager.V);
mixer.SetColorLimits(colorManager.HsvSaturationRange[0], colorManager.HsvSaturationRange[1], colorManager.HsvValueRange[0], colorManager.HsvValueRange[1]);
mixer.OnChange += () => onChange(mixer.Color);
mixer.Set(initialColor);
hueSlider.OnChange += h =>
{
mixer.SetColorLimits(colorManager.HsvSaturationRange[0], colorManager.HsvSaturationRange[1], colorManager.HsvValueRange[0], colorManager.HsvValueRange[1], h);
var (_, _, s, v) = mixer.Color.ToAhsv();
mixer.Set(Color.FromAhsv(h, s, v));
onChange(mixer.Color);
};
hueSlider.UpdateValue(initialColor.ToAhsv().H);
var randomButton = widget.GetOrNull<ButtonWidget>("RANDOM_BUTTON");
if (randomButton != null)
{
@@ -45,7 +56,12 @@ namespace OpenRA.Mods.Common.Widgets.Logic
.Distinct()
.ToList();
var playerColors = Enumerable.Empty<Color>();
randomButton.OnClick = () => mixer.Set(colorManager.RandomValidColor(world.LocalRandom, terrainColors, playerColors));
randomButton.OnClick = () =>
{
var randomColor = colorManager.RandomValidColor(world.LocalRandom, terrainColors, playerColors);
mixer.Set(randomColor);
hueSlider.UpdateValue(randomColor.ToAhsv().H);
};
}
if (initialFaction == null || !colorManager.FactionPreviewActors.TryGetValue(initialFaction, out var actorType))
@@ -117,13 +133,13 @@ namespace OpenRA.Mods.Common.Widgets.Logic
if (!int.TryParse(yaml.Value, out paletteCustomRows))
throw new YamlException($"Invalid value for PaletteCustomRows: {yaml.Value}");
var presetColors = colorManager.PresetColors().ToList();
var presetColors = colorManager.PresetColors;
for (var j = 0; j < palettePresetRows; j++)
{
for (var i = 0; i < paletteCols; i++)
{
var colorIndex = j * paletteCols + i;
if (colorIndex >= presetColors.Count)
if (colorIndex >= presetColors.Length)
break;
var color = presetColors[colorIndex];
@@ -136,7 +152,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
newSwatch.OnMouseUp = m =>
{
mixer.Set(color);
onChange(color);
hueSlider.UpdateValue(color.ToAhsv().H);
};
presetArea.AddChild(newSwatch);
@@ -160,7 +176,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{
var color = newSwatch.GetColor();
mixer.Set(color);
onChange(color);
hueSlider.UpdateValue(color.ToAhsv().H);
};
customArea.AddChild(newSwatch);