Closed TimonPeng closed 11 months ago
I wrote a demo.
GEM Proxy:
#ifndef _GEM_PROXY_H
#define _GEM_PROXY_H
#include <Arduino.h>
#include <GEM_u8g2.h>
// https://blog.stratifylabs.dev/device/2020-12-15-Method-Chaining-in-Cpp/
class GEMProxy : public GEM_u8g2 {
public:
using GEM_u8g2::GEM_u8g2;
GEMProxy& setSplash(byte width, byte height, const unsigned char* image) {
GEM_u8g2::setSplash(width, height, image);
return *this;
};
GEMProxy& hideVersion(boolean flag = true) {
GEM_u8g2::hideVersion(flag);
return *this;
};
GEMProxy& enableCyrillic(boolean flag = true) {
GEM_u8g2::enableCyrillic(flag);
return *this;
};
GEMProxy& setMenuPageCurrent(GEMPage& menuPageCurrent) {
GEM_u8g2::setMenuPageCurrent(menuPageCurrent);
return *this;
};
GEMProxy& init() {
GEM_u8g2::init();
return *this;
};
GEMProxy& drawMenu() {
GEM_u8g2::drawMenu();
return *this;
};
};
class GEMPageProxy : public GEMPage {
public:
using GEMPage::GEMPage;
GEMPageProxy(const char* title_, GEMPage& parentMenuPage,
void (*exitAction_)() = nullptr)
: GEMPage(title_, exitAction_) {
GEMPage::setParentMenuPage(parentMenuPage);
};
GEMPageProxy& setParentMenuPage(GEMPage& parentMenuPage) {
GEMPage::setParentMenuPage(parentMenuPage);
return *this;
};
GEMPageProxy& addMenuItem(GEMItem* menuItem) {
GEMPage::addMenuItem(*menuItem);
return *this;
};
};
#endif
Usage:
GEMProxy menu = GEMProxy(u8g2);
// Pages
GEMPageProxy menuPageMain("Main Menu");
GEMPageProxy menuPageSystemSettings("SYSTEM SETTINGS", menuPageMain);
int number = -512;
boolean enablePrint = false;
void setup() {
menu.setSplash(logo_width, logo_height, logo_bits)
.hideVersion()
.enableCyrillic()
.setMenuPageCurrent(mainPage)
.init();
menuPageMain.addMenuItem(new GEMItem("Number:", number))
.addMenuItem(new GEMItem("Enable print:", enablePrint))
.addMenuItem(new GEMItem("System Settings", menuPageSystemSettings));
}
That's a great suggestion, never thought about it! Will definitely look into it.
Inspired by m5ez and Qt, maybe any void functions can return instance for chain operations.
Before:
After: