@@ -115,6 +115,26 @@ namespace
115
115
textInput.drawInRoi ( field.x + 4 + ( maxFileNameWidth - textInput.width () ) / 2 , field.y + 4 , output, field );
116
116
}
117
117
118
+ struct CompareByTimestamp
119
+ {
120
+ bool operator ()( const Maps::FileInfo & info, uint32_t timestamp ) const
121
+ {
122
+ return info.timestamp > timestamp;
123
+ }
124
+ bool operator ()( uint32_t timestamp, const Maps::FileInfo & info ) const
125
+ {
126
+ return timestamp > info.timestamp ;
127
+ }
128
+ };
129
+
130
+ struct CompareByFilename
131
+ {
132
+ bool operator ()( const Maps::FileInfo & info, const std::string & filename ) const
133
+ {
134
+ return Maps::CaseInsensitiveCompare ( info.filename , filename );
135
+ }
136
+ };
137
+
118
138
class FileInfoListBox : public Interface ::ListBox<Maps::FileInfo>
119
139
{
120
140
public:
@@ -341,16 +361,16 @@ namespace
341
361
if ( !lastfile.empty () ) {
342
362
filename = System::GetStem ( lastfile );
343
363
charInsertPos = filename.size ();
344
-
345
- MapsFileInfoList::iterator it = lists. begin ();
346
- for ( ; it != lists.end (); ++it ) {
347
- if ( it-> filename == lastfile ) {
348
- break ;
349
- }
364
+ MapsFileInfoList::const_iterator it;
365
+ if ( settings. GetSaveFileSortingMethod () == SaveFileSortingMethod::FILENAME ) {
366
+ it = std::lower_bound ( lists.cbegin (), lists. cend (), lastfile, CompareByFilename () );
367
+ }
368
+ else {
369
+ it = std::find_if ( it, lists. cend (), [&lastfile]( const Maps::FileInfo & info ) { return info. filename == lastfile; } );
350
370
}
351
371
352
- if ( it != lists.end () ) {
353
- listbox.SetCurrent ( std::distance ( lists.begin (), it ) );
372
+ if ( it != lists.cend () ) {
373
+ listbox.SetCurrent ( std::distance ( lists.cbegin (), it ) );
354
374
}
355
375
else {
356
376
if ( !isEditing ) {
@@ -485,22 +505,32 @@ namespace
485
505
}
486
506
else if ( le.MouseClickLeft ( buttonSort.area () ) ) {
487
507
const int currentId = listbox.getCurrentId ();
508
+ std::string lastChoice{};
509
+ uint32_t lastChoiceTimestamp{};
510
+ if ( currentId >= 0 && static_cast <size_t >( currentId ) < lists.size () ) {
511
+ lastChoice = lists[currentId].filename ;
512
+ lastChoiceTimestamp = lists[currentId].timestamp ;
513
+ }
488
514
489
515
settings.changeSaveFileSortingMethod ();
490
516
sortMapInfos ( lists );
491
517
listUpdated = true ;
492
518
493
519
// re-select the last selected file if any, unless we're typing in the list box
494
- if ( !filename.empty () && !isListboxSelected ) {
495
- const std::string lastChoice = System::concatPath ( Game::GetSaveDir (), filename + Game::GetSaveFileExtension () );
496
-
520
+ if ( !lastChoice.empty () ) {
497
521
MapsFileInfoList::const_iterator it = lists.cbegin ();
498
- for ( ; it != lists.end (); ++it ) {
499
- if ( it->filename == lastChoice ) {
500
- break ;
501
- }
522
+ MapsFileInfoList::const_iterator end = lists.cend ();
523
+ const SaveFileSortingMethod sortingMethod = settings.GetSaveFileSortingMethod ();
524
+
525
+ if ( sortingMethod == SaveFileSortingMethod::TIMESTAMP ) {
526
+ std::tie ( it, end ) = std::equal_range ( lists.cbegin (), lists.cend (), lastChoiceTimestamp, CompareByTimestamp () );
527
+ it = std::find_if ( it, end, [&lastChoice]( const Maps::FileInfo & info ) { return info.filename == lastChoice; } );
502
528
}
503
- if ( it != lists.cend () ) {
529
+ else {
530
+ it = std::lower_bound ( lists.cbegin (), lists.cend (), lastChoice, CompareByFilename () );
531
+ }
532
+
533
+ if ( it != end ) {
504
534
const int newId = static_cast <int >( std::distance ( lists.cbegin (), it ) );
505
535
if ( newId != currentId ) {
506
536
listbox.SetCurrent ( newId );
0 commit comments