summaryrefslogtreecommitdiff
path: root/lib/nes/app/ui
diff options
context:
space:
mode:
authorHannes Schulze <haschu0103@outlook.de>2025-02-02 22:49:01 +0100
committerHannes Schulze <haschu0103@outlook.de>2025-02-02 22:49:01 +0100
commit0f22ed2ef160eef2da6080782943b22c9539650d (patch)
tree1b43aada2d0183273e79b4d1db7b1e60dd809855 /lib/nes/app/ui
parent4fe67b36e3bbe6ea5505ed7233c245be9374f0ce (diff)
wip: hacky file browser
Diffstat (limited to 'lib/nes/app/ui')
-rw-r--r--lib/nes/app/ui/action.hh43
-rw-r--r--lib/nes/app/ui/screen-browser.cc79
-rw-r--r--lib/nes/app/ui/screen-browser.hh26
-rw-r--r--lib/nes/app/ui/screen-title.cc8
4 files changed, 126 insertions, 30 deletions
diff --git a/lib/nes/app/ui/action.hh b/lib/nes/app/ui/action.hh
index 52e5ba6..b701606 100644
--- a/lib/nes/app/ui/action.hh
+++ b/lib/nes/app/ui/action.hh
@@ -1,15 +1,44 @@
#pragma once
+#include <string_view>
+
namespace nes::app
{
/// An action for the application to process after a scene has handled its events.
- enum class action
+ class action
{
- none,
- go_to_title,
- go_to_browser,
- go_to_settings,
- go_to_help,
- launch_game,
+ public:
+ enum class type
+ {
+ none,
+ go_to_title,
+ go_to_browser,
+ go_to_settings,
+ go_to_help,
+ launch_game,
+ };
+
+ private:
+ type type_{ type::none };
+ std::string_view file_name_;
+
+ 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 }; }
+
+ auto get_type() const -> type { return type_; }
+ auto get_file_name() const -> std::string_view { return file_name_; }
+
+ private:
+ explicit action(type const type, std::string_view const file_name)
+ : type_{ type }
+ , file_name_{ file_name }
+ {
+ }
};
} // namespace nes::app \ No newline at end of file
diff --git a/lib/nes/app/ui/screen-browser.cc b/lib/nes/app/ui/screen-browser.cc
index da2d82d..daca18e 100644
--- a/lib/nes/app/ui/screen-browser.cc
+++ b/lib/nes/app/ui/screen-browser.cc
@@ -1,30 +1,53 @@
#include "nes/app/ui/screen-browser.hh"
#include "nes/app/graphics/renderer.hh"
#include "nes/app/graphics/color.hh"
+#include "nes/app/graphics/tiles/icons.hh"
#include "nes/app/input/input-device-keyboard.hh"
#include "nes/common/string-builder.hh"
+#include "nes/common/path-view.hh"
namespace nes::app
{
auto screen_browser::selection_impl::render_item(
- renderer& renderer, i32 const& item, i32 const x, i32 const y, u32 const width, color const color) const -> void
+ renderer& renderer, item const& item, i32 const x, i32 const y, u32 const width,
+ color const color) const -> void
{
- renderer.render_text_format(x, y, color, text_attributes{}.set_max_width(width), "Item {}", item);
+ switch (item.data.get_type())
+ {
+ case file_browser::item_type::file: renderer.render_mask_tile(x, y, tiles::icon_cartridge, color); break;
+ case file_browser::item_type::directory: renderer.render_mask_tile(x, y, tiles::icon_directory, color); break;
+ }
+
+ renderer.render_text(x + 2, y, item.data.get_name(), color, text_attributes{}
+ .set_ellipsize_mode(ellipsize_mode::end)
+ .set_max_width(width - 2));
}
- auto screen_browser::selection_impl::load_page(int (&items)[5], u32 const page) -> u32
+ auto screen_browser::selection_impl::load_page(item (&items)[8], u32 const page) -> u32
{
- for (auto i = u32{ 0 }; i < 5; ++i)
+ auto offset = u32{ 0 };
+ if (page == 0 && file_browser_.get_path().get_component_count() > 0)
+ {
+ items[0].is_parent = true;
+ items[0].data = file_browser::item{ file_browser::item_type::directory, "[Parent Directory]" };
+ offset = 1;
+ }
+ file_browser::item buffer[8];
+ auto const count = file_browser_.read_items(page * 8, buffer, 8 - offset);
+ for (auto i = u32{ 0 }; i < count; ++i)
{
- items[i] = static_cast<i32>(page * 5 + i);
+ items[i + offset].is_parent = false;
+ items[i + offset].data = buffer[i];
}
- return 5;
+ return count + offset;
}
- screen_browser::screen_browser(input_device_keyboard& keyboard)
+ screen_browser::screen_browser(input_device_keyboard& keyboard, file_browser& file_browser)
: keyboard_{ keyboard }
+ , file_browser_{ file_browser }
+ , selection_{ file_browser }
{
- selection_.set_page_count(2);
+ selection_.set_page_count(std::max(u32{ 1 }, (file_browser_.read_item_count() + 7) / 8));
}
auto screen_browser::render(renderer& renderer) -> void
@@ -32,11 +55,21 @@ namespace nes::app
renderer.render_fill(color::background_primary);
renderer.render_rect(0, 0, 32, 3, color::background_secondary);
- renderer.render_text(2, 1, "Load Game", color::foreground_primary);
- renderer.render_text_format(
+ auto const page_length = renderer.render_text_format(
30, 1, color::foreground_secondary, text_attributes{}.set_alignment(text_alignment::right),
"Page {}/{}", selection_.get_current_page() + 1, selection_.get_page_count());
+ char path_buffer[32];
+ auto path_builder = string_builder{ path_buffer, sizeof(path_buffer) };
+ for (auto const component : file_browser_.get_path())
+ {
+ path_builder.append_format("> {} ", component);
+ }
+ path_builder.append_string(">");
+ renderer.render_text(
+ 2, 1, path_builder.get_result(), color::foreground_primary,
+ text_attributes{}.set_max_width(26 - page_length).set_ellipsize_mode(ellipsize_mode::end));
+
selection_.render(renderer);
}
@@ -58,14 +91,34 @@ namespace nes::app
}
else if (event == input_event::key_down(key::backspace))
{
- return action::go_to_title;
+ return action::go_to_title();
}
else if (event == input_event::key_down(key::enter))
{
- return action::launch_game;
+ auto selected = selection_.get_selected();
+ if (selected)
+ {
+ if (selected->is_parent)
+ {
+ file_browser_.navigate_up();
+ selection_.set_page_count(std::max(u32{ 1 }, (file_browser_.read_item_count() + 7) / 8));
+ }
+ else
+ {
+ switch (selected->data.get_type())
+ {
+ case file_browser::item_type::directory:
+ file_browser_.navigate(selected->data.get_name());
+ selection_.set_page_count(std::max(u32{ 1 }, (file_browser_.read_item_count() + 7) / 8));
+ break;
+ case file_browser::item_type::file:
+ return action::launch_game(selected->data.get_name());
+ }
+ }
+ }
}
}
- return action::none;
+ return action{};
}
} // namespace nes::app \ No newline at end of file
diff --git a/lib/nes/app/ui/screen-browser.hh b/lib/nes/app/ui/screen-browser.hh
index eb66266..7a711c5 100644
--- a/lib/nes/app/ui/screen-browser.hh
+++ b/lib/nes/app/ui/screen-browser.hh
@@ -2,6 +2,7 @@
#include "nes/app/ui/screen.hh"
#include "nes/app/ui/selection.hh"
+#include "nes/app/file-browser.hh"
namespace nes::app
{
@@ -10,21 +11,34 @@ namespace nes::app
/// An interface which allows the user to browse the filesystem for a game.
class screen_browser final : public screen
{
- class selection_impl final : public selection<i32, 5>
+ struct item
{
+ bool is_parent{ false };
+ file_browser::item data{ file_browser::item_type::directory, "" };
+ };
+
+ class selection_impl final : public selection<item, 8>
+ {
+ file_browser& file_browser_;
+
public:
- using selection::selection;
+ explicit selection_impl(file_browser& file_browser)
+ : selection{ 2, 5, 28 }
+ , file_browser_{ file_browser }
+ {
+ }
protected:
- auto render_item(renderer&, i32 const&, i32 x, i32 y, u32 width, color) const -> void override;
- auto load_page(i32(&)[5], u32 page) -> u32 override;
+ auto render_item(renderer&, item const&, i32 x, i32 y, u32 width, color) const -> void override;
+ auto load_page(item(&)[8], u32 page) -> u32 override;
};
input_device_keyboard& keyboard_;
- selection_impl selection_{ 2, 5, 28 };
+ file_browser& file_browser_;
+ selection_impl selection_;
public:
- explicit screen_browser(input_device_keyboard&);
+ explicit screen_browser(input_device_keyboard&, file_browser&);
auto render(renderer&) -> void override;
auto process_events() -> action override;
diff --git a/lib/nes/app/ui/screen-title.cc b/lib/nes/app/ui/screen-title.cc
index 32a6ea6..15cd601 100644
--- a/lib/nes/app/ui/screen-title.cc
+++ b/lib/nes/app/ui/screen-title.cc
@@ -61,13 +61,13 @@ namespace nes::app
{
switch (*selection_.get_selected())
{
- case item::load_game: return action::go_to_browser;
- case item::settings: return action::go_to_settings;
- case item::help: return action::go_to_help;
+ case item::load_game: return action::go_to_browser();
+ case item::settings: return action::go_to_settings();
+ case item::help: return action::go_to_help();
}
}
}
- return action::none;
+ return action{};
}
} // namespace nes::app \ No newline at end of file