diff --git a/OpenRA.Game/Graphics/RgbaColorRenderer.cs b/OpenRA.Game/Graphics/RgbaColorRenderer.cs index f1153693b3..5b9e6acd1b 100644 --- a/OpenRA.Game/Graphics/RgbaColorRenderer.cs +++ b/OpenRA.Game/Graphics/RgbaColorRenderer.cs @@ -238,6 +238,29 @@ namespace OpenRA.Graphics parent.DrawRGBAVertices(vertices); } + public void FillRect(float3 a, float3 b, float3 c, float3 d, Color topLeftColor, Color topRightColor, Color bottomRightColor, Color bottomLeftColor) + { + vertices[0] = VertexWithColor(a + Offset, topLeftColor); + vertices[1] = VertexWithColor(b + Offset, topRightColor); + vertices[2] = VertexWithColor(c + Offset, bottomRightColor); + vertices[3] = VertexWithColor(c + Offset, bottomRightColor); + vertices[4] = VertexWithColor(d + Offset, bottomLeftColor); + vertices[5] = VertexWithColor(a + Offset, topLeftColor); + + parent.DrawRGBAVertices(vertices); + } + + static Vertex VertexWithColor(float3 xyz, Color color) + { + color = Util.PremultiplyAlpha(color); + var cr = color.R / 255.0f; + var cg = color.G / 255.0f; + var cb = color.B / 255.0f; + var ca = color.A / 255.0f; + + return new Vertex(xyz, cr, cg, cb, ca, 0, 0); + } + public void FillEllipse(float3 tl, float3 br, Color color, int vertices = 32) { // TODO: Create an ellipse polygon instead diff --git a/OpenRA.Game/Widgets/WidgetUtils.cs b/OpenRA.Game/Widgets/WidgetUtils.cs index 09ee803f5d..2c0dbeac91 100644 --- a/OpenRA.Game/Widgets/WidgetUtils.cs +++ b/OpenRA.Game/Widgets/WidgetUtils.cs @@ -71,6 +71,17 @@ namespace OpenRA.Widgets Game.Renderer.RgbaColorRenderer.FillRect(tl, br, c); } + public static void FillRectWithColor(Rectangle r, Color topLeftColor, Color topRightColor, Color bottomRightColor, Color bottomLeftColor) + { + var tl = new float2(r.Left - 0.5f, r.Top - 0.5f); + var br = new float2(r.Right - 0.5f, r.Bottom - 0.5f); + + var tr = new float3(br.X, tl.Y, 0); + var bl = new float3(tl.X, br.Y, 0); + + Game.Renderer.RgbaColorRenderer.FillRect(tl, tr, br, bl, topLeftColor, topRightColor, bottomRightColor, bottomLeftColor); + } + public static void FillEllipseWithColor(Rectangle r, Color c) { var tl = new float2(r.Left, r.Top); diff --git a/OpenRA.Mods.Common/Widgets/GradientColorBlockWidget.cs b/OpenRA.Mods.Common/Widgets/GradientColorBlockWidget.cs new file mode 100644 index 0000000000..189e5bb182 --- /dev/null +++ b/OpenRA.Mods.Common/Widgets/GradientColorBlockWidget.cs @@ -0,0 +1,57 @@ +#region Copyright & License Information +/* + * Copyright 2007-2019 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 + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using System; +using OpenRA.Primitives; +using OpenRA.Widgets; + +namespace OpenRA.Mods.Common.Widgets +{ + public class GradientColorBlockWidget : Widget + { + public Color TopLeftColor { get; set; } + public Color TopRightColor { get; set; } + public Color BottomRightColor { get; set; } + public Color BottomLeftColor { get; set; } + + public Func GetTopLeftColor; + public Func GetTopRightColor; + public Func GetBottomRightColor; + public Func GetBottomLeftColor; + + public GradientColorBlockWidget() + { + GetTopLeftColor = () => TopLeftColor; + GetTopRightColor = () => TopRightColor; + GetBottomRightColor = () => BottomRightColor; + GetBottomLeftColor = () => BottomLeftColor; + } + + protected GradientColorBlockWidget(GradientColorBlockWidget widget) + : base(widget) + { + GetTopLeftColor = widget.GetTopLeftColor; + GetTopRightColor = widget.GetTopRightColor; + GetBottomRightColor = widget.GetBottomRightColor; + GetBottomLeftColor = widget.GetBottomLeftColor; + } + + public override Widget Clone() + { + return new GradientColorBlockWidget(this); + } + + public override void Draw() + { + WidgetUtils.FillRectWithColor(RenderBounds, GetTopLeftColor(), GetTopRightColor(), GetBottomRightColor(), GetBottomLeftColor()); + } + } +}