Fix exception throwing on no saved designs. Allow mousewheel scrolling of the combo.

This commit is contained in:
Ottermandias 2023-11-10 18:21:13 +01:00
parent 4328f5d680
commit eb6e665147

View file

@ -21,6 +21,7 @@ public abstract class DesignComboBase : FilterComboCache<Tuple<Design, string>>,
private readonly DesignChanged _designChanged; private readonly DesignChanged _designChanged;
protected readonly TabSelected TabSelected; protected readonly TabSelected TabSelected;
protected float InnerWidth; protected float InnerWidth;
private Design? _currentDesign;
protected DesignComboBase(Func<IReadOnlyList<Tuple<Design, string>>> generator, Logger log, DesignChanged designChanged, protected DesignComboBase(Func<IReadOnlyList<Tuple<Design, string>>> generator, Logger log, DesignChanged designChanged,
TabSelected tabSelected, Configuration config) TabSelected tabSelected, Configuration config)
@ -63,11 +64,17 @@ public abstract class DesignComboBase : FilterComboCache<Tuple<Design, string>>,
return ret; return ret;
} }
protected override int UpdateCurrentSelected(int currentSelected)
{
CurrentSelectionIdx = Items.IndexOf(p => _currentDesign == p.Item1);
CurrentSelection = CurrentSelectionIdx >= 0 ? Items[CurrentSelectionIdx] : null;
return CurrentSelectionIdx;
}
protected bool Draw(Design? currentDesign, string? label, float width) protected bool Draw(Design? currentDesign, string? label, float width)
{ {
_currentDesign = currentDesign;
InnerWidth = 400 * ImGuiHelpers.GlobalScale; InnerWidth = 400 * ImGuiHelpers.GlobalScale;
CurrentSelectionIdx = Math.Max(Items.IndexOf(p => currentDesign == p.Item1), 0);
CurrentSelection = Items[CurrentSelectionIdx];
var name = label ?? "Select Design Here..."; var name = label ?? "Select Design Here...";
var ret = Draw("##design", name, string.Empty, width, ImGui.GetTextLineHeightWithSpacing()) var ret = Draw("##design", name, string.Empty, width, ImGui.GetTextLineHeightWithSpacing())
&& CurrentSelection != null; && CurrentSelection != null;
@ -79,6 +86,7 @@ public abstract class DesignComboBase : FilterComboCache<Tuple<Design, string>>,
ImGuiUtil.HoverTooltip("Control + Right-Click to move to design."); ImGuiUtil.HoverTooltip("Control + Right-Click to move to design.");
} }
_currentDesign = null;
return ret; return ret;
} }
@ -106,8 +114,8 @@ public abstract class DesignComboBase : FilterComboCache<Tuple<Design, string>>,
Cleanup(); Cleanup();
if (CurrentSelection?.Item1 == design) if (CurrentSelection?.Item1 == design)
{ {
CurrentSelectionIdx = -1; CurrentSelectionIdx = Items.Count > 0 ? 0 : -1;
CurrentSelection = null; CurrentSelection = Items[CurrentSelectionIdx];
} }
break; break;
@ -117,20 +125,43 @@ public abstract class DesignComboBase : FilterComboCache<Tuple<Design, string>>,
public sealed class DesignCombo : DesignComboBase public sealed class DesignCombo : DesignComboBase
{ {
private readonly DesignManager _manager;
public DesignCombo(DesignManager designs, DesignFileSystem fileSystem, Logger log, DesignChanged designChanged, TabSelected tabSelected, public DesignCombo(DesignManager designs, DesignFileSystem fileSystem, Logger log, DesignChanged designChanged, TabSelected tabSelected,
Configuration config) Configuration config)
: base( : base(() => designs.Designs
() => designs.Designs
.Select(d => new Tuple<Design, string>(d, fileSystem.FindLeaf(d, out var l) ? l.FullName() : string.Empty)) .Select(d => new Tuple<Design, string>(d, fileSystem.FindLeaf(d, out var l) ? l.FullName() : string.Empty))
.OrderBy(d => d.Item2) .OrderBy(d => d.Item2)
.ToList(), log, designChanged, tabSelected, config) .ToList(), log, designChanged, tabSelected, config)
{ } {
_manager = designs;
if (designs.Designs.Count == 0)
return;
CurrentSelection = Items[0];
CurrentSelectionIdx = 0;
}
public Design? Design public Design? Design
=> CurrentSelection?.Item1; => CurrentSelection?.Item1;
public void Draw(float width) public void Draw(float width)
=> Draw(Design, (Incognito ? Design?.Incognito : Design?.Name.Text) ?? string.Empty, width); {
Draw(Design, (Incognito ? Design?.Incognito : Design?.Name.Text) ?? string.Empty, width);
if (ImGui.IsItemHovered() && _manager.Designs.Count > 1)
{
var mouseWheel = -(int)ImGui.GetIO().MouseWheel % _manager.Designs.Count;
CurrentSelectionIdx = mouseWheel switch
{
< 0 when CurrentSelectionIdx < 0 => _manager.Designs.Count - 1 + mouseWheel,
< 0 => (CurrentSelectionIdx + _manager.Designs.Count + mouseWheel) % _manager.Designs.Count,
> 0 when CurrentSelectionIdx < 0 => mouseWheel,
> 0 => (CurrentSelectionIdx + mouseWheel) % _manager.Designs.Count,
_ => CurrentSelectionIdx,
};
CurrentSelection = Items[CurrentSelectionIdx];
}
}
} }
public sealed class RevertDesignCombo : DesignComboBase, IDisposable public sealed class RevertDesignCombo : DesignComboBase, IDisposable