GEM icon indicating copy to clipboard operation
GEM copied to clipboard

Simpler create layout way and chain operations

Open TimonPeng opened this issue 1 year ago • 2 comments

Inspired by m5ez and Qt, maybe any void functions can return instance for chain operations.

Before:

GEM_u8g2 menu(u8g2);

GEMPage menuPageMain("Main Menu");

int number = -512;
boolean enablePrint = false;

GEMItem menuItemInt("Number:", number);
GEMItem menuItemBool("Enable print:", enablePrint);

GEMPage menuPageSystemSettings("SYSTEM SETTINGS");
GEMItem menuItemSystemSettings("System Settings", menuPageSystemSettings);

void setup() {
  // ...
  menu.setSplash(logo_width, logo_height, logo_bits);
  menu.hideVersion();
  menu.enableCyrillic();

  menuPageSystemSettings.setParentMenuPage(menuPageMain);

  menuPageMain.addMenuItem(menuItemInt);
  menuPageMain.addMenuItem(menuItemBool);
  menuPageMain.addMenuItem(menuItemSystemSettings);

  menu.setMenuPageCurrent(menuPageMain);

  menu.init();
  // ...
}

After:

int number = -512;
boolean enablePrint = false;

GEMPage menuPageSystemSettings("SYSTEM SETTINGS")
    .setXXX(...);

GEMPage menuPageMain("Main Menu")
    .addMenuItem("Number:", number)
    .addMenuItem("Enable print:", enablePrint);
    .addMenuItem("System Settings", menuPageSystemSettings);

GEM_u8g2 menu(u8g2)
    .setSplash(logo_width, logo_height, logo_bits)
    .hideVersion()
    .enableCyrillic()
    .setMenuPageCurrent(menuPageMain);

void setup() {
  // ...
  menu.init();
  // ...
}

TimonPeng avatar Aug 27 '22 09:08 TimonPeng

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));
}

TimonPeng avatar Aug 27 '22 11:08 TimonPeng

That's a great suggestion, never thought about it! Will definitely look into it.

Spirik avatar Aug 27 '22 20:08 Spirik