Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add selection grid feature for TFT_eSPI #3370

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions TFT_eSPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2654,6 +2654,44 @@ void TFT_eSPI::fillRoundRect(int32_t x, int32_t y, int32_t w, int32_t h, int32_t
end_tft_write(); // Does nothing if Sprite class uses this function
}

/***************************************************************************************
** Function name: fillRoundRectWithText
** Description: Draw a button with text inside
***************************************************************************************/
void TFT_eSPI::drawRoundRectWithText(int32_t x, int32_t y, int32_t w, int32_t h, int32_t r, uint32_t rectColor, const char* label, uint32_t textColor, uint8_t textDatum) {
// Draw a roundRect
fillRoundRect(x, y, w, h, r, rectColor);

// Calculate font size according to rectangle height
int16_t textHeight;
setTextDatum(textDatum);
setTextColor(textColor, rectColor);
setTextSize(1);
textHeight = fontHeight(2);

while (textHeight < (h - 2 * r)) {
setTextSize(getTextSize() + 1);
textHeight = fontHeight(2);
}

setTextSize(getTextSize() - 1);

// draw text inside the roundRect
drawString(label, x + w / 2, y + h / 2);
}

/***************************************************************************************
** Function name: drawSelectionGrid
** Description: Draw a row selection interface
***************************************************************************************/

void TFT_eSPI::drawSelectionGrid(int32_t x, int32_t y, int32_t buttonWidth, int32_t buttonHeight, int32_t buttonSpacing, uint32_t colorNormal, uint32_t colorSelection, int32_t numOfSelect, const char* labels[], int32_t numLabels) {
for (int i = 0; i < numLabels; i++) {
int32_t btnY = y + i * (buttonHeight + buttonSpacing);
uint32_t rectColor = (i == numOfSelect) ? colorSelection : colorNormal;
drawRoundRectWithText(x, btnY, buttonWidth, buttonHeight, 10, rectColor, labels[i], 0xFFFF-rectColor, MC_DATUM);
}
}

/***************************************************************************************
** Function name: drawTriangle
Expand Down
2 changes: 2 additions & 0 deletions TFT_eSPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ class TFT_eSPI : public Print { friend class TFT_eSprite; // Sprite class has ac
drawRect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color),
drawRoundRect(int32_t x, int32_t y, int32_t w, int32_t h, int32_t radius, uint32_t color),
fillRoundRect(int32_t x, int32_t y, int32_t w, int32_t h, int32_t radius, uint32_t color);
drawRoundRectWithText(int32_t x, int32_t y, int32_t w, int32_t h, int32_t r, uint32_t rectColor, const char* label, uint32_t textColor, uint8_t textDatum);
drawSelectionGrid(int32_t x, int32_t y, int32_t buttonWidth, int32_t buttonHeight, int32_t buttonSpacing, uint32_t colorNormal, uint32_t colorSelection, int32_t numOfSelect, const char* labels[], int32_t numLabels);

void fillRectVGradient(int16_t x, int16_t y, int16_t w, int16_t h, uint32_t color1, uint32_t color2);
void fillRectHGradient(int16_t x, int16_t y, int16_t w, int16_t h, uint32_t color1, uint32_t color2);
Expand Down
1 change: 1 addition & 0 deletions build/.cmake/api/v1/query/client-vscode/query.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"requests":[{"kind":"cache","version":2},{"kind":"codemodel","version":2},{"kind":"toolchains","version":1},{"kind":"cmakeFiles","version":1}]}
Loading