From 92bdec7f1d7ef6db3ba421b944c30a8f24695949 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Sat, 8 Oct 2011 17:52:57 +1300 Subject: [PATCH] factor out layout policy for ScrollPanelWidget; add GridLayout option. --- OpenRA.Game/OpenRA.Game.csproj | 4 ++- OpenRA.Game/Widgets/GridLayout.cs | 46 ++++++++++++++++++++++++ OpenRA.Game/Widgets/ListLayout.cs | 28 +++++++++++++++ OpenRA.Game/Widgets/ScrollPanelWidget.cs | 18 +++------- OpenRA.Game/Widgets/Widget.cs | 6 +++- 5 files changed, 87 insertions(+), 15 deletions(-) create mode 100644 OpenRA.Game/Widgets/GridLayout.cs create mode 100644 OpenRA.Game/Widgets/ListLayout.cs diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index d2acfc6fa6..cfecd37945 100755 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -1,4 +1,4 @@ - + Debug @@ -202,6 +202,8 @@ + + diff --git a/OpenRA.Game/Widgets/GridLayout.cs b/OpenRA.Game/Widgets/GridLayout.cs new file mode 100644 index 0000000000..a9f63116d5 --- /dev/null +++ b/OpenRA.Game/Widgets/GridLayout.cs @@ -0,0 +1,46 @@ +#region Copyright & License Information +/* + * Copyright 2007-2011 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. For more information, + * see COPYING. + */ +#endregion + +using System; + +namespace OpenRA.Widgets +{ + public class GridLayout : ILayout + { + ScrollPanelWidget widget; + int2 pos; + + public GridLayout(ScrollPanelWidget w) { widget = w; } + + public void AdjustChild(Widget w) + { + if (widget.Children.Count == 0) + { + widget.ContentHeight = widget.ItemSpacing; + pos = new int2(widget.ItemSpacing, widget.ItemSpacing); + } + + if (pos.X + widget.ItemSpacing + w.Bounds.Width > widget.Bounds.Width - widget.ScrollbarWidth) + { + /* start a new row */ + pos.X = widget.ItemSpacing; + pos.Y = widget.ContentHeight; + } + + w.Bounds.X += pos.X; + w.Bounds.Y += pos.Y; + + pos.X += widget.Bounds.Width + widget.ItemSpacing; + + widget.ContentHeight = Math.Max(widget.ContentHeight, pos.Y + widget.ItemSpacing + w.Bounds.Height); + } + } +} + diff --git a/OpenRA.Game/Widgets/ListLayout.cs b/OpenRA.Game/Widgets/ListLayout.cs new file mode 100644 index 0000000000..69d3526da5 --- /dev/null +++ b/OpenRA.Game/Widgets/ListLayout.cs @@ -0,0 +1,28 @@ +#region Copyright & License Information +/* + * Copyright 2007-2011 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. For more information, + * see COPYING. + */ +#endregion + +namespace OpenRA.Widgets +{ + public class ListLayout : ILayout + { + ScrollPanelWidget widget; + + public ListLayout(ScrollPanelWidget w) { widget = w; } + + public void AdjustChild(Widget w) + { + if (widget.Children.Count == 0) + widget.ContentHeight = widget.ItemSpacing; + + w.Bounds.Y += widget.ContentHeight; + widget.ContentHeight += w.Bounds.Height + widget.ItemSpacing; + } + } +} diff --git a/OpenRA.Game/Widgets/ScrollPanelWidget.cs b/OpenRA.Game/Widgets/ScrollPanelWidget.cs index 8a0047d055..99598b685f 100644 --- a/OpenRA.Game/Widgets/ScrollPanelWidget.cs +++ b/OpenRA.Game/Widgets/ScrollPanelWidget.cs @@ -14,6 +14,8 @@ using OpenRA.Graphics; namespace OpenRA.Widgets { + public interface ILayout { void AdjustChild(Widget w); } + public class ScrollPanelWidget : Widget { public int ScrollbarWidth = 24; @@ -22,6 +24,7 @@ namespace OpenRA.Widgets public int ButtonDepth = ChromeMetrics.Get("ButtonDepth"); public string Background = "scrollpanel-bg"; public int ContentHeight = 0; + public ILayout Layout; protected float ListOffset = 0; protected bool UpPressed = false; protected bool DownPressed = false; @@ -32,12 +35,7 @@ namespace OpenRA.Widgets protected Rectangle scrollbarRect; protected Rectangle thumbRect; - public ScrollPanelWidget() : base() {} - protected ScrollPanelWidget(ScrollPanelWidget other) - : base(other) - { - throw new NotImplementedException(); - } + public ScrollPanelWidget() : base() { Layout = new ListLayout(this); } public override void RemoveChildren() { @@ -48,11 +46,7 @@ namespace OpenRA.Widgets public override void AddChild(Widget child) { // Initial setup of margins/height - if (Children.Count == 0) - ContentHeight = ItemSpacing; - - child.Bounds.Y += ContentHeight; - ContentHeight += child.Bounds.Height + ItemSpacing; + Layout.AdjustChild(child); base.AddChild(child); } @@ -192,7 +186,5 @@ namespace OpenRA.Widgets return (UpPressed || DownPressed || ThumbPressed); } - - public override Widget Clone() { return new ScrollPanelWidget(this); } } } diff --git a/OpenRA.Game/Widgets/Widget.cs b/OpenRA.Game/Widgets/Widget.cs index 40a91ded1b..4b0b1794c2 100644 --- a/OpenRA.Game/Widgets/Widget.cs +++ b/OpenRA.Game/Widgets/Widget.cs @@ -134,7 +134,7 @@ namespace OpenRA.Widgets public Widget Parent = null; public Func IsVisible; public Widget() { IsVisible = () => Visible; } - protected readonly List Children = new List(); + public readonly List Children = new List(); public Widget(Widget widget) { @@ -283,6 +283,7 @@ namespace OpenRA.Widgets public virtual void MouseEntered() {} public virtual void MouseExited() {} public virtual bool HandleMouseInput(MouseInput mi) { return false; } + public bool HandleMouseInputOuter(MouseInput mi) { // Are we able to handle this event? @@ -305,6 +306,7 @@ namespace OpenRA.Widgets } public virtual bool HandleKeyPress(KeyInput e) { return false; } + public virtual bool HandleKeyPressOuter(KeyInput e) { if (!IsVisible()) @@ -322,6 +324,7 @@ namespace OpenRA.Widgets } public virtual void Draw() {} + public virtual void DrawOuter() { if (IsVisible()) @@ -333,6 +336,7 @@ namespace OpenRA.Widgets } public virtual void Tick() {} + public virtual void TickOuter() { if (IsVisible())