Add ScrollPanel.ScrollToItem().

This commit is contained in:
Paul Chote
2013-04-05 23:14:08 +13:00
parent 4907ea1f0c
commit 01248de66b
2 changed files with 30 additions and 0 deletions

View File

@@ -14,6 +14,8 @@ namespace OpenRA.Widgets
{ {
public class ScrollItemWidget : ButtonWidget public class ScrollItemWidget : ButtonWidget
{ {
public string ItemKey;
public ScrollItemWidget() public ScrollItemWidget()
: base() : base()
{ {
@@ -28,6 +30,7 @@ namespace OpenRA.Widgets
IsVisible = () => false; IsVisible = () => false;
VisualHeight = 0; VisualHeight = 0;
IgnoreChildMouseOver = true; IgnoreChildMouseOver = true;
Key = other.Key;
} }
public Func<bool> IsSelected = () => false; public Func<bool> IsSelected = () => false;
@@ -59,5 +62,12 @@ namespace OpenRA.Widgets
w.OnDoubleClick = onDoubleClick; w.OnDoubleClick = onDoubleClick;
return w; return w;
} }
public static ScrollItemWidget Setup(string key, ScrollItemWidget template, Func<bool> isSelected, Action onClick)
{
var w = Setup(template, isSelected, onClick);
w.ItemKey = key;
return w;
}
} }
} }

View File

@@ -10,6 +10,7 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Linq;
using OpenRA.Graphics; using OpenRA.Graphics;
namespace OpenRA.Widgets namespace OpenRA.Widgets
@@ -143,6 +144,25 @@ namespace OpenRA.Widgets
ListOffset = 0; 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 () public override void Tick ()
{ {
if (UpPressed) Scroll(1); if (UpPressed) Scroll(1);