pulling menu logic into own file, added cursor sprite
This commit is contained in:
parent
f6881b3e40
commit
6f2afc4734
2
Makefile
2
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
|
||||
|
|
|
@ -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);
|
||||
}
|
79
slangin.ino
79
slangin.ino
|
@ -5,10 +5,12 @@ License: WTFPL
|
|||
*/
|
||||
|
||||
#include <Arduboy2.h>
|
||||
#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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue