Add ScrollPanel.ScrollToItem().
This commit is contained in:
@@ -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<bool> IsSelected = () => false;
|
||||
@@ -59,5 +62,12 @@ namespace OpenRA.Widgets
|
||||
w.OnDoubleClick = onDoubleClick;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user