From f024cba8c27b79f924eefa75c4f0251b1f77cb0e Mon Sep 17 00:00:00 2001 From: jaby Date: Wed, 3 Jan 2024 16:10:00 -0600 Subject: [PATCH] Support simple Menu --- .../PoolBox/application/src/application.cpp | 19 ++++++++-- .../PoolBox/application/src/include/menu.hpp | 29 +++++++++++++++ examples/PoolBox/application/src/menu.cpp | 36 +++++++++++++++++++ support/include/FontWriter/fonts.hpp | 2 +- 4 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 examples/PoolBox/application/src/include/menu.hpp create mode 100644 examples/PoolBox/application/src/menu.cpp diff --git a/examples/PoolBox/application/src/application.cpp b/examples/PoolBox/application/src/application.cpp index 2aeed044..80b254cb 100644 --- a/examples/PoolBox/application/src/application.cpp +++ b/examples/PoolBox/application/src/application.cpp @@ -1,34 +1,49 @@ #include "../include/asset_mgr.hpp" #include "include/font_writer.hpp" +#include "include/menu.hpp" #include "include/paco.hpp" #include #include +#include #include using namespace JabyEngine; +static const Menu::SimpleMenu::Entry MenuEntries[] = { + {"Menu 1"}, + {"Menu 2"}, + {"Menu 3"}, + {"Menu 4"}, + {"Menu 5"} +}; + // Do we want this namespace? // Do we want Paco to be HERE?! -static object::Paco paco; +static object::Paco paco; +static Menu::SimpleMenu menu; static void setup() { Assets::Main::load(); FontWriter::setup(); paco.setup(); + menu.setup(MenuEntries); } static void update() { - static const char Title[] = "Pool Box"; + static const char Title[] = "Pool Box"; static constexpr auto TitleLength = (sizeof(Title) - 1)*DefaultFont::Info.font_size.width; + Periphery::query_controller(); auto cursor = FontWriter::update(JabyEngine::Make::PositionI16((GPU::Display::Width-TitleLength)/2, 16)); paco.update(); FontWriter::new_font_writer.write(cursor, Title, GPU::Color24::Yellow(), &FontWriter::wiggle); + menu.update(FontWriter::bios_font_writer, cursor, Make::PositionI16(8, 64)); } static void render() { FontWriter::new_font_writer.render(); + FontWriter::bios_font_writer.render(); paco.render(); } diff --git a/examples/PoolBox/application/src/include/menu.hpp b/examples/PoolBox/application/src/include/menu.hpp new file mode 100644 index 00000000..11fb19b8 --- /dev/null +++ b/examples/PoolBox/application/src/include/menu.hpp @@ -0,0 +1,29 @@ +#pragma once +#include "font_writer.hpp" +#include + +namespace Menu { + using namespace JabyEngine; + + class SimpleMenu { + public: + struct Entry { + const char* name; + }; + + private: + const Entry* entries; + size_t size; + uint8_t cur_selection; + + public: + void setup(const Entry* entries, size_t size); + + template + void setup(const Entry (&entries)[N]) { + SimpleMenu::setup(entries, N); + } + + void update(JabyEngine::FontWriter& font_writer, State& cursor, const GPU::PositionI16& start); + }; +} \ No newline at end of file diff --git a/examples/PoolBox/application/src/menu.cpp b/examples/PoolBox/application/src/menu.cpp new file mode 100644 index 00000000..0743e138 --- /dev/null +++ b/examples/PoolBox/application/src/menu.cpp @@ -0,0 +1,36 @@ +#include "include/menu.hpp" +#include + +namespace Menu { + void SimpleMenu :: setup(const Entry* entries, size_t size) { + this->entries = entries; + this->size = size; + this->cur_selection = 0; + } + + void SimpleMenu :: update(JabyEngine::FontWriter& font_writer, State& cursor, const GPU::PositionI16& start) { + using DigitalButton = Periphery::GenericController::Button; + const auto& controller = Periphery::get_primary_controller_as(); + + if(controller.button.went_down(DigitalButton::Up) && this->cur_selection > 0) { + this->cur_selection -= 1; + } + + if(controller.button.went_down(DigitalButton::Down) && this->cur_selection < (this->size - 1)) { + this->cur_selection += 1; + } + + cursor.pos = Make::PositionI16(8, 64); + for(size_t n = 0; n < this->size; n++) { + const auto& cur_entry = this->entries[n]; + + if(this->cur_selection == n) { + FontWriter::bios_font_writer.write(cursor, ">%s<\n", cur_entry.name); + } + + else { + FontWriter::bios_font_writer.write(cursor, "%s\n", cur_entry.name); + } + } + } +} \ No newline at end of file diff --git a/support/include/FontWriter/fonts.hpp b/support/include/FontWriter/fonts.hpp index 63dd9335..c965ee75 100644 --- a/support/include/FontWriter/fonts.hpp +++ b/support/include/FontWriter/fonts.hpp @@ -6,7 +6,7 @@ namespace JabyEngine { struct DefaultFont { static constexpr auto Info = FontInfo { .texture_size = {64, 80}, - .font_size = {12, 16} + .font_size = {12, 16} }; static void load(uint32_t* work_area, SimpleTIM vram_dst);