From dd22e1ab7ab7b5bac841dae2b62bfa200b36c4d8 Mon Sep 17 00:00:00 2001 From: Hannes Schulze Date: Wed, 12 Feb 2025 23:57:32 +0100 Subject: add encryption/decryption --- lib/nes/app/ui/CMakeLists.txt | 4 ++- lib/nes/app/ui/screen-browser.cc | 8 +++++ lib/nes/app/ui/screen-prompt-key.cc | 66 +++++++++++++++++++++++++++++++++++++ lib/nes/app/ui/screen-prompt-key.hh | 28 ++++++++++++++++ 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 lib/nes/app/ui/screen-prompt-key.cc create mode 100644 lib/nes/app/ui/screen-prompt-key.hh (limited to 'lib/nes/app/ui') diff --git a/lib/nes/app/ui/CMakeLists.txt b/lib/nes/app/ui/CMakeLists.txt index c43bb13..1f8d138 100644 --- a/lib/nes/app/ui/CMakeLists.txt +++ b/lib/nes/app/ui/CMakeLists.txt @@ -14,4 +14,6 @@ target_sources( screen-error.hh screen-error.cc screen-confirm-quit.hh - screen-confirm-quit.cc) \ No newline at end of file + screen-confirm-quit.cc + screen-prompt-key.hh + screen-prompt-key.cc) \ 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 50b0060..84fb647 100644 --- a/lib/nes/app/ui/screen-browser.cc +++ b/lib/nes/app/ui/screen-browser.cc @@ -111,6 +111,14 @@ 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(); diff --git a/lib/nes/app/ui/screen-prompt-key.cc b/lib/nes/app/ui/screen-prompt-key.cc new file mode 100644 index 0000000..e15d724 --- /dev/null +++ b/lib/nes/app/ui/screen-prompt-key.cc @@ -0,0 +1,66 @@ +#include "nes/app/ui/screen-prompt-key.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_prompt_key::screen_prompt_key(input_device_keyboard& keyboard) + : keyboard_{ keyboard } + { + } + + auto screen_prompt_key::reset(string_view const file_name) -> void + { + file_name_ = file_name; + key_.clear(); + } + + auto screen_prompt_key::render(renderer& renderer) -> void + { + renderer.render_rect(2, 8, 28, 10, color::background_secondary); + renderer.render_border(2, 8, 28, 10, color::foreground_primary); + renderer.render_rect(4, 12, 24, 1, color::background_primary); + + auto const attrs = text_attributes{} + .set_alignment(text_alignment::center) + .set_ellipsize_mode(ellipsize_mode::end) + .set_max_width(26); + renderer.render_text(16, 10, "Key:", color::foreground_primary, attrs); + auto const key_attrs = text_attributes{} + .set_alignment(text_alignment::left) + .set_ellipsize_mode(ellipsize_mode::clip) + .set_max_width(24); + renderer.render_text_format(4, 12, color::foreground_primary, key_attrs, "{}_", key_.get_result()); + renderer.render_text(16, 15, "> OK <", color::accent_primary, attrs); + } + + auto screen_prompt_key::process_events() -> action + { + while (auto const event = keyboard_.poll_event()) + { + if (event == input_event::key_down(key::escape)) + { + return action::close_popup(); + } + else if (event == input_event::key_down(key::enter)) + { + return action::launch_game(file_name_, key_.get_result()); + } + else if (event == input_event::key_down(key::backspace)) + { + key_.remove_last(); + } + else if (event.get_type() == input_event::type::key_down) + { + auto const symbol = to_ascii(event.get_key(), keyboard_.read_modifiers()); + if (symbol != 0) + { + key_.append_char(symbol); + } + } + } + + return action{}; + } +} // namespace nes::app \ No newline at end of file diff --git a/lib/nes/app/ui/screen-prompt-key.hh b/lib/nes/app/ui/screen-prompt-key.hh new file mode 100644 index 0000000..337e1b6 --- /dev/null +++ b/lib/nes/app/ui/screen-prompt-key.hh @@ -0,0 +1,28 @@ +#pragma once + +#include "nes/app/ui/screen.hh" +#include "nes/common/containers/string-builder.hh" +#include "nes/common/containers/string-view.hh" + +namespace nes::app +{ + class input_device_keyboard; + + /// Prompts the user for a decryption key. + class screen_prompt_key final : public screen + { + static constexpr auto max_key_length = u32{ 23 }; + + input_device_keyboard& keyboard_; + string_view file_name_; + string_buffer key_; + + public: + explicit screen_prompt_key(input_device_keyboard&); + + auto reset(string_view file_name) -> void; + + auto render(renderer&) -> void override; + auto process_events() -> action override; + }; +} // namespace nes::app \ No newline at end of file -- cgit v1.3.1