From 01248de66b4799ecf343a9feba53daa06ee08c46 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Fri, 5 Apr 2013 23:14:08 +1300 Subject: [PATCH] Add ScrollPanel.ScrollToItem(). --- OpenRA.Game/Widgets/ScrollItemWidget.cs | 10 ++++++++++ OpenRA.Game/Widgets/ScrollPanelWidget.cs | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/OpenRA.Game/Widgets/ScrollItemWidget.cs b/OpenRA.Game/Widgets/ScrollItemWidget.cs index d5deb1ef09..99de7c2dc3 100644 --- a/OpenRA.Game/Widgets/ScrollItemWidget.cs +++ b/OpenRA.Game/Widgets/ScrollItemWidget.cs @@ -14,6 +14,8 @@ namespace OpenRA.Widgets { public class ScrollItemWidget : ButtonWidget { + public string ItemKey; + public ScrollItemWidget() : base() { @@ -28,6 +30,7 @@ namespace OpenRA.Widgets IsVisible = () => false; VisualHeight = 0; IgnoreChildMouseOver = true; + Key = other.Key; } public Func IsSelected = () => false; @@ -59,5 +62,12 @@ namespace OpenRA.Widgets w.OnDoubleClick = onDoubleClick; return w; } + + public static ScrollItemWidget Setup(string key, ScrollItemWidget template, Func isSelected, Action onClick) + { + var w = Setup(template, isSelected, onClick); + w.ItemKey = key; + return w; + } } } \ No newline at end of file diff --git a/OpenRA.Game/Widgets/ScrollPanelWidget.cs b/OpenRA.Game/Widgets/ScrollPanelWidget.cs index 185a41cfed..b227547bb2 100644 --- a/OpenRA.Game/Widgets/ScrollPanelWidget.cs +++ b/OpenRA.Game/Widgets/ScrollPanelWidget.cs @@ -10,6 +10,7 @@ using System; using System.Drawing; +using System.Linq; using OpenRA.Graphics; namespace OpenRA.Widgets @@ -143,6 +144,25 @@ namespace OpenRA.Widgets ListOffset = 0; } + public void ScrollToItem(string itemKey) + { + var item = Children.FirstOrDefault(c => + { + var si = c as ScrollItemWidget; + return si != null && si.ItemKey == itemKey; + }); + + if (item == null) + return; + + // Scroll the item to be visible + if (item.Bounds.Top + ListOffset < 0) + ListOffset = ItemSpacing - item.Bounds.Top; + + if (item.Bounds.Bottom + ListOffset > RenderBounds.Height) + ListOffset = RenderBounds.Height - item.Bounds.Bottom - ItemSpacing; + } + public override void Tick () { if (UpPressed) Scroll(1);