Sort files by name in the menu

This commit is contained in:
Andrea Orru 2020-05-11 00:39:52 +09:00
parent ecd3bc07db
commit 9a85d09f29
3 changed files with 22 additions and 11 deletions

View file

@ -1,6 +1,6 @@
BSD 2-Clause License
Copyright (c) 2016, Andrea Orru
Copyright (c) 2020, Andrea Orru
All rights reserved.
Redistribution and use in source and binary forms, with or without

View file

@ -21,8 +21,8 @@ class Entry
Entry(std::string label, std::function<void()> callback = []{});
~Entry();
void setLabel(std::string label);
inline std::string& getLabel() { return label; }
void set_label(std::string label);
inline std::string& get_label() { return label; }
virtual void select() { selected = true; };
virtual void unselect() { selected = false; };
@ -56,6 +56,7 @@ class Menu
void add(Entry* entry);
void clear();
void sort_by_label();
void update(u8 const* keys);
void render();
};

View file

@ -1,3 +1,4 @@
#include <algorithm>
#include <dirent.h>
#include <unistd.h>
#include "cartridge.hpp"
@ -10,7 +11,7 @@ using namespace std;
Entry::Entry(string label, function<void()> callback) : callback(callback)
{
setLabel(label);
set_label(label);
}
Entry::~Entry()
@ -19,7 +20,7 @@ Entry::~Entry()
SDL_DestroyTexture(redTexture);
}
void Entry::setLabel(string label)
void Entry::set_label(string label)
{
this->label = label;
@ -37,7 +38,7 @@ void Entry::render(int x, int y) {
ControlEntry::ControlEntry(string action, SDL_Scancode* key) : key(key),
Entry::Entry(
action,
[&]{ keyEntry->setLabel(SDL_GetScancodeName(*(this->key) = query_key())); })
[&]{ keyEntry->set_label(SDL_GetScancodeName(*(this->key) = query_key())); })
{
this->keyEntry = new Entry(SDL_GetScancodeName(*key), []{});
}
@ -45,7 +46,7 @@ ControlEntry::ControlEntry(string action, SDL_Scancode* key) : key(key),
ControlEntry::ControlEntry(string action, int* button) : button(button),
Entry::Entry(
action,
[&]{ keyEntry->setLabel(to_string(*(this->button) = query_button())); })
[&]{ keyEntry->set_label(to_string(*(this->button) = query_button())); })
{
this->keyEntry = new Entry(to_string(*button), []{});
}
@ -66,6 +67,17 @@ void Menu::clear()
cursor = 0;
}
void Menu::sort_by_label()
{
if (entries.empty())
return;
entries[0]->unselect();
sort(entries.begin(), entries.end(), [](Entry* a, Entry* b) {
return a->get_label() < b->get_label();
});
entries[0]->select();
}
void Menu::update(u8 const* keys)
{
int oldCursor = cursor;
@ -117,17 +129,15 @@ void FileMenu::change_dir(string dir)
if (name[0] == '.' and name != "..") continue;
if (dirp->d_type == DT_DIR)
{
add(new Entry(name + "/",
[=]{ change_dir(path); }));
}
else if (name.size() > 4 and name.substr(name.size() - 4) == ".nes")
{
add(new Entry(name,
[=]{ Cartridge::load(path.c_str()); toggle_pause(); }));
}
}
closedir(dp);
sort_by_label();
}
FileMenu::FileMenu()