Replace Rectangle widget bounds with a new WidgetBounds struct.

This commit is contained in:
Paul Chote
2023-10-31 17:06:34 +00:00
committed by Pavel Penev
parent 31c37662cf
commit 03b413a892
7 changed files with 47 additions and 29 deletions

View File

@@ -182,6 +182,28 @@ namespace OpenRA.Widgets
protected virtual void Dispose(bool disposing) { } protected virtual void Dispose(bool disposing) { }
} }
public struct WidgetBounds
{
public int X, Y, Width, Height;
public readonly int Left => X;
public readonly int Right => X + Width;
public readonly int Top => Y;
public readonly int Bottom => Y + Height;
public WidgetBounds(int x, int y, int width, int height)
{
X = x;
Y = y;
Width = width;
Height = height;
}
public readonly Rectangle ToRectangle()
{
return new Rectangle(X, Y, Width, Height);
}
}
public abstract class Widget public abstract class Widget
{ {
string defaultCursor = null; string defaultCursor = null;
@@ -201,7 +223,7 @@ namespace OpenRA.Widgets
public bool IgnoreChildMouseOver; public bool IgnoreChildMouseOver;
// Calculated internally // Calculated internally
public Rectangle Bounds; public WidgetBounds Bounds;
public Widget Parent = null; public Widget Parent = null;
public Func<bool> IsVisible; public Func<bool> IsVisible;
@@ -261,7 +283,7 @@ namespace OpenRA.Widgets
// Parse the YAML equations to find the widget bounds // Parse the YAML equations to find the widget bounds
var parentBounds = (Parent == null) var parentBounds = (Parent == null)
? new Rectangle(0, 0, Game.Renderer.Resolution.Width, Game.Renderer.Resolution.Height) ? new WidgetBounds(0, 0, Game.Renderer.Resolution.Width, Game.Renderer.Resolution.Height)
: Parent.Bounds; : Parent.Bounds;
var substitutions = args.TryGetValue("substitutions", out var subs) ? var substitutions = args.TryGetValue("substitutions", out var subs) ?
@@ -276,15 +298,15 @@ namespace OpenRA.Widgets
substitutions.Add("PARENT_BOTTOM", parentBounds.Height); substitutions.Add("PARENT_BOTTOM", parentBounds.Height);
var readOnlySubstitutions = new ReadOnlyDictionary<string, int>(substitutions); var readOnlySubstitutions = new ReadOnlyDictionary<string, int>(substitutions);
var width = Width != null ? Width.Evaluate(readOnlySubstitutions) : 0; var width = Width?.Evaluate(readOnlySubstitutions) ?? 0;
var height = Height != null ? Height.Evaluate(readOnlySubstitutions) : 0; var height = Height?.Evaluate(readOnlySubstitutions) ?? 0;
substitutions.Add("WIDTH", width); substitutions.Add("WIDTH", width);
substitutions.Add("HEIGHT", height); substitutions.Add("HEIGHT", height);
var x = X != null ? X.Evaluate(readOnlySubstitutions) : 0; var x = X?.Evaluate(readOnlySubstitutions) ?? 0;
var y = Y != null ? Y.Evaluate(readOnlySubstitutions) : 0; var y = Y?.Evaluate(readOnlySubstitutions) ?? 0;
Bounds = new Rectangle(x, y, width, height); Bounds = new WidgetBounds(x, y, width, height);
} }
public void PostInit(WidgetArgs args) public void PostInit(WidgetArgs args)

View File

@@ -9,7 +9,6 @@
*/ */
#endregion #endregion
using OpenRA.Primitives;
using OpenRA.Widgets; using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets namespace OpenRA.Mods.Common.Widgets
@@ -50,11 +49,11 @@ namespace OpenRA.Mods.Common.Widgets
break; break;
case MouseInputEvent.Down: case MouseInputEvent.Down:
moving = true; moving = true;
Bounds = new Rectangle(Bounds.X + vec.X, Bounds.Y + vec.Y, Bounds.Width, Bounds.Height); Bounds = new WidgetBounds(Bounds.X + vec.X, Bounds.Y + vec.Y, Bounds.Width, Bounds.Height);
break; break;
case MouseInputEvent.Move: case MouseInputEvent.Move:
if (moving) if (moving)
Bounds = new Rectangle(Bounds.X + vec.X, Bounds.Y + vec.Y, Bounds.Width, Bounds.Height); Bounds = new WidgetBounds(Bounds.X + vec.X, Bounds.Y + vec.Y, Bounds.Width, Bounds.Height);
break; break;
} }

View File

@@ -13,7 +13,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Widgets; using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets namespace OpenRA.Mods.Common.Widgets
@@ -107,7 +106,7 @@ namespace OpenRA.Mods.Common.Widgets
// Mask to prevent any clicks from being sent to other widgets // Mask to prevent any clicks from being sent to other widgets
fullscreenMask = new MaskWidget fullscreenMask = new MaskWidget
{ {
Bounds = new Rectangle(0, 0, Game.Renderer.Resolution.Width, Game.Renderer.Resolution.Height) Bounds = new WidgetBounds(0, 0, Game.Renderer.Resolution.Width, Game.Renderer.Resolution.Height)
}; };
fullscreenMask.OnMouseDown += mi => { Game.Sound.PlayNotification(ModRules, null, "Sounds", ClickSound, null); RemovePanel(); }; fullscreenMask.OnMouseDown += mi => { Game.Sound.PlayNotification(ModRules, null, "Sounds", ClickSound, null); RemovePanel(); };
@@ -129,7 +128,7 @@ namespace OpenRA.Mods.Common.Widgets
if (panelY + oldBounds.Height > Game.Renderer.Resolution.Height) if (panelY + oldBounds.Height > Game.Renderer.Resolution.Height)
panelY -= Bounds.Height + oldBounds.Height; panelY -= Bounds.Height + oldBounds.Height;
panel.Bounds = new Rectangle( panel.Bounds = new WidgetBounds(
panelX, panelX,
panelY, panelY,
oldBounds.Width, oldBounds.Width,

View File

@@ -16,7 +16,6 @@ using System.Globalization;
using System.Linq; using System.Linq;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits; using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Widgets; using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic namespace OpenRA.Mods.Common.Widgets.Logic
@@ -501,7 +500,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
return; return;
var parentBounds = w.Parent == null var parentBounds = w.Parent == null
? new Rectangle(0, 0, Game.Renderer.Resolution.Width, Game.Renderer.Resolution.Height) ? new WidgetBounds(0, 0, Game.Renderer.Resolution.Width, Game.Renderer.Resolution.Height)
: w.Parent.Bounds; : w.Parent.Bounds;
var substitutions = new Dictionary<string, int> var substitutions = new Dictionary<string, int>
@@ -515,18 +514,18 @@ namespace OpenRA.Mods.Common.Widgets.Logic
}; };
var readOnlySubstitutions = new ReadOnlyDictionary<string, int>(substitutions); var readOnlySubstitutions = new ReadOnlyDictionary<string, int>(substitutions);
var width = w.Width != null ? w.Width.Evaluate(readOnlySubstitutions) : 0; var width = w.Width?.Evaluate(readOnlySubstitutions) ?? 0;
var height = w.Height != null ? w.Height.Evaluate(readOnlySubstitutions) : 0; var height = w.Height?.Evaluate(readOnlySubstitutions) ?? 0;
substitutions.Add("WIDTH", width); substitutions.Add("WIDTH", width);
substitutions.Add("HEIGHT", height); substitutions.Add("HEIGHT", height);
if (insideScrollPanel) if (insideScrollPanel)
w.Bounds = new Rectangle(w.Bounds.X, w.Bounds.Y, width, w.Bounds.Height); w.Bounds = new WidgetBounds(w.Bounds.X, w.Bounds.Y, width, w.Bounds.Height);
else else
w.Bounds = new Rectangle( w.Bounds = new WidgetBounds(
w.X != null ? w.X.Evaluate(readOnlySubstitutions) : 0, w.X?.Evaluate(readOnlySubstitutions) ?? 0,
w.Y != null ? w.Y.Evaluate(readOnlySubstitutions) : 0, w.Y?.Evaluate(readOnlySubstitutions) ?? 0,
width, width,
height); height);

View File

@@ -225,11 +225,10 @@ namespace OpenRA.Mods.Common.Widgets
// ChildOrigin enumerates the widget tree, so only evaluate it once // ChildOrigin enumerates the widget tree, so only evaluate it once
var co = ChildOrigin; var co = ChildOrigin;
drawBounds.X -= co.X; drawBounds = new Rectangle(drawBounds.X - co.X, drawBounds.Y - co.Y, drawBounds.Width, drawBounds.Height);
drawBounds.Y -= co.Y;
foreach (var child in Children) foreach (var child in Children)
if (child.Bounds.IntersectsWith(drawBounds)) if (child.Bounds.ToRectangle().IntersectsWith(drawBounds))
child.DrawOuter(); child.DrawOuter();
Game.Renderer.DisableScissor(); Game.Renderer.DisableScissor();

View File

@@ -78,7 +78,7 @@ namespace OpenRA.Mods.Common.Widgets
foreach (var t in texts) foreach (var t in texts)
{ {
var textSize = font.Measure(t.Text); var textSize = font.Measure(t.Text);
var location = new float2(Bounds.Location) + new float2(0, y); var location = new float2(Bounds.X, Bounds.Y + y);
if (Align == TextAlign.Center) if (Align == TextAlign.Center)
location += new int2((Bounds.Width - textSize.X) / 2, 0); location += new int2((Bounds.Width - textSize.X) / 2, 0);

View File

@@ -51,9 +51,8 @@ namespace OpenRA.Mods.Common.Widgets
var wholeLines = (int)Math.Floor((double)((Bounds.Height - BottomSpacing) / lineHeight)); var wholeLines = (int)Math.Floor((double)((Bounds.Height - BottomSpacing) / lineHeight));
var visibleChildrenHeight = wholeLines * lineHeight; var visibleChildrenHeight = wholeLines * lineHeight;
overflowDrawBounds = new Rectangle(RenderOrigin.X, RenderOrigin.Y, Bounds.Width, Bounds.Height); var y = RenderOrigin.Y + Bounds.Height - visibleChildrenHeight;
overflowDrawBounds.Y += Bounds.Height - visibleChildrenHeight; overflowDrawBounds = new Rectangle(RenderOrigin.X, y, Bounds.Width, visibleChildrenHeight);
overflowDrawBounds.Height = visibleChildrenHeight;
} }
public override void DrawOuter() public override void DrawOuter()
@@ -66,9 +65,10 @@ namespace OpenRA.Mods.Common.Widgets
if (mostRecentMessageOverflows && HideOverflow) if (mostRecentMessageOverflows && HideOverflow)
Game.Renderer.EnableScissor(overflowDrawBounds); Game.Renderer.EnableScissor(overflowDrawBounds);
var bounds = Bounds.ToRectangle();
for (var i = Children.Count - 1; i >= 0; i--) for (var i = Children.Count - 1; i >= 0; i--)
{ {
if (Bounds.Contains(Children[i].Bounds) || !HideOverflow || mostRecentMessageOverflows) if (!HideOverflow || mostRecentMessageOverflows || bounds.Contains(Children[i].Bounds.ToRectangle()))
Children[i].DrawOuter(); Children[i].DrawOuter();
if (mostRecentMessageOverflows) if (mostRecentMessageOverflows)