From 6f2afc4734659d1ab66b08573b753216a840f4ec Mon Sep 17 00:00:00 2001 From: Rudis Muiznieks Date: Fri, 17 Jun 2022 16:38:40 -0500 Subject: [PATCH] pulling menu logic into own file, added cursor sprite --- Makefile | 2 +- menu.ino | 95 +++++++++++++++++++++++++++++++++++++ slangin.ino | 79 ++++++------------------------ src/{ => fonts}/Font3x5.cpp | 0 src/{ => fonts}/Font3x5.h | 0 src/{ => fonts}/Font4x6.cpp | 0 src/{ => fonts}/Font4x6.h | 0 src/{ => fonts}/LICENSE | 0 8 files changed, 111 insertions(+), 65 deletions(-) create mode 100644 menu.ino rename src/{ => fonts}/Font3x5.cpp (100%) rename src/{ => fonts}/Font3x5.h (100%) rename src/{ => fonts}/Font4x6.cpp (100%) rename src/{ => fonts}/Font4x6.h (100%) rename src/{ => fonts}/LICENSE (100%) diff --git a/Makefile b/Makefile index 6322c17..a717ca6 100644 --- a/Makefile +++ b/Makefile @@ -8,4 +8,4 @@ clean: rm -rf ./build ./slangin.hex run: slangin.hex - sim_arduboy -p 8 ./slangin.hex + sim_arduboy -p 6 ./slangin.hex diff --git a/menu.ino b/menu.ino new file mode 100644 index 0000000..1db134e --- /dev/null +++ b/menu.ino @@ -0,0 +1,95 @@ +/* +Slangin' +Menu Helpers +Author: Rudis Muiznieks +License: WTFPL +*/ + +const uint8_t PROGMEM spriteMenuCursor[] = { + 3, 5, + 0x1f, 0x0e, 0x04, +}; + +void computeMenuSizes(const bool small, const bool cols, + const int itemCount, const String items[], + int& x1, int& x2, int& y) { + const int charWidth = small ? 4 : 5; + const int charHeight = small ? 8 : 9; + + if (cols) { // two colums + int col1Count = round(itemCount / 2.0); + int col2Count = itemCount - col1Count; + int col1Width = 0; + int col2Width = 0; + for (int i = 0; i < col1Count; i++) { + const int newLen = items[i].length(); + if (newLen > col1Width) col1Width = newLen; + } + for (int i = 0; i < col2Count; i++) { + const int newLen = items[i + col1Count].length(); + if (newLen > col2Width) col2Width = newLen; + } + x1 = 66 - round((col1Width + col2Width + 2.5) * (charWidth / 2.0)); + x2 = x1 + round((col1Width + 2.5) * charWidth); + y = 40 - round(col1Count * (charHeight / 2.0)); + } else { // one column + int colWidth = 0; + for (int i = 0; i < itemCount; i++) { + const int newLen = items[i].length(); + if (newLen > colWidth) colWidth = newLen; + } + x1 = 66 - round(colWidth * (charWidth / 2.0)); + x2 = -1; + y = 40 - round(itemCount * (charHeight / 2.0)); + } +} + +void drawMenu(const bool small, const bool cols, + const int itemCount, const String items[]) { + const int charWidth = small ? 4 : 5; + const int charHeight = small ? 8 : 9; + int x1, x2, y; + computeMenuSizes(small, cols, itemCount, items, x1, x2, y); + const int col1Count = round(itemCount / 2.0); + for (int i = 0; i < itemCount; i++) { + int cx, cy; + if (cols) { // two columns + const bool col1 = i < col1Count; + cx = col1 ? x1 : x2; + cy = col1 + ? i * charHeight + y + : (i - col1Count) * charHeight + y; + } else { // one column + cx = x1; + cy = i * charHeight + y; + } + if (small) { + font3x5.setCursor(cx, cy); + font3x5.print(items[i]); + } else { + font4x6.setCursor(cx, cy); + font4x6.print(items[i]); + } + } +} + +void drawMenuIndicator(const int menuSelected, const bool small, + const bool cols, const int itemCount, const String items[]) { + const int charWidth = small ? 4 : 5; + const int charHeight = small ? 8 : 9; + int x1, x2, y; + computeMenuSizes(small, cols, itemCount, items, x1, x2, y); + int cx, cy; + if (cols) { // two columns + const int col1Count = round(itemCount / 2.0); + const bool col1 = menuSelected < col1Count; + cx = col1 ? x1 : x2; + cy = col1 + ? menuSelected * charHeight + y + : (menuSelected - col1Count) * charHeight + y; + } else { // one column + cx = x1; + cy = menuSelected * charHeight + y; + } + Sprites::drawOverwrite(cx - 4, cy + (small ? 1 : 2), spriteMenuCursor, 0); +} diff --git a/slangin.ino b/slangin.ino index 91e66c3..0b5a0d3 100644 --- a/slangin.ino +++ b/slangin.ino @@ -5,10 +5,12 @@ License: WTFPL */ #include -#include "src/Font4x6.h"; +#include "src/fonts/Font3x5.h"; +#include "src/fonts/Font4x6.h"; Arduboy2 arduboy; Font4x6 font4x6 = Font4x6(); +Font3x5 font3x5 = Font3x5(); enum GameState { STATE_TITLE = 0, @@ -86,18 +88,23 @@ void loop() { String menu[8]; int menuLength = 0; + bool menuCols = false; bool menuSmall = false; + int menuSelected = 0; if (!screenInitialized) { + menuSelected = 0; arduboy.clear(); switch (sGameState) { case STATE_TITLE: - drawTitle(F("Slangin'")); + arduboy.setCursor(25, 10); + arduboy.print(F("Slangin' v0.9")); menu[0] = F("New Game"); - menu[1] = F("Continue"); - menuLength = 2; - menuSmall = true; - draw1ColMenu(menuSmall, menuLength, menu); + menuLength = 1; + menuSmall = false; + menuCols = false; + drawMenu(menuSmall, menuCols, menuLength, menu); + drawMenuIndicator(menuSelected, menuSmall, menuCols, menuLength, menu); break; } arduboy.display(); @@ -108,62 +115,6 @@ void loop() { /* screen draw heplers */ /***********************/ void drawTitle(const String title) { - arduboy.setCursor(6, 8); - arduboy.print(title); -} - -void draw1ColMenu(const bool small, const int itemCount, const String items[]) { - int colWidth = 0; - for (int i = 0; i < itemCount; i++) { - const int newLen = items[i].length(); - if (newLen > colWidth) colWidth = newLen; - } - const int charWidth = small ? 5 : 6; - const int charHeight = small ? 9 : 10; - int x = 66 - round(colWidth * (charWidth / 2.0)); - int y = 18 - round(itemCount * (charHeight / 2.0)); - for (int i = 0; i < itemCount; i++) { - const int cy = i * charHeight + 22 + y; - if (small) { - font4x6.setCursor(x, cy); - font4x6.print(items[i]); - } else { - arduboy.setCursor(x, cy); - arduboy.print(items[i]); - } - } -} - -void draw2ColMenu(const bool small, const int itemCount, const String items[]) { - const int col1Count = round(itemCount / 2.0); - const int col2Count = itemCount - col1Count; - int col1Width = 0; - int col2Width = 0; - for (int i = 0; i < col1Count; i++) { - const int newLen = items[i].length(); - if (newLen > col1Width) col1Width = newLen; - } - for (int i = 0; i < col2Count; i++) { - const int newLen = items[i].length(); - if (newLen > col2Width) col2Width = newLen; - } - const int charWidth = small ? 5 : 6; - const int charHeight = small ? 9 : 10; - int x = 66 - round((col1Width + col2Width + 2) * (charWidth / 2.0)); - int y = 18 - round(col1Count * (charHeight / 2.0)); - for (int i = 0; i < itemCount; i++) { - const int cx = (i < col1Count) - ? x - : x + (col1Width + 2) * charWidth; - const int cy = (i < col1Count) - ? i * charHeight + 22 + y - : (i - col1Count) * charHeight + 22 + y; - if (small) { - font4x6.setCursor(cx, cy); - font4x6.print(items[i]); - } else { - arduboy.setCursor(cx, cy); - arduboy.print(items[i]); - } - } + font4x6.setCursor(6, 8); + font4x6.print(title); } diff --git a/src/Font3x5.cpp b/src/fonts/Font3x5.cpp similarity index 100% rename from src/Font3x5.cpp rename to src/fonts/Font3x5.cpp diff --git a/src/Font3x5.h b/src/fonts/Font3x5.h similarity index 100% rename from src/Font3x5.h rename to src/fonts/Font3x5.h diff --git a/src/Font4x6.cpp b/src/fonts/Font4x6.cpp similarity index 100% rename from src/Font4x6.cpp rename to src/fonts/Font4x6.cpp diff --git a/src/Font4x6.h b/src/fonts/Font4x6.h similarity index 100% rename from src/Font4x6.h rename to src/fonts/Font4x6.h diff --git a/src/LICENSE b/src/fonts/LICENSE similarity index 100% rename from src/LICENSE rename to src/fonts/LICENSE