Skip to content

Commit

Permalink
Change visibility for process and draw functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ShishkinDmitriy committed Sep 18, 2024
1 parent 761a33a commit 0f4fb09
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 58 deletions.
15 changes: 10 additions & 5 deletions docs/classes.puml
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,31 @@ class LcdMenu {
#virtual bool back()
}

struct MenuItem::Context {
LcdMenu* menu;
DisplayInterface* display;
const unsigned char command;
}

class MenuItem {
+virtual bool handle(char c)
#virtual bool handle(Context context)
}

class ItemCommand {
+bool handle(char c) override
#bool handle(Context context) override
#bool enter() override
}

class ItemList {
+bool handle(char c) override
#bool handle(Context context) override
#bool up() override
#bool down() override
#bool enter() override
#bool back() override
}

class ItemInput {
+bool handle(char c) override
#bool handle(Context context) override
#bool up() override
#bool down() override
#bool enter() override
Expand All @@ -45,5 +51,4 @@ LcdMenu::handle -r-> MenuItem::handle
ItemCommand -u-|> MenuItem
ItemList -u-|> MenuItem
ItemInput -u-|> MenuItem

@enduml
5 changes: 1 addition & 4 deletions examples/SimpleInput/SimpleInput.ino
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ void setup() {
void loop() {
if (!Serial.available()) return;
char command = Serial.read();

if (!processWithSimpleCommand(&navConfig, command) && command != '\n') {
menu.process(command);
}
processWithSimpleCommand(&navConfig, command);
}
/**
* Define callback
Expand Down
1 change: 1 addition & 0 deletions src/ItemBack.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ItemBack : public MenuItem {
*/
ItemBack(const char* text = ".."): MenuItem(text, MENU_ITEM_COMMAND) {}

Check failure on line 25 in src/ItemBack.h

View workflow job for this annotation

GitHub Actions / clang_format

src/ItemBack.h#L25

code should be clang-formatted [-Wclang-format-violations]

protected:
bool process(Context context) override {
switch (context.command) {
case ENTER: context.menu->process(BACK); return true;
Expand Down
3 changes: 1 addition & 2 deletions src/ItemCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,13 @@ class ItemCommand : public MenuItem {
*/
void setCallBack(fptr callback) { this->callback = callback; };

protected:
bool process(Context context) override {
switch (context.command) {
case ENTER: return enter(context);
default: return false;
}
}

protected:
bool enter(Context context) {
if (callback != NULL) {
callback();
Expand Down
69 changes: 33 additions & 36 deletions src/ItemInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,6 @@ class ItemInput : public MenuItem {
* When length of `value` < `viewSize` this index should be 0.
*/
uint8_t view = 0;
/**
* @brief The number of visible characters.
*
* visible area
* ┌───────────────┐
* X X X X│X X X X █ X X X│X X
* ├───────────────┤
* │<── viewSize ─>│
*
* Effectively const, but initialized lately when display is injected.
*/
uint8_t viewSize;
/**
* @brief Current index of the char to be edited.
*
Expand All @@ -76,8 +64,22 @@ class ItemInput : public MenuItem {
* First parameter will be a `value` string.
*/
fptrStr callback;
/**
* @brief The number of visible characters.
*
* visible area
* ┌───────────────┐
* X X X X│X X X X █ X X X│X X
* ├───────────────┤
* │<── viewSize ─>│
*
* Effectively const, but initialized lately when display is injected.
*/
inline uint8_t getViewSize(DisplayInterface* display) {
return display->getMaxCols() - (strlen(text) + 2) - 1;
};

uint8_t constrainBlinkerPosition(uint8_t blinkerPosition) {
uint8_t constrainBlinkerPosition(DisplayInterface* display, uint8_t blinkerPosition) {
uint8_t maxCols = display->getMaxCols();
//
// calculate lower and upper bound
Expand All @@ -102,7 +104,6 @@ class ItemInput : public MenuItem {
*/
ItemInput(const char* text, char* value, fptrStr callback)
: MenuItem(text, MENU_ITEM_INPUT), value(value), callback(callback) {}

/**
* Construct a new ItemInput object with no initial value.
*
Expand All @@ -112,37 +113,31 @@ class ItemInput : public MenuItem {
*/
ItemInput(const char* text, fptrStr callback)
: ItemInput(text, (char*)"", callback) {}

void initialize(DisplayInterface* display) override {
MenuItem::initialize(display);
viewSize = display->getMaxCols() - (strlen(text) + 2) - 1;
}

/**
* Get the current input value for this item.
*
* @return The current input value as a string.
*/
char* getValue() { return value; }

/**
* Set the input value for this item.
*
* @param value The new input value.
*/
void setValue(char* value) { this->value = value; }

/**
* Get the callback function for this item.
*
* @return The function pointer to the callback function.
*/
fptrStr getCallbackStr() { return callback; }

protected:
bool process(Context context) override {
unsigned char c = context.command;
DisplayInterface* display = context.display;
if (isprint(c)) {
return typeChar(c);
return typeChar(context);
}
switch (c) {
case ENTER: return enter(context);
Expand All @@ -160,11 +155,10 @@ class ItemInput : public MenuItem {
void draw(DisplayInterface* display, uint8_t row) override {
uint8_t maxCols = display->getMaxCols();
static char* buf = new char[maxCols];
substring(value, view, viewSize, buf);
substring(value, view, getViewSize(display), buf);
display->drawItem(row, text, ':', buf);
}

protected:
bool enter(Context context) {
DisplayInterface* display = context.display;
if (display->getEditModeEnabled()) {
Expand All @@ -174,13 +168,14 @@ class ItemInput : public MenuItem {
uint8_t length = strlen(value);
cursor = length;
// Move view if needed
uint8_t viewSize = getViewSize(display);
if (cursor > viewSize) {
view = length - (viewSize - 1);
}
// Redraw
MenuItem::draw(display);
display->setEditModeEnabled(true);
display->resetBlinker(constrainBlinkerPosition(strlen(text) + 2 + cursor - view));
display->resetBlinker(constrainBlinkerPosition(display, strlen(text) + 2 + cursor - view));
display->drawBlinker();
return true;
};
Expand All @@ -194,7 +189,7 @@ class ItemInput : public MenuItem {
// Move view to 0 and redraw before exit
cursor = 0;
view = 0;
MenuItem::draw();
MenuItem::draw(display);
if (callback != NULL) {
callback(value);
}
Expand All @@ -211,9 +206,9 @@ class ItemInput : public MenuItem {
cursor--;
if (cursor < view) {
view--;
MenuItem::draw();
MenuItem::draw(display);
}
display->resetBlinker(constrainBlinkerPosition(display->getBlinkerPosition() - 1));
display->resetBlinker(constrainBlinkerPosition(display, display->getBlinkerPosition() - 1));
// Log
printCmd(F("LEFT"), value[display->getBlinkerPosition() - (strlen(text) + 2)]);
return true;
Expand All @@ -227,11 +222,12 @@ class ItemInput : public MenuItem {
return true;
}
cursor++;
uint8_t viewSize = getViewSize(display);
if (cursor > (view + viewSize - 1)) {
view++;
MenuItem::draw();
MenuItem::draw(display);
}
display->resetBlinker(constrainBlinkerPosition(display->getBlinkerPosition() + 1));
display->resetBlinker(constrainBlinkerPosition(display, display->getBlinkerPosition() + 1));
// Log
printCmd(F("RIGHT"), value[display->getBlinkerPosition() - (strlen(text) + 2)]);
return true;
Expand All @@ -257,8 +253,8 @@ class ItemInput : public MenuItem {
if (cursor < view) {
view--;
}
MenuItem::draw();
display->resetBlinker(constrainBlinkerPosition(display->getBlinkerPosition() - 1));
MenuItem::draw(display);
display->resetBlinker(constrainBlinkerPosition(display, display->getBlinkerPosition() - 1));
return true;
}
/**
Expand All @@ -268,7 +264,7 @@ class ItemInput : public MenuItem {
*/
bool typeChar(Context context) {
DisplayInterface* display = context.display;
const char character = context.command
const char character = context.command;
if (!display->getEditModeEnabled()) {
return false;
}
Expand All @@ -287,11 +283,12 @@ class ItemInput : public MenuItem {
value = buf;
}
cursor++;
uint8_t viewSize = getViewSize(display);
if (cursor > (view + viewSize - 1)) {
view++;
}
MenuItem::draw(display);
display->resetBlinker(constrainBlinkerPosition(display->getBlinkerPosition() + 1));
display->resetBlinker(constrainBlinkerPosition(display, display->getBlinkerPosition() + 1));
// Log
printCmd(F("TYPE-CHAR"), character);
return true;
Expand All @@ -314,7 +311,7 @@ class ItemInput : public MenuItem {
// update blinker position
//
MenuItem::draw(display);
display->resetBlinker(constrainBlinkerPosition(strlen(text) + 2));
display->resetBlinker(constrainBlinkerPosition(display, strlen(text) + 2));
return true;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/ItemInputCharset.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class ItemInputCharset : public ItemInput {
ItemInputCharset(const char* text, const char* charset, fptrStr callback)
: ItemInputCharset(text, (char*)"", charset, callback) {}

protected:
bool process(Context context) override {
const unsigned char c = context.command;
switch (c) {
Expand All @@ -62,7 +63,6 @@ class ItemInputCharset : public ItemInput {
}
}

protected:
bool enter(Context context) {
DisplayInterface* display = context.display;
if (!display->getEditModeEnabled()) {
Expand Down
2 changes: 1 addition & 1 deletion src/ItemList.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class ItemList : public MenuItem {
*/
String* getItems() { return items; }

protected:
void draw(DisplayInterface* display, uint8_t row) override {
uint8_t maxCols = display->getMaxCols();
static char* buf = new char[maxCols];
Expand All @@ -103,7 +104,6 @@ class ItemList : public MenuItem {
}
}

protected:
bool enter(Context context) {
DisplayInterface* display = context.display;
if (display->getEditModeEnabled()) {
Expand Down
2 changes: 1 addition & 1 deletion src/ItemProgress.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class ItemProgress : public MenuItem {
return mapping(progress);
}

protected:
void draw(DisplayInterface* display, uint8_t row) override {
uint8_t maxCols = display->getMaxCols();
static char* buf = new char[maxCols];
Expand All @@ -120,7 +121,6 @@ class ItemProgress : public MenuItem {
}
}

protected:
bool enter(Context context) {
DisplayInterface* display = context.display;
if (display->getEditModeEnabled()) {
Expand Down
3 changes: 1 addition & 2 deletions src/ItemToggle.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,13 @@ class ItemToggle : public MenuItem {
display->drawItem(row, text, ':', buf);
};

protected:
bool process(Context context) override {
switch (context.command) {
case ENTER: return enter(context);
default: return false;
}
};

protected:
bool enter(Context context) {
enabled = !enabled;
if (callback != NULL) {
Expand Down
19 changes: 13 additions & 6 deletions src/MenuItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class LcdMenu;
*
*/
class MenuItem {

Check failure on line 44 in src/MenuItem.h

View workflow job for this annotation

GitHub Actions / clang_format

src/MenuItem.h#L44

code should be clang-formatted [-Wclang-format-violations]
friend LcdMenu;

Check failure on line 45 in src/MenuItem.h

View workflow job for this annotation

GitHub Actions / clang_format

src/MenuItem.h#L45

code should be clang-formatted [-Wclang-format-violations]
protected:
const char* text = NULL;
byte type = MENU_ITEM_NONE;
Expand Down Expand Up @@ -74,6 +75,8 @@ class MenuItem {
* @param text text to display for the item
*/
void setText(const char* text) { this->text = text; };

protected:
/**
* @brief Process a command decoded in 1 byte.
* It can be a printable character or a control command like `ENTER` or `LEFT`.
Expand All @@ -85,22 +88,26 @@ class MenuItem {
* @return true if command was successfully handled by item.
*/
virtual bool process(Context context) { return false; };

/**
* @brief Draw this menu item on specified display on current line.
* @param display the display that should be used for draw
*/
virtual void draw(DisplayInterface* display) {
draw(display, display->getCursorRow());
};

/**
* @brief Draw this menu item on specified display on specified line.
* @param display the display that should be used for draw
* @param row the number of row to draw on
*/
virtual void draw(DisplayInterface* display, uint8_t row) {
uint8_t maxCols = display->getMaxCols();
static char* buf = new char[maxCols];
substring(text, 0, maxCols - 2, buf);
display->drawItem(row, buf);
};

/**
* Operators
*/

public:
/**
* Get item at index from the submenu
* @param index for the item
Expand Down

0 comments on commit 0f4fb09

Please sign in to comment.