Skip to content

Commit

Permalink
Fix #74, by forcing scroll views to scroll up until items are visible. (
Browse files Browse the repository at this point in the history
#126)

Sometimes the search bar seemed to return nothing when there are 
items that match. That happened because the scrolling view was not 
reset, which resulted in showing empty space until you scroll back up.

This PR fixes that issue. It fixes at least the object explorer 
(SelectObjectPanels) and the production sheet list 
(MainScreen.allPages), and it doesn't visibly affect the production tables.
  • Loading branch information
shpaass authored May 11, 2024
2 parents 98016ec + ca40a4a commit e1ca158
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
8 changes: 8 additions & 0 deletions FactorioCalc.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommandLineToolExample", "C
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yafc.Model.Tests", "Yafc.Model.Tests\Yafc.Model.Tests.csproj", "{66B66728-84F0-4242-B49A-B9D746A3CCA5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D6A715CB-5C17-4DD7-9ADA-5D7F44FADFCF}"
ProjectSection(SolutionItems) = preProject
changelog.txt = changelog.txt
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -44,4 +49,7 @@ Global
{66B66728-84F0-4242-B49A-B9D746A3CCA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{66B66728-84F0-4242-B49A-B9D746A3CCA5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
21 changes: 13 additions & 8 deletions Yafc.UI/ImGui/ScrollArea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,16 @@ public class ScrollArea(float height, GuiBuilder builder, Padding padding = defa

public class VirtualScrollList<TData> : ScrollAreaBase {
private readonly Vector2 elementSize;
protected readonly int bufferRows;
protected int firstVisibleBlock;
protected int elementsPerRow;
// When rendering the scrollable content, render 'blocks' of 4 rows at a time. (As far as I can tell, any positive value works. Shadow picked 4, so I kept that.)
private readonly int bufferRows = 4;
// The first block of bufferRows that was rendered last time BuildContents was called. If it changes while scrolling, we need to re-render the scrollable content.
private int firstVisibleBlock;
private int elementsPerRow;
private IReadOnlyList<TData> _data = [];
private readonly int maxRowsVisible;
private readonly Drawer drawer;
public float _spacing;
protected readonly Action<int, int> reorder;
private float _spacing;
private readonly Action<int, int> reorder;

public float spacing {
get => _spacing;
Expand All @@ -229,10 +231,9 @@ public IReadOnlyList<TData> data {
}
}

public VirtualScrollList(float height, Vector2 elementSize, Drawer drawer, int bufferRows = 4, Padding padding = default, Action<int, int> reorder = null, bool collapsible = false) : base(height, padding, collapsible) {
public VirtualScrollList(float height, Vector2 elementSize, Drawer drawer, Padding padding = default, Action<int, int> reorder = null, bool collapsible = false) : base(height, padding, collapsible) {
this.elementSize = elementSize;
maxRowsVisible = MathUtils.Ceil(height / this.elementSize.Y) + bufferRows + 1;
this.bufferRows = bufferRows;
this.drawer = drawer;
this.reorder = reorder;
}
Expand All @@ -258,9 +259,13 @@ protected override void BuildContents(ImGui gui) {

int rowCount = ((_data.Count - 1) / elementsPerRow) + 1;
firstVisibleBlock = CalcFirstBlock();
int firstRow = firstVisibleBlock * bufferRows;
// Scroll up until there are maxRowsVisible, or to the top.
int firstRow = Math.Max(0, Math.Min(firstVisibleBlock * bufferRows, rowCount - maxRowsVisible));
int index = firstRow * elementsPerRow;
if (index >= _data.Count) {
// If _data is empty, there's nothing to draw. Make sure MeasureContent reports that, instead of the size of the most recent non-empty content.
// This will remove the scroll bar when the search doesn't match anything.
gui.lastContentRect = new Rect(gui.lastContentRect.X, gui.lastContentRect.Y, 0, 0);
return;
}

Expand Down
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Date: soon
- Add a help message and proper handling for command line arguments
- Removed default pollution cost from calculation. Added a setting to customize pollution cost.
- Add fuel consumption recipe for products
- Fix list displays below search boxes. If necessary, they now scroll up until items are visible.
----------------------------------------------------------------------------------------------------------------------
Version: 0.6.4
Date: April 16th 2024
Expand Down

0 comments on commit e1ca158

Please sign in to comment.