57 lines
1.5 KiB
C
57 lines
1.5 KiB
C
#include "menu_dice.h"
|
|
#include "menus.h"
|
|
#include "config.h"
|
|
#include "state.h"
|
|
#include "draw.h"
|
|
#include "events.h"
|
|
#include "esp_random.h"
|
|
#include <stdio.h>
|
|
|
|
static const uint8_t s_icon_d6[2][ICON_W] = {
|
|
// D6 showing 6-face: square outline + 6 dots in 2x3 arrangement
|
|
{0x00,0xFE,0x02,0x02,0x9A,0x9A,0x02,0x02,0x02,0x02,0x9A,0x9A,0x02,0x02,0xFE,0x00},
|
|
{0x00,0x7F,0x40,0x40,0x59,0x59,0x40,0x40,0x40,0x40,0x59,0x59,0x40,0x40,0x7F,0x00},
|
|
};
|
|
|
|
static void dice_nav_short(int d)
|
|
{
|
|
g_dice_item = (g_dice_item + d + 3) % 3;
|
|
}
|
|
|
|
static void dice_lr_delta(int d)
|
|
{
|
|
if (g_dice_item == 0) {
|
|
g_dice_num += d;
|
|
if (g_dice_num < 1) g_dice_num = 1;
|
|
} else if (g_dice_item == 1) {
|
|
g_dice_sides = (g_dice_sides + d + NUM_DICE_SIDES) % NUM_DICE_SIDES;
|
|
} else {
|
|
int sides = die_sides[g_dice_sides];
|
|
g_dice_sum = 0;
|
|
int pos = 0;
|
|
g_dice_csv[0] = '\0';
|
|
for (int i = 0; i < g_dice_num; i++) {
|
|
int roll = (int)(esp_random() % (uint32_t)sides) + 1;
|
|
g_dice_sum += roll;
|
|
if (pos < DICE_CSV_LEN - 5) {
|
|
if (i > 0) g_dice_csv[pos++] = ',';
|
|
pos += snprintf(g_dice_csv + pos, DICE_CSV_LEN - pos, "%d", roll);
|
|
}
|
|
}
|
|
g_dice_rolled = 1;
|
|
}
|
|
}
|
|
|
|
static void dice_draw(void)
|
|
{
|
|
oled_draw_dice();
|
|
}
|
|
|
|
const menu_vtable_t menu_dice_vtable = {
|
|
.nav_short = dice_nav_short,
|
|
.lr_delta = dice_lr_delta,
|
|
.draw = dice_draw,
|
|
.on_tick = NULL,
|
|
.icon = s_icon_d6,
|
|
};
|