mirror of
https://github.com/Ottermandias/Glamourer.git
synced 2025-12-12 10:17:23 +01:00
Fix scrolling on lists.
This commit is contained in:
parent
c3d0d24713
commit
fb909aaf87
1 changed files with 82 additions and 61 deletions
|
|
@ -17,8 +17,10 @@ namespace Glamourer.Gui
|
|||
private readonly float _size;
|
||||
private float _previewSize;
|
||||
private readonly IReadOnlyList<T> _items;
|
||||
private readonly IReadOnlyList<string> _itemNamesLower;
|
||||
private readonly IReadOnlyList<(string, int)> _itemNamesLower;
|
||||
private readonly Func<T, string> _itemToName;
|
||||
private IReadOnlyList<(string, int)> _currentItemNames;
|
||||
private bool _needsClear;
|
||||
|
||||
public Action? PrePreview;
|
||||
public Action? PostPreview;
|
||||
|
|
@ -32,6 +34,22 @@ namespace Glamourer.Gui
|
|||
public ImGuiComboFlags Flags { get; set; } = ImGuiComboFlags.None;
|
||||
public int ItemsAtOnce { get; set; } = 12;
|
||||
|
||||
private void UpdateFilter(string newFilter)
|
||||
{
|
||||
if (newFilter == _currentFilter)
|
||||
return;
|
||||
|
||||
var lower = newFilter.ToLowerInvariant();
|
||||
if (_currentFilterLower.Any() && lower.Contains(_currentFilterLower))
|
||||
_currentItemNames = _currentItemNames.Where(p => p.Item1.Contains(lower)).ToArray();
|
||||
else if (lower.Any())
|
||||
_currentItemNames = _itemNamesLower.Where(p => p.Item1.Contains(lower)).ToArray();
|
||||
else
|
||||
_currentItemNames = _itemNamesLower;
|
||||
_currentFilter = newFilter;
|
||||
_currentFilterLower = lower;
|
||||
}
|
||||
|
||||
public ComboWithFilter(string label, float size, float previewSize, IReadOnlyList<T> items, Func<T, string> itemToName)
|
||||
{
|
||||
_label = label;
|
||||
|
|
@ -42,7 +60,8 @@ namespace Glamourer.Gui
|
|||
_size = size;
|
||||
_previewSize = previewSize;
|
||||
|
||||
_itemNamesLower = _items.Select(i => _itemToName(i).ToLowerInvariant()).ToList();
|
||||
_itemNamesLower = _items.Select((i, idx) => (_itemToName(i).ToLowerInvariant(), idx)).ToArray();
|
||||
_currentItemNames = _itemNamesLower;
|
||||
}
|
||||
|
||||
public ComboWithFilter(string label, ComboWithFilter<T> other)
|
||||
|
|
@ -53,6 +72,7 @@ namespace Glamourer.Gui
|
|||
_itemToName = other._itemToName;
|
||||
_items = other._items;
|
||||
_itemNamesLower = other._itemNamesLower;
|
||||
_currentItemNames = other._currentItemNames;
|
||||
_size = other._size;
|
||||
_previewSize = other._previewSize;
|
||||
PrePreview = other.PrePreview;
|
||||
|
|
@ -83,7 +103,6 @@ namespace Glamourer.Gui
|
|||
_focus = true;
|
||||
}
|
||||
|
||||
|
||||
var scrollY = Math.Max((int) (ImGui.GetScrollY() / _heightPerItem) - 1, 0);
|
||||
var restHeight = scrollY * _heightPerItem;
|
||||
numItems = 0;
|
||||
|
|
@ -92,16 +111,13 @@ namespace Glamourer.Gui
|
|||
if (restHeight > 0)
|
||||
ImGui.Dummy(Vector2.UnitY * restHeight);
|
||||
|
||||
for (var i = scrollY; i < _items.Count; ++i)
|
||||
for (var i = scrollY; i < _currentItemNames.Count; ++i)
|
||||
{
|
||||
if (!_itemNamesLower[i].Contains(_currentFilterLower))
|
||||
if (++numItems > ItemsAtOnce + 2)
|
||||
continue;
|
||||
|
||||
++numItems;
|
||||
if (numItems <= ItemsAtOnce + 2)
|
||||
{
|
||||
nodeIdx = i;
|
||||
var item = _items[i]!;
|
||||
nodeIdx = _currentItemNames[i].Item2;
|
||||
var item = _items[nodeIdx]!;
|
||||
bool success;
|
||||
if (CreateSelectable != null)
|
||||
{
|
||||
|
|
@ -120,10 +136,9 @@ namespace Glamourer.Gui
|
|||
ret = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (numItems > ItemsAtOnce + 2)
|
||||
ImGui.Dummy(Vector2.UnitY * (numItems - ItemsAtOnce - 2) * _heightPerItem);
|
||||
if (_currentItemNames.Count > ItemsAtOnce + 2)
|
||||
ImGui.Dummy(Vector2.UnitY * (_currentItemNames.Count - ItemsAtOnce - 2 - scrollY) * _heightPerItem);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
|
@ -143,13 +158,18 @@ namespace Glamourer.Gui
|
|||
PrePreview?.Invoke();
|
||||
if (!ImGui.BeginCombo(_label, currentName, Flags))
|
||||
{
|
||||
if (_needsClear)
|
||||
{
|
||||
_needsClear = false;
|
||||
_focus = false;
|
||||
_currentFilter = string.Empty;
|
||||
_currentFilterLower = string.Empty;
|
||||
UpdateFilter(string.Empty);
|
||||
}
|
||||
|
||||
PostPreview?.Invoke();
|
||||
return false;
|
||||
}
|
||||
|
||||
_needsClear = true;
|
||||
PostPreview?.Invoke();
|
||||
|
||||
_heightPerItem = HeightPerItem ?? ImGui.GetTextLineHeightWithSpacing();
|
||||
|
|
@ -158,8 +178,9 @@ namespace Glamourer.Gui
|
|||
try
|
||||
{
|
||||
ImGui.SetNextItemWidth(-1);
|
||||
if (ImGui.InputTextWithHint(_filterLabel, "Filter...", ref _currentFilter, 255))
|
||||
_currentFilterLower = _currentFilter.ToLowerInvariant();
|
||||
var tmp = _currentFilter;
|
||||
if (ImGui.InputTextWithHint(_filterLabel, "Filter...", ref tmp, 255))
|
||||
UpdateFilter(tmp);
|
||||
|
||||
var isFocused = ImGui.IsItemActive();
|
||||
if (!_focus)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue