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