slangin/menu.ino

163 lines
4.3 KiB
Arduino
Raw Normal View History

/*
Slangin'
Menu Helpers
Author: Rudis Muiznieks
License: WTFPL
*/
const uint8_t PROGMEM spriteMenuCursor[] = {
3, 5,
0x1f, 0x0e, 0x04,
};
String lookupLocation(GameLocation loc) {
switch (loc) {
case LOC_BRONX:
return F("Bronx");
case LOC_GHETTO:
return F("Ghetto");
case LOC_CENTRAL_PARK:
return F("Central Park");
case LOC_MANHATTEN:
return F("Manhatten");
case LOC_CONEY_ISLAND:
return F("Coney Island");
case LOC_BROOKLYN:
return F("Brooklyn");
default:
return F("");
}
}
String lookupDrug(Drug drug) {
switch (drug) {
case DRUG_COCAINE:
return F("Cocaine");
case DRUG_HEROINE:
return F("Heroine");
case DRUG_ACID:
return F("Acid");
case DRUG_WEED:
return F("Weed");
case DRUG_SPEED:
return F("Speed");
case DRUG_LUDES:
return F("Ludes");
default:
return F("");
}
}
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;
}
2022-06-18 00:05:30 -05:00
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;
2022-06-18 00:05:30 -05:00
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,
2022-06-17 18:55:36 -05:00
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;
}
2022-06-17 18:55:36 -05:00
if (clear) {
arduboy.fillRect(cx - 4, cy + (small ? 1 : 2), 3, 5, BLACK);
} else {
Sprites::drawOverwrite(cx - 4, cy + (small ? 1 : 2), spriteMenuCursor, 0);
}
}
void buildDrugMenu(const long extra[6]) {
int lengths[6];
int col1Max = 0;
int col2Max = 0;
for (int i = 0; i < 6; i++) {
int extraLen = String(extra[i]).length();
lengths[i] = lookupDrug(i).length() + 1 + extraLen;
if (i < 3 && lengths[i] > col1Max) col1Max = lengths[i];
if (i >= 3 && lengths[i] > col2Max) col2Max = lengths[i];
}
for (int i = 0; i < 6; i++) {
const int colMax = i < 3 ? col1Max : col2Max;
menu[i] = lookupDrug(i);
menu[i] += F(" ");
for (int s = 0; s < colMax - lengths[i]; s++) {
menu[i] += F(" ");
}
menu[i] += String(extra[i]);
}
menuLength = 6;
menuCols = true;
menuSmall = col1Max + col2Max > 22;
}