Files
OpenRA/OpenRA.Mods.Common/Widgets/HueSliderWidget.cs
RoosterDragon 5f97e2de5a Make Color use uint for ARGB.
This is a more natural representation than int that allows removal of casts in many places that require uint. Additionally, we can change the internal representation from long to uint, making the Color struct smaller. Since arrays of colors are common, this can save on memory.
2024-03-09 21:10:02 +02:00

68 lines
1.7 KiB
C#

#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 = (uint*)cc;
for (var h = 0; h < 256; h++)
{
*(c + 0 * 256 + h) = Color.FromAhsv(h / 255f, 1, 1).ToArgb();
}
}
}
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);
}
}
}