Fix linear forwards backwards

This commit is contained in:
2025-03-17 08:47:39 +01:00
parent 2df23dde06
commit 872a59b27c
2 changed files with 113 additions and 38 deletions

View File

@@ -15,6 +15,7 @@ import 'package:rimworld_modman/mod_list.dart';
class ModListTroubleshooter {
final ModList originalModList;
ModList currentModList;
// These indices should ALWAYS represent the CURRENT selection of mods
int _startIndex = 0;
int _endIndex = 0;
@@ -48,19 +49,24 @@ class ModListTroubleshooter {
return currentModList;
}
// If the current selection is not equal to our proposed step size
// We do not MOVE but instead just return the correct amount of mods from the start
ModList linearForward({int stepSize = 20}) {
final totalMods = originalModList.activeMods.length;
if (_startIndex >= totalMods || _startIndex == _endIndex) {
int newStart = totalMods - stepSize;
if (newStart < 0) {
newStart = 0;
}
_startIndex = newStart;
// If we are not "in step"
if (_endIndex - _startIndex == stepSize) {
// Move the indices forward by the step size, step forward
_startIndex += stepSize;
_endIndex += stepSize;
} else {
// Correct the end index to be in step
_endIndex = _startIndex + stepSize;
}
if (_endIndex > originalModList.activeMods.length) {
// If we are at the end of the list, move the start index such that we return
// At most the step size amount of mods
_endIndex = originalModList.activeMods.length;
_startIndex = (_endIndex - stepSize).clamp(0, _endIndex);
}
_startIndex = _startIndex.clamp(0, totalMods);
_endIndex = (_startIndex + stepSize).clamp(0, totalMods);
final subset = originalModList.activeMods.keys.toList().sublist(
_startIndex,
@@ -69,24 +75,20 @@ class ModListTroubleshooter {
currentModList.disableAll();
currentModList.enableMods(subset);
_startIndex = _endIndex;
return currentModList;
}
ModList linearBackward({int stepSize = 20}) {
final totalMods = originalModList.activeMods.length;
if (_endIndex <= 0 || _startIndex == _endIndex) {
int newEnd = stepSize;
if (newEnd > totalMods) {
newEnd = totalMods;
}
_endIndex = newEnd;
if (_endIndex - _startIndex == stepSize) {
_startIndex -= stepSize;
_endIndex -= stepSize;
} else {
_startIndex = _endIndex - stepSize;
}
if (_startIndex < 0) {
_startIndex = 0;
_endIndex = stepSize.clamp(0, originalModList.activeMods.length);
}
_endIndex = _endIndex.clamp(0, totalMods);
_startIndex = (_endIndex - stepSize).clamp(0, _endIndex);
final subset = originalModList.activeMods.keys.toList().sublist(
_startIndex,
@@ -95,8 +97,6 @@ class ModListTroubleshooter {
currentModList.disableAll();
currentModList.enableMods(subset);
_endIndex = _startIndex;
return currentModList;
}