Skip to content

Commit

Permalink
Remove itemIndex dependency from renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
forntoh committed Oct 6, 2024
1 parent dfed2d0 commit 9552522
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/ItemInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class ItemInput : public MenuItem {
fptrStr getCallbackStr() { return callback; }

protected:
void draw(MenuRenderer* renderer, uint8_t itemIndex, uint8_t screenRow) override {
void draw(MenuRenderer* renderer, uint8_t screenRow) override {
uint8_t viewSize = getViewSize(renderer);
char* vbuf = new char[viewSize + 1];
substring(value, view, viewSize, vbuf);
Expand All @@ -139,7 +139,7 @@ class ItemInput : public MenuItem {
char buf[maxCols];
concat(text, ':', buf);
concat(buf, vbuf, buf);
renderer->drawItem(itemIndex, screenRow, buf);
renderer->drawItem(screenRow, buf);

delete[] vbuf; // Free allocated memory
}
Expand Down
4 changes: 2 additions & 2 deletions src/ItemList.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ class ItemList : public MenuItem {
}

protected:
void draw(MenuRenderer* renderer, uint8_t itemIndex, uint8_t screenRow) override {
void draw(MenuRenderer* renderer, uint8_t screenRow) override {
uint8_t maxCols = renderer->getMaxCols();
char buf[maxCols];
concat(text, ':', buf);
concat(buf, getValue(), buf);
renderer->drawItem(itemIndex, screenRow, buf);
renderer->drawItem(screenRow, buf);
}

bool process(LcdMenu* menu, const unsigned char command) override {
Expand Down
4 changes: 2 additions & 2 deletions src/ItemRangeBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ class ItemRangeBase : public MenuItem {
virtual char* getDisplayValue() = 0;

protected:
void draw(MenuRenderer* renderer, uint8_t itemIndex, uint8_t screenRow) override {
void draw(MenuRenderer* renderer, uint8_t screenRow) override {
uint8_t maxCols = renderer->getMaxCols();
char buf[maxCols];
concat(text, ':', buf);
concat(buf, getDisplayValue(), buf);
renderer->drawItem(itemIndex, screenRow, buf);
renderer->drawItem(screenRow, buf);
}

bool process(LcdMenu* menu, const unsigned char command) override {
Expand Down
4 changes: 2 additions & 2 deletions src/ItemToggle.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ class ItemToggle : public MenuItem {

const char* getTextOff() { return this->textOff; }

void draw(MenuRenderer* renderer, uint8_t itemIndex, uint8_t screenRow) override {
void draw(MenuRenderer* renderer, uint8_t screenRow) override {
uint8_t maxCols = renderer->getMaxCols();
char buf[maxCols];
concat(text, ':', buf);
concat(buf, enabled ? textOn : textOff, buf);
renderer->drawItem(itemIndex, screenRow, buf);
renderer->drawItem(screenRow, buf);
};

protected:
Expand Down
6 changes: 3 additions & 3 deletions src/MenuItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ class MenuItem {
* @param renderer The renderer to use for drawing.
*/
const void draw(MenuRenderer* renderer) {
draw(renderer, renderer->getItemIndex(), renderer->getActiveRow());
draw(renderer, renderer->getActiveRow());
};
/**
* @brief Draw this menu item on specified display on specified row.
* @param renderer The renderer to use for drawing.
* @param itemIndex The index of the item in the menu.
* @param screenRow The row on the screen where the item should be drawn.
*/
virtual void draw(MenuRenderer* renderer, uint8_t itemIndex, uint8_t screenRow) {
renderer->drawItem(itemIndex, screenRow, text);
virtual void draw(MenuRenderer* renderer, uint8_t screenRow) {
renderer->drawItem(screenRow, text);
};
};

Expand Down
18 changes: 16 additions & 2 deletions src/MenuScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,27 @@ void MenuScreen::draw(MenuRenderer* renderer) {
if (item == nullptr) {
break;
}
item->draw(renderer, view + i, i);
markScroll(i, renderer);
item->draw(renderer, i);
}
}

void MenuScreen::markScroll(uint8_t i, MenuRenderer* renderer) {
if (i == 0 && view > 0) {
renderer->markUpScroll();
} else {
renderer->clearUpScroll();
}
if (i == renderer->maxRows - 1 && (view + renderer->maxRows) < itemCount) {
renderer->markDownScroll();
} else {
renderer->clearDownScroll();
}
}

bool MenuScreen::process(LcdMenu* menu, const unsigned char command) {
MenuRenderer* renderer = menu->getRenderer();
renderer->itemIndex = cursor;
markScroll(cursor - view, renderer);
if (items[cursor]->process(menu, command)) {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/MenuScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ class MenuScreen {
* @param renderer The renderer to use for drawing.
*/
void draw(MenuRenderer* renderer);
/**
* @brief Mark scroll indicators.
*/
void markScroll(uint8_t i, MenuRenderer* renderer);
/**
* @brief Process the command.
* @return `true` if the command was processed, `false` otherwise.
Expand Down
28 changes: 22 additions & 6 deletions src/renderer/CharacterDisplayRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ void CharacterDisplayRenderer::begin() {
static_cast<CharacterDisplayInterface*>(display)->createChar(2, downArrow);
}

void CharacterDisplayRenderer::drawItem(uint8_t itemIndex, uint8_t screenRow, const char* text) {
MenuRenderer::drawItem(itemIndex, screenRow, text);
void CharacterDisplayRenderer::drawItem(uint8_t screenRow, const char* text) {
MenuRenderer::drawItem(screenRow, text);
char buf[maxCols + 1];

appendCursorToText(screenRow, text, buf);
buf[calculateAvailableLength()] = '\0';
uint8_t cursorCol = strlen(buf);

padText(buf, maxCols, buf);
appendIndicatorToText(itemIndex, screenRow, buf, buf);
appendIndicatorToText(screenRow, buf, buf);

display->setCursor(0, screenRow);
display->draw(buf);
Expand Down Expand Up @@ -65,11 +65,11 @@ void CharacterDisplayRenderer::appendCursorToText(uint8_t screenRow, const char*
}
}

void CharacterDisplayRenderer::appendIndicatorToText(uint8_t itemIndex, uint8_t screenRow, const char* text, char* buf) {
void CharacterDisplayRenderer::appendIndicatorToText(uint8_t screenRow, const char* text, char* buf) {
uint8_t indicator = 0;
if (screenRow == 0 && itemIndex > 0) {
if (upScroll) {
indicator = 1;
} else if (screenRow == maxRows - 1 && itemIndex < itemCount - 1) {
} else if (downScroll) {
indicator = 2;
}

Expand All @@ -94,4 +94,20 @@ void CharacterDisplayRenderer::padText(const char* text, uint8_t itemIndex, char

uint8_t CharacterDisplayRenderer::calculateAvailableLength() {
return maxCols - (upArrow != NULL || downArrow != NULL ? 1 : 0);
}

void CharacterDisplayRenderer::markUpScroll() {
upScroll = true;
}

void CharacterDisplayRenderer::clearUpScroll() {
upScroll = false;
}

void CharacterDisplayRenderer::markDownScroll() {
downScroll = true;
}

void CharacterDisplayRenderer::clearDownScroll() {
downScroll = false;
}
11 changes: 8 additions & 3 deletions src/renderer/CharacterDisplayRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class CharacterDisplayRenderer : public MenuRenderer {
uint8_t* downArrow;
const uint8_t cursorIcon;
const uint8_t editCursorIcon;
bool upScroll = false;
bool downScroll = false;

/**
* @brief Appends a cursor icon to the given text if the specified screen row is active.
Expand All @@ -38,12 +40,11 @@ class CharacterDisplayRenderer : public MenuRenderer {
/**
* @brief Appends an indicator to the provided text based on the item index and screen row.
*
* @param itemIndex The index of the item in the list.
* @param screenRow The row on the screen where the text will be displayed.
* @param text The original text to which the indicator may be appended.
* @param buf The buffer where the resulting text with the indicator will be stored.
*/
void appendIndicatorToText(uint8_t itemIndex, uint8_t screenRow, const char* text, char* buf);
void appendIndicatorToText(uint8_t screenRow, const char* text, char* buf);

/**
* @brief Pads the given text with spaces to fit within the available length.
Expand Down Expand Up @@ -112,9 +113,13 @@ class CharacterDisplayRenderer : public MenuRenderer {
* @param screenRow The row on the screen where the item should be drawn.
* @param text The text of the menu item to be drawn.
*/
void drawItem(uint8_t itemIndex, uint8_t screenRow, const char* text) override;
void drawItem(uint8_t screenRow, const char* text) override;
void draw(uint8_t byte) override;
void drawBlinker() override;
void clearBlinker() override;
void moveCursor(uint8_t cursorCol, uint8_t cursorRow) override;
void markUpScroll() override;
void clearUpScroll() override;
void markDownScroll() override;
void clearDownScroll() override;
};
5 changes: 1 addition & 4 deletions src/renderer/MenuRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ void MenuRenderer::begin() {
startTime = millis();
}

void MenuRenderer::drawItem(uint8_t itemIndex, uint8_t screenRow, const char* text) {
void MenuRenderer::drawItem(uint8_t screenRow, const char* text) {
this->cursorRow = screenRow;
this->itemIndex = itemIndex;
}

void MenuRenderer::moveCursor(uint8_t cursorCol, uint8_t cursorRow) {
Expand Down Expand Up @@ -40,6 +39,4 @@ uint8_t MenuRenderer::getMaxRows() const { return maxRows; }

uint8_t MenuRenderer::getMaxCols() const { return maxCols; }

uint8_t MenuRenderer::getItemIndex() const { return itemIndex; }

uint8_t MenuRenderer::getActiveRow() const { return activeRow; }
14 changes: 6 additions & 8 deletions src/renderer/MenuRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class MenuRenderer {

uint8_t blinkerPosition;

uint8_t itemIndex;
uint8_t itemCount;

bool inEditMode;
Expand Down Expand Up @@ -65,7 +64,12 @@ class MenuRenderer {
* @param screenRow Row on the screen where the item should be drawn.
* @param text Text of the item to be drawn.
*/
virtual void drawItem(uint8_t itemIndex, uint8_t screenRow, const char* text);
virtual void drawItem(uint8_t screenRow, const char* text);

virtual void markUpScroll() = 0;
virtual void clearUpScroll() = 0;
virtual void markDownScroll() = 0;
virtual void clearDownScroll() = 0;

/**
* @brief Function to clear the blinker from the display.
Expand Down Expand Up @@ -136,12 +140,6 @@ class MenuRenderer {
*/
uint8_t getMaxCols() const;

/**
* @brief Gets the index of the current item.
* @return Index of the current item.
*/
uint8_t getItemIndex() const;

/**
* @brief Get the active row.
* @return the active row.
Expand Down

0 comments on commit 9552522

Please sign in to comment.