diff options
| author | Hannes Schulze <haschu0103@outlook.de> | 2025-02-04 23:11:08 +0100 |
|---|---|---|
| committer | Hannes Schulze <haschu0103@outlook.de> | 2025-02-04 23:11:08 +0100 |
| commit | 79553ab1fa3d707612e80dd03bf95ca65fa0c411 (patch) | |
| tree | f44bec41548d569cfca43c44e0e4ffa7b72ed4ed /lib/nes/app | |
| parent | 9c45e9c7df1efa5bc1c8e73b6eaef4726b828d3e (diff) | |
refactor file browser
Diffstat (limited to 'lib/nes/app')
| -rw-r--r-- | lib/nes/app/application.cc | 18 | ||||
| -rw-r--r-- | lib/nes/app/file-browser.hh | 20 | ||||
| -rw-r--r-- | lib/nes/app/input/input-buffer.cc | 2 | ||||
| -rw-r--r-- | lib/nes/app/input/input-manager.cc | 7 | ||||
| -rw-r--r-- | lib/nes/app/ui/action.hh | 26 | ||||
| -rw-r--r-- | lib/nes/app/ui/screen-browser.cc | 28 |
6 files changed, 72 insertions, 29 deletions
diff --git a/lib/nes/app/application.cc b/lib/nes/app/application.cc index b528e61..6b7253e 100644 --- a/lib/nes/app/application.cc +++ b/lib/nes/app/application.cc @@ -46,7 +46,7 @@ namespace nes::app // display_proxy::switch_buffers). console_->step(elapsed_time); - if (console_->get_status() != sys::status::success) + if (console_->get_status() != status::success) { std::cerr << "Invalid state: " << to_string(console_->get_status()) << std::endl; std::abort(); @@ -93,10 +93,15 @@ namespace nes::app case action::type::launch_game: { u8 buffer[sys::cartridge::max_file_size]; - auto const length = file_browser_.load(a.get_file_name(), buffer); + auto length = u32{ 0 }; + if (auto const s = file_browser_.load(a.get_file_name(), buffer, &length); s != status::success) + { + std::cerr << "Unable to open cartridge file: " << to_string(console_->get_status()) << std::endl; + std::abort(); + } - console_.emplace(display_, span<u8 const>{ buffer, length }); - if (console_->get_status() != sys::status::success) + console_.emplace(display_, span{ buffer, length }); + if (console_->get_status() != status::success) { std::cerr << "Unable to load cartridge: " << to_string(console_->get_status()) << std::endl; std::abort(); @@ -105,6 +110,11 @@ namespace nes::app display_.screen = nullptr; break; } + case action::type::show_error: + { + // TODO + break; + } } } } // namespace nes::app
\ No newline at end of file diff --git a/lib/nes/app/file-browser.hh b/lib/nes/app/file-browser.hh index 6ce55aa..ef5343c 100644 --- a/lib/nes/app/file-browser.hh +++ b/lib/nes/app/file-browser.hh @@ -2,6 +2,8 @@ #include "nes/common/types.hh" #include "nes/common/span.hh" +#include "nes/common/status.hh" +#include <string_view> namespace nes { @@ -10,6 +12,7 @@ namespace nes namespace nes::app { + /// A file browser that has a current directory whose items are iterated. class file_browser { public: @@ -46,12 +49,23 @@ namespace nes::app auto operator=(file_browser const&) -> file_browser& = delete; auto operator=(file_browser&&) -> file_browser& = delete; + /// Get the path components of the current working directory. virtual auto get_path() const -> path_view = 0; + + /// Go to directory entry at the given index. virtual auto seek(u32) -> void = 0; + + /// Read the directory entry at the current index. virtual auto read_next(item* out_item) -> bool = 0; - virtual auto navigate_up() -> void = 0; - virtual auto navigate(std::string_view) -> void = 0; - virtual auto load(std::string_view, span<u8>) -> u32 = 0; + + /// Change the current working directory to the parent directory. + virtual auto navigate_up() -> status = 0; + + /// Change the current working directory to an item. + virtual auto navigate(std::string_view) -> status = 0; + + /// Load a file in the current working directory into memory. + virtual auto load(std::string_view, span<u8>, u32* out_length) -> status = 0; protected: explicit file_browser() = default; diff --git a/lib/nes/app/input/input-buffer.cc b/lib/nes/app/input/input-buffer.cc index e8da51c..a9ccf2b 100644 --- a/lib/nes/app/input/input-buffer.cc +++ b/lib/nes/app/input/input-buffer.cc @@ -23,7 +23,7 @@ namespace nes::app auto it = event_queue_.begin(); if (it == event_queue_.end()) { return input_event{}; } - auto const event = *it; + auto const event = it; auto const k = get_key(*event); auto const pressed = event->pressed; event_queue_.remove(it); diff --git a/lib/nes/app/input/input-manager.cc b/lib/nes/app/input/input-manager.cc index 1af0f5b..ceb6690 100644 --- a/lib/nes/app/input/input-manager.cc +++ b/lib/nes/app/input/input-manager.cc @@ -68,8 +68,7 @@ namespace nes::app // This input is not assigned -> use the next available controller if available. while (it != controllers_.end()) { - auto controller = *it; - if (controller->is_reliable() && controller->get_index() == input_device_controller::index::unused) + if (it->is_reliable() && it->get_index() == input_device_controller::index::unused) { break; } @@ -79,9 +78,9 @@ namespace nes::app if (it != controllers_.end()) { // We've found an unused controller -> assign it. - inputs_[i].controller = *it; + inputs_[i].controller = &*it; inputs_[i].controller->set_index(static_cast<input_device_controller::index>(i)); - inputs_[i].device = *it; + inputs_[i].device = &*it; } else { diff --git a/lib/nes/app/ui/action.hh b/lib/nes/app/ui/action.hh index b701606..66f77f8 100644 --- a/lib/nes/app/ui/action.hh +++ b/lib/nes/app/ui/action.hh @@ -1,5 +1,6 @@ #pragma once +#include "nes/common/status.hh" #include <string_view> namespace nes::app @@ -16,28 +17,41 @@ namespace nes::app go_to_settings, go_to_help, launch_game, + show_error, }; private: type type_{ type::none }; std::string_view file_name_; + status error_{ status::success }; public: explicit action() = default; - static auto go_to_title() -> action { return action{ type::go_to_title, "" }; } - static auto go_to_browser() -> action { return action{ type::go_to_browser, "" }; } - static auto go_to_settings() -> action { return action{ type::go_to_settings, "" }; } - static auto go_to_help() -> action { return action{ type::go_to_help, "" }; } - static auto launch_game(std::string_view const file_name) -> action { return action{ type::launch_game, file_name }; } + static auto go_to_title() -> action { return action{ type::go_to_title, "", status::success }; } + static auto go_to_browser() -> action { return action{ type::go_to_browser, "", status::success }; } + static auto go_to_settings() -> action { return action{ type::go_to_settings, "", status::success }; } + static auto go_to_help() -> action { return action{ type::go_to_help, "", status::success }; } + + static auto launch_game(std::string_view const file_name) -> action + { + return action{ type::launch_game, file_name, status::success }; + } + + static auto show_error(status const error) + { + return action{ type::show_error, "", error }; + } auto get_type() const -> type { return type_; } auto get_file_name() const -> std::string_view { return file_name_; } + auto get_error() const -> status { return error_; } private: - explicit action(type const type, std::string_view const file_name) + explicit action(type const type, std::string_view const file_name, status const error) : type_{ type } , file_name_{ file_name } + , error_{ error } { } }; diff --git a/lib/nes/app/ui/screen-browser.cc b/lib/nes/app/ui/screen-browser.cc index 6a18d3c..14ddc23 100644 --- a/lib/nes/app/ui/screen-browser.cc +++ b/lib/nes/app/ui/screen-browser.cc @@ -99,7 +99,10 @@ namespace nes::app { if (navigation_steps_ > 0) { - file_browser_.navigate_up(); + if (auto const s = file_browser_.navigate_up(); s != status::success) + { + return action::show_error(s); + } refresh(); navigation_steps_ -= 1; } @@ -115,22 +118,25 @@ namespace nes::app { if (selected->is_parent) { - file_browser_.navigate_up(); + if (auto const s = file_browser_.navigate_up(); s != status::success) + { + return action::show_error(s); + } refresh(); if (navigation_steps_ > 0) { navigation_steps_ -= 1; } } - else + else if (selected->data.get_type() == file_browser::item_type::directory) { - switch (selected->data.get_type()) + if (auto const s = file_browser_.navigate(selected->data.get_name()); s != status::success) { - case file_browser::item_type::directory: - file_browser_.navigate(selected->data.get_name()); - refresh(); - navigation_steps_ += 1; - break; - case file_browser::item_type::file: - return action::launch_game(selected->data.get_name()); + return action::show_error(s); } + refresh(); + navigation_steps_ += 1; + } + else if (selected->data.get_type() == file_browser::item_type::file) + { + return action::launch_game(selected->data.get_name()); } } } |
