Makes dropdown menus auto-scroll to selected item.

This commit is contained in:
deniz1a
2015-08-02 22:13:21 +03:00
parent 1eb46d4922
commit 53329b7068
2 changed files with 29 additions and 10 deletions

View File

@@ -94,6 +94,10 @@ namespace OpenRA.Mods.Common.Widgets
oldBounds.Width, oldBounds.Width,
oldBounds.Height); oldBounds.Height);
panelRoot.AddChild(panel); panelRoot.AddChild(panel);
var scrollPanel = panel as ScrollPanelWidget;
if (scrollPanel != null)
scrollPanel.ScrollToSelectedItem();
} }
public void ShowDropDown<T>(string panelTemplate, int maxHeight, IEnumerable<T> options, Func<T, ScrollItemWidget, ScrollItemWidget> setupItem) public void ShowDropDown<T>(string panelTemplate, int maxHeight, IEnumerable<T> options, Func<T, ScrollItemWidget, ScrollItemWidget> setupItem)

View File

@@ -214,17 +214,8 @@ namespace OpenRA.Mods.Common.Widgets
get { return targetListOffset == Math.Min(0, Bounds.Height - ContentHeight) || ContentHeight <= Bounds.Height; } get { return targetListOffset == Math.Min(0, Bounds.Height - ContentHeight) || ContentHeight <= Bounds.Height; }
} }
public void ScrollToItem(string itemKey, bool smooth = false) void ScrollToItem(Widget item, bool smooth = false)
{ {
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 // Scroll the item to be visible
float? newOffset = null; float? newOffset = null;
if (item.Bounds.Top + currentListOffset < 0) if (item.Bounds.Top + currentListOffset < 0)
@@ -237,6 +228,30 @@ namespace OpenRA.Mods.Common.Widgets
SetListOffset(newOffset.Value, smooth); SetListOffset(newOffset.Value, smooth);
} }
public void ScrollToItem(string itemKey, bool smooth = false)
{
var item = Children.FirstOrDefault(c =>
{
var si = c as ScrollItemWidget;
return si != null && si.ItemKey == itemKey;
});
if (item != null)
ScrollToItem(item, smooth);
}
public void ScrollToSelectedItem()
{
var item = Children.FirstOrDefault(c =>
{
var si = c as ScrollItemWidget;
return si != null && si.IsSelected();
});
if (item != null)
ScrollToItem(item);
}
public override void Tick() public override void Tick()
{ {
if (upPressed) if (upPressed)