mirror of
https://github.com/eried/portapack-mayhem.git
synced 2025-08-07 22:22:21 -04:00
parent
53fcdedb88
commit
00667cecf9
7 changed files with 472 additions and 192 deletions
|
@ -27,17 +27,21 @@
|
|||
#include "ui_navigation.hpp"
|
||||
#include "ui_painter.hpp"
|
||||
#include "ui_widget.hpp"
|
||||
//#include "ui_textentry.hpp"
|
||||
|
||||
#include "circular_buffer.hpp"
|
||||
#include "file.hpp"
|
||||
#include "optional.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace ui {
|
||||
|
||||
/* TODO:
|
||||
* - Copy on write into temp file so startup is fast.
|
||||
*/
|
||||
|
||||
enum class LineEnding : uint8_t {
|
||||
LF,
|
||||
CRLF
|
||||
|
@ -80,6 +84,7 @@ class FileWrapper {
|
|||
private:
|
||||
/* Number of newline offsets to cache. */
|
||||
static constexpr Offset max_newlines = 64;
|
||||
static constexpr size_t buffer_size = 512;
|
||||
|
||||
void initialize();
|
||||
std::string read(Offset offset, Offset length = 30);
|
||||
|
@ -107,6 +112,160 @@ class FileWrapper {
|
|||
CircularBuffer<Offset, max_newlines + 1> newlines_{};
|
||||
};
|
||||
|
||||
/* Control that renders a text file. */
|
||||
class TextViewer : public Widget {
|
||||
public:
|
||||
TextViewer(Rect parent_rect);
|
||||
|
||||
TextViewer(const TextViewer&) = delete;
|
||||
TextViewer(TextViewer&&) = delete;
|
||||
TextViewer& operator=(const TextViewer&) = delete;
|
||||
TextViewer& operator=(TextViewer&&) = delete;
|
||||
|
||||
std::function<void()> on_select{};
|
||||
std::function<void()> on_cursor_moved{};
|
||||
|
||||
void paint(Painter& painter) override;
|
||||
bool on_key(KeyEvent delta) override;
|
||||
bool on_encoder(EncoderEvent delta) override;
|
||||
|
||||
void redraw(bool redraw_text = false);
|
||||
|
||||
void set_file(FileWrapper& file) { reset_file(&file); }
|
||||
void clear_file() { reset_file(); }
|
||||
bool has_file() const { return file_ != nullptr; }
|
||||
|
||||
uint32_t line() const { return cursor_.line; }
|
||||
uint32_t col() const { return cursor_.col; }
|
||||
|
||||
private:
|
||||
static constexpr int8_t char_width = 5;
|
||||
static constexpr int8_t char_height = 8;
|
||||
static constexpr Style style_text{
|
||||
.font = font::fixed_5x8,
|
||||
.background = Color::black(),
|
||||
.foreground = Color::white(),
|
||||
};
|
||||
|
||||
const uint8_t max_line = 32;
|
||||
const uint8_t max_col = 48;
|
||||
|
||||
/* Returns true if the cursor was updated. */
|
||||
bool apply_scrolling_constraints(
|
||||
int16_t delta_line,
|
||||
int16_t delta_col);
|
||||
|
||||
void paint_text(Painter& painter, uint32_t line, uint16_t col);
|
||||
void paint_cursor(Painter& painter);
|
||||
|
||||
void reset_file(FileWrapper* file = nullptr);
|
||||
|
||||
// Gets the length of the current line.
|
||||
uint16_t line_length();
|
||||
|
||||
FileWrapper* file_{};
|
||||
|
||||
struct {
|
||||
// Previous cursor state.
|
||||
uint32_t line{};
|
||||
uint16_t col{};
|
||||
|
||||
// Previous draw state.
|
||||
uint32_t first_line{};
|
||||
uint16_t first_col{};
|
||||
bool redraw_text{true};
|
||||
} paint_state_{};
|
||||
|
||||
struct {
|
||||
uint32_t line{};
|
||||
uint16_t col{};
|
||||
ScrollDirection dir{ScrollDirection::Vertical};
|
||||
} cursor_{};
|
||||
};
|
||||
|
||||
/* Menu control for the TextEditor. */
|
||||
class TextEditorMenu : public View {
|
||||
public:
|
||||
TextEditorMenu();
|
||||
|
||||
void on_show() override;
|
||||
void on_hide() override;
|
||||
|
||||
std::function<void()>& on_cut() { return button_cut.on_select; }
|
||||
std::function<void()>& on_paste() { return button_paste.on_select; }
|
||||
std::function<void()>& on_copy() { return button_copy.on_select; }
|
||||
|
||||
std::function<void()>& on_delete_line() { return button_delline.on_select; }
|
||||
std::function<void()>& on_edit_line() { return button_edit.on_select; }
|
||||
std::function<void()>& on_add_line() { return button_addline.on_select; }
|
||||
|
||||
std::function<void()>& on_open() { return button_open.on_select; }
|
||||
std::function<void()>& on_save() { return button_save.on_select; }
|
||||
std::function<void()>& on_exit() { return button_exit.on_select; }
|
||||
|
||||
private:
|
||||
void hide_children(bool hidden);
|
||||
|
||||
Rectangle rect_frame{
|
||||
{0 * 8, 0 * 8, 23 * 8, 23 * 8},
|
||||
Color::dark_grey()};
|
||||
|
||||
NewButton button_cut{
|
||||
{1 * 8, 1 * 8, 7 * 8, 7 * 8},
|
||||
"Cut",
|
||||
&bitmap_icon_cut,
|
||||
Color::dark_grey()};
|
||||
|
||||
NewButton button_paste{
|
||||
{8 * 8, 1 * 8, 7 * 8, 7 * 8},
|
||||
"Paste",
|
||||
&bitmap_icon_paste,
|
||||
Color::dark_grey()};
|
||||
|
||||
NewButton button_copy{
|
||||
{15 * 8, 1 * 8, 7 * 8, 7 * 8},
|
||||
"Copy",
|
||||
&bitmap_icon_copy,
|
||||
Color::dark_grey()};
|
||||
|
||||
NewButton button_delline{
|
||||
{1 * 8, 8 * 8, 7 * 8, 7 * 8},
|
||||
"-Line",
|
||||
&bitmap_icon_delete,
|
||||
Color::dark_red()};
|
||||
|
||||
NewButton button_edit{
|
||||
{8 * 8, 8 * 8, 7 * 8, 7 * 8},
|
||||
"Edit",
|
||||
&bitmap_icon_rename,
|
||||
Color::dark_blue()};
|
||||
|
||||
NewButton button_addline{
|
||||
{15 * 8, 8 * 8, 7 * 8, 7 * 8},
|
||||
"+Line",
|
||||
&bitmap_icon_scanner,
|
||||
Color::dark_blue()};
|
||||
|
||||
NewButton button_open{
|
||||
{1 * 8, 15 * 8, 7 * 8, 7 * 8},
|
||||
"Open",
|
||||
&bitmap_icon_load,
|
||||
Color::green()};
|
||||
|
||||
NewButton button_save{
|
||||
{8 * 8, 15 * 8, 7 * 8, 7 * 8},
|
||||
"Save",
|
||||
&bitmap_icon_save,
|
||||
Color::green()};
|
||||
|
||||
NewButton button_exit{
|
||||
{15 * 8, 15 * 8, 7 * 8, 7 * 8},
|
||||
"Exit",
|
||||
&bitmap_icon_previous,
|
||||
Color::dark_red()};
|
||||
};
|
||||
|
||||
/* View viewing and minor edits on a text file. */
|
||||
class TextEditorView : public View {
|
||||
public:
|
||||
TextEditorView(NavigationView& nav);
|
||||
|
@ -118,72 +277,37 @@ class TextEditorView : public View {
|
|||
return "Notepad";
|
||||
};
|
||||
|
||||
void on_focus() override;
|
||||
void paint(Painter& painter) override;
|
||||
bool on_key(KeyEvent delta) override;
|
||||
bool on_encoder(EncoderEvent delta) override;
|
||||
void on_show() override;
|
||||
|
||||
private:
|
||||
static constexpr uint8_t max_line = 32;
|
||||
static constexpr uint8_t max_col = 48;
|
||||
static constexpr int8_t char_width = 5;
|
||||
static constexpr int8_t char_height = 8;
|
||||
|
||||
static constexpr Style style_default{
|
||||
.font = font::fixed_5x8,
|
||||
.background = Color::black(),
|
||||
.foreground = Color::white(),
|
||||
};
|
||||
|
||||
/* Returns true if the cursor was updated. */
|
||||
bool apply_scrolling_constraints(
|
||||
int16_t delta_line,
|
||||
int16_t delta_col);
|
||||
|
||||
void refresh_ui();
|
||||
void open_file(const std::filesystem::path& path);
|
||||
|
||||
void paint_text(Painter& painter, uint32_t line, uint16_t col);
|
||||
void paint_cursor(Painter& painter);
|
||||
|
||||
// Gets the length of the current line.
|
||||
uint16_t line_length();
|
||||
void refresh_ui();
|
||||
void update_position();
|
||||
void hide_menu(bool hidden = true);
|
||||
void show_file_picker();
|
||||
void show_nyi();
|
||||
|
||||
NavigationView& nav_;
|
||||
std::unique_ptr<FileWrapper> file_{};
|
||||
|
||||
FileWrapper file_{};
|
||||
TextViewer viewer{
|
||||
/* 272 = 320 - 16 (top bar) - 32 (bottom controls) */
|
||||
{0, 0, 240, 272}};
|
||||
|
||||
struct {
|
||||
// Previous cursor state.
|
||||
uint32_t line{};
|
||||
uint16_t col{};
|
||||
TextEditorMenu menu{};
|
||||
|
||||
// Previous draw state.
|
||||
uint32_t first_line{};
|
||||
uint16_t first_col{};
|
||||
bool redraw_text{true};
|
||||
bool has_file{false};
|
||||
} paint_state_{};
|
||||
|
||||
struct {
|
||||
uint32_t line{};
|
||||
uint16_t col{};
|
||||
ScrollDirection dir{ScrollDirection::Vertical};
|
||||
} cursor_{};
|
||||
|
||||
// TODO: The scrollable view should be its own widget
|
||||
// otherwise control navigation doesn't work.
|
||||
|
||||
Button button_open{
|
||||
{24 * 8, 34 * 8, 6 * 8, 4 * 8},
|
||||
"Open"};
|
||||
NewButton button_menu{
|
||||
{26 * 8, 34 * 8, 4 * 8, 4 * 8},
|
||||
{},
|
||||
&bitmap_icon_controls,
|
||||
Color::dark_grey()};
|
||||
|
||||
Text text_position{
|
||||
{0 * 8, 34 * 8, 24 * 8, 2 * 8},
|
||||
{0 * 8, 34 * 8, 26 * 8, 2 * 8},
|
||||
""};
|
||||
|
||||
Text text_size{
|
||||
{0 * 8, 36 * 8, 24 * 8, 2 * 8},
|
||||
{0 * 8, 36 * 8, 26 * 8, 2 * 8},
|
||||
""};
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue