Sort files by name in the menu
This commit is contained in:
parent
ecd3bc07db
commit
9a85d09f29
2
LICENSE
2
LICENSE
|
@ -1,6 +1,6 @@
|
||||||
BSD 2-Clause License
|
BSD 2-Clause License
|
||||||
|
|
||||||
Copyright (c) 2016, Andrea Orru
|
Copyright (c) 2020, Andrea Orru
|
||||||
All rights reserved.
|
All rights reserved.
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
|
|
@ -21,8 +21,8 @@ class Entry
|
||||||
Entry(std::string label, std::function<void()> callback = []{});
|
Entry(std::string label, std::function<void()> callback = []{});
|
||||||
~Entry();
|
~Entry();
|
||||||
|
|
||||||
void setLabel(std::string label);
|
void set_label(std::string label);
|
||||||
inline std::string& getLabel() { return label; }
|
inline std::string& get_label() { return label; }
|
||||||
|
|
||||||
virtual void select() { selected = true; };
|
virtual void select() { selected = true; };
|
||||||
virtual void unselect() { selected = false; };
|
virtual void unselect() { selected = false; };
|
||||||
|
@ -56,6 +56,7 @@ class Menu
|
||||||
|
|
||||||
void add(Entry* entry);
|
void add(Entry* entry);
|
||||||
void clear();
|
void clear();
|
||||||
|
void sort_by_label();
|
||||||
void update(u8 const* keys);
|
void update(u8 const* keys);
|
||||||
void render();
|
void render();
|
||||||
};
|
};
|
||||||
|
|
26
src/menu.cpp
26
src/menu.cpp
|
@ -1,3 +1,4 @@
|
||||||
|
#include <algorithm>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "cartridge.hpp"
|
#include "cartridge.hpp"
|
||||||
|
@ -10,7 +11,7 @@ using namespace std;
|
||||||
|
|
||||||
Entry::Entry(string label, function<void()> callback) : callback(callback)
|
Entry::Entry(string label, function<void()> callback) : callback(callback)
|
||||||
{
|
{
|
||||||
setLabel(label);
|
set_label(label);
|
||||||
}
|
}
|
||||||
|
|
||||||
Entry::~Entry()
|
Entry::~Entry()
|
||||||
|
@ -19,7 +20,7 @@ Entry::~Entry()
|
||||||
SDL_DestroyTexture(redTexture);
|
SDL_DestroyTexture(redTexture);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Entry::setLabel(string label)
|
void Entry::set_label(string label)
|
||||||
{
|
{
|
||||||
this->label = label;
|
this->label = label;
|
||||||
|
|
||||||
|
@ -37,7 +38,7 @@ void Entry::render(int x, int y) {
|
||||||
ControlEntry::ControlEntry(string action, SDL_Scancode* key) : key(key),
|
ControlEntry::ControlEntry(string action, SDL_Scancode* key) : key(key),
|
||||||
Entry::Entry(
|
Entry::Entry(
|
||||||
action,
|
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), []{});
|
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),
|
ControlEntry::ControlEntry(string action, int* button) : button(button),
|
||||||
Entry::Entry(
|
Entry::Entry(
|
||||||
action,
|
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), []{});
|
this->keyEntry = new Entry(to_string(*button), []{});
|
||||||
}
|
}
|
||||||
|
@ -66,6 +67,17 @@ void Menu::clear()
|
||||||
cursor = 0;
|
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)
|
void Menu::update(u8 const* keys)
|
||||||
{
|
{
|
||||||
int oldCursor = cursor;
|
int oldCursor = cursor;
|
||||||
|
@ -117,17 +129,15 @@ void FileMenu::change_dir(string dir)
|
||||||
if (name[0] == '.' and name != "..") continue;
|
if (name[0] == '.' and name != "..") continue;
|
||||||
|
|
||||||
if (dirp->d_type == DT_DIR)
|
if (dirp->d_type == DT_DIR)
|
||||||
{
|
|
||||||
add(new Entry(name + "/",
|
add(new Entry(name + "/",
|
||||||
[=]{ change_dir(path); }));
|
[=]{ change_dir(path); }));
|
||||||
}
|
|
||||||
else if (name.size() > 4 and name.substr(name.size() - 4) == ".nes")
|
else if (name.size() > 4 and name.substr(name.size() - 4) == ".nes")
|
||||||
{
|
|
||||||
add(new Entry(name,
|
add(new Entry(name,
|
||||||
[=]{ Cartridge::load(path.c_str()); toggle_pause(); }));
|
[=]{ Cartridge::load(path.c_str()); toggle_pause(); }));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
closedir(dp);
|
closedir(dp);
|
||||||
|
sort_by_label();
|
||||||
}
|
}
|
||||||
|
|
||||||
FileMenu::FileMenu()
|
FileMenu::FileMenu()
|
||||||
|
|
Reference in a new issue