WatchyWatchFace/src/Menu.cpp

129 lines
3.1 KiB
C++
Raw Normal View History

2023-05-29 18:25:28 +03:00
#include "config.h"
#include "Menu.h"
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSansBold9pt7b.h>
RTC_DATA_ATTR WatchyDisplay * Menu::m_display = nullptr;
RTC_DATA_ATTR std::function<void()> Menu::m_exitCallback = nullptr;
RTC_DATA_ATTR uint8_t Menu::m_numPages = 0, Menu::m_currentPage = 0, Menu::m_numMenuItemsinCurrentPage = 0, Menu::m_currentMenuItem = 0;
Menu::Menu()
{
}
Menu::~Menu()
{
}
void Menu::Init(WatchyDisplay & display)
{
m_display = &display;
m_pages.clear();
}
void Menu::HandleButtonPress(uint64_t buttonMask)
{
if (m_currentPage == 0) {
if (buttonMask & BACK_BTN_MASK) {
if (m_exitCallback) {
m_exitCallback();
}
}
}
if (m_pages.size() == 0) {
if (m_exitCallback) {
m_exitCallback();
}
return;
}
if (buttonMask & UP_BTN_MASK) {
if (m_currentMenuItem > 0) {
m_currentMenuItem--;
}
}
if (buttonMask & DOWN_BTN_MASK) {
if (m_currentMenuItem < m_pages[m_currentPage].menuItems.size() - 1) {
m_currentMenuItem++;
}
}
if (buttonMask & MENU_BTN_MASK) {
if (m_pages[m_currentPage].menuItems[m_currentMenuItem].callback) {
m_pages[m_currentPage].menuItems[m_currentMenuItem].callback();
}
if (m_pages[m_currentPage].menuItems[m_currentMenuItem].pageNum > 0) {
m_currentPage = m_pages[m_currentPage].menuItems[m_currentMenuItem].pageNum;
m_currentMenuItem = 0;
}
}
}
void Menu::SetPages(const std::vector<MenuPage> & pages)
{
// If the number of pages has changed, reset the current page and menu item.
if (pages.size() != m_numPages) {
m_currentPage = 0;
m_currentMenuItem = 0;
}
// If the number of menu items has changed, reset the current menu item.
if (pages.size() > 0 && pages[m_currentMenuItem].menuItems.size() != m_numMenuItemsinCurrentPage) {
m_currentMenuItem = 0;
}
m_pages = pages;
}
void Menu::SetExitCallback(std::function<void()> callback)
{
m_exitCallback = callback;
}
void Menu::Redraw(bool partialRefresh)
{
if (m_display == nullptr) {
return;
}
if (!partialRefresh) {
m_display->clearScreen();
}
m_display->setFullWindow();
m_display->fillScreen(GxEPD_WHITE);
if (m_pages.size() > 0) {
bool hasTitle = false;
if (m_pages[m_currentPage].title.length() > 0) {
hasTitle = true;
m_display->setFont(&FreeSansBold9pt7b);
m_display->setTextColor(GxEPD_BLACK);
int16_t x, y;
uint16_t w, h;
m_display->getTextBounds(m_pages[m_currentPage].title.c_str(), 0, 0, &x, &y, &w, &h);
if (m_pages[m_currentPage].titleAlignment == ALIGNMENT_LEFT) { // Left
x = 5;
} else if (m_pages[m_currentPage].titleAlignment == ALIGNMENT_CENTER) { // Center
x = (m_display->width() - w) / 2;
} else if (m_pages[m_currentPage].titleAlignment == ALIGNMENT_RIGHT) { // Right
x = m_display->width() - w - 5;
}
m_display->setCursor(x, 15);
m_display->print(m_pages[m_currentPage].title.c_str());
m_display->drawLine(5, 25, m_display->width() - 10, 25, GxEPD_BLACK);
}
}
m_display->display(partialRefresh);
}