/* 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 + 1.75) * (charWidth / 2.0)); x2 = x1 + round((col1Width + 1.75) * charWidth); y = 42 - 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 = 42 - 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 bool clear) { 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; } if (clear) { arduboy.fillRect(cx - 4, cy + (small ? 1 : 2), 3, 5, BLACK); } else { Sprites::drawOverwrite(cx - 4, cy + (small ? 1 : 2), spriteMenuCursor, 0); } }