diff options
| author | Hannes Schulze <hannes.hs.schulze@fau.de> | 2025-05-02 15:16:35 +0200 |
|---|---|---|
| committer | Hannes Schulze <hannes.hs.schulze@fau.de> | 2025-05-02 15:16:35 +0200 |
| commit | 91c9b3addf5f3db972fd28752855eda811221afe (patch) | |
| tree | e3f23bc153e66afa0481430c9c828f6d7a28859b /lib/nes/app | |
| parent | 24c521f5a4b9f2e1a98668e9d9f7b19b4c9a9bd7 (diff) | |
different action depending on file extension
Diffstat (limited to 'lib/nes/app')
| -rw-r--r-- | lib/nes/app/action.hh | 11 | ||||
| -rw-r--r-- | lib/nes/app/application.cc | 4 | ||||
| -rw-r--r-- | lib/nes/app/ui/screen-browser.cc | 35 | ||||
| -rw-r--r-- | lib/nes/app/ui/screen-browser.hh | 2 |
4 files changed, 40 insertions, 12 deletions
diff --git a/lib/nes/app/action.hh b/lib/nes/app/action.hh index c88a5f1..8caf969 100644 --- a/lib/nes/app/action.hh +++ b/lib/nes/app/action.hh @@ -2,6 +2,7 @@ #include "nes/common/containers/string-view.hh" #include "nes/common/status.hh" +#include <cstdio> namespace nes::app { @@ -22,6 +23,7 @@ namespace nes::app launch_game, show_error, prompt_key, + view_file, }; private: @@ -65,6 +67,13 @@ namespace nes::app return res; } + static auto view_file(string_view const file_name) -> action + { + auto res = action{ type::view_file }; + res.file_name_ = file_name; + return res; + } + auto get_type() const -> type { return type_; } auto get_file_name() const -> string_view { return file_name_; } auto get_key() const -> string_view { return key_; } @@ -77,4 +86,4 @@ namespace nes::app { } }; -} // namespace nes::app
\ No newline at end of file +} // namespace nes::app diff --git a/lib/nes/app/application.cc b/lib/nes/app/application.cc index 5646fa9..521cc40 100644 --- a/lib/nes/app/application.cc +++ b/lib/nes/app/application.cc @@ -191,6 +191,10 @@ namespace nes::app display_.visible_popup = nullptr; break; } + case action::type::view_file: + { + break; + } case action::type::show_error: { show_error(a.get_message(), a.get_error()); diff --git a/lib/nes/app/ui/screen-browser.cc b/lib/nes/app/ui/screen-browser.cc index 84fb647..080f869 100644 --- a/lib/nes/app/ui/screen-browser.cc +++ b/lib/nes/app/ui/screen-browser.cc @@ -8,6 +8,29 @@ namespace nes::app { + namespace + { + auto default_file_action(string_view const file_name) -> action + { + if (file_name.has_suffix(".nes")) + { + return action::launch_game(file_name); + } + else if (file_name.has_suffix(".aes")) + { + return action::prompt_key(file_name); + } + else if (file_name.has_suffix(".txt")) + { + return action::view_file(file_name); + } + else + { + return action::show_error("Unable to open file", status::error_unknown_file_type); + } + } + } // namespace + auto screen_browser::selection_impl::render_item( renderer& renderer, item const& item, i32 const x, i32 const y, u32 const width, color const c) const -> void { @@ -111,14 +134,6 @@ namespace nes::app return action::go_to_title(); } } - else if (event == input_event::key_down(key::letter_d)) - { - auto const selected = selection_.get_selected(); - if (selected && selected->entry.get_type() == file_browser::entry_type::file) - { - return action::prompt_key(selected->entry.get_name()); - } - } else if (event == input_event::key_down(key::enter)) { auto const selected = selection_.get_selected(); @@ -144,7 +159,7 @@ namespace nes::app } else if (selected->entry.get_type() == file_browser::entry_type::file) { - return action::launch_game(selected->entry.get_name()); + return default_file_action(selected->entry.get_name()); } } } @@ -163,4 +178,4 @@ namespace nes::app // Always show at least one page. selection_.set_page_count(max(u32{ 1 }, page_count)); } -} // namespace nes::app
\ No newline at end of file +} // namespace nes::app diff --git a/lib/nes/app/ui/screen-browser.hh b/lib/nes/app/ui/screen-browser.hh index 4ba6e73..d31bef3 100644 --- a/lib/nes/app/ui/screen-browser.hh +++ b/lib/nes/app/ui/screen-browser.hh @@ -49,4 +49,4 @@ namespace nes::app private: auto refresh() -> void; }; -} // namespace nes::app
\ No newline at end of file +} // namespace nes::app |
