diff --git a/LICENSE b/LICENSE index 8fb34fd..eae4b64 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/src/include/menu.hpp b/src/include/menu.hpp index 7bac1c8..612ce91 100644 --- a/src/include/menu.hpp +++ b/src/include/menu.hpp @@ -21,8 +21,8 @@ class Entry Entry(std::string label, std::function 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(); }; diff --git a/src/menu.cpp b/src/menu.cpp index 99c754c..7882ea0 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -1,3 +1,4 @@ +#include #include #include #include "cartridge.hpp" @@ -10,7 +11,7 @@ using namespace std; Entry::Entry(string label, function 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()