summaryrefslogtreecommitdiff
path: root/lib/nes/app/ui
diff options
context:
space:
mode:
authorHannes Schulze <haschu0103@outlook.de>2025-02-04 23:39:24 +0100
committerHannes Schulze <haschu0103@outlook.de>2025-02-04 23:39:24 +0100
commit64308c9e520dbd8e72b675d0ba064ecdaf9bd494 (patch)
treeff8dc176d62340df9565e424f19b9d6349c4e8b8 /lib/nes/app/ui
parent79553ab1fa3d707612e80dd03bf95ca65fa0c411 (diff)
add error handling to ui
Diffstat (limited to 'lib/nes/app/ui')
-rw-r--r--lib/nes/app/ui/CMakeLists.txt5
-rw-r--r--lib/nes/app/ui/action.hh58
-rw-r--r--lib/nes/app/ui/screen-browser.cc22
-rw-r--r--lib/nes/app/ui/screen-browser.hh2
-rw-r--r--lib/nes/app/ui/screen-error.cc38
-rw-r--r--lib/nes/app/ui/screen-error.hh27
-rw-r--r--lib/nes/app/ui/screen.hh2
7 files changed, 81 insertions, 73 deletions
diff --git a/lib/nes/app/ui/CMakeLists.txt b/lib/nes/app/ui/CMakeLists.txt
index 0980e97..de42744 100644
--- a/lib/nes/app/ui/CMakeLists.txt
+++ b/lib/nes/app/ui/CMakeLists.txt
@@ -1,10 +1,11 @@
target_sources(
nes
PRIVATE
- action.hh
selection.hh
screen.hh
screen-title.hh
screen-title.cc
screen-browser.hh
- screen-browser.cc) \ No newline at end of file
+ screen-browser.cc
+ screen-error.hh
+ screen-error.cc) \ No newline at end of file
diff --git a/lib/nes/app/ui/action.hh b/lib/nes/app/ui/action.hh
deleted file mode 100644
index 66f77f8..0000000
--- a/lib/nes/app/ui/action.hh
+++ /dev/null
@@ -1,58 +0,0 @@
-#pragma once
-
-#include "nes/common/status.hh"
-#include <string_view>
-
-namespace nes::app
-{
- /// An action for the application to process after a scene has handled its events.
- class action
- {
- public:
- enum class type
- {
- none,
- go_to_title,
- go_to_browser,
- 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, "", 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, status const error)
- : type_{ type }
- , file_name_{ file_name }
- , error_{ error }
- {
- }
- };
-} // 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 14ddc23..a82e96d 100644
--- a/lib/nes/app/ui/screen-browser.cc
+++ b/lib/nes/app/ui/screen-browser.cc
@@ -11,13 +11,13 @@ namespace nes::app
renderer& renderer, item const& item, i32 const x, i32 const y, u32 const width,
color const color) const -> void
{
- switch (item.data.get_type())
+ switch (item.base.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{}
+ renderer.render_text(x + 2, y, item.base.get_name(), color, text_attributes{}
.set_ellipsize_mode(ellipsize_mode::end)
.set_max_width(width - 2));
}
@@ -31,7 +31,7 @@ namespace nes::app
if (page == 0)
{
items[count].is_parent = true;
- items[count].data = file_browser::item{ file_browser::item_type::directory, "[Parent Directory]" };
+ items[count].base = file_browser::item{ file_browser::item_type::directory, "[Parent Directory]" };
count += 1;
}
else
@@ -48,7 +48,7 @@ namespace nes::app
if (!file_browser_.read_next(&item)) { break; }
items[count].is_parent = false;
- items[count].data = item;
+ items[count].base = item;
count += 1;
}
@@ -101,7 +101,7 @@ namespace nes::app
{
if (auto const s = file_browser_.navigate_up(); s != status::success)
{
- return action::show_error(s);
+ return action::show_error("Unable to navigate to directory", s);
}
refresh();
navigation_steps_ -= 1;
@@ -120,23 +120,23 @@ namespace nes::app
{
if (auto const s = file_browser_.navigate_up(); s != status::success)
{
- return action::show_error(s);
+ return action::show_error("Unable to navigate to directory", s);
}
refresh();
if (navigation_steps_ > 0) { navigation_steps_ -= 1; }
}
- else if (selected->data.get_type() == file_browser::item_type::directory)
+ else if (selected->base.get_type() == file_browser::item_type::directory)
{
- if (auto const s = file_browser_.navigate(selected->data.get_name()); s != status::success)
+ if (auto const s = file_browser_.navigate(selected->base.get_name()); s != status::success)
{
- return action::show_error(s);
+ return action::show_error("Unable to navigate to directory", s);
}
refresh();
navigation_steps_ += 1;
}
- else if (selected->data.get_type() == file_browser::item_type::file)
+ else if (selected->base.get_type() == file_browser::item_type::file)
{
- return action::launch_game(selected->data.get_name());
+ return action::launch_game(selected->base.get_name());
}
}
}
diff --git a/lib/nes/app/ui/screen-browser.hh b/lib/nes/app/ui/screen-browser.hh
index 3870f02..0ccf0b6 100644
--- a/lib/nes/app/ui/screen-browser.hh
+++ b/lib/nes/app/ui/screen-browser.hh
@@ -16,7 +16,7 @@ namespace nes::app
struct item
{
bool is_parent{ false };
- file_browser::item data{ file_browser::item_type::directory, "" };
+ file_browser::item base{};
};
class selection_impl final : public selection<item, page_size>
diff --git a/lib/nes/app/ui/screen-error.cc b/lib/nes/app/ui/screen-error.cc
new file mode 100644
index 0000000..67a3244
--- /dev/null
+++ b/lib/nes/app/ui/screen-error.cc
@@ -0,0 +1,38 @@
+#include "nes/app/ui/screen-error.hh"
+#include "nes/app/graphics/renderer.hh"
+#include "nes/app/graphics/color.hh"
+#include "nes/app/input/input-device-keyboard.hh"
+
+namespace nes::app
+{
+ screen_error::screen_error(input_device_keyboard& keyboard)
+ : keyboard_{ keyboard }
+ {
+ }
+
+ auto screen_error::render(renderer& renderer) -> void
+ {
+ // TODO: Nicer box
+ renderer.render_rect(2, 9, 28, 5, color::background_secondary);
+
+ auto const attrs = text_attributes{}
+ .set_alignment(text_alignment::center)
+ .set_ellipsize_mode(ellipsize_mode::end)
+ .set_max_width(26);
+ renderer.render_text_format(16, 10, color::foreground_primary, attrs, "{}:", message_);
+ renderer.render_text(16, 12, to_string(error_), color::foreground_primary, attrs);
+ }
+
+ auto screen_error::process_events() -> action
+ {
+ while (auto const event = keyboard_.poll_event())
+ {
+ if (event == input_event::key_down(key::enter))
+ {
+ return action::close_popup();
+ }
+ }
+
+ return action{};
+ }
+} // namespace nes::app \ No newline at end of file
diff --git a/lib/nes/app/ui/screen-error.hh b/lib/nes/app/ui/screen-error.hh
new file mode 100644
index 0000000..ec877e1
--- /dev/null
+++ b/lib/nes/app/ui/screen-error.hh
@@ -0,0 +1,27 @@
+#pragma once
+
+#include "nes/app/ui/screen.hh"
+
+namespace nes::app
+{
+ class input_device_keyboard;
+
+ /// An error popup.
+ class screen_error final : public screen
+ {
+ input_device_keyboard& keyboard_;
+ std::string_view message_;
+ status error_{ status::success };
+
+ public:
+ explicit screen_error(input_device_keyboard&);
+
+ auto get_message() const -> std::string_view { return message_; }
+ auto get_error() const -> status { return error_; }
+ auto set_message(std::string_view const v) { message_ = v; }
+ auto set_error(status const v) { error_ = v; }
+
+ auto render(renderer&) -> void override;
+ auto process_events() -> action override;
+ };
+} // namespace nes::app \ No newline at end of file
diff --git a/lib/nes/app/ui/screen.hh b/lib/nes/app/ui/screen.hh
index 07e9d6f..6d30b7a 100644
--- a/lib/nes/app/ui/screen.hh
+++ b/lib/nes/app/ui/screen.hh
@@ -1,6 +1,6 @@
#pragma once
-#include "nes/app/ui/action.hh"
+#include "nes/app/action.hh"
namespace nes::app
{